blob: c4e7ecc0c377cde2fb197da19af4561338b67b88 [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"
17using namespace clang;
18
19/// TypeNameInfo - A link exists here for each scope that an identifier is
20/// defined.
21struct TypeNameInfo {
22 TypeNameInfo *Prev;
23 bool isTypeName;
24
25 TypeNameInfo(bool istypename, TypeNameInfo *prev) {
26 isTypeName = istypename;
27 Prev = prev;
28 }
29};
30
Daniel Dunbare10b0f22008-10-31 08:56:51 +000031MinimalAction::MinimalAction(Preprocessor &pp)
32 : Idents(pp.getIdentifierTable()), PP(pp) {}
33
34void MinimalAction::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
Steve Naroff8ee529b2007-10-31 18:42:27 +000035 TUScope = S;
Daniel Dunbare10b0f22008-10-31 08:56:51 +000036 if (!PP.getLangOptions().ObjC1) return;
37
38 // recognize the ObjC built-in type identifiers.
Steve Naroffb4292f22007-10-31 20:55:39 +000039 IdentifierInfo *II;
40 TypeNameInfo *TI;
Steve Naroffb4292f22007-10-31 20:55:39 +000041 II = &Idents.get("id");
42 TI = new TypeNameInfo(1, II->getFETokenInfo<TypeNameInfo>());
43 II->setFETokenInfo(TI);
44 II = &Idents.get("SEL");
45 TI = new TypeNameInfo(1, II->getFETokenInfo<TypeNameInfo>());
46 II->setFETokenInfo(TI);
47 II = &Idents.get("Class");
48 TI = new TypeNameInfo(1, II->getFETokenInfo<TypeNameInfo>());
49 II->setFETokenInfo(TI);
Fariborz Jahanian66c5dfc2007-12-07 00:18:54 +000050 II = &Idents.get("Protocol");
51 TI = new TypeNameInfo(1, II->getFETokenInfo<TypeNameInfo>());
52 II->setFETokenInfo(TI);
Steve Naroff8ee529b2007-10-31 18:42:27 +000053}
54
Reid Spencer5f016e22007-07-11 17:01:13 +000055/// isTypeName - This looks at the IdentifierInfo::FETokenInfo field to
56/// determine whether the name is a type name (objc class name or typedef) or
57/// not in this scope.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000058///
59/// FIXME: Use the passed CXXScopeSpec for accurate C++ type checking.
Argyrios Kyrtzidis39caa082008-08-01 10:35:27 +000060Action::TypeTy *
Douglas Gregor2def4832008-11-17 20:34:05 +000061MinimalAction::isTypeName(IdentifierInfo &II, Scope *S,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000062 const CXXScopeSpec *SS) {
Reid Spencer5f016e22007-07-11 17:01:13 +000063 if (TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>())
64 if (TI->isTypeName)
65 return TI;
66 return 0;
67}
68
Douglas Gregorb48fe382008-10-31 09:07:45 +000069/// isCurrentClassName - Always returns false, because MinimalAction
70/// does not support C++ classes with constructors.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000071bool MinimalAction::isCurrentClassName(const IdentifierInfo &, Scope *,
72 const CXXScopeSpec *) {
Douglas Gregorb48fe382008-10-31 09:07:45 +000073 return false;
74}
75
Douglas Gregord6fb7ef2008-12-18 19:37:40 +000076 /// isTemplateName - Determines whether the identifier II is a
77 /// template name in the current scope, and returns the template
78 /// declaration if II names a template. An optional CXXScope can be
79 /// passed to indicate the C++ scope in which the identifier will be
80 /// found.
81Action::DeclTy *MinimalAction::isTemplateName(IdentifierInfo &II, Scope *S,
82 const CXXScopeSpec *SS ) {
83 return 0;
84}
85
Steve Naroff08d92e42007-09-15 18:49:24 +000086/// ActOnDeclarator - If this is a typedef declarator, we modify the
Reid Spencer5f016e22007-07-11 17:01:13 +000087/// IdentifierInfo::FETokenInfo field to keep track of this fact, until S is
88/// popped.
89Action::DeclTy *
Daniel Dunbar914701e2008-08-05 16:28:08 +000090MinimalAction::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *LastInGroup) {
Reid Spencer5f016e22007-07-11 17:01:13 +000091 IdentifierInfo *II = D.getIdentifier();
92
93 // If there is no identifier associated with this declarator, bail out.
94 if (II == 0) return 0;
95
96 TypeNameInfo *weCurrentlyHaveTypeInfo = II->getFETokenInfo<TypeNameInfo>();
97 bool isTypeName =
98 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef;
99
100 // this check avoids creating TypeNameInfo objects for the common case.
Ted Kremeneka34ea072008-08-04 22:51:42 +0000101 // It does need to handle the uncommon case of shadowing a typedef name with a
Reid Spencer5f016e22007-07-11 17:01:13 +0000102 // non-typedef name. e.g. { typedef int a; a xx; { int a; } }
103 if (weCurrentlyHaveTypeInfo || isTypeName) {
104 TypeNameInfo *TI = new TypeNameInfo(isTypeName, weCurrentlyHaveTypeInfo);
105
106 II->setFETokenInfo(TI);
107
108 // Remember that this needs to be removed when the scope is popped.
109 S->AddDecl(II);
110 }
111 return 0;
112}
113
Steve Naroff3536b442007-09-06 21:24:23 +0000114Action::DeclTy *
Chris Lattnercfb0ef52008-07-21 07:13:18 +0000115MinimalAction::ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
Chris Lattner06036d32008-07-26 04:13:19 +0000116 IdentifierInfo *ClassName,
117 SourceLocation ClassLoc,
118 IdentifierInfo *SuperName,
119 SourceLocation SuperLoc,
120 DeclTy * const *ProtoRefs,
121 unsigned NumProtocols,
122 SourceLocation EndProtoLoc,
123 AttributeList *AttrList) {
Steve Naroff3536b442007-09-06 21:24:23 +0000124 TypeNameInfo *TI =
125 new TypeNameInfo(1, ClassName->getFETokenInfo<TypeNameInfo>());
126
127 ClassName->setFETokenInfo(TI);
128 return 0;
129}
130
Steve Naroff37e58d12007-10-02 22:39:18 +0000131/// ActOnForwardClassDeclaration -
Reid Spencer5f016e22007-07-11 17:01:13 +0000132/// Scope will always be top level file scope.
133Action::DeclTy *
Steve Naroffe440eb82007-10-10 17:32:04 +0000134MinimalAction::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Steve Naroff3536b442007-09-06 21:24:23 +0000135 IdentifierInfo **IdentList, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000136 for (unsigned i = 0; i != NumElts; ++i) {
137 TypeNameInfo *TI =
138 new TypeNameInfo(1, IdentList[i]->getFETokenInfo<TypeNameInfo>());
139
140 IdentList[i]->setFETokenInfo(TI);
141
142 // Remember that this needs to be removed when the scope is popped.
Steve Naroffe440eb82007-10-10 17:32:04 +0000143 TUScope->AddDecl(IdentList[i]);
Reid Spencer5f016e22007-07-11 17:01:13 +0000144 }
145 return 0;
146}
147
Ted Kremeneka34ea072008-08-04 22:51:42 +0000148/// ActOnPopScope - When a scope is popped, if any typedefs are now
149/// out-of-scope, they are removed from the IdentifierInfo::FETokenInfo field.
Steve Naroff640db422007-10-10 17:45:44 +0000150void MinimalAction::ActOnPopScope(SourceLocation Loc, Scope *S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000151 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
152 I != E; ++I) {
153 IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I);
154 TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>();
155 assert(TI && "This decl didn't get pushed??");
156
157 if (TI) {
158 TypeNameInfo *Next = TI->Prev;
159 delete TI;
160
161 II.setFETokenInfo(Next);
162 }
163 }
164}