clang  3.7.0
ASTCommon.cpp
Go to the documentation of this file.
1 //===--- ASTCommon.cpp - Common stuff for ASTReader/ASTWriter----*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines common functions that both ASTReader and ASTWriter use.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "ASTCommon.h"
15 #include "clang/AST/DeclCXX.h"
16 #include "clang/AST/DeclObjC.h"
19 #include "llvm/ADT/StringExtras.h"
20 
21 using namespace clang;
22 
23 // Give ASTDeserializationListener's VTable a home.
25 
28  unsigned ID = 0;
29  switch (BT->getKind()) {
30  case BuiltinType::Void: ID = PREDEF_TYPE_VOID_ID; break;
31  case BuiltinType::Bool: ID = PREDEF_TYPE_BOOL_ID; break;
32  case BuiltinType::Char_U: ID = PREDEF_TYPE_CHAR_U_ID; break;
33  case BuiltinType::UChar: ID = PREDEF_TYPE_UCHAR_ID; break;
34  case BuiltinType::UShort: ID = PREDEF_TYPE_USHORT_ID; break;
35  case BuiltinType::UInt: ID = PREDEF_TYPE_UINT_ID; break;
36  case BuiltinType::ULong: ID = PREDEF_TYPE_ULONG_ID; break;
37  case BuiltinType::ULongLong: ID = PREDEF_TYPE_ULONGLONG_ID; break;
38  case BuiltinType::UInt128: ID = PREDEF_TYPE_UINT128_ID; break;
39  case BuiltinType::Char_S: ID = PREDEF_TYPE_CHAR_S_ID; break;
40  case BuiltinType::SChar: ID = PREDEF_TYPE_SCHAR_ID; break;
41  case BuiltinType::WChar_S:
42  case BuiltinType::WChar_U: ID = PREDEF_TYPE_WCHAR_ID; break;
43  case BuiltinType::Short: ID = PREDEF_TYPE_SHORT_ID; break;
44  case BuiltinType::Int: ID = PREDEF_TYPE_INT_ID; break;
45  case BuiltinType::Long: ID = PREDEF_TYPE_LONG_ID; break;
46  case BuiltinType::LongLong: ID = PREDEF_TYPE_LONGLONG_ID; break;
47  case BuiltinType::Int128: ID = PREDEF_TYPE_INT128_ID; break;
48  case BuiltinType::Half: ID = PREDEF_TYPE_HALF_ID; break;
49  case BuiltinType::Float: ID = PREDEF_TYPE_FLOAT_ID; break;
50  case BuiltinType::Double: ID = PREDEF_TYPE_DOUBLE_ID; break;
51  case BuiltinType::LongDouble: ID = PREDEF_TYPE_LONGDOUBLE_ID; break;
52  case BuiltinType::NullPtr: ID = PREDEF_TYPE_NULLPTR_ID; break;
53  case BuiltinType::Char16: ID = PREDEF_TYPE_CHAR16_ID; break;
54  case BuiltinType::Char32: ID = PREDEF_TYPE_CHAR32_ID; break;
55  case BuiltinType::Overload: ID = PREDEF_TYPE_OVERLOAD_ID; break;
56  case BuiltinType::BoundMember:ID = PREDEF_TYPE_BOUND_MEMBER; break;
57  case BuiltinType::PseudoObject:ID = PREDEF_TYPE_PSEUDO_OBJECT;break;
58  case BuiltinType::Dependent: ID = PREDEF_TYPE_DEPENDENT_ID; break;
59  case BuiltinType::UnknownAny: ID = PREDEF_TYPE_UNKNOWN_ANY; break;
60  case BuiltinType::ARCUnbridgedCast:
62  case BuiltinType::ObjCId: ID = PREDEF_TYPE_OBJC_ID; break;
63  case BuiltinType::ObjCClass: ID = PREDEF_TYPE_OBJC_CLASS; break;
64  case BuiltinType::ObjCSel: ID = PREDEF_TYPE_OBJC_SEL; break;
65  case BuiltinType::OCLImage1d: ID = PREDEF_TYPE_IMAGE1D_ID; break;
66  case BuiltinType::OCLImage1dArray: ID = PREDEF_TYPE_IMAGE1D_ARR_ID; break;
67  case BuiltinType::OCLImage1dBuffer: ID = PREDEF_TYPE_IMAGE1D_BUFF_ID; break;
68  case BuiltinType::OCLImage2d: ID = PREDEF_TYPE_IMAGE2D_ID; break;
69  case BuiltinType::OCLImage2dArray: ID = PREDEF_TYPE_IMAGE2D_ARR_ID; break;
70  case BuiltinType::OCLImage3d: ID = PREDEF_TYPE_IMAGE3D_ID; break;
71  case BuiltinType::OCLSampler: ID = PREDEF_TYPE_SAMPLER_ID; break;
72  case BuiltinType::OCLEvent: ID = PREDEF_TYPE_EVENT_ID; break;
73  case BuiltinType::BuiltinFn:
74  ID = PREDEF_TYPE_BUILTIN_FN; break;
75 
76  }
77 
78  return TypeIdx(ID);
79 }
80 
82  unsigned N = Sel.getNumArgs();
83  if (N == 0)
84  ++N;
85  unsigned R = 5381;
86  for (unsigned I = 0; I != N; ++I)
88  R = llvm::HashString(II->getName(), R);
89  return R;
90 }
91 
92 const DeclContext *
94  switch (DC->getDeclKind()) {
95  // These entities may have multiple definitions.
96  case Decl::TranslationUnit:
97  case Decl::ExternCContext:
98  case Decl::Namespace:
99  case Decl::LinkageSpec:
100  return nullptr;
101 
102  // C/C++ tag types can only be defined in one place.
103  case Decl::Enum:
104  case Decl::Record:
105  if (const TagDecl *Def = cast<TagDecl>(DC)->getDefinition())
106  return Def;
107  return nullptr;
108 
109  // FIXME: These can be defined in one place... except special member
110  // functions and out-of-line definitions.
111  case Decl::CXXRecord:
112  case Decl::ClassTemplateSpecialization:
113  case Decl::ClassTemplatePartialSpecialization:
114  return nullptr;
115 
116  // Each function, method, and block declaration is its own DeclContext.
117  case Decl::Function:
118  case Decl::CXXMethod:
119  case Decl::CXXConstructor:
120  case Decl::CXXDestructor:
121  case Decl::CXXConversion:
122  case Decl::ObjCMethod:
123  case Decl::Block:
124  case Decl::Captured:
125  // Objective C categories, category implementations, and class
126  // implementations can only be defined in one place.
127  case Decl::ObjCCategory:
128  case Decl::ObjCCategoryImpl:
129  case Decl::ObjCImplementation:
130  return DC;
131 
132  case Decl::ObjCProtocol:
133  if (const ObjCProtocolDecl *Def
134  = cast<ObjCProtocolDecl>(DC)->getDefinition())
135  return Def;
136  return nullptr;
137 
138  // FIXME: These are defined in one place, but properties in class extensions
139  // end up being back-patched into the main interface. See
140  // Sema::HandlePropertyInClassExtension for the offending code.
141  case Decl::ObjCInterface:
142  return nullptr;
143 
144  default:
145  llvm_unreachable("Unhandled DeclContext in AST reader");
146  }
147 
148  llvm_unreachable("Unhandled decl kind");
149 }
150 
152  switch (static_cast<Decl::Kind>(Kind)) {
153  case Decl::TranslationUnit:
154  case Decl::ExternCContext:
155  // Special case of a "merged" declaration.
156  return true;
157 
158  case Decl::Namespace:
159  case Decl::NamespaceAlias:
160  case Decl::Typedef:
161  case Decl::TypeAlias:
162  case Decl::Enum:
163  case Decl::Record:
164  case Decl::CXXRecord:
165  case Decl::ClassTemplateSpecialization:
166  case Decl::ClassTemplatePartialSpecialization:
167  case Decl::VarTemplateSpecialization:
168  case Decl::VarTemplatePartialSpecialization:
169  case Decl::Function:
170  case Decl::CXXMethod:
171  case Decl::CXXConstructor:
172  case Decl::CXXDestructor:
173  case Decl::CXXConversion:
174  case Decl::UsingShadow:
175  case Decl::Var:
176  case Decl::FunctionTemplate:
177  case Decl::ClassTemplate:
178  case Decl::VarTemplate:
179  case Decl::TypeAliasTemplate:
180  case Decl::ObjCProtocol:
181  case Decl::ObjCInterface:
182  case Decl::Empty:
183  return true;
184 
185  // Never redeclarable.
186  case Decl::UsingDirective:
187  case Decl::Label:
188  case Decl::UnresolvedUsingTypename:
189  case Decl::TemplateTypeParm:
190  case Decl::EnumConstant:
191  case Decl::UnresolvedUsingValue:
192  case Decl::IndirectField:
193  case Decl::Field:
194  case Decl::MSProperty:
195  case Decl::ObjCIvar:
196  case Decl::ObjCAtDefsField:
197  case Decl::NonTypeTemplateParm:
198  case Decl::TemplateTemplateParm:
199  case Decl::Using:
200  case Decl::ObjCMethod:
201  case Decl::ObjCCategory:
202  case Decl::ObjCCategoryImpl:
203  case Decl::ObjCImplementation:
204  case Decl::ObjCProperty:
205  case Decl::ObjCCompatibleAlias:
206  case Decl::LinkageSpec:
207  case Decl::ObjCPropertyImpl:
208  case Decl::FileScopeAsm:
209  case Decl::AccessSpec:
210  case Decl::Friend:
211  case Decl::FriendTemplate:
212  case Decl::StaticAssert:
213  case Decl::Block:
214  case Decl::Captured:
215  case Decl::ClassScopeFunctionSpecialization:
216  case Decl::Import:
217  case Decl::OMPThreadPrivate:
218  return false;
219 
220  // These indirectly derive from Redeclarable<T> but are not actually
221  // redeclarable.
222  case Decl::ImplicitParam:
223  case Decl::ParmVar:
224  case Decl::ObjCTypeParam:
225  return false;
226  }
227 
228  llvm_unreachable("Unhandled declaration kind");
229 }
230 
232  // Friend declarations in dependent contexts aren't anonymous in the usual
233  // sense, but they cannot be found by name lookup in their semantic context
234  // (or indeed in any context), so we treat them as anonymous.
235  //
236  // This doesn't apply to friend tag decls; Sema makes those available to name
237  // lookup in the surrounding context.
238  if (D->getFriendObjectKind() &&
239  D->getLexicalDeclContext()->isDependentContext() && !isa<TagDecl>(D)) {
240  // For function templates and class templates, the template is numbered and
241  // not its pattern.
242  if (auto *FD = dyn_cast<FunctionDecl>(D))
243  return !FD->getDescribedFunctionTemplate();
244  if (auto *RD = dyn_cast<CXXRecordDecl>(D))
245  return !RD->getDescribedClassTemplate();
246  return true;
247  }
248 
249  // Otherwise, we only care about anonymous class members.
250  if (D->getDeclName() || !isa<CXXRecordDecl>(D->getLexicalDeclContext()))
251  return false;
252  return isa<TagDecl>(D) || isa<FieldDecl>(D);
253 }
254 
Kind getKind() const
Definition: Type.h:2006
Smart pointer class that efficiently represents Objective-C method names.
bool isRedeclarableDeclKind(unsigned Kind)
Determine whether the given declaration kind is redeclarable.
Definition: ASTCommon.cpp:151
The (signed) 'long long' type.
Definition: ASTBitCodes.h:720
The 'bool' or '_Bool' type.
Definition: ASTBitCodes.h:694
The 'unknown any' placeholder type.
Definition: ASTBitCodes.h:748
The placeholder type for builtin functions.
Definition: ASTBitCodes.h:764
bool isDependentContext() const
Determines whether this context is dependent on a template parameter.
Definition: DeclBase.cpp:851
ARC's unbridged-cast placeholder type.
Definition: ASTBitCodes.h:758
const DeclContext * getDefinitiveDeclContext(const DeclContext *DC)
Retrieve the "definitive" declaration that provides all of the visible entries for the given declarat...
Definition: ASTCommon.cpp:93
IdentifierInfo * getIdentifierInfoForSlot(unsigned argIndex) const
Retrieve the identifier at a given position in the selector.
The 'char' type, when it is signed.
Definition: ASTBitCodes.h:708
ObjCProtocolDecl * getDefinition()
Retrieve the definition of this protocol, if any.
Definition: DeclObjC.h:1866
Represents an Objective-C protocol declaration.
Definition: DeclObjC.h:1731
DeclContext * getLexicalDeclContext()
Definition: DeclBase.h:697
ID
Defines the set of possible language-specific address spaces.
Definition: AddressSpaces.h:27
The 'char' type, when it is unsigned.
Definition: ASTBitCodes.h:696
unsigned getNumArgs() const
The placeholder type for overloaded function sets.
Definition: ASTBitCodes.h:728
The OpenCL 'half' / ARM NEON __fp16 type.
Definition: ASTBitCodes.h:756
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
DeclarationName getDeclName() const
Definition: Decl.h:189
TagDecl * getDefinition() const
Definition: Decl.cpp:3442
The placeholder type for bound member functions.
Definition: ASTBitCodes.h:750
The 'unsigned long long' type.
Definition: ASTBitCodes.h:706
Kind
TagDecl - Represents the declaration of a struct/union/class/enum.
Definition: Decl.h:2694
TypeIdx TypeIdxFromBuiltin(const BuiltinType *BT)
Definition: ASTCommon.cpp:27
unsigned ComputeHash(Selector Sel)
Definition: ASTCommon.cpp:81
The placeholder type for dependent types.
Definition: ASTBitCodes.h:730
Decl::Kind getDeclKind() const
Definition: DeclBase.h:1168
FriendObjectKind getFriendObjectKind() const
Determines whether this declaration is the object of a friend declaration and, if so...
Definition: DeclBase.h:958
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate.h) and friends (in DeclFriend.h).
bool needsAnonymousDeclarationNumber(const NamedDecl *D)
Determine whether the given declaration needs an anonymous declaration number.
Definition: ASTCommon.cpp:231
A type index; the type ID with the qualifier bits removed.
Definition: ASTBitCodes.h:85
The pseudo-object placeholder type.
Definition: ASTBitCodes.h:760