summaryrefslogtreecommitdiff
path: root/clang-plugin/type-manager.cpp
blob: 46c731941a6f10388806977556c0d0b134e55659 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* -*- Mode: C++; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/*
 * Tartan
 * Copyright © 2014 Collabora Ltd.
 *
 * Tartan is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tartan is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Tartan.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *     Philip Withnall <philip.withnall@collabora.co.uk>
 */

#include "config.h"

#include <unordered_map>

#include <clang/AST/Attr.h>
#include <clang/Lex/Lexer.h>

#include "type-manager.h"
#include "debug.h"

namespace tartan {

/* Find a #QualType for the typedeffed type with the given @name. This is a very
 * slow call (it requires iterating through all defined types in the given
 * @context), so its results are cached where possible.
 *
 * If type lookup fails, a null type is returned. */
const QualType
TypeManager::find_type_by_name (const std::string name)
{
	/* Look up the type in the cache first. */
	std::unordered_map<std::string, QualType>::const_iterator cached =
		this->_type_cache.find (name);
	if (cached != this->_type_cache.end ()) {
		return (*cached).second;
	}

	for (SmallVectorImpl<Type *>::const_iterator it = this->_context.getTypes ().begin (),
	     ie = this->_context.getTypes ().end (); it != ie; ++it) {
		const Type *t = *it;
		const TypedefType *tt = t->getAs<TypedefType> ();

		if (tt != NULL) {
			const TypedefNameDecl *decl = tt->getDecl ();

			if (decl->getName () == name) {
				DEBUG ("Found type ‘" << name << "’ with "
				       "desugared type ‘" <<
				       tt->desugar ().getAsString () << "’.");

				QualType qt (tt, 0);

				/* Insert it into the cache. */
				std::string _name (name);
				this->_type_cache.emplace (_name, qt);

				return qt;
			}
		}
	}

	DEBUG ("Failed to find type ‘" << name << "’.");

	return QualType ();
}

/* Version of _find_type_by_name() which makes it a pointer type. */
const QualType
TypeManager::find_pointer_type_by_name (const std::string name)
{
	QualType qt = this->find_type_by_name (name);
	if (!qt.isNull ()) {
		return this->_context.getPointerType (qt);
	}

	return QualType ();
}

} /* namespace tartan */