blob: bc7c41bec398e734a85c59f2cdeb2b3a2b6dd9ea [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
Steve Naroff8ee529b2007-10-31 18:42:27 +000031void MinimalAction:: ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
32 TUScope = S;
33 // FIXME: add id/SEL/Class. We need to get our paws on the identifier table.
34}
35
Reid Spencer5f016e22007-07-11 17:01:13 +000036/// isTypeName - This looks at the IdentifierInfo::FETokenInfo field to
37/// determine whether the name is a type name (objc class name or typedef) or
38/// not in this scope.
39Action::DeclTy *
40MinimalAction::isTypeName(const IdentifierInfo &II, Scope *S) const {
41 if (TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>())
42 if (TI->isTypeName)
43 return TI;
44 return 0;
45}
46
Steve Naroff08d92e42007-09-15 18:49:24 +000047/// ActOnDeclarator - If this is a typedef declarator, we modify the
Reid Spencer5f016e22007-07-11 17:01:13 +000048/// IdentifierInfo::FETokenInfo field to keep track of this fact, until S is
49/// popped.
50Action::DeclTy *
Steve Naroff08d92e42007-09-15 18:49:24 +000051MinimalAction::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *LastInGroup) {
Reid Spencer5f016e22007-07-11 17:01:13 +000052 IdentifierInfo *II = D.getIdentifier();
53
54 // If there is no identifier associated with this declarator, bail out.
55 if (II == 0) return 0;
56
57 TypeNameInfo *weCurrentlyHaveTypeInfo = II->getFETokenInfo<TypeNameInfo>();
58 bool isTypeName =
59 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef;
60
61 // this check avoids creating TypeNameInfo objects for the common case.
62 // It does need to handle the uncommon case of shadowing a typedef name with a
63 // non-typedef name. e.g. { typedef int a; a xx; { int a; } }
64 if (weCurrentlyHaveTypeInfo || isTypeName) {
65 TypeNameInfo *TI = new TypeNameInfo(isTypeName, weCurrentlyHaveTypeInfo);
66
67 II->setFETokenInfo(TI);
68
69 // Remember that this needs to be removed when the scope is popped.
70 S->AddDecl(II);
71 }
72 return 0;
73}
74
Steve Naroff3536b442007-09-06 21:24:23 +000075Action::DeclTy *
Steve Naroffe440eb82007-10-10 17:32:04 +000076MinimalAction::ActOnStartClassInterface(SourceLocation AtInterafceLoc,
Steve Naroff3536b442007-09-06 21:24:23 +000077 IdentifierInfo *ClassName, SourceLocation ClassLoc,
78 IdentifierInfo *SuperName, SourceLocation SuperLoc,
79 IdentifierInfo **ProtocolNames, unsigned NumProtocols,
Steve Narofff908a872007-10-30 02:23:23 +000080 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Steve Naroff3536b442007-09-06 21:24:23 +000081 TypeNameInfo *TI =
82 new TypeNameInfo(1, ClassName->getFETokenInfo<TypeNameInfo>());
83
84 ClassName->setFETokenInfo(TI);
85 return 0;
86}
87
Steve Naroff37e58d12007-10-02 22:39:18 +000088/// ActOnForwardClassDeclaration -
Reid Spencer5f016e22007-07-11 17:01:13 +000089/// Scope will always be top level file scope.
90Action::DeclTy *
Steve Naroffe440eb82007-10-10 17:32:04 +000091MinimalAction::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Steve Naroff3536b442007-09-06 21:24:23 +000092 IdentifierInfo **IdentList, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +000093 for (unsigned i = 0; i != NumElts; ++i) {
94 TypeNameInfo *TI =
95 new TypeNameInfo(1, IdentList[i]->getFETokenInfo<TypeNameInfo>());
96
97 IdentList[i]->setFETokenInfo(TI);
98
99 // Remember that this needs to be removed when the scope is popped.
Steve Naroffe440eb82007-10-10 17:32:04 +0000100 TUScope->AddDecl(IdentList[i]);
Reid Spencer5f016e22007-07-11 17:01:13 +0000101 }
102 return 0;
103}
104
Steve Naroff640db422007-10-10 17:45:44 +0000105/// ActOnPopScope - When a scope is popped, if any typedefs are now out-of-scope,
Reid Spencer5f016e22007-07-11 17:01:13 +0000106/// they are removed from the IdentifierInfo::FETokenInfo field.
Steve Naroff640db422007-10-10 17:45:44 +0000107void MinimalAction::ActOnPopScope(SourceLocation Loc, Scope *S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000108 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
109 I != E; ++I) {
110 IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I);
111 TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>();
112 assert(TI && "This decl didn't get pushed??");
113
114 if (TI) {
115 TypeNameInfo *Next = TI->Prev;
116 delete TI;
117
118 II.setFETokenInfo(Next);
119 }
120 }
121}