blob: 65bdca64db8951402d8dfb59a98cb4006a935628 [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
71/// ParsedObjcClassDeclaration -
72/// Scope will always be top level file scope.
73Action::DeclTy *
74MinimalAction::ParsedObjcClassDeclaration(Scope *S,
75 IdentifierInfo **IdentList,
76 unsigned NumElts) {
77 for (unsigned i = 0; i != NumElts; ++i) {
78 TypeNameInfo *TI =
79 new TypeNameInfo(1, IdentList[i]->getFETokenInfo<TypeNameInfo>());
80
81 IdentList[i]->setFETokenInfo(TI);
82
83 // Remember that this needs to be removed when the scope is popped.
84 S->AddDecl(IdentList[i]);
85 }
86 return 0;
87}
88
89/// PopScope - When a scope is popped, if any typedefs are now out-of-scope,
90/// they are removed from the IdentifierInfo::FETokenInfo field.
91void MinimalAction::PopScope(SourceLocation Loc, Scope *S) {
92 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
93 I != E; ++I) {
94 IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I);
95 TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>();
96 assert(TI && "This decl didn't get pushed??");
97
98 if (TI) {
99 TypeNameInfo *Next = TI->Prev;
100 delete TI;
101
102 II.setFETokenInfo(Next);
103 }
104 }
105}