blob: 5f48897235ab0aef57eb667a2a6a4e756562389b [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 Lattnerace65072009-05-01 16:33:20 +000017#include "clang/Basic/TargetInfo.h"
Chris Lattnere1ae1e92009-01-18 09:39:41 +000018#include "llvm/Support/Allocator.h"
19#include "llvm/Support/RecyclingAllocator.h"
Chris Lattner21ff9c92009-03-05 01:25:28 +000020#include "llvm/Support/raw_ostream.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021using namespace clang;
22
Sebastian Redlf53597f2009-03-15 17:47:39 +000023/// Out-of-line virtual destructor to provide home for ActionBase class.
Chris Lattner0102c302009-03-05 07:24:28 +000024ActionBase::~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
Chris Lattnerb28317a2009-03-28 19:18:32 +000030Action::DeclPtrTy Action::ActOnUsingDirective(Scope *CurScope,
31 SourceLocation UsingLoc,
32 SourceLocation NamespcLoc,
33 const CXXScopeSpec &SS,
34 SourceLocation IdentLoc,
35 IdentifierInfo *NamespcName,
36 AttributeList *AttrList) {
Mike Stump1eb44332009-09-09 15:08:12 +000037
Chris Lattner0102c302009-03-05 07:24:28 +000038 // 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;
Chris Lattnerb28317a2009-03-28 19:18:32 +000042 return DeclPtrTy();
Chris Lattner0102c302009-03-05 07:24:28 +000043}
44
Douglas Gregor12c118a2009-11-04 16:30:06 +000045// Defined out-of-line here because of dependency on AttributeList
Douglas Gregor9cfbe482009-06-20 00:51:54 +000046Action::DeclPtrTy Action::ActOnUsingDeclaration(Scope *CurScope,
Anders Carlsson595adc12009-08-29 19:54:19 +000047 AccessSpecifier AS,
John McCall60fa3cf2009-12-11 02:10:03 +000048 bool HasUsingKeyword,
Anders Carlsson595adc12009-08-29 19:54:19 +000049 SourceLocation UsingLoc,
50 const CXXScopeSpec &SS,
Douglas Gregor12c118a2009-11-04 16:30:06 +000051 UnqualifiedId &Name,
Anders Carlsson595adc12009-08-29 19:54:19 +000052 AttributeList *AttrList,
John McCall7ba107a2009-11-18 02:36:19 +000053 bool IsTypeName,
54 SourceLocation TypenameLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +000055
Douglas Gregor9cfbe482009-06-20 00:51:54 +000056 // 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
Chris Lattner0102c302009-03-05 07:24:28 +000063
Chris Lattner49f28ca2009-03-05 08:00:35 +000064void PrettyStackTraceActionsDecl::print(llvm::raw_ostream &OS) const {
Chris Lattner21ff9c92009-03-05 01:25:28 +000065 if (Loc.isValid()) {
66 Loc.print(OS, SM);
67 OS << ": ";
68 }
69 OS << Message;
Mike Stump1eb44332009-09-09 15:08:12 +000070
Chris Lattner21ff9c92009-03-05 01:25:28 +000071 std::string Name = Actions.getDeclName(TheDecl);
72 if (!Name.empty())
73 OS << " '" << Name << '\'';
Mike Stump1eb44332009-09-09 15:08:12 +000074
Chris Lattner21ff9c92009-03-05 01:25:28 +000075 OS << '\n';
76}
77
Reid Spencer5f016e22007-07-11 17:01:13 +000078/// TypeNameInfo - A link exists here for each scope that an identifier is
79/// defined.
Chris Lattnere1ae1e92009-01-18 09:39:41 +000080namespace {
81 struct TypeNameInfo {
82 TypeNameInfo *Prev;
83 bool isTypeName;
Mike Stump1eb44332009-09-09 15:08:12 +000084
Chris Lattnere1ae1e92009-01-18 09:39:41 +000085 TypeNameInfo(bool istypename, TypeNameInfo *prev) {
86 isTypeName = istypename;
87 Prev = prev;
88 }
89 };
90
91 struct TypeNameInfoTable {
92 llvm::RecyclingAllocator<llvm::BumpPtrAllocator, TypeNameInfo> Allocator;
Mike Stump1eb44332009-09-09 15:08:12 +000093
Chris Lattnere1ae1e92009-01-18 09:39:41 +000094 void AddEntry(bool isTypename, IdentifierInfo *II) {
95 TypeNameInfo *TI = Allocator.Allocate<TypeNameInfo>();
Chris Lattnerf26d5102009-03-24 17:05:27 +000096 new (TI) TypeNameInfo(isTypename, II->getFETokenInfo<TypeNameInfo>());
Chris Lattnere1ae1e92009-01-18 09:39:41 +000097 II->setFETokenInfo(TI);
98 }
Mike Stump1eb44332009-09-09 15:08:12 +000099
Chris Lattnere1ae1e92009-01-18 09:39:41 +0000100 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}
Reid Spencer5f016e22007-07-11 17:01:13 +0000110
Mike Stump1eb44332009-09-09 15:08:12 +0000111MinimalAction::MinimalAction(Preprocessor &pp)
Chris Lattnere1ae1e92009-01-18 09:39:41 +0000112 : Idents(pp.getIdentifierTable()), PP(pp) {
113 TypeNameInfoTablePtr = new TypeNameInfoTable();
114}
115
116MinimalAction::~MinimalAction() {
117 delete getTable(TypeNameInfoTablePtr);
118}
Daniel Dunbare10b0f22008-10-31 08:56:51 +0000119
120void MinimalAction::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
Steve Naroff8ee529b2007-10-31 18:42:27 +0000121 TUScope = S;
Chris Lattnere1ae1e92009-01-18 09:39:41 +0000122
123 TypeNameInfoTable &TNIT = *getTable(TypeNameInfoTablePtr);
Chris Lattnerace65072009-05-01 16:33:20 +0000124
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 }
Mike Stump1eb44332009-09-09 15:08:12 +0000130
Chris Lattnerace65072009-05-01 16:33:20 +0000131 if (PP.getLangOptions().ObjC1) {
Mike Stump1eb44332009-09-09 15:08:12 +0000132 // Recognize the ObjC built-in type identifiers as types.
Chris Lattnerace65072009-05-01 16:33:20 +0000133 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 }
Steve Naroff8ee529b2007-10-31 18:42:27 +0000138}
139
Reid Spencer5f016e22007-07-11 17:01:13 +0000140/// 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.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000143///
144/// FIXME: Use the passed CXXScopeSpec for accurate C++ type checking.
Argyrios Kyrtzidis39caa082008-08-01 10:35:27 +0000145Action::TypeTy *
Douglas Gregorb696ea32009-02-04 17:00:24 +0000146MinimalAction::getTypeName(IdentifierInfo &II, SourceLocation Loc,
Douglas Gregor42c39f32009-08-26 18:27:52 +0000147 Scope *S, const CXXScopeSpec *SS,
Douglas Gregorf6e6fc82009-11-20 22:03:38 +0000148 bool isClassName, TypeTy *ObjectType) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000149 if (TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>())
150 if (TI->isTypeName)
151 return TI;
152 return 0;
153}
154
Douglas Gregorb48fe382008-10-31 09:07:45 +0000155/// isCurrentClassName - Always returns false, because MinimalAction
156/// does not support C++ classes with constructors.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000157bool MinimalAction::isCurrentClassName(const IdentifierInfo &, Scope *,
158 const CXXScopeSpec *) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000159 return false;
160}
161
Mike Stump1eb44332009-09-09 15:08:12 +0000162TemplateNameKind
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000163MinimalAction::isTemplateName(Scope *S,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000164 const CXXScopeSpec &SS,
165 UnqualifiedId &Name,
Mike Stump1eb44332009-09-09 15:08:12 +0000166 TypeTy *ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000167 bool EnteringScope,
168 TemplateTy &TemplateDecl) {
Douglas Gregor55f6b142009-02-09 18:46:07 +0000169 return TNK_Non_template;
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000170}
171
Steve Naroff08d92e42007-09-15 18:49:24 +0000172/// ActOnDeclarator - If this is a typedef declarator, we modify the
Reid Spencer5f016e22007-07-11 17:01:13 +0000173/// IdentifierInfo::FETokenInfo field to keep track of this fact, until S is
174/// popped.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000175Action::DeclPtrTy
Chris Lattner682bf922009-03-29 16:50:03 +0000176MinimalAction::ActOnDeclarator(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000177 IdentifierInfo *II = D.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Reid Spencer5f016e22007-07-11 17:01:13 +0000179 // If there is no identifier associated with this declarator, bail out.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000180 if (II == 0) return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +0000181
Reid Spencer5f016e22007-07-11 17:01:13 +0000182 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.
Ted Kremeneka34ea072008-08-04 22:51:42 +0000187 // It does need to handle the uncommon case of shadowing a typedef name with a
Reid Spencer5f016e22007-07-11 17:01:13 +0000188 // non-typedef name. e.g. { typedef int a; a xx; { int a; } }
189 if (weCurrentlyHaveTypeInfo || isTypeName) {
Chris Lattnere1ae1e92009-01-18 09:39:41 +0000190 // Allocate and add the 'TypeNameInfo' "decl".
191 getTable(TypeNameInfoTablePtr)->AddEntry(isTypeName, II);
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Reid Spencer5f016e22007-07-11 17:01:13 +0000193 // Remember that this needs to be removed when the scope is popped.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000194 S->AddDecl(DeclPtrTy::make(II));
Mike Stump1eb44332009-09-09 15:08:12 +0000195 }
Chris Lattnerb28317a2009-03-28 19:18:32 +0000196 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +0000197}
198
Chris Lattnerb28317a2009-03-28 19:18:32 +0000199Action::DeclPtrTy
Chris Lattnercfb0ef52008-07-21 07:13:18 +0000200MinimalAction::ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
Chris Lattner06036d32008-07-26 04:13:19 +0000201 IdentifierInfo *ClassName,
202 SourceLocation ClassLoc,
203 IdentifierInfo *SuperName,
204 SourceLocation SuperLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000205 const DeclPtrTy *ProtoRefs,
Chris Lattner06036d32008-07-26 04:13:19 +0000206 unsigned NumProtocols,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000207 const SourceLocation *ProtoLocs,
Chris Lattner06036d32008-07-26 04:13:19 +0000208 SourceLocation EndProtoLoc,
209 AttributeList *AttrList) {
Chris Lattnere1ae1e92009-01-18 09:39:41 +0000210 // Allocate and add the 'TypeNameInfo' "decl".
211 getTable(TypeNameInfoTablePtr)->AddEntry(true, ClassName);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000212 return DeclPtrTy();
Steve Naroff3536b442007-09-06 21:24:23 +0000213}
214
Mike Stump1eb44332009-09-09 15:08:12 +0000215/// ActOnForwardClassDeclaration -
216/// Scope will always be top level file scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000217Action::DeclPtrTy
Steve Naroffe440eb82007-10-10 17:32:04 +0000218MinimalAction::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Ted Kremenekc09cba62009-11-17 23:12:20 +0000219 IdentifierInfo **IdentList,
220 SourceLocation *IdentLocs,
221 unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000222 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnere1ae1e92009-01-18 09:39:41 +0000223 // Allocate and add the 'TypeNameInfo' "decl".
224 getTable(TypeNameInfoTablePtr)->AddEntry(true, IdentList[i]);
Mike Stump1eb44332009-09-09 15:08:12 +0000225
Reid Spencer5f016e22007-07-11 17:01:13 +0000226 // Remember that this needs to be removed when the scope is popped.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000227 TUScope->AddDecl(DeclPtrTy::make(IdentList[i]));
Reid Spencer5f016e22007-07-11 17:01:13 +0000228 }
Chris Lattnerb28317a2009-03-28 19:18:32 +0000229 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +0000230}
231
Ted Kremeneka34ea072008-08-04 22:51:42 +0000232/// ActOnPopScope - When a scope is popped, if any typedefs are now
233/// out-of-scope, they are removed from the IdentifierInfo::FETokenInfo field.
Steve Naroff640db422007-10-10 17:45:44 +0000234void MinimalAction::ActOnPopScope(SourceLocation Loc, Scope *S) {
Chris Lattnere1ae1e92009-01-18 09:39:41 +0000235 TypeNameInfoTable &Table = *getTable(TypeNameInfoTablePtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Reid Spencer5f016e22007-07-11 17:01:13 +0000237 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
238 I != E; ++I) {
Chris Lattnerb28317a2009-03-28 19:18:32 +0000239 IdentifierInfo &II = *(*I).getAs<IdentifierInfo>();
Reid Spencer5f016e22007-07-11 17:01:13 +0000240 TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>();
241 assert(TI && "This decl didn't get pushed??");
Mike Stump1eb44332009-09-09 15:08:12 +0000242
Reid Spencer5f016e22007-07-11 17:01:13 +0000243 if (TI) {
244 TypeNameInfo *Next = TI->Prev;
Chris Lattnere1ae1e92009-01-18 09:39:41 +0000245 Table.DeleteEntry(TI);
Mike Stump1eb44332009-09-09 15:08:12 +0000246
Reid Spencer5f016e22007-07-11 17:01:13 +0000247 II.setFETokenInfo(Next);
248 }
249 }
250}