1 //===--- MinimalAction.cpp - Implement the MinimalAction class ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the MinimalAction interface.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Parse/Parser.h"
15 #include "clang/Parse/DeclSpec.h"
16 #include "clang/Parse/Scope.h"
17 #include "clang/Basic/TargetInfo.h"
18 #include "llvm/Support/Allocator.h"
19 #include "llvm/Support/RecyclingAllocator.h"
20 #include "llvm/Support/raw_ostream.h"
21 using namespace clang;
23 /// Out-of-line virtual destructor to provide home for ActionBase class.
24 ActionBase::~ActionBase() {}
26 /// Out-of-line virtual destructor to provide home for Action class.
29 // Defined out-of-line here because of dependecy on AttributeList
30 Action::DeclPtrTy Action::ActOnUsingDirective(Scope *CurScope,
31 SourceLocation UsingLoc,
32 SourceLocation NamespcLoc,
33 const CXXScopeSpec &SS,
34 SourceLocation IdentLoc,
35 IdentifierInfo *NamespcName,
36 AttributeList *AttrList) {
38 // FIXME: Parser seems to assume that Action::ActOn* takes ownership over
39 // passed AttributeList, however other actions don't free it, is it
40 // temporary state or bug?
46 void PrettyStackTraceActionsDecl::print(llvm::raw_ostream &OS) const {
53 std::string Name = Actions.getDeclName(TheDecl);
55 OS << " '" << Name << '\'';
60 /// TypeNameInfo - A link exists here for each scope that an identifier is
67 TypeNameInfo(bool istypename, TypeNameInfo *prev) {
68 isTypeName = istypename;
73 struct TypeNameInfoTable {
74 llvm::RecyclingAllocator<llvm::BumpPtrAllocator, TypeNameInfo> Allocator;
76 void AddEntry(bool isTypename, IdentifierInfo *II) {
77 TypeNameInfo *TI = Allocator.Allocate<TypeNameInfo>();
78 new (TI) TypeNameInfo(isTypename, II->getFETokenInfo<TypeNameInfo>());
79 II->setFETokenInfo(TI);
82 void DeleteEntry(TypeNameInfo *Entry) {
83 Entry->~TypeNameInfo();
84 Allocator.Deallocate(Entry);
89 static TypeNameInfoTable *getTable(void *TP) {
90 return static_cast<TypeNameInfoTable*>(TP);
93 MinimalAction::MinimalAction(Preprocessor &pp)
94 : Idents(pp.getIdentifierTable()), PP(pp) {
95 TypeNameInfoTablePtr = new TypeNameInfoTable();
98 MinimalAction::~MinimalAction() {
99 delete getTable(TypeNameInfoTablePtr);
102 void MinimalAction::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
105 TypeNameInfoTable &TNIT = *getTable(TypeNameInfoTablePtr);
107 if (PP.getTargetInfo().getPointerWidth(0) >= 64) {
108 // Install [u]int128_t for 64-bit targets.
109 TNIT.AddEntry(true, &Idents.get("__int128_t"));
110 TNIT.AddEntry(true, &Idents.get("__uint128_t"));
113 if (PP.getLangOptions().ObjC1) {
114 // Recognize the ObjC built-in type identifiers as types.
115 TNIT.AddEntry(true, &Idents.get("id"));
116 TNIT.AddEntry(true, &Idents.get("SEL"));
117 TNIT.AddEntry(true, &Idents.get("Class"));
118 TNIT.AddEntry(true, &Idents.get("Protocol"));
122 /// isTypeName - This looks at the IdentifierInfo::FETokenInfo field to
123 /// determine whether the name is a type name (objc class name or typedef) or
124 /// not in this scope.
126 /// FIXME: Use the passed CXXScopeSpec for accurate C++ type checking.
128 MinimalAction::getTypeName(IdentifierInfo &II, SourceLocation Loc,
129 Scope *S, const CXXScopeSpec *SS) {
130 if (TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>())
136 /// isCurrentClassName - Always returns false, because MinimalAction
137 /// does not support C++ classes with constructors.
138 bool MinimalAction::isCurrentClassName(const IdentifierInfo &, Scope *,
139 const CXXScopeSpec *) {
144 MinimalAction::isTemplateName(const IdentifierInfo &II, Scope *S,
145 TemplateTy &TemplateDecl,
146 const CXXScopeSpec *SS) {
147 return TNK_Non_template;
150 /// ActOnDeclarator - If this is a typedef declarator, we modify the
151 /// IdentifierInfo::FETokenInfo field to keep track of this fact, until S is
154 MinimalAction::ActOnDeclarator(Scope *S, Declarator &D) {
155 IdentifierInfo *II = D.getIdentifier();
157 // If there is no identifier associated with this declarator, bail out.
158 if (II == 0) return DeclPtrTy();
160 TypeNameInfo *weCurrentlyHaveTypeInfo = II->getFETokenInfo<TypeNameInfo>();
162 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef;
164 // this check avoids creating TypeNameInfo objects for the common case.
165 // It does need to handle the uncommon case of shadowing a typedef name with a
166 // non-typedef name. e.g. { typedef int a; a xx; { int a; } }
167 if (weCurrentlyHaveTypeInfo || isTypeName) {
168 // Allocate and add the 'TypeNameInfo' "decl".
169 getTable(TypeNameInfoTablePtr)->AddEntry(isTypeName, II);
171 // Remember that this needs to be removed when the scope is popped.
172 S->AddDecl(DeclPtrTy::make(II));
178 MinimalAction::ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
179 IdentifierInfo *ClassName,
180 SourceLocation ClassLoc,
181 IdentifierInfo *SuperName,
182 SourceLocation SuperLoc,
183 const DeclPtrTy *ProtoRefs,
184 unsigned NumProtocols,
185 SourceLocation EndProtoLoc,
186 AttributeList *AttrList) {
187 // Allocate and add the 'TypeNameInfo' "decl".
188 getTable(TypeNameInfoTablePtr)->AddEntry(true, ClassName);
192 /// ActOnForwardClassDeclaration -
193 /// Scope will always be top level file scope.
195 MinimalAction::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
196 IdentifierInfo **IdentList, unsigned NumElts) {
197 for (unsigned i = 0; i != NumElts; ++i) {
198 // Allocate and add the 'TypeNameInfo' "decl".
199 getTable(TypeNameInfoTablePtr)->AddEntry(true, IdentList[i]);
201 // Remember that this needs to be removed when the scope is popped.
202 TUScope->AddDecl(DeclPtrTy::make(IdentList[i]));
207 /// ActOnPopScope - When a scope is popped, if any typedefs are now
208 /// out-of-scope, they are removed from the IdentifierInfo::FETokenInfo field.
209 void MinimalAction::ActOnPopScope(SourceLocation Loc, Scope *S) {
210 TypeNameInfoTable &Table = *getTable(TypeNameInfoTablePtr);
212 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
214 IdentifierInfo &II = *(*I).getAs<IdentifierInfo>();
215 TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>();
216 assert(TI && "This decl didn't get pushed??");
219 TypeNameInfo *Next = TI->Prev;
220 Table.DeleteEntry(TI);
222 II.setFETokenInfo(Next);