NameSearchContext.cpp
5.92 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//===-- NameSearchContext.cpp ---------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "NameSearchContext.h"
#include "ClangUtil.h"
using namespace clang;
using namespace lldb_private;
clang::NamedDecl *NameSearchContext::AddVarDecl(const CompilerType &type) {
assert(type && "Type for variable must be valid!");
if (!type.IsValid())
return nullptr;
TypeSystemClang *lldb_ast =
llvm::dyn_cast<TypeSystemClang>(type.GetTypeSystem());
if (!lldb_ast)
return nullptr;
IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo();
clang::ASTContext &ast = lldb_ast->getASTContext();
clang::NamedDecl *Decl = VarDecl::Create(
ast, const_cast<DeclContext *>(m_decl_context), SourceLocation(),
SourceLocation(), ii, ClangUtil::GetQualType(type), nullptr, SC_Static);
m_decls.push_back(Decl);
return Decl;
}
clang::NamedDecl *NameSearchContext::AddFunDecl(const CompilerType &type,
bool extern_c) {
assert(type && "Type for variable must be valid!");
if (!type.IsValid())
return nullptr;
if (m_function_types.count(type))
return nullptr;
TypeSystemClang *lldb_ast =
llvm::dyn_cast<TypeSystemClang>(type.GetTypeSystem());
if (!lldb_ast)
return nullptr;
m_function_types.insert(type);
QualType qual_type(ClangUtil::GetQualType(type));
clang::ASTContext &ast = lldb_ast->getASTContext();
const bool isInlineSpecified = false;
const bool hasWrittenPrototype = true;
const bool isConstexprSpecified = false;
clang::DeclContext *context = const_cast<DeclContext *>(m_decl_context);
if (extern_c) {
context = LinkageSpecDecl::Create(
ast, context, SourceLocation(), SourceLocation(),
clang::LinkageSpecDecl::LanguageIDs::lang_c, false);
}
// Pass the identifier info for functions the decl_name is needed for
// operators
clang::DeclarationName decl_name =
m_decl_name.getNameKind() == DeclarationName::Identifier
? m_decl_name.getAsIdentifierInfo()
: m_decl_name;
clang::FunctionDecl *func_decl = FunctionDecl::Create(
ast, context, SourceLocation(), SourceLocation(), decl_name, qual_type,
nullptr, SC_Extern, isInlineSpecified, hasWrittenPrototype,
isConstexprSpecified ? CSK_constexpr : CSK_unspecified);
// We have to do more than just synthesize the FunctionDecl. We have to
// synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
// this, we raid the function's FunctionProtoType for types.
const FunctionProtoType *func_proto_type =
qual_type.getTypePtr()->getAs<FunctionProtoType>();
if (func_proto_type) {
unsigned NumArgs = func_proto_type->getNumParams();
unsigned ArgIndex;
SmallVector<ParmVarDecl *, 5> parm_var_decls;
for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex) {
QualType arg_qual_type(func_proto_type->getParamType(ArgIndex));
parm_var_decls.push_back(
ParmVarDecl::Create(ast, const_cast<DeclContext *>(context),
SourceLocation(), SourceLocation(), nullptr,
arg_qual_type, nullptr, SC_Static, nullptr));
}
func_decl->setParams(ArrayRef<ParmVarDecl *>(parm_var_decls));
} else {
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
LLDB_LOG(log, "Function type wasn't a FunctionProtoType");
}
// If this is an operator (e.g. operator new or operator==), only insert the
// declaration we inferred from the symbol if we can provide the correct
// number of arguments. We shouldn't really inject random decl(s) for
// functions that are analyzed semantically in a special way, otherwise we
// will crash in clang.
clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
if (func_proto_type &&
TypeSystemClang::IsOperator(decl_name.getAsString().c_str(), op_kind)) {
if (!TypeSystemClang::CheckOverloadedOperatorKindParameterCount(
false, op_kind, func_proto_type->getNumParams()))
return nullptr;
}
m_decls.push_back(func_decl);
return func_decl;
}
clang::NamedDecl *NameSearchContext::AddGenericFunDecl() {
FunctionProtoType::ExtProtoInfo proto_info;
proto_info.Variadic = true;
QualType generic_function_type(
GetASTContext().getFunctionType(GetASTContext().UnknownAnyTy, // result
ArrayRef<QualType>(), // argument types
proto_info));
return AddFunDecl(m_clang_ts.GetType(generic_function_type), true);
}
clang::NamedDecl *
NameSearchContext::AddTypeDecl(const CompilerType &clang_type) {
if (ClangUtil::IsClangType(clang_type)) {
QualType qual_type = ClangUtil::GetQualType(clang_type);
if (const TypedefType *typedef_type =
llvm::dyn_cast<TypedefType>(qual_type)) {
TypedefNameDecl *typedef_name_decl = typedef_type->getDecl();
m_decls.push_back(typedef_name_decl);
return (NamedDecl *)typedef_name_decl;
} else if (const TagType *tag_type = qual_type->getAs<TagType>()) {
TagDecl *tag_decl = tag_type->getDecl();
m_decls.push_back(tag_decl);
return tag_decl;
} else if (const ObjCObjectType *objc_object_type =
qual_type->getAs<ObjCObjectType>()) {
ObjCInterfaceDecl *interface_decl = objc_object_type->getInterface();
m_decls.push_back((NamedDecl *)interface_decl);
return (NamedDecl *)interface_decl;
}
}
return nullptr;
}
void NameSearchContext::AddLookupResult(clang::DeclContextLookupResult result) {
for (clang::NamedDecl *decl : result)
m_decls.push_back(decl);
}
void NameSearchContext::AddNamedDecl(clang::NamedDecl *decl) {
m_decls.push_back(decl);
}