blob: 6fd5fc03152ba870ff52fc78b2fbb1bd291fd47e [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 Lattner4b009652007-07-25 00:24:17 +000019using namespace clang;
20
21/// TypeNameInfo - A link exists here for each scope that an identifier is
22/// defined.
Chris Lattnerc5583732009-01-18 09:39:41 +000023namespace {
24 struct TypeNameInfo {
25 TypeNameInfo *Prev;
26 bool isTypeName;
27
28 TypeNameInfo(bool istypename, TypeNameInfo *prev) {
29 isTypeName = istypename;
30 Prev = prev;
31 }
32 };
33
34 struct TypeNameInfoTable {
35 llvm::RecyclingAllocator<llvm::BumpPtrAllocator, TypeNameInfo> Allocator;
36
37 void AddEntry(bool isTypename, IdentifierInfo *II) {
38 TypeNameInfo *TI = Allocator.Allocate<TypeNameInfo>();
39 new (TI) TypeNameInfo(1, II->getFETokenInfo<TypeNameInfo>());
40 II->setFETokenInfo(TI);
41 }
42
43 void DeleteEntry(TypeNameInfo *Entry) {
44 Entry->~TypeNameInfo();
45 Allocator.Deallocate(Entry);
46 }
47 };
48}
49
50static TypeNameInfoTable *getTable(void *TP) {
51 return static_cast<TypeNameInfoTable*>(TP);
52}
Chris Lattner4b009652007-07-25 00:24:17 +000053
Daniel Dunbar747a95e2008-10-31 08:56:51 +000054MinimalAction::MinimalAction(Preprocessor &pp)
Chris Lattnerc5583732009-01-18 09:39:41 +000055 : Idents(pp.getIdentifierTable()), PP(pp) {
56 TypeNameInfoTablePtr = new TypeNameInfoTable();
57}
58
59MinimalAction::~MinimalAction() {
60 delete getTable(TypeNameInfoTablePtr);
61}
Daniel Dunbar747a95e2008-10-31 08:56:51 +000062
63void MinimalAction::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
Steve Naroffae84af82007-10-31 18:42:27 +000064 TUScope = S;
Daniel Dunbar747a95e2008-10-31 08:56:51 +000065 if (!PP.getLangOptions().ObjC1) return;
66
Chris Lattnerc5583732009-01-18 09:39:41 +000067
68 TypeNameInfoTable &TNIT = *getTable(TypeNameInfoTablePtr);
69
70 // Recognize the ObjC built-in type identifiers as types.
71 TNIT.AddEntry(true, &Idents.get("id"));
72 TNIT.AddEntry(true, &Idents.get("SEL"));
73 TNIT.AddEntry(true, &Idents.get("Class"));
74 TNIT.AddEntry(true, &Idents.get("Protocol"));
Steve Naroffae84af82007-10-31 18:42:27 +000075}
76
Chris Lattner4b009652007-07-25 00:24:17 +000077/// isTypeName - This looks at the IdentifierInfo::FETokenInfo field to
78/// determine whether the name is a type name (objc class name or typedef) or
79/// not in this scope.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +000080///
81/// FIXME: Use the passed CXXScopeSpec for accurate C++ type checking.
Argiris Kirtzidis46403632008-08-01 10:35:27 +000082Action::TypeTy *
Steve Naroff7b36a1b2009-01-28 19:39:02 +000083MinimalAction::getTypeName(IdentifierInfo &II, Scope *S,
84 const CXXScopeSpec *SS) {
Chris Lattner4b009652007-07-25 00:24:17 +000085 if (TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>())
86 if (TI->isTypeName)
87 return TI;
88 return 0;
89}
90
Douglas Gregorf15ac4b2008-10-31 09:07:45 +000091/// isCurrentClassName - Always returns false, because MinimalAction
92/// does not support C++ classes with constructors.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +000093bool MinimalAction::isCurrentClassName(const IdentifierInfo &, Scope *,
94 const CXXScopeSpec *) {
Douglas Gregorf15ac4b2008-10-31 09:07:45 +000095 return false;
96}
97
Douglas Gregor2fa10442008-12-18 19:37:40 +000098 /// isTemplateName - Determines whether the identifier II is a
99 /// template name in the current scope, and returns the template
100 /// declaration if II names a template. An optional CXXScope can be
101 /// passed to indicate the C++ scope in which the identifier will be
102 /// found.
103Action::DeclTy *MinimalAction::isTemplateName(IdentifierInfo &II, Scope *S,
104 const CXXScopeSpec *SS ) {
105 return 0;
106}
107
Steve Naroff0acc9c92007-09-15 18:49:24 +0000108/// ActOnDeclarator - If this is a typedef declarator, we modify the
Chris Lattner4b009652007-07-25 00:24:17 +0000109/// IdentifierInfo::FETokenInfo field to keep track of this fact, until S is
110/// popped.
111Action::DeclTy *
Daniel Dunbar72eaf8a2008-08-05 16:28:08 +0000112MinimalAction::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *LastInGroup) {
Chris Lattner4b009652007-07-25 00:24:17 +0000113 IdentifierInfo *II = D.getIdentifier();
114
115 // If there is no identifier associated with this declarator, bail out.
116 if (II == 0) return 0;
117
118 TypeNameInfo *weCurrentlyHaveTypeInfo = II->getFETokenInfo<TypeNameInfo>();
119 bool isTypeName =
120 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef;
121
122 // this check avoids creating TypeNameInfo objects for the common case.
Ted Kremeneka3195a32008-08-04 22:51:42 +0000123 // It does need to handle the uncommon case of shadowing a typedef name with a
Chris Lattner4b009652007-07-25 00:24:17 +0000124 // non-typedef name. e.g. { typedef int a; a xx; { int a; } }
125 if (weCurrentlyHaveTypeInfo || isTypeName) {
Chris Lattnerc5583732009-01-18 09:39:41 +0000126 // Allocate and add the 'TypeNameInfo' "decl".
127 getTable(TypeNameInfoTablePtr)->AddEntry(isTypeName, II);
Chris Lattner4b009652007-07-25 00:24:17 +0000128
129 // Remember that this needs to be removed when the scope is popped.
130 S->AddDecl(II);
131 }
132 return 0;
133}
134
Steve Naroff81f1bba2007-09-06 21:24:23 +0000135Action::DeclTy *
Chris Lattner51002752008-07-21 07:13:18 +0000136MinimalAction::ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
Chris Lattnerae1ae492008-07-26 04:13:19 +0000137 IdentifierInfo *ClassName,
138 SourceLocation ClassLoc,
139 IdentifierInfo *SuperName,
140 SourceLocation SuperLoc,
141 DeclTy * const *ProtoRefs,
142 unsigned NumProtocols,
143 SourceLocation EndProtoLoc,
144 AttributeList *AttrList) {
Chris Lattnerc5583732009-01-18 09:39:41 +0000145 // Allocate and add the 'TypeNameInfo' "decl".
146 getTable(TypeNameInfoTablePtr)->AddEntry(true, ClassName);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000147 return 0;
148}
149
Steve Naroffb4dfe362007-10-02 22:39:18 +0000150/// ActOnForwardClassDeclaration -
Chris Lattner4b009652007-07-25 00:24:17 +0000151/// Scope will always be top level file scope.
152Action::DeclTy *
Steve Naroff415c1832007-10-10 17:32:04 +0000153MinimalAction::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Steve Naroff81f1bba2007-09-06 21:24:23 +0000154 IdentifierInfo **IdentList, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +0000155 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnerc5583732009-01-18 09:39:41 +0000156 // Allocate and add the 'TypeNameInfo' "decl".
157 getTable(TypeNameInfoTablePtr)->AddEntry(true, IdentList[i]);
Chris Lattner4b009652007-07-25 00:24:17 +0000158
159 // Remember that this needs to be removed when the scope is popped.
Steve Naroff415c1832007-10-10 17:32:04 +0000160 TUScope->AddDecl(IdentList[i]);
Chris Lattner4b009652007-07-25 00:24:17 +0000161 }
162 return 0;
163}
164
Ted Kremeneka3195a32008-08-04 22:51:42 +0000165/// ActOnPopScope - When a scope is popped, if any typedefs are now
166/// out-of-scope, they are removed from the IdentifierInfo::FETokenInfo field.
Steve Naroffbd5c5fb2007-10-10 17:45:44 +0000167void MinimalAction::ActOnPopScope(SourceLocation Loc, Scope *S) {
Chris Lattnerc5583732009-01-18 09:39:41 +0000168 TypeNameInfoTable &Table = *getTable(TypeNameInfoTablePtr);
169
Chris Lattner4b009652007-07-25 00:24:17 +0000170 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
171 I != E; ++I) {
172 IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I);
173 TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>();
174 assert(TI && "This decl didn't get pushed??");
175
176 if (TI) {
177 TypeNameInfo *Next = TI->Prev;
Chris Lattnerc5583732009-01-18 09:39:41 +0000178 Table.DeleteEntry(TI);
Chris Lattner4b009652007-07-25 00:24:17 +0000179
180 II.setFETokenInfo(Next);
181 }
182 }
183}