blob: 019e652dde801f875f9d66cadd38fdc9ec1a20aa [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 *
71MinimalAction::ObjcStartClassInterface(SourceLocation AtInterafceLoc,
72 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
83/// ObjcClassDeclaration -
Reid Spencer5f016e22007-07-11 17:01:13 +000084/// Scope will always be top level file scope.
85Action::DeclTy *
Steve Naroff3536b442007-09-06 21:24:23 +000086MinimalAction::ObjcClassDeclaration(Scope *S, SourceLocation AtClassLoc,
87 IdentifierInfo **IdentList, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +000088 for (unsigned i = 0; i != NumElts; ++i) {
89 TypeNameInfo *TI =
90 new TypeNameInfo(1, IdentList[i]->getFETokenInfo<TypeNameInfo>());
91
92 IdentList[i]->setFETokenInfo(TI);
93
94 // Remember that this needs to be removed when the scope is popped.
95 S->AddDecl(IdentList[i]);
96 }
97 return 0;
98}
99
100/// PopScope - When a scope is popped, if any typedefs are now out-of-scope,
101/// they are removed from the IdentifierInfo::FETokenInfo field.
102void MinimalAction::PopScope(SourceLocation Loc, Scope *S) {
103 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
104 I != E; ++I) {
105 IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I);
106 TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>();
107 assert(TI && "This decl didn't get pushed??");
108
109 if (TI) {
110 TypeNameInfo *Next = TI->Prev;
111 delete TI;
112
113 II.setFETokenInfo(Next);
114 }
115 }
116}