blob: 5f48897235ab0aef57eb667a2a6a4e756562389b [file] [log] [blame]
Shih-wei Liaof8fd82b2010-02-10 11:10:31 -08001//===--- MinimalAction.cpp - Implement the MinimalAction class ------------===//
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 implements the MinimalAction interface.
11//
12//===----------------------------------------------------------------------===//
13
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"
21using namespace clang;
22
23/// Out-of-line virtual destructor to provide home for ActionBase class.
24ActionBase::~ActionBase() {}
25
26/// Out-of-line virtual destructor to provide home for Action class.
27Action::~Action() {}
28
29// Defined out-of-line here because of dependecy on AttributeList
30Action::DeclPtrTy Action::ActOnUsingDirective(Scope *CurScope,
31 SourceLocation UsingLoc,
32 SourceLocation NamespcLoc,
33 const CXXScopeSpec &SS,
34 SourceLocation IdentLoc,
35 IdentifierInfo *NamespcName,
36 AttributeList *AttrList) {
37
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?
41 delete AttrList;
42 return DeclPtrTy();
43}
44
45// Defined out-of-line here because of dependency on AttributeList
46Action::DeclPtrTy Action::ActOnUsingDeclaration(Scope *CurScope,
47 AccessSpecifier AS,
48 bool HasUsingKeyword,
49 SourceLocation UsingLoc,
50 const CXXScopeSpec &SS,
51 UnqualifiedId &Name,
52 AttributeList *AttrList,
53 bool IsTypeName,
54 SourceLocation TypenameLoc) {
55
56 // FIXME: Parser seems to assume that Action::ActOn* takes ownership over
57 // passed AttributeList, however other actions don't free it, is it
58 // temporary state or bug?
59 delete AttrList;
60 return DeclPtrTy();
61}
62
63
64void PrettyStackTraceActionsDecl::print(llvm::raw_ostream &OS) const {
65 if (Loc.isValid()) {
66 Loc.print(OS, SM);
67 OS << ": ";
68 }
69 OS << Message;
70
71 std::string Name = Actions.getDeclName(TheDecl);
72 if (!Name.empty())
73 OS << " '" << Name << '\'';
74
75 OS << '\n';
76}
77
78/// TypeNameInfo - A link exists here for each scope that an identifier is
79/// defined.
80namespace {
81 struct TypeNameInfo {
82 TypeNameInfo *Prev;
83 bool isTypeName;
84
85 TypeNameInfo(bool istypename, TypeNameInfo *prev) {
86 isTypeName = istypename;
87 Prev = prev;
88 }
89 };
90
91 struct TypeNameInfoTable {
92 llvm::RecyclingAllocator<llvm::BumpPtrAllocator, TypeNameInfo> Allocator;
93
94 void AddEntry(bool isTypename, IdentifierInfo *II) {
95 TypeNameInfo *TI = Allocator.Allocate<TypeNameInfo>();
96 new (TI) TypeNameInfo(isTypename, II->getFETokenInfo<TypeNameInfo>());
97 II->setFETokenInfo(TI);
98 }
99
100 void DeleteEntry(TypeNameInfo *Entry) {
101 Entry->~TypeNameInfo();
102 Allocator.Deallocate(Entry);
103 }
104 };
105}
106
107static TypeNameInfoTable *getTable(void *TP) {
108 return static_cast<TypeNameInfoTable*>(TP);
109}
110
111MinimalAction::MinimalAction(Preprocessor &pp)
112 : Idents(pp.getIdentifierTable()), PP(pp) {
113 TypeNameInfoTablePtr = new TypeNameInfoTable();
114}
115
116MinimalAction::~MinimalAction() {
117 delete getTable(TypeNameInfoTablePtr);
118}
119
120void MinimalAction::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
121 TUScope = S;
122
123 TypeNameInfoTable &TNIT = *getTable(TypeNameInfoTablePtr);
124
125 if (PP.getTargetInfo().getPointerWidth(0) >= 64) {
126 // Install [u]int128_t for 64-bit targets.
127 TNIT.AddEntry(true, &Idents.get("__int128_t"));
128 TNIT.AddEntry(true, &Idents.get("__uint128_t"));
129 }
130
131 if (PP.getLangOptions().ObjC1) {
132 // Recognize the ObjC built-in type identifiers as types.
133 TNIT.AddEntry(true, &Idents.get("id"));
134 TNIT.AddEntry(true, &Idents.get("SEL"));
135 TNIT.AddEntry(true, &Idents.get("Class"));
136 TNIT.AddEntry(true, &Idents.get("Protocol"));
137 }
138}
139
140/// isTypeName - This looks at the IdentifierInfo::FETokenInfo field to
141/// determine whether the name is a type name (objc class name or typedef) or
142/// not in this scope.
143///
144/// FIXME: Use the passed CXXScopeSpec for accurate C++ type checking.
145Action::TypeTy *
146MinimalAction::getTypeName(IdentifierInfo &II, SourceLocation Loc,
147 Scope *S, const CXXScopeSpec *SS,
148 bool isClassName, TypeTy *ObjectType) {
149 if (TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>())
150 if (TI->isTypeName)
151 return TI;
152 return 0;
153}
154
155/// isCurrentClassName - Always returns false, because MinimalAction
156/// does not support C++ classes with constructors.
157bool MinimalAction::isCurrentClassName(const IdentifierInfo &, Scope *,
158 const CXXScopeSpec *) {
159 return false;
160}
161
162TemplateNameKind
163MinimalAction::isTemplateName(Scope *S,
164 const CXXScopeSpec &SS,
165 UnqualifiedId &Name,
166 TypeTy *ObjectType,
167 bool EnteringScope,
168 TemplateTy &TemplateDecl) {
169 return TNK_Non_template;
170}
171
172/// ActOnDeclarator - If this is a typedef declarator, we modify the
173/// IdentifierInfo::FETokenInfo field to keep track of this fact, until S is
174/// popped.
175Action::DeclPtrTy
176MinimalAction::ActOnDeclarator(Scope *S, Declarator &D) {
177 IdentifierInfo *II = D.getIdentifier();
178
179 // If there is no identifier associated with this declarator, bail out.
180 if (II == 0) return DeclPtrTy();
181
182 TypeNameInfo *weCurrentlyHaveTypeInfo = II->getFETokenInfo<TypeNameInfo>();
183 bool isTypeName =
184 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef;
185
186 // this check avoids creating TypeNameInfo objects for the common case.
187 // It does need to handle the uncommon case of shadowing a typedef name with a
188 // non-typedef name. e.g. { typedef int a; a xx; { int a; } }
189 if (weCurrentlyHaveTypeInfo || isTypeName) {
190 // Allocate and add the 'TypeNameInfo' "decl".
191 getTable(TypeNameInfoTablePtr)->AddEntry(isTypeName, II);
192
193 // Remember that this needs to be removed when the scope is popped.
194 S->AddDecl(DeclPtrTy::make(II));
195 }
196 return DeclPtrTy();
197}
198
199Action::DeclPtrTy
200MinimalAction::ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
201 IdentifierInfo *ClassName,
202 SourceLocation ClassLoc,
203 IdentifierInfo *SuperName,
204 SourceLocation SuperLoc,
205 const DeclPtrTy *ProtoRefs,
206 unsigned NumProtocols,
207 const SourceLocation *ProtoLocs,
208 SourceLocation EndProtoLoc,
209 AttributeList *AttrList) {
210 // Allocate and add the 'TypeNameInfo' "decl".
211 getTable(TypeNameInfoTablePtr)->AddEntry(true, ClassName);
212 return DeclPtrTy();
213}
214
215/// ActOnForwardClassDeclaration -
216/// Scope will always be top level file scope.
217Action::DeclPtrTy
218MinimalAction::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
219 IdentifierInfo **IdentList,
220 SourceLocation *IdentLocs,
221 unsigned NumElts) {
222 for (unsigned i = 0; i != NumElts; ++i) {
223 // Allocate and add the 'TypeNameInfo' "decl".
224 getTable(TypeNameInfoTablePtr)->AddEntry(true, IdentList[i]);
225
226 // Remember that this needs to be removed when the scope is popped.
227 TUScope->AddDecl(DeclPtrTy::make(IdentList[i]));
228 }
229 return DeclPtrTy();
230}
231
232/// ActOnPopScope - When a scope is popped, if any typedefs are now
233/// out-of-scope, they are removed from the IdentifierInfo::FETokenInfo field.
234void MinimalAction::ActOnPopScope(SourceLocation Loc, Scope *S) {
235 TypeNameInfoTable &Table = *getTable(TypeNameInfoTablePtr);
236
237 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
238 I != E; ++I) {
239 IdentifierInfo &II = *(*I).getAs<IdentifierInfo>();
240 TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>();
241 assert(TI && "This decl didn't get pushed??");
242
243 if (TI) {
244 TypeNameInfo *Next = TI->Prev;
245 Table.DeleteEntry(TI);
246
247 II.setFETokenInfo(Next);
248 }
249 }
250}