blob: 41ad36c83ee72501a8c9bc11743fee60814ee44c [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- MinimalAction.cpp - Implement the MinimalAction class ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
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"
Chris Lattnere1ae1e92009-01-18 09:39:41 +000017#include "llvm/Support/Allocator.h"
18#include "llvm/Support/RecyclingAllocator.h"
Chris Lattner21ff9c92009-03-05 01:25:28 +000019#include "llvm/Support/raw_ostream.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020using namespace clang;
21
Chris Lattner0102c302009-03-05 07:24:28 +000022/// Out-of-line virtual destructor to provide home for Action class.
23ActionBase::~ActionBase() {}
24
25/// Out-of-line virtual destructor to provide home for Action class.
26Action::~Action() {}
27
28// Defined out-of-line here because of dependecy on AttributeList
29Action::DeclTy *Action::ActOnUsingDirective(Scope *CurScope,
30 SourceLocation UsingLoc,
31 SourceLocation NamespcLoc,
32 const CXXScopeSpec &SS,
33 SourceLocation IdentLoc,
34 IdentifierInfo *NamespcName,
35 AttributeList *AttrList) {
36
37 // FIXME: Parser seems to assume that Action::ActOn* takes ownership over
38 // passed AttributeList, however other actions don't free it, is it
39 // temporary state or bug?
40 delete AttrList;
41 return 0;
42}
43
44
Chris Lattner49f28ca2009-03-05 08:00:35 +000045void PrettyStackTraceActionsDecl::print(llvm::raw_ostream &OS) const {
Chris Lattner21ff9c92009-03-05 01:25:28 +000046 if (Loc.isValid()) {
47 Loc.print(OS, SM);
48 OS << ": ";
49 }
50 OS << Message;
51
52 std::string Name = Actions.getDeclName(TheDecl);
53 if (!Name.empty())
54 OS << " '" << Name << '\'';
55
56 OS << '\n';
57}
58
Reid Spencer5f016e22007-07-11 17:01:13 +000059/// TypeNameInfo - A link exists here for each scope that an identifier is
60/// defined.
Chris Lattnere1ae1e92009-01-18 09:39:41 +000061namespace {
62 struct TypeNameInfo {
63 TypeNameInfo *Prev;
64 bool isTypeName;
65
66 TypeNameInfo(bool istypename, TypeNameInfo *prev) {
67 isTypeName = istypename;
68 Prev = prev;
69 }
70 };
71
72 struct TypeNameInfoTable {
73 llvm::RecyclingAllocator<llvm::BumpPtrAllocator, TypeNameInfo> Allocator;
74
75 void AddEntry(bool isTypename, IdentifierInfo *II) {
76 TypeNameInfo *TI = Allocator.Allocate<TypeNameInfo>();
77 new (TI) TypeNameInfo(1, II->getFETokenInfo<TypeNameInfo>());
78 II->setFETokenInfo(TI);
79 }
80
81 void DeleteEntry(TypeNameInfo *Entry) {
82 Entry->~TypeNameInfo();
83 Allocator.Deallocate(Entry);
84 }
85 };
86}
87
88static TypeNameInfoTable *getTable(void *TP) {
89 return static_cast<TypeNameInfoTable*>(TP);
90}
Reid Spencer5f016e22007-07-11 17:01:13 +000091
Daniel Dunbare10b0f22008-10-31 08:56:51 +000092MinimalAction::MinimalAction(Preprocessor &pp)
Chris Lattnere1ae1e92009-01-18 09:39:41 +000093 : Idents(pp.getIdentifierTable()), PP(pp) {
94 TypeNameInfoTablePtr = new TypeNameInfoTable();
95}
96
97MinimalAction::~MinimalAction() {
98 delete getTable(TypeNameInfoTablePtr);
99}
Daniel Dunbare10b0f22008-10-31 08:56:51 +0000100
101void MinimalAction::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
Steve Naroff8ee529b2007-10-31 18:42:27 +0000102 TUScope = S;
Daniel Dunbare10b0f22008-10-31 08:56:51 +0000103 if (!PP.getLangOptions().ObjC1) return;
104
Chris Lattnere1ae1e92009-01-18 09:39:41 +0000105
106 TypeNameInfoTable &TNIT = *getTable(TypeNameInfoTablePtr);
107
108 // Recognize the ObjC built-in type identifiers as types.
109 TNIT.AddEntry(true, &Idents.get("id"));
110 TNIT.AddEntry(true, &Idents.get("SEL"));
111 TNIT.AddEntry(true, &Idents.get("Class"));
112 TNIT.AddEntry(true, &Idents.get("Protocol"));
Steve Naroff8ee529b2007-10-31 18:42:27 +0000113}
114
Reid Spencer5f016e22007-07-11 17:01:13 +0000115/// isTypeName - This looks at the IdentifierInfo::FETokenInfo field to
116/// determine whether the name is a type name (objc class name or typedef) or
117/// not in this scope.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000118///
119/// FIXME: Use the passed CXXScopeSpec for accurate C++ type checking.
Argyrios Kyrtzidis39caa082008-08-01 10:35:27 +0000120Action::TypeTy *
Douglas Gregorb696ea32009-02-04 17:00:24 +0000121MinimalAction::getTypeName(IdentifierInfo &II, SourceLocation Loc,
122 Scope *S, const CXXScopeSpec *SS) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000123 if (TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>())
124 if (TI->isTypeName)
125 return TI;
126 return 0;
127}
128
Douglas Gregorb48fe382008-10-31 09:07:45 +0000129/// isCurrentClassName - Always returns false, because MinimalAction
130/// does not support C++ classes with constructors.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000131bool MinimalAction::isCurrentClassName(const IdentifierInfo &, Scope *,
132 const CXXScopeSpec *) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000133 return false;
134}
135
Douglas Gregor39a8de12009-02-25 19:37:18 +0000136TemplateNameKind
Douglas Gregor55f6b142009-02-09 18:46:07 +0000137MinimalAction::isTemplateName(IdentifierInfo &II, Scope *S,
138 DeclTy *&TemplateDecl,
139 const CXXScopeSpec *SS) {
140 return TNK_Non_template;
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000141}
142
Steve Naroff08d92e42007-09-15 18:49:24 +0000143/// ActOnDeclarator - If this is a typedef declarator, we modify the
Reid Spencer5f016e22007-07-11 17:01:13 +0000144/// IdentifierInfo::FETokenInfo field to keep track of this fact, until S is
145/// popped.
146Action::DeclTy *
Daniel Dunbar914701e2008-08-05 16:28:08 +0000147MinimalAction::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *LastInGroup) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 IdentifierInfo *II = D.getIdentifier();
149
150 // If there is no identifier associated with this declarator, bail out.
151 if (II == 0) return 0;
152
153 TypeNameInfo *weCurrentlyHaveTypeInfo = II->getFETokenInfo<TypeNameInfo>();
154 bool isTypeName =
155 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef;
156
157 // this check avoids creating TypeNameInfo objects for the common case.
Ted Kremeneka34ea072008-08-04 22:51:42 +0000158 // It does need to handle the uncommon case of shadowing a typedef name with a
Reid Spencer5f016e22007-07-11 17:01:13 +0000159 // non-typedef name. e.g. { typedef int a; a xx; { int a; } }
160 if (weCurrentlyHaveTypeInfo || isTypeName) {
Chris Lattnere1ae1e92009-01-18 09:39:41 +0000161 // Allocate and add the 'TypeNameInfo' "decl".
162 getTable(TypeNameInfoTablePtr)->AddEntry(isTypeName, II);
Reid Spencer5f016e22007-07-11 17:01:13 +0000163
164 // Remember that this needs to be removed when the scope is popped.
165 S->AddDecl(II);
166 }
167 return 0;
168}
169
Steve Naroff3536b442007-09-06 21:24:23 +0000170Action::DeclTy *
Chris Lattnercfb0ef52008-07-21 07:13:18 +0000171MinimalAction::ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
Chris Lattner06036d32008-07-26 04:13:19 +0000172 IdentifierInfo *ClassName,
173 SourceLocation ClassLoc,
174 IdentifierInfo *SuperName,
175 SourceLocation SuperLoc,
176 DeclTy * const *ProtoRefs,
177 unsigned NumProtocols,
178 SourceLocation EndProtoLoc,
179 AttributeList *AttrList) {
Chris Lattnere1ae1e92009-01-18 09:39:41 +0000180 // Allocate and add the 'TypeNameInfo' "decl".
181 getTable(TypeNameInfoTablePtr)->AddEntry(true, ClassName);
Steve Naroff3536b442007-09-06 21:24:23 +0000182 return 0;
183}
184
Steve Naroff37e58d12007-10-02 22:39:18 +0000185/// ActOnForwardClassDeclaration -
Reid Spencer5f016e22007-07-11 17:01:13 +0000186/// Scope will always be top level file scope.
187Action::DeclTy *
Steve Naroffe440eb82007-10-10 17:32:04 +0000188MinimalAction::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Steve Naroff3536b442007-09-06 21:24:23 +0000189 IdentifierInfo **IdentList, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000190 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnere1ae1e92009-01-18 09:39:41 +0000191 // Allocate and add the 'TypeNameInfo' "decl".
192 getTable(TypeNameInfoTablePtr)->AddEntry(true, IdentList[i]);
Reid Spencer5f016e22007-07-11 17:01:13 +0000193
194 // Remember that this needs to be removed when the scope is popped.
Steve Naroffe440eb82007-10-10 17:32:04 +0000195 TUScope->AddDecl(IdentList[i]);
Reid Spencer5f016e22007-07-11 17:01:13 +0000196 }
197 return 0;
198}
199
Ted Kremeneka34ea072008-08-04 22:51:42 +0000200/// ActOnPopScope - When a scope is popped, if any typedefs are now
201/// out-of-scope, they are removed from the IdentifierInfo::FETokenInfo field.
Steve Naroff640db422007-10-10 17:45:44 +0000202void MinimalAction::ActOnPopScope(SourceLocation Loc, Scope *S) {
Chris Lattnere1ae1e92009-01-18 09:39:41 +0000203 TypeNameInfoTable &Table = *getTable(TypeNameInfoTablePtr);
204
Reid Spencer5f016e22007-07-11 17:01:13 +0000205 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
206 I != E; ++I) {
207 IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I);
208 TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>();
209 assert(TI && "This decl didn't get pushed??");
210
211 if (TI) {
212 TypeNameInfo *Next = TI->Prev;
Chris Lattnere1ae1e92009-01-18 09:39:41 +0000213 Table.DeleteEntry(TI);
Reid Spencer5f016e22007-07-11 17:01:13 +0000214
215 II.setFETokenInfo(Next);
216 }
217 }
218}