blob: dc0a9cc4e9e77ed580821b2cbb37e48d47d47601 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- MinimalAction.cpp - Implement the MinimalAction class ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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
31/// isTypeName - This looks at the IdentifierInfo::FETokenInfo field to
32/// determine whether the name is a type name (objc class name or typedef) or
33/// not in this scope.
34Action::DeclTy *
35MinimalAction::isTypeName(const IdentifierInfo &II, Scope *S) const {
36 if (TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>())
37 if (TI->isTypeName)
38 return TI;
39 return 0;
40}
41
Steve Naroff08d92e42007-09-15 18:49:24 +000042/// ActOnDeclarator - If this is a typedef declarator, we modify the
Reid Spencer5f016e22007-07-11 17:01:13 +000043/// IdentifierInfo::FETokenInfo field to keep track of this fact, until S is
44/// popped.
45Action::DeclTy *
Steve Naroff08d92e42007-09-15 18:49:24 +000046MinimalAction::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *LastInGroup) {
Reid Spencer5f016e22007-07-11 17:01:13 +000047 IdentifierInfo *II = D.getIdentifier();
48
49 // If there is no identifier associated with this declarator, bail out.
50 if (II == 0) return 0;
51
52 TypeNameInfo *weCurrentlyHaveTypeInfo = II->getFETokenInfo<TypeNameInfo>();
53 bool isTypeName =
54 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef;
55
56 // this check avoids creating TypeNameInfo objects for the common case.
57 // It does need to handle the uncommon case of shadowing a typedef name with a
58 // non-typedef name. e.g. { typedef int a; a xx; { int a; } }
59 if (weCurrentlyHaveTypeInfo || isTypeName) {
60 TypeNameInfo *TI = new TypeNameInfo(isTypeName, weCurrentlyHaveTypeInfo);
61
62 II->setFETokenInfo(TI);
63
64 // Remember that this needs to be removed when the scope is popped.
65 S->AddDecl(II);
66 }
67 return 0;
68}
69
Steve Naroff3536b442007-09-06 21:24:23 +000070Action::DeclTy *
Fariborz Jahanianccb4f312007-09-25 18:38:09 +000071MinimalAction::ObjcStartClassInterface(Scope* S, SourceLocation AtInterafceLoc,
Steve Naroff3536b442007-09-06 21:24:23 +000072 IdentifierInfo *ClassName, SourceLocation ClassLoc,
73 IdentifierInfo *SuperName, SourceLocation SuperLoc,
74 IdentifierInfo **ProtocolNames, unsigned NumProtocols,
75 AttributeList *AttrList) {
76 TypeNameInfo *TI =
77 new TypeNameInfo(1, ClassName->getFETokenInfo<TypeNameInfo>());
78
79 ClassName->setFETokenInfo(TI);
80 return 0;
81}
82
Fariborz Jahanian25e077d2007-09-17 21:07:36 +000083Action::DeclTy *
Fariborz Jahanianccb4f312007-09-25 18:38:09 +000084MinimalAction::ObjcStartProtoInterface(Scope* S,
85 SourceLocation AtProtoInterfaceLoc,
Fariborz Jahanian25e077d2007-09-17 21:07:36 +000086 IdentifierInfo *ProtocolName, SourceLocation ProtocolLoc,
87 IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs) {
88
89 TypeNameInfo *TI =
90 new TypeNameInfo(1, ProtocolName->getFETokenInfo<TypeNameInfo>());
91
92 ProtocolName->setFETokenInfo(TI);
93 return 0;
94}
95
Steve Naroff37e58d12007-10-02 22:39:18 +000096/// ActOnForwardClassDeclaration -
Reid Spencer5f016e22007-07-11 17:01:13 +000097/// Scope will always be top level file scope.
98Action::DeclTy *
Steve Naroff37e58d12007-10-02 22:39:18 +000099MinimalAction::ActOnForwardClassDeclaration(Scope *S, SourceLocation AtClassLoc,
Steve Naroff3536b442007-09-06 21:24:23 +0000100 IdentifierInfo **IdentList, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000101 for (unsigned i = 0; i != NumElts; ++i) {
102 TypeNameInfo *TI =
103 new TypeNameInfo(1, IdentList[i]->getFETokenInfo<TypeNameInfo>());
104
105 IdentList[i]->setFETokenInfo(TI);
106
107 // Remember that this needs to be removed when the scope is popped.
108 S->AddDecl(IdentList[i]);
109 }
110 return 0;
111}
112
Steve Naroff37e58d12007-10-02 22:39:18 +0000113/// ActOnForwardProtocolDeclaration -
Fariborz Jahanian894c57f2007-09-21 15:40:54 +0000114/// Scope will always be top level file scope.
115Action::DeclTy *
Steve Naroff37e58d12007-10-02 22:39:18 +0000116MinimalAction::ActOnForwardProtocolDeclaration(Scope *S,
117 SourceLocation AtClassLoc, IdentifierInfo **IdentList, unsigned NumElts) {
Fariborz Jahanian894c57f2007-09-21 15:40:54 +0000118 for (unsigned i = 0; i != NumElts; ++i) {
119 TypeNameInfo *TI =
120 new TypeNameInfo(1, IdentList[i]->getFETokenInfo<TypeNameInfo>());
121
122 IdentList[i]->setFETokenInfo(TI);
123
124 // Remember that this needs to be removed when the scope is popped.
125 S->AddDecl(IdentList[i]);
126 }
127 return 0;
128}
129
Reid Spencer5f016e22007-07-11 17:01:13 +0000130/// PopScope - When a scope is popped, if any typedefs are now out-of-scope,
131/// they are removed from the IdentifierInfo::FETokenInfo field.
132void MinimalAction::PopScope(SourceLocation Loc, Scope *S) {
133 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
134 I != E; ++I) {
135 IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I);
136 TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>();
137 assert(TI && "This decl didn't get pushed??");
138
139 if (TI) {
140 TypeNameInfo *Next = TI->Prev;
141 delete TI;
142
143 II.setFETokenInfo(Next);
144 }
145 }
146}