blob: b7205160c1774368b14609bfc55bc1f82a66433c [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
Douglas Gregor1dbca6e2010-04-14 02:22:16 +000029Action::ObjCMessageKind Action::getObjCMessageKind(Scope *S,
Douglas Gregor1569f952010-04-21 20:38:13 +000030 IdentifierInfo *Name,
Douglas Gregor1dbca6e2010-04-14 02:22:16 +000031 SourceLocation NameLoc,
32 bool IsSuper,
Douglas Gregor1569f952010-04-21 20:38:13 +000033 bool HasTrailingDot,
34 TypeTy *&ReceiverType) {
35 ReceiverType = 0;
36
Douglas Gregor1dbca6e2010-04-14 02:22:16 +000037 if (IsSuper && !HasTrailingDot && S->isInObjcMethodScope())
38 return ObjCSuperMessage;
39
Douglas Gregor1569f952010-04-21 20:38:13 +000040 if (TypeTy *TyName = getTypeName(*Name, NameLoc, S)) {
41 DeclSpec DS;
42 const char *PrevSpec = 0;
43 unsigned DiagID = 0;
44 if (!DS.SetTypeSpecType(DeclSpec::TST_typename, NameLoc, PrevSpec,
45 DiagID, TyName)) {
46 DS.SetRangeEnd(NameLoc);
47 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
48 TypeResult Ty = ActOnTypeName(S, DeclaratorInfo);
49 if (!Ty.isInvalid())
50 ReceiverType = Ty.get();
51 }
Douglas Gregor1dbca6e2010-04-14 02:22:16 +000052 return ObjCClassMessage;
Douglas Gregor1569f952010-04-21 20:38:13 +000053 }
Douglas Gregor1dbca6e2010-04-14 02:22:16 +000054
55 return ObjCInstanceMessage;
56}
57
Chris Lattner0102c302009-03-05 07:24:28 +000058// Defined out-of-line here because of dependecy on AttributeList
Chris Lattnerb28317a2009-03-28 19:18:32 +000059Action::DeclPtrTy Action::ActOnUsingDirective(Scope *CurScope,
60 SourceLocation UsingLoc,
61 SourceLocation NamespcLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +000062 CXXScopeSpec &SS,
Chris Lattnerb28317a2009-03-28 19:18:32 +000063 SourceLocation IdentLoc,
64 IdentifierInfo *NamespcName,
65 AttributeList *AttrList) {
Mike Stump1eb44332009-09-09 15:08:12 +000066
Chris Lattner0102c302009-03-05 07:24:28 +000067 // FIXME: Parser seems to assume that Action::ActOn* takes ownership over
68 // passed AttributeList, however other actions don't free it, is it
69 // temporary state or bug?
70 delete AttrList;
Chris Lattnerb28317a2009-03-28 19:18:32 +000071 return DeclPtrTy();
Chris Lattner0102c302009-03-05 07:24:28 +000072}
73
Douglas Gregor12c118a2009-11-04 16:30:06 +000074// Defined out-of-line here because of dependency on AttributeList
Douglas Gregor9cfbe482009-06-20 00:51:54 +000075Action::DeclPtrTy Action::ActOnUsingDeclaration(Scope *CurScope,
Anders Carlsson595adc12009-08-29 19:54:19 +000076 AccessSpecifier AS,
John McCall60fa3cf2009-12-11 02:10:03 +000077 bool HasUsingKeyword,
Anders Carlsson595adc12009-08-29 19:54:19 +000078 SourceLocation UsingLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +000079 CXXScopeSpec &SS,
Douglas Gregor12c118a2009-11-04 16:30:06 +000080 UnqualifiedId &Name,
Anders Carlsson595adc12009-08-29 19:54:19 +000081 AttributeList *AttrList,
John McCall7ba107a2009-11-18 02:36:19 +000082 bool IsTypeName,
83 SourceLocation TypenameLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +000084
Douglas Gregor9cfbe482009-06-20 00:51:54 +000085 // FIXME: Parser seems to assume that Action::ActOn* takes ownership over
86 // passed AttributeList, however other actions don't free it, is it
87 // temporary state or bug?
88 delete AttrList;
89 return DeclPtrTy();
90}
91
Chris Lattner0102c302009-03-05 07:24:28 +000092
Chris Lattner49f28ca2009-03-05 08:00:35 +000093void PrettyStackTraceActionsDecl::print(llvm::raw_ostream &OS) const {
Chris Lattner21ff9c92009-03-05 01:25:28 +000094 if (Loc.isValid()) {
95 Loc.print(OS, SM);
96 OS << ": ";
97 }
98 OS << Message;
Mike Stump1eb44332009-09-09 15:08:12 +000099
Chris Lattner21ff9c92009-03-05 01:25:28 +0000100 std::string Name = Actions.getDeclName(TheDecl);
101 if (!Name.empty())
102 OS << " '" << Name << '\'';
Mike Stump1eb44332009-09-09 15:08:12 +0000103
Chris Lattner21ff9c92009-03-05 01:25:28 +0000104 OS << '\n';
105}
106
Reid Spencer5f016e22007-07-11 17:01:13 +0000107/// TypeNameInfo - A link exists here for each scope that an identifier is
108/// defined.
Chris Lattnere1ae1e92009-01-18 09:39:41 +0000109namespace {
110 struct TypeNameInfo {
111 TypeNameInfo *Prev;
112 bool isTypeName;
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Chris Lattnere1ae1e92009-01-18 09:39:41 +0000114 TypeNameInfo(bool istypename, TypeNameInfo *prev) {
115 isTypeName = istypename;
116 Prev = prev;
117 }
118 };
119
120 struct TypeNameInfoTable {
121 llvm::RecyclingAllocator<llvm::BumpPtrAllocator, TypeNameInfo> Allocator;
Mike Stump1eb44332009-09-09 15:08:12 +0000122
Chris Lattnere1ae1e92009-01-18 09:39:41 +0000123 void AddEntry(bool isTypename, IdentifierInfo *II) {
124 TypeNameInfo *TI = Allocator.Allocate<TypeNameInfo>();
Chris Lattnerf26d5102009-03-24 17:05:27 +0000125 new (TI) TypeNameInfo(isTypename, II->getFETokenInfo<TypeNameInfo>());
Chris Lattnere1ae1e92009-01-18 09:39:41 +0000126 II->setFETokenInfo(TI);
127 }
Mike Stump1eb44332009-09-09 15:08:12 +0000128
Chris Lattnere1ae1e92009-01-18 09:39:41 +0000129 void DeleteEntry(TypeNameInfo *Entry) {
130 Entry->~TypeNameInfo();
131 Allocator.Deallocate(Entry);
132 }
133 };
134}
135
136static TypeNameInfoTable *getTable(void *TP) {
137 return static_cast<TypeNameInfoTable*>(TP);
138}
Reid Spencer5f016e22007-07-11 17:01:13 +0000139
Mike Stump1eb44332009-09-09 15:08:12 +0000140MinimalAction::MinimalAction(Preprocessor &pp)
Chris Lattnere1ae1e92009-01-18 09:39:41 +0000141 : Idents(pp.getIdentifierTable()), PP(pp) {
142 TypeNameInfoTablePtr = new TypeNameInfoTable();
143}
144
145MinimalAction::~MinimalAction() {
146 delete getTable(TypeNameInfoTablePtr);
147}
Daniel Dunbare10b0f22008-10-31 08:56:51 +0000148
149void MinimalAction::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
Steve Naroff8ee529b2007-10-31 18:42:27 +0000150 TUScope = S;
Chris Lattnere1ae1e92009-01-18 09:39:41 +0000151
152 TypeNameInfoTable &TNIT = *getTable(TypeNameInfoTablePtr);
Chris Lattnerace65072009-05-01 16:33:20 +0000153
154 if (PP.getTargetInfo().getPointerWidth(0) >= 64) {
155 // Install [u]int128_t for 64-bit targets.
156 TNIT.AddEntry(true, &Idents.get("__int128_t"));
157 TNIT.AddEntry(true, &Idents.get("__uint128_t"));
158 }
Mike Stump1eb44332009-09-09 15:08:12 +0000159
Chris Lattnerace65072009-05-01 16:33:20 +0000160 if (PP.getLangOptions().ObjC1) {
Mike Stump1eb44332009-09-09 15:08:12 +0000161 // Recognize the ObjC built-in type identifiers as types.
Chris Lattnerace65072009-05-01 16:33:20 +0000162 TNIT.AddEntry(true, &Idents.get("id"));
163 TNIT.AddEntry(true, &Idents.get("SEL"));
164 TNIT.AddEntry(true, &Idents.get("Class"));
165 TNIT.AddEntry(true, &Idents.get("Protocol"));
166 }
Steve Naroff8ee529b2007-10-31 18:42:27 +0000167}
168
Reid Spencer5f016e22007-07-11 17:01:13 +0000169/// isTypeName - This looks at the IdentifierInfo::FETokenInfo field to
170/// determine whether the name is a type name (objc class name or typedef) or
171/// not in this scope.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000172///
173/// FIXME: Use the passed CXXScopeSpec for accurate C++ type checking.
Argyrios Kyrtzidis39caa082008-08-01 10:35:27 +0000174Action::TypeTy *
Douglas Gregorb696ea32009-02-04 17:00:24 +0000175MinimalAction::getTypeName(IdentifierInfo &II, SourceLocation Loc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000176 Scope *S, CXXScopeSpec *SS,
Douglas Gregorf6e6fc82009-11-20 22:03:38 +0000177 bool isClassName, TypeTy *ObjectType) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 if (TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>())
179 if (TI->isTypeName)
180 return TI;
181 return 0;
182}
183
Douglas Gregorb48fe382008-10-31 09:07:45 +0000184/// isCurrentClassName - Always returns false, because MinimalAction
185/// does not support C++ classes with constructors.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000186bool MinimalAction::isCurrentClassName(const IdentifierInfo &, Scope *,
187 const CXXScopeSpec *) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000188 return false;
189}
190
Mike Stump1eb44332009-09-09 15:08:12 +0000191TemplateNameKind
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000192MinimalAction::isTemplateName(Scope *S,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000193 CXXScopeSpec &SS,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000194 UnqualifiedId &Name,
Mike Stump1eb44332009-09-09 15:08:12 +0000195 TypeTy *ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000196 bool EnteringScope,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000197 TemplateTy &TemplateDecl,
198 bool &MemberOfUnknownSpecialization) {
199 MemberOfUnknownSpecialization = false;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000200 return TNK_Non_template;
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000201}
202
Steve Naroff08d92e42007-09-15 18:49:24 +0000203/// ActOnDeclarator - If this is a typedef declarator, we modify the
Reid Spencer5f016e22007-07-11 17:01:13 +0000204/// IdentifierInfo::FETokenInfo field to keep track of this fact, until S is
205/// popped.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000206Action::DeclPtrTy
Chris Lattner682bf922009-03-29 16:50:03 +0000207MinimalAction::ActOnDeclarator(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000208 IdentifierInfo *II = D.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Reid Spencer5f016e22007-07-11 17:01:13 +0000210 // If there is no identifier associated with this declarator, bail out.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000211 if (II == 0) return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +0000212
Reid Spencer5f016e22007-07-11 17:01:13 +0000213 TypeNameInfo *weCurrentlyHaveTypeInfo = II->getFETokenInfo<TypeNameInfo>();
214 bool isTypeName =
215 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef;
216
217 // this check avoids creating TypeNameInfo objects for the common case.
Ted Kremeneka34ea072008-08-04 22:51:42 +0000218 // It does need to handle the uncommon case of shadowing a typedef name with a
Reid Spencer5f016e22007-07-11 17:01:13 +0000219 // non-typedef name. e.g. { typedef int a; a xx; { int a; } }
220 if (weCurrentlyHaveTypeInfo || isTypeName) {
Chris Lattnere1ae1e92009-01-18 09:39:41 +0000221 // Allocate and add the 'TypeNameInfo' "decl".
222 getTable(TypeNameInfoTablePtr)->AddEntry(isTypeName, II);
Mike Stump1eb44332009-09-09 15:08:12 +0000223
Reid Spencer5f016e22007-07-11 17:01:13 +0000224 // Remember that this needs to be removed when the scope is popped.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000225 S->AddDecl(DeclPtrTy::make(II));
Mike Stump1eb44332009-09-09 15:08:12 +0000226 }
Chris Lattnerb28317a2009-03-28 19:18:32 +0000227 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +0000228}
229
Chris Lattnerb28317a2009-03-28 19:18:32 +0000230Action::DeclPtrTy
Chris Lattnercfb0ef52008-07-21 07:13:18 +0000231MinimalAction::ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
Chris Lattner06036d32008-07-26 04:13:19 +0000232 IdentifierInfo *ClassName,
233 SourceLocation ClassLoc,
234 IdentifierInfo *SuperName,
235 SourceLocation SuperLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000236 const DeclPtrTy *ProtoRefs,
Chris Lattner06036d32008-07-26 04:13:19 +0000237 unsigned NumProtocols,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000238 const SourceLocation *ProtoLocs,
Chris Lattner06036d32008-07-26 04:13:19 +0000239 SourceLocation EndProtoLoc,
240 AttributeList *AttrList) {
Chris Lattnere1ae1e92009-01-18 09:39:41 +0000241 // Allocate and add the 'TypeNameInfo' "decl".
242 getTable(TypeNameInfoTablePtr)->AddEntry(true, ClassName);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000243 return DeclPtrTy();
Steve Naroff3536b442007-09-06 21:24:23 +0000244}
245
Mike Stump1eb44332009-09-09 15:08:12 +0000246/// ActOnForwardClassDeclaration -
247/// Scope will always be top level file scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000248Action::DeclPtrTy
Steve Naroffe440eb82007-10-10 17:32:04 +0000249MinimalAction::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Ted Kremenekc09cba62009-11-17 23:12:20 +0000250 IdentifierInfo **IdentList,
251 SourceLocation *IdentLocs,
252 unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000253 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnere1ae1e92009-01-18 09:39:41 +0000254 // Allocate and add the 'TypeNameInfo' "decl".
255 getTable(TypeNameInfoTablePtr)->AddEntry(true, IdentList[i]);
Mike Stump1eb44332009-09-09 15:08:12 +0000256
Reid Spencer5f016e22007-07-11 17:01:13 +0000257 // Remember that this needs to be removed when the scope is popped.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000258 TUScope->AddDecl(DeclPtrTy::make(IdentList[i]));
Reid Spencer5f016e22007-07-11 17:01:13 +0000259 }
Chris Lattnerb28317a2009-03-28 19:18:32 +0000260 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +0000261}
262
Ted Kremeneka34ea072008-08-04 22:51:42 +0000263/// ActOnPopScope - When a scope is popped, if any typedefs are now
264/// out-of-scope, they are removed from the IdentifierInfo::FETokenInfo field.
Steve Naroff640db422007-10-10 17:45:44 +0000265void MinimalAction::ActOnPopScope(SourceLocation Loc, Scope *S) {
Chris Lattnere1ae1e92009-01-18 09:39:41 +0000266 TypeNameInfoTable &Table = *getTable(TypeNameInfoTablePtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Reid Spencer5f016e22007-07-11 17:01:13 +0000268 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
269 I != E; ++I) {
Chris Lattnerb28317a2009-03-28 19:18:32 +0000270 IdentifierInfo &II = *(*I).getAs<IdentifierInfo>();
Reid Spencer5f016e22007-07-11 17:01:13 +0000271 TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>();
272 assert(TI && "This decl didn't get pushed??");
Mike Stump1eb44332009-09-09 15:08:12 +0000273
Reid Spencer5f016e22007-07-11 17:01:13 +0000274 if (TI) {
275 TypeNameInfo *Next = TI->Prev;
Chris Lattnere1ae1e92009-01-18 09:39:41 +0000276 Table.DeleteEntry(TI);
Mike Stump1eb44332009-09-09 15:08:12 +0000277
Reid Spencer5f016e22007-07-11 17:01:13 +0000278 II.setFETokenInfo(Next);
279 }
280 }
281}