blob: cb130c3d268aa77334ba5a17b98d6e5f47d4049c [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
Steve Naroff8ee529b2007-10-31 18:42:27 +000031void MinimalAction:: ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
32 TUScope = S;
Steve Naroffb4292f22007-10-31 20:55:39 +000033 IdentifierInfo *II;
34 TypeNameInfo *TI;
35
36 // recognize the ObjC built-in type identifiers.
37 II = &Idents.get("id");
38 TI = new TypeNameInfo(1, II->getFETokenInfo<TypeNameInfo>());
39 II->setFETokenInfo(TI);
40 II = &Idents.get("SEL");
41 TI = new TypeNameInfo(1, II->getFETokenInfo<TypeNameInfo>());
42 II->setFETokenInfo(TI);
43 II = &Idents.get("Class");
44 TI = new TypeNameInfo(1, II->getFETokenInfo<TypeNameInfo>());
45 II->setFETokenInfo(TI);
Fariborz Jahanian66c5dfc2007-12-07 00:18:54 +000046 II = &Idents.get("Protocol");
47 TI = new TypeNameInfo(1, II->getFETokenInfo<TypeNameInfo>());
48 II->setFETokenInfo(TI);
Steve Naroff8ee529b2007-10-31 18:42:27 +000049}
50
Reid Spencer5f016e22007-07-11 17:01:13 +000051/// isTypeName - This looks at the IdentifierInfo::FETokenInfo field to
52/// determine whether the name is a type name (objc class name or typedef) or
53/// not in this scope.
Argyrios Kyrtzidis39caa082008-08-01 10:35:27 +000054Action::TypeTy *
Steve Naroffb327ce02008-04-02 14:35:35 +000055MinimalAction::isTypeName(const IdentifierInfo &II, Scope *S) {
Reid Spencer5f016e22007-07-11 17:01:13 +000056 if (TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>())
57 if (TI->isTypeName)
58 return TI;
59 return 0;
60}
61
Steve Naroff08d92e42007-09-15 18:49:24 +000062/// ActOnDeclarator - If this is a typedef declarator, we modify the
Reid Spencer5f016e22007-07-11 17:01:13 +000063/// IdentifierInfo::FETokenInfo field to keep track of this fact, until S is
64/// popped.
65Action::DeclTy *
Steve Naroff08d92e42007-09-15 18:49:24 +000066MinimalAction::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *LastInGroup) {
Reid Spencer5f016e22007-07-11 17:01:13 +000067 IdentifierInfo *II = D.getIdentifier();
68
69 // If there is no identifier associated with this declarator, bail out.
70 if (II == 0) return 0;
71
72 TypeNameInfo *weCurrentlyHaveTypeInfo = II->getFETokenInfo<TypeNameInfo>();
73 bool isTypeName =
74 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef;
75
76 // this check avoids creating TypeNameInfo objects for the common case.
Ted Kremeneka34ea072008-08-04 22:51:42 +000077 // It does need to handle the uncommon case of shadowing a typedef name with a
Reid Spencer5f016e22007-07-11 17:01:13 +000078 // non-typedef name. e.g. { typedef int a; a xx; { int a; } }
79 if (weCurrentlyHaveTypeInfo || isTypeName) {
80 TypeNameInfo *TI = new TypeNameInfo(isTypeName, weCurrentlyHaveTypeInfo);
81
82 II->setFETokenInfo(TI);
83
84 // Remember that this needs to be removed when the scope is popped.
85 S->AddDecl(II);
86 }
87 return 0;
88}
89
Steve Naroff3536b442007-09-06 21:24:23 +000090Action::DeclTy *
Chris Lattnercfb0ef52008-07-21 07:13:18 +000091MinimalAction::ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
Chris Lattner06036d32008-07-26 04:13:19 +000092 IdentifierInfo *ClassName,
93 SourceLocation ClassLoc,
94 IdentifierInfo *SuperName,
95 SourceLocation SuperLoc,
96 DeclTy * const *ProtoRefs,
97 unsigned NumProtocols,
98 SourceLocation EndProtoLoc,
99 AttributeList *AttrList) {
Steve Naroff3536b442007-09-06 21:24:23 +0000100 TypeNameInfo *TI =
101 new TypeNameInfo(1, ClassName->getFETokenInfo<TypeNameInfo>());
102
103 ClassName->setFETokenInfo(TI);
104 return 0;
105}
106
Steve Naroff37e58d12007-10-02 22:39:18 +0000107/// ActOnForwardClassDeclaration -
Reid Spencer5f016e22007-07-11 17:01:13 +0000108/// Scope will always be top level file scope.
109Action::DeclTy *
Steve Naroffe440eb82007-10-10 17:32:04 +0000110MinimalAction::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Steve Naroff3536b442007-09-06 21:24:23 +0000111 IdentifierInfo **IdentList, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000112 for (unsigned i = 0; i != NumElts; ++i) {
113 TypeNameInfo *TI =
114 new TypeNameInfo(1, IdentList[i]->getFETokenInfo<TypeNameInfo>());
115
116 IdentList[i]->setFETokenInfo(TI);
117
118 // Remember that this needs to be removed when the scope is popped.
Steve Naroffe440eb82007-10-10 17:32:04 +0000119 TUScope->AddDecl(IdentList[i]);
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 }
121 return 0;
122}
123
Ted Kremeneka34ea072008-08-04 22:51:42 +0000124/// ActOnPopScope - When a scope is popped, if any typedefs are now
125/// out-of-scope, they are removed from the IdentifierInfo::FETokenInfo field.
Steve Naroff640db422007-10-10 17:45:44 +0000126void MinimalAction::ActOnPopScope(SourceLocation Loc, Scope *S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
128 I != E; ++I) {
129 IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I);
130 TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>();
131 assert(TI && "This decl didn't get pushed??");
132
133 if (TI) {
134 TypeNameInfo *Next = TI->Prev;
135 delete TI;
136
137 II.setFETokenInfo(Next);
138 }
139 }
140}