blob: 8ca72589ad2c8adf712a1925ea261297a826d9f9 [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
42/// ParseDeclarator - If this is a typedef declarator, we modify the
43/// IdentifierInfo::FETokenInfo field to keep track of this fact, until S is
44/// popped.
45Action::DeclTy *
46MinimalAction::ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init,
47 DeclTy *LastInGroup) {
48 IdentifierInfo *II = D.getIdentifier();
49
50 // If there is no identifier associated with this declarator, bail out.
51 if (II == 0) return 0;
52
53 TypeNameInfo *weCurrentlyHaveTypeInfo = II->getFETokenInfo<TypeNameInfo>();
54 bool isTypeName =
55 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef;
56
57 // this check avoids creating TypeNameInfo objects for the common case.
58 // It does need to handle the uncommon case of shadowing a typedef name with a
59 // non-typedef name. e.g. { typedef int a; a xx; { int a; } }
60 if (weCurrentlyHaveTypeInfo || isTypeName) {
61 TypeNameInfo *TI = new TypeNameInfo(isTypeName, weCurrentlyHaveTypeInfo);
62
63 II->setFETokenInfo(TI);
64
65 // Remember that this needs to be removed when the scope is popped.
66 S->AddDecl(II);
67 }
68 return 0;
69}
70
Steve Naroff3536b442007-09-06 21:24:23 +000071Action::DeclTy *
72MinimalAction::ObjcStartClassInterface(SourceLocation AtInterafceLoc,
73 IdentifierInfo *ClassName, SourceLocation ClassLoc,
74 IdentifierInfo *SuperName, SourceLocation SuperLoc,
75 IdentifierInfo **ProtocolNames, unsigned NumProtocols,
76 AttributeList *AttrList) {
77 TypeNameInfo *TI =
78 new TypeNameInfo(1, ClassName->getFETokenInfo<TypeNameInfo>());
79
80 ClassName->setFETokenInfo(TI);
81 return 0;
82}
83
84/// ObjcClassDeclaration -
Reid Spencer5f016e22007-07-11 17:01:13 +000085/// Scope will always be top level file scope.
86Action::DeclTy *
Steve Naroff3536b442007-09-06 21:24:23 +000087MinimalAction::ObjcClassDeclaration(Scope *S, SourceLocation AtClassLoc,
88 IdentifierInfo **IdentList, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +000089 for (unsigned i = 0; i != NumElts; ++i) {
90 TypeNameInfo *TI =
91 new TypeNameInfo(1, IdentList[i]->getFETokenInfo<TypeNameInfo>());
92
93 IdentList[i]->setFETokenInfo(TI);
94
95 // Remember that this needs to be removed when the scope is popped.
96 S->AddDecl(IdentList[i]);
97 }
98 return 0;
99}
100
101/// PopScope - When a scope is popped, if any typedefs are now out-of-scope,
102/// they are removed from the IdentifierInfo::FETokenInfo field.
103void MinimalAction::PopScope(SourceLocation Loc, Scope *S) {
104 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
105 I != E; ++I) {
106 IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I);
107 TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>();
108 assert(TI && "This decl didn't get pushed??");
109
110 if (TI) {
111 TypeNameInfo *Next = TI->Prev;
112 delete TI;
113
114 II.setFETokenInfo(Next);
115 }
116 }
117}