blob: 9ded366b29f91b5c23719d8338725395194fc5c1 [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) {
Chris Lattner0102c302009-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 Lattnerb28317a2009-03-28 19:18:32 +000042 return DeclPtrTy();
Chris Lattner0102c302009-03-05 07:24:28 +000043}
44
Douglas Gregor9cfbe482009-06-20 00:51:54 +000045// Defined out-of-line here because of dependecy on AttributeList
46Action::DeclPtrTy Action::ActOnUsingDeclaration(Scope *CurScope,
47 SourceLocation UsingLoc,
48 const CXXScopeSpec &SS,
49 SourceLocation IdentLoc,
50 IdentifierInfo *TargetName,
51 AttributeList *AttrList,
52 bool IsTypeName) {
53
54 // FIXME: Parser seems to assume that Action::ActOn* takes ownership over
55 // passed AttributeList, however other actions don't free it, is it
56 // temporary state or bug?
57 delete AttrList;
58 return DeclPtrTy();
59}
60
Chris Lattner0102c302009-03-05 07:24:28 +000061
Chris Lattner49f28ca2009-03-05 08:00:35 +000062void PrettyStackTraceActionsDecl::print(llvm::raw_ostream &OS) const {
Chris Lattner21ff9c92009-03-05 01:25:28 +000063 if (Loc.isValid()) {
64 Loc.print(OS, SM);
65 OS << ": ";
66 }
67 OS << Message;
68
69 std::string Name = Actions.getDeclName(TheDecl);
70 if (!Name.empty())
71 OS << " '" << Name << '\'';
72
73 OS << '\n';
74}
75
Reid Spencer5f016e22007-07-11 17:01:13 +000076/// TypeNameInfo - A link exists here for each scope that an identifier is
77/// defined.
Chris Lattnere1ae1e92009-01-18 09:39:41 +000078namespace {
79 struct TypeNameInfo {
80 TypeNameInfo *Prev;
81 bool isTypeName;
82
83 TypeNameInfo(bool istypename, TypeNameInfo *prev) {
84 isTypeName = istypename;
85 Prev = prev;
86 }
87 };
88
89 struct TypeNameInfoTable {
90 llvm::RecyclingAllocator<llvm::BumpPtrAllocator, TypeNameInfo> Allocator;
91
92 void AddEntry(bool isTypename, IdentifierInfo *II) {
93 TypeNameInfo *TI = Allocator.Allocate<TypeNameInfo>();
Chris Lattnerf26d5102009-03-24 17:05:27 +000094 new (TI) TypeNameInfo(isTypename, II->getFETokenInfo<TypeNameInfo>());
Chris Lattnere1ae1e92009-01-18 09:39:41 +000095 II->setFETokenInfo(TI);
96 }
97
98 void DeleteEntry(TypeNameInfo *Entry) {
99 Entry->~TypeNameInfo();
100 Allocator.Deallocate(Entry);
101 }
102 };
103}
104
105static TypeNameInfoTable *getTable(void *TP) {
106 return static_cast<TypeNameInfoTable*>(TP);
107}
Reid Spencer5f016e22007-07-11 17:01:13 +0000108
Daniel Dunbare10b0f22008-10-31 08:56:51 +0000109MinimalAction::MinimalAction(Preprocessor &pp)
Chris Lattnere1ae1e92009-01-18 09:39:41 +0000110 : Idents(pp.getIdentifierTable()), PP(pp) {
111 TypeNameInfoTablePtr = new TypeNameInfoTable();
112}
113
114MinimalAction::~MinimalAction() {
115 delete getTable(TypeNameInfoTablePtr);
116}
Daniel Dunbare10b0f22008-10-31 08:56:51 +0000117
118void MinimalAction::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
Steve Naroff8ee529b2007-10-31 18:42:27 +0000119 TUScope = S;
Chris Lattnere1ae1e92009-01-18 09:39:41 +0000120
121 TypeNameInfoTable &TNIT = *getTable(TypeNameInfoTablePtr);
Chris Lattnerace65072009-05-01 16:33:20 +0000122
123 if (PP.getTargetInfo().getPointerWidth(0) >= 64) {
124 // Install [u]int128_t for 64-bit targets.
125 TNIT.AddEntry(true, &Idents.get("__int128_t"));
126 TNIT.AddEntry(true, &Idents.get("__uint128_t"));
127 }
Chris Lattnere1ae1e92009-01-18 09:39:41 +0000128
Chris Lattnerace65072009-05-01 16:33:20 +0000129 if (PP.getLangOptions().ObjC1) {
130 // Recognize the ObjC built-in type identifiers as types.
131 TNIT.AddEntry(true, &Idents.get("id"));
132 TNIT.AddEntry(true, &Idents.get("SEL"));
133 TNIT.AddEntry(true, &Idents.get("Class"));
134 TNIT.AddEntry(true, &Idents.get("Protocol"));
135 }
Steve Naroff8ee529b2007-10-31 18:42:27 +0000136}
137
Reid Spencer5f016e22007-07-11 17:01:13 +0000138/// isTypeName - This looks at the IdentifierInfo::FETokenInfo field to
139/// determine whether the name is a type name (objc class name or typedef) or
140/// not in this scope.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000141///
142/// FIXME: Use the passed CXXScopeSpec for accurate C++ type checking.
Argyrios Kyrtzidis39caa082008-08-01 10:35:27 +0000143Action::TypeTy *
Douglas Gregorb696ea32009-02-04 17:00:24 +0000144MinimalAction::getTypeName(IdentifierInfo &II, SourceLocation Loc,
145 Scope *S, const CXXScopeSpec *SS) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000146 if (TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>())
147 if (TI->isTypeName)
148 return TI;
149 return 0;
150}
151
Douglas Gregorb48fe382008-10-31 09:07:45 +0000152/// isCurrentClassName - Always returns false, because MinimalAction
153/// does not support C++ classes with constructors.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000154bool MinimalAction::isCurrentClassName(const IdentifierInfo &, Scope *,
155 const CXXScopeSpec *) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000156 return false;
157}
158
Douglas Gregor39a8de12009-02-25 19:37:18 +0000159TemplateNameKind
Douglas Gregorc45c2322009-03-31 00:43:58 +0000160MinimalAction::isTemplateName(const IdentifierInfo &II, Scope *S,
Douglas Gregor7532dc62009-03-30 22:58:21 +0000161 TemplateTy &TemplateDecl,
Douglas Gregor55f6b142009-02-09 18:46:07 +0000162 const CXXScopeSpec *SS) {
163 return TNK_Non_template;
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000164}
165
Steve Naroff08d92e42007-09-15 18:49:24 +0000166/// ActOnDeclarator - If this is a typedef declarator, we modify the
Reid Spencer5f016e22007-07-11 17:01:13 +0000167/// IdentifierInfo::FETokenInfo field to keep track of this fact, until S is
168/// popped.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000169Action::DeclPtrTy
Chris Lattner682bf922009-03-29 16:50:03 +0000170MinimalAction::ActOnDeclarator(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 IdentifierInfo *II = D.getIdentifier();
172
173 // If there is no identifier associated with this declarator, bail out.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000174 if (II == 0) return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +0000175
176 TypeNameInfo *weCurrentlyHaveTypeInfo = II->getFETokenInfo<TypeNameInfo>();
177 bool isTypeName =
178 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef;
179
180 // this check avoids creating TypeNameInfo objects for the common case.
Ted Kremeneka34ea072008-08-04 22:51:42 +0000181 // It does need to handle the uncommon case of shadowing a typedef name with a
Reid Spencer5f016e22007-07-11 17:01:13 +0000182 // non-typedef name. e.g. { typedef int a; a xx; { int a; } }
183 if (weCurrentlyHaveTypeInfo || isTypeName) {
Chris Lattnere1ae1e92009-01-18 09:39:41 +0000184 // Allocate and add the 'TypeNameInfo' "decl".
185 getTable(TypeNameInfoTablePtr)->AddEntry(isTypeName, II);
Reid Spencer5f016e22007-07-11 17:01:13 +0000186
187 // Remember that this needs to be removed when the scope is popped.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000188 S->AddDecl(DeclPtrTy::make(II));
Reid Spencer5f016e22007-07-11 17:01:13 +0000189 }
Chris Lattnerb28317a2009-03-28 19:18:32 +0000190 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +0000191}
192
Chris Lattnerb28317a2009-03-28 19:18:32 +0000193Action::DeclPtrTy
Chris Lattnercfb0ef52008-07-21 07:13:18 +0000194MinimalAction::ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
Chris Lattner06036d32008-07-26 04:13:19 +0000195 IdentifierInfo *ClassName,
196 SourceLocation ClassLoc,
197 IdentifierInfo *SuperName,
198 SourceLocation SuperLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000199 const DeclPtrTy *ProtoRefs,
Chris Lattner06036d32008-07-26 04:13:19 +0000200 unsigned NumProtocols,
201 SourceLocation EndProtoLoc,
202 AttributeList *AttrList) {
Chris Lattnere1ae1e92009-01-18 09:39:41 +0000203 // Allocate and add the 'TypeNameInfo' "decl".
204 getTable(TypeNameInfoTablePtr)->AddEntry(true, ClassName);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000205 return DeclPtrTy();
Steve Naroff3536b442007-09-06 21:24:23 +0000206}
207
Steve Naroff37e58d12007-10-02 22:39:18 +0000208/// ActOnForwardClassDeclaration -
Reid Spencer5f016e22007-07-11 17:01:13 +0000209/// Scope will always be top level file scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000210Action::DeclPtrTy
Steve Naroffe440eb82007-10-10 17:32:04 +0000211MinimalAction::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Steve Naroff3536b442007-09-06 21:24:23 +0000212 IdentifierInfo **IdentList, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000213 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnere1ae1e92009-01-18 09:39:41 +0000214 // Allocate and add the 'TypeNameInfo' "decl".
215 getTable(TypeNameInfoTablePtr)->AddEntry(true, IdentList[i]);
Reid Spencer5f016e22007-07-11 17:01:13 +0000216
217 // Remember that this needs to be removed when the scope is popped.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000218 TUScope->AddDecl(DeclPtrTy::make(IdentList[i]));
Reid Spencer5f016e22007-07-11 17:01:13 +0000219 }
Chris Lattnerb28317a2009-03-28 19:18:32 +0000220 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +0000221}
222
Ted Kremeneka34ea072008-08-04 22:51:42 +0000223/// ActOnPopScope - When a scope is popped, if any typedefs are now
224/// out-of-scope, they are removed from the IdentifierInfo::FETokenInfo field.
Steve Naroff640db422007-10-10 17:45:44 +0000225void MinimalAction::ActOnPopScope(SourceLocation Loc, Scope *S) {
Chris Lattnere1ae1e92009-01-18 09:39:41 +0000226 TypeNameInfoTable &Table = *getTable(TypeNameInfoTablePtr);
227
Reid Spencer5f016e22007-07-11 17:01:13 +0000228 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
229 I != E; ++I) {
Chris Lattnerb28317a2009-03-28 19:18:32 +0000230 IdentifierInfo &II = *(*I).getAs<IdentifierInfo>();
Reid Spencer5f016e22007-07-11 17:01:13 +0000231 TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>();
232 assert(TI && "This decl didn't get pushed??");
233
234 if (TI) {
235 TypeNameInfo *Next = TI->Prev;
Chris Lattnere1ae1e92009-01-18 09:39:41 +0000236 Table.DeleteEntry(TI);
Reid Spencer5f016e22007-07-11 17:01:13 +0000237
238 II.setFETokenInfo(Next);
239 }
240 }
241}