blob: 5238a899574cbdaa0c086fff6e6d8670697005ff [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 Lattnerc5583732009-01-18 09:39:41 +000017#include "llvm/Support/Allocator.h"
18#include "llvm/Support/RecyclingAllocator.h"
Chris Lattnerd042d0f2009-03-05 01:25:28 +000019#include "llvm/Support/raw_ostream.h"
Chris Lattner4b009652007-07-25 00:24:17 +000020using namespace clang;
21
Chris Lattnerd042d0f2009-03-05 01:25:28 +000022void PrettyStackTraceDecl::print(llvm::raw_ostream &OS) const {
23 if (Loc.isValid()) {
24 Loc.print(OS, SM);
25 OS << ": ";
26 }
27 OS << Message;
28
29 std::string Name = Actions.getDeclName(TheDecl);
30 if (!Name.empty())
31 OS << " '" << Name << '\'';
32
33 OS << '\n';
34}
35
Chris Lattner4b009652007-07-25 00:24:17 +000036/// TypeNameInfo - A link exists here for each scope that an identifier is
37/// defined.
Chris Lattnerc5583732009-01-18 09:39:41 +000038namespace {
39 struct TypeNameInfo {
40 TypeNameInfo *Prev;
41 bool isTypeName;
42
43 TypeNameInfo(bool istypename, TypeNameInfo *prev) {
44 isTypeName = istypename;
45 Prev = prev;
46 }
47 };
48
49 struct TypeNameInfoTable {
50 llvm::RecyclingAllocator<llvm::BumpPtrAllocator, TypeNameInfo> Allocator;
51
52 void AddEntry(bool isTypename, IdentifierInfo *II) {
53 TypeNameInfo *TI = Allocator.Allocate<TypeNameInfo>();
54 new (TI) TypeNameInfo(1, II->getFETokenInfo<TypeNameInfo>());
55 II->setFETokenInfo(TI);
56 }
57
58 void DeleteEntry(TypeNameInfo *Entry) {
59 Entry->~TypeNameInfo();
60 Allocator.Deallocate(Entry);
61 }
62 };
63}
64
65static TypeNameInfoTable *getTable(void *TP) {
66 return static_cast<TypeNameInfoTable*>(TP);
67}
Chris Lattner4b009652007-07-25 00:24:17 +000068
Daniel Dunbar747a95e2008-10-31 08:56:51 +000069MinimalAction::MinimalAction(Preprocessor &pp)
Chris Lattnerc5583732009-01-18 09:39:41 +000070 : Idents(pp.getIdentifierTable()), PP(pp) {
71 TypeNameInfoTablePtr = new TypeNameInfoTable();
72}
73
74MinimalAction::~MinimalAction() {
75 delete getTable(TypeNameInfoTablePtr);
76}
Daniel Dunbar747a95e2008-10-31 08:56:51 +000077
78void MinimalAction::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
Steve Naroffae84af82007-10-31 18:42:27 +000079 TUScope = S;
Daniel Dunbar747a95e2008-10-31 08:56:51 +000080 if (!PP.getLangOptions().ObjC1) return;
81
Chris Lattnerc5583732009-01-18 09:39:41 +000082
83 TypeNameInfoTable &TNIT = *getTable(TypeNameInfoTablePtr);
84
85 // Recognize the ObjC built-in type identifiers as types.
86 TNIT.AddEntry(true, &Idents.get("id"));
87 TNIT.AddEntry(true, &Idents.get("SEL"));
88 TNIT.AddEntry(true, &Idents.get("Class"));
89 TNIT.AddEntry(true, &Idents.get("Protocol"));
Steve Naroffae84af82007-10-31 18:42:27 +000090}
91
Chris Lattner4b009652007-07-25 00:24:17 +000092/// isTypeName - This looks at the IdentifierInfo::FETokenInfo field to
93/// determine whether the name is a type name (objc class name or typedef) or
94/// not in this scope.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +000095///
96/// FIXME: Use the passed CXXScopeSpec for accurate C++ type checking.
Argiris Kirtzidis46403632008-08-01 10:35:27 +000097Action::TypeTy *
Douglas Gregor1075a162009-02-04 17:00:24 +000098MinimalAction::getTypeName(IdentifierInfo &II, SourceLocation Loc,
99 Scope *S, const CXXScopeSpec *SS) {
Chris Lattner4b009652007-07-25 00:24:17 +0000100 if (TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>())
101 if (TI->isTypeName)
102 return TI;
103 return 0;
104}
105
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000106/// isCurrentClassName - Always returns false, because MinimalAction
107/// does not support C++ classes with constructors.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000108bool MinimalAction::isCurrentClassName(const IdentifierInfo &, Scope *,
109 const CXXScopeSpec *) {
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000110 return false;
111}
112
Douglas Gregor0c281a82009-02-25 19:37:18 +0000113TemplateNameKind
Douglas Gregor8e458f42009-02-09 18:46:07 +0000114MinimalAction::isTemplateName(IdentifierInfo &II, Scope *S,
115 DeclTy *&TemplateDecl,
116 const CXXScopeSpec *SS) {
117 return TNK_Non_template;
Douglas Gregor2fa10442008-12-18 19:37:40 +0000118}
119
Steve Naroff0acc9c92007-09-15 18:49:24 +0000120/// ActOnDeclarator - If this is a typedef declarator, we modify the
Chris Lattner4b009652007-07-25 00:24:17 +0000121/// IdentifierInfo::FETokenInfo field to keep track of this fact, until S is
122/// popped.
123Action::DeclTy *
Daniel Dunbar72eaf8a2008-08-05 16:28:08 +0000124MinimalAction::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *LastInGroup) {
Chris Lattner4b009652007-07-25 00:24:17 +0000125 IdentifierInfo *II = D.getIdentifier();
126
127 // If there is no identifier associated with this declarator, bail out.
128 if (II == 0) return 0;
129
130 TypeNameInfo *weCurrentlyHaveTypeInfo = II->getFETokenInfo<TypeNameInfo>();
131 bool isTypeName =
132 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef;
133
134 // this check avoids creating TypeNameInfo objects for the common case.
Ted Kremeneka3195a32008-08-04 22:51:42 +0000135 // It does need to handle the uncommon case of shadowing a typedef name with a
Chris Lattner4b009652007-07-25 00:24:17 +0000136 // non-typedef name. e.g. { typedef int a; a xx; { int a; } }
137 if (weCurrentlyHaveTypeInfo || isTypeName) {
Chris Lattnerc5583732009-01-18 09:39:41 +0000138 // Allocate and add the 'TypeNameInfo' "decl".
139 getTable(TypeNameInfoTablePtr)->AddEntry(isTypeName, II);
Chris Lattner4b009652007-07-25 00:24:17 +0000140
141 // Remember that this needs to be removed when the scope is popped.
142 S->AddDecl(II);
143 }
144 return 0;
145}
146
Steve Naroff81f1bba2007-09-06 21:24:23 +0000147Action::DeclTy *
Chris Lattner51002752008-07-21 07:13:18 +0000148MinimalAction::ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
Chris Lattnerae1ae492008-07-26 04:13:19 +0000149 IdentifierInfo *ClassName,
150 SourceLocation ClassLoc,
151 IdentifierInfo *SuperName,
152 SourceLocation SuperLoc,
153 DeclTy * const *ProtoRefs,
154 unsigned NumProtocols,
155 SourceLocation EndProtoLoc,
156 AttributeList *AttrList) {
Chris Lattnerc5583732009-01-18 09:39:41 +0000157 // Allocate and add the 'TypeNameInfo' "decl".
158 getTable(TypeNameInfoTablePtr)->AddEntry(true, ClassName);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000159 return 0;
160}
161
Steve Naroffb4dfe362007-10-02 22:39:18 +0000162/// ActOnForwardClassDeclaration -
Chris Lattner4b009652007-07-25 00:24:17 +0000163/// Scope will always be top level file scope.
164Action::DeclTy *
Steve Naroff415c1832007-10-10 17:32:04 +0000165MinimalAction::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Steve Naroff81f1bba2007-09-06 21:24:23 +0000166 IdentifierInfo **IdentList, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +0000167 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnerc5583732009-01-18 09:39:41 +0000168 // Allocate and add the 'TypeNameInfo' "decl".
169 getTable(TypeNameInfoTablePtr)->AddEntry(true, IdentList[i]);
Chris Lattner4b009652007-07-25 00:24:17 +0000170
171 // Remember that this needs to be removed when the scope is popped.
Steve Naroff415c1832007-10-10 17:32:04 +0000172 TUScope->AddDecl(IdentList[i]);
Chris Lattner4b009652007-07-25 00:24:17 +0000173 }
174 return 0;
175}
176
Ted Kremeneka3195a32008-08-04 22:51:42 +0000177/// ActOnPopScope - When a scope is popped, if any typedefs are now
178/// out-of-scope, they are removed from the IdentifierInfo::FETokenInfo field.
Steve Naroffbd5c5fb2007-10-10 17:45:44 +0000179void MinimalAction::ActOnPopScope(SourceLocation Loc, Scope *S) {
Chris Lattnerc5583732009-01-18 09:39:41 +0000180 TypeNameInfoTable &Table = *getTable(TypeNameInfoTablePtr);
181
Chris Lattner4b009652007-07-25 00:24:17 +0000182 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
183 I != E; ++I) {
184 IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I);
185 TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>();
186 assert(TI && "This decl didn't get pushed??");
187
188 if (TI) {
189 TypeNameInfo *Next = TI->Prev;
Chris Lattnerc5583732009-01-18 09:39:41 +0000190 Table.DeleteEntry(TI);
Chris Lattner4b009652007-07-25 00:24:17 +0000191
192 II.setFETokenInfo(Next);
193 }
194 }
195}