blob: ee23c0038ac088ea8a141d4add3c94c8cbf0e9c9 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- MinimalAction.cpp - Implement the MinimalAction class ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner53b63492009-05-01 16:33:20 +000017#include "clang/Basic/TargetInfo.h"
Chris Lattnerc5583732009-01-18 09:39:41 +000018#include "llvm/Support/Allocator.h"
19#include "llvm/Support/RecyclingAllocator.h"
Chris Lattnerd042d0f2009-03-05 01:25:28 +000020#include "llvm/Support/raw_ostream.h"
Chris Lattner4b009652007-07-25 00:24:17 +000021using namespace clang;
22
Sebastian Redl76bb8ec2009-03-15 17:47:39 +000023/// Out-of-line virtual destructor to provide home for ActionBase class.
Chris Lattner6b3833c2009-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 Lattner5261d0c2009-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) {
Chris Lattner6b3833c2009-03-05 07:24:28 +000037
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;
Chris Lattner5261d0c2009-03-28 19:18:32 +000042 return DeclPtrTy();
Chris Lattner6b3833c2009-03-05 07:24:28 +000043}
44
Douglas Gregor683a1142009-06-20 00:51:54 +000045// Defined out-of-line here because of dependecy on AttributeList
46Action::DeclPtrTy Action::ActOnUsingDeclaration(Scope *CurScope,
Anders Carlssone16b4fe2009-08-29 19:54:19 +000047 AccessSpecifier AS,
48 SourceLocation UsingLoc,
49 const CXXScopeSpec &SS,
50 SourceLocation IdentLoc,
51 IdentifierInfo *TargetName,
52 OverloadedOperatorKind Op,
53 AttributeList *AttrList,
54 bool IsTypeName) {
Douglas Gregor683a1142009-06-20 00:51:54 +000055
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
Chris Lattner6b3833c2009-03-05 07:24:28 +000063
Chris Lattnerc309ade2009-03-05 08:00:35 +000064void PrettyStackTraceActionsDecl::print(llvm::raw_ostream &OS) const {
Chris Lattnerd042d0f2009-03-05 01:25:28 +000065 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
Chris Lattner4b009652007-07-25 00:24:17 +000078/// TypeNameInfo - A link exists here for each scope that an identifier is
79/// defined.
Chris Lattnerc5583732009-01-18 09:39:41 +000080namespace {
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>();
Chris Lattnerb59c4a72009-03-24 17:05:27 +000096 new (TI) TypeNameInfo(isTypename, II->getFETokenInfo<TypeNameInfo>());
Chris Lattnerc5583732009-01-18 09:39:41 +000097 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}
Chris Lattner4b009652007-07-25 00:24:17 +0000110
Daniel Dunbar747a95e2008-10-31 08:56:51 +0000111MinimalAction::MinimalAction(Preprocessor &pp)
Chris Lattnerc5583732009-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 Dunbar747a95e2008-10-31 08:56:51 +0000119
120void MinimalAction::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
Steve Naroffae84af82007-10-31 18:42:27 +0000121 TUScope = S;
Chris Lattnerc5583732009-01-18 09:39:41 +0000122
123 TypeNameInfoTable &TNIT = *getTable(TypeNameInfoTablePtr);
Chris Lattner53b63492009-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 }
Chris Lattnerc5583732009-01-18 09:39:41 +0000130
Chris Lattner53b63492009-05-01 16:33:20 +0000131 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 }
Steve Naroffae84af82007-10-31 18:42:27 +0000138}
139
Chris Lattner4b009652007-07-25 00:24:17 +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.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000143///
144/// FIXME: Use the passed CXXScopeSpec for accurate C++ type checking.
Argiris Kirtzidis46403632008-08-01 10:35:27 +0000145Action::TypeTy *
Douglas Gregor1075a162009-02-04 17:00:24 +0000146MinimalAction::getTypeName(IdentifierInfo &II, SourceLocation Loc,
Douglas Gregor64037c82009-08-26 18:27:52 +0000147 Scope *S, const CXXScopeSpec *SS,
148 bool isClassName) {
Chris Lattner4b009652007-07-25 00:24:17 +0000149 if (TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>())
150 if (TI->isTypeName)
151 return TI;
152 return 0;
153}
154
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000155/// isCurrentClassName - Always returns false, because MinimalAction
156/// does not support C++ classes with constructors.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000157bool MinimalAction::isCurrentClassName(const IdentifierInfo &, Scope *,
158 const CXXScopeSpec *) {
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000159 return false;
160}
161
Douglas Gregor0c281a82009-02-25 19:37:18 +0000162TemplateNameKind
Douglas Gregoraabb8502009-03-31 00:43:58 +0000163MinimalAction::isTemplateName(const IdentifierInfo &II, Scope *S,
Douglas Gregorc03d3302009-08-25 22:51:20 +0000164 const CXXScopeSpec *SS,
165 bool EnteringScope,
166 TemplateTy &TemplateDecl) {
Douglas Gregor8e458f42009-02-09 18:46:07 +0000167 return TNK_Non_template;
Douglas Gregor2fa10442008-12-18 19:37:40 +0000168}
169
Steve Naroff0acc9c92007-09-15 18:49:24 +0000170/// ActOnDeclarator - If this is a typedef declarator, we modify the
Chris Lattner4b009652007-07-25 00:24:17 +0000171/// IdentifierInfo::FETokenInfo field to keep track of this fact, until S is
172/// popped.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000173Action::DeclPtrTy
Chris Lattnera17991f2009-03-29 16:50:03 +0000174MinimalAction::ActOnDeclarator(Scope *S, Declarator &D) {
Chris Lattner4b009652007-07-25 00:24:17 +0000175 IdentifierInfo *II = D.getIdentifier();
176
177 // If there is no identifier associated with this declarator, bail out.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000178 if (II == 0) return DeclPtrTy();
Chris Lattner4b009652007-07-25 00:24:17 +0000179
180 TypeNameInfo *weCurrentlyHaveTypeInfo = II->getFETokenInfo<TypeNameInfo>();
181 bool isTypeName =
182 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef;
183
184 // this check avoids creating TypeNameInfo objects for the common case.
Ted Kremeneka3195a32008-08-04 22:51:42 +0000185 // It does need to handle the uncommon case of shadowing a typedef name with a
Chris Lattner4b009652007-07-25 00:24:17 +0000186 // non-typedef name. e.g. { typedef int a; a xx; { int a; } }
187 if (weCurrentlyHaveTypeInfo || isTypeName) {
Chris Lattnerc5583732009-01-18 09:39:41 +0000188 // Allocate and add the 'TypeNameInfo' "decl".
189 getTable(TypeNameInfoTablePtr)->AddEntry(isTypeName, II);
Chris Lattner4b009652007-07-25 00:24:17 +0000190
191 // Remember that this needs to be removed when the scope is popped.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000192 S->AddDecl(DeclPtrTy::make(II));
Chris Lattner4b009652007-07-25 00:24:17 +0000193 }
Chris Lattner5261d0c2009-03-28 19:18:32 +0000194 return DeclPtrTy();
Chris Lattner4b009652007-07-25 00:24:17 +0000195}
196
Chris Lattner5261d0c2009-03-28 19:18:32 +0000197Action::DeclPtrTy
Chris Lattner51002752008-07-21 07:13:18 +0000198MinimalAction::ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
Chris Lattnerae1ae492008-07-26 04:13:19 +0000199 IdentifierInfo *ClassName,
200 SourceLocation ClassLoc,
201 IdentifierInfo *SuperName,
202 SourceLocation SuperLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +0000203 const DeclPtrTy *ProtoRefs,
Chris Lattnerae1ae492008-07-26 04:13:19 +0000204 unsigned NumProtocols,
205 SourceLocation EndProtoLoc,
206 AttributeList *AttrList) {
Chris Lattnerc5583732009-01-18 09:39:41 +0000207 // Allocate and add the 'TypeNameInfo' "decl".
208 getTable(TypeNameInfoTablePtr)->AddEntry(true, ClassName);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000209 return DeclPtrTy();
Steve Naroff81f1bba2007-09-06 21:24:23 +0000210}
211
Steve Naroffb4dfe362007-10-02 22:39:18 +0000212/// ActOnForwardClassDeclaration -
Chris Lattner4b009652007-07-25 00:24:17 +0000213/// Scope will always be top level file scope.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000214Action::DeclPtrTy
Steve Naroff415c1832007-10-10 17:32:04 +0000215MinimalAction::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Steve Naroff81f1bba2007-09-06 21:24:23 +0000216 IdentifierInfo **IdentList, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +0000217 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnerc5583732009-01-18 09:39:41 +0000218 // Allocate and add the 'TypeNameInfo' "decl".
219 getTable(TypeNameInfoTablePtr)->AddEntry(true, IdentList[i]);
Chris Lattner4b009652007-07-25 00:24:17 +0000220
221 // Remember that this needs to be removed when the scope is popped.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000222 TUScope->AddDecl(DeclPtrTy::make(IdentList[i]));
Chris Lattner4b009652007-07-25 00:24:17 +0000223 }
Chris Lattner5261d0c2009-03-28 19:18:32 +0000224 return DeclPtrTy();
Chris Lattner4b009652007-07-25 00:24:17 +0000225}
226
Ted Kremeneka3195a32008-08-04 22:51:42 +0000227/// ActOnPopScope - When a scope is popped, if any typedefs are now
228/// out-of-scope, they are removed from the IdentifierInfo::FETokenInfo field.
Steve Naroffbd5c5fb2007-10-10 17:45:44 +0000229void MinimalAction::ActOnPopScope(SourceLocation Loc, Scope *S) {
Chris Lattnerc5583732009-01-18 09:39:41 +0000230 TypeNameInfoTable &Table = *getTable(TypeNameInfoTablePtr);
231
Chris Lattner4b009652007-07-25 00:24:17 +0000232 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
233 I != E; ++I) {
Chris Lattner5261d0c2009-03-28 19:18:32 +0000234 IdentifierInfo &II = *(*I).getAs<IdentifierInfo>();
Chris Lattner4b009652007-07-25 00:24:17 +0000235 TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>();
236 assert(TI && "This decl didn't get pushed??");
237
238 if (TI) {
239 TypeNameInfo *Next = TI->Prev;
Chris Lattnerc5583732009-01-18 09:39:41 +0000240 Table.DeleteEntry(TI);
Chris Lattner4b009652007-07-25 00:24:17 +0000241
242 II.setFETokenInfo(Next);
243 }
244 }
245}