blob: 5ba684477147a405d663d21b8533ee94cb870ec7 [file] [log] [blame]
Chris Lattnera5534f92006-08-14 00:38:06 +00001//===--- EmptyAction.cpp - Minimalistic action implementation -------------===//
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 EmptyAction interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattnerffe65b32006-08-14 01:28:29 +000015#include "clang/Parse/Declarations.h"
16#include "clang/Parse/Scope.h"
Chris Lattnera5534f92006-08-14 00:38:06 +000017using namespace llvm;
18using namespace clang;
19
Steve Naroffb419d3a2006-10-27 23:18:49 +000020/// TypeNameInfo - A link exists here for each scope that an identifier is
Chris Lattnerffe65b32006-08-14 01:28:29 +000021/// defined.
Steve Naroffb419d3a2006-10-27 23:18:49 +000022struct TypeNameInfo {
23 TypeNameInfo *Prev;
24 bool isTypeName;
25
26 TypeNameInfo(bool istypename, TypeNameInfo *prev) {
27 isTypeName = istypename;
28 Prev = prev;
29 }
30
Chris Lattnerffe65b32006-08-14 01:28:29 +000031};
32
Chris Lattnera5534f92006-08-14 00:38:06 +000033/// isTypedefName - This looks at the IdentifierInfo::FETokenInfo field to
34/// determine whether the name is a typedef or not in this scope.
Steve Naroffb419d3a2006-10-27 23:18:49 +000035bool EmptyAction::isTypeName(const IdentifierInfo &II, Scope *S) const {
36 TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>();
37 return TI != 0 && TI->isTypeName;
Chris Lattnera5534f92006-08-14 00:38:06 +000038}
39
40/// ParseDeclarator - If this is a typedef declarator, we modify the
41/// IdentifierInfo::FETokenInfo field to keep track of this fact, until S is
42/// popped.
Chris Lattner2dacc3f2006-10-16 00:33:54 +000043Action::DeclTy *
44EmptyAction::ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init,
45 DeclTy *LastInGroup) {
Steve Naroffb419d3a2006-10-27 23:18:49 +000046 IdentifierInfo *II = D.getIdentifier();
47
Chris Lattnerffe65b32006-08-14 01:28:29 +000048 // If there is no identifier associated with this declarator, bail out.
Steve Naroffb419d3a2006-10-27 23:18:49 +000049 if (II == 0) return 0;
Chris Lattnerffe65b32006-08-14 01:28:29 +000050
Steve Naroffb419d3a2006-10-27 23:18:49 +000051 TypeNameInfo *weCurrentlyHaveTypeInfo = II->getFETokenInfo<TypeNameInfo>();
52 bool isTypeName = D.getDeclSpec().StorageClassSpec == DeclSpec::SCS_typedef;
Chris Lattnerffe65b32006-08-14 01:28:29 +000053
Steve Naroffb419d3a2006-10-27 23:18:49 +000054 // this check avoids creating TypeNameInfo objects for the common case.
55 // It does need to handle the uncommon case of shadowing a typedef name with a
56 // non-typedef name. e.g. { typedef int a; a xx; { int a; } }
57 if (weCurrentlyHaveTypeInfo || isTypeName) {
58 TypeNameInfo *TI = new TypeNameInfo(isTypeName, weCurrentlyHaveTypeInfo);
59
60 II->setFETokenInfo(TI);
Chris Lattnerffe65b32006-08-14 01:28:29 +000061
Steve Naroffb419d3a2006-10-27 23:18:49 +000062 // Remember that this needs to be removed when the scope is popped.
63 S->AddDecl(II);
64 }
65 return 0;
66}
67
68// Scope will always be top level file scope.
69
70Action::DeclTy *
71EmptyAction::ParsedClassDeclaration(Scope *S,
72 IdentifierInfo **identList, unsigned nElements) {
73 for (unsigned i = 0; i < nElements; i++) {
74 TypeNameInfo *TI = new TypeNameInfo(1, identList[i]->getFETokenInfo<TypeNameInfo>());
75
76 identList[i]->setFETokenInfo(TI);
77
78 // Remember that this needs to be removed when the scope is popped.
79 S->AddDecl(identList[i]);
80 }
Chris Lattner2dacc3f2006-10-16 00:33:54 +000081 return 0;
Chris Lattnera5534f92006-08-14 00:38:06 +000082}
83
84/// PopScope - When a scope is popped, if any typedefs are now out-of-scope,
85/// they are removed from the IdentifierInfo::FETokenInfo field.
86void EmptyAction::PopScope(SourceLocation Loc, Scope *S) {
Chris Lattnerffe65b32006-08-14 01:28:29 +000087 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
88 I != E; ++I) {
89 IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I);
Steve Naroffb419d3a2006-10-27 23:18:49 +000090 TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>();
Chris Lattnerffe65b32006-08-14 01:28:29 +000091 assert(TI && "This decl didn't get pushed??");
Chris Lattnerffe65b32006-08-14 01:28:29 +000092
Steve Naroffb419d3a2006-10-27 23:18:49 +000093 if (TI) {
94 TypeNameInfo *Next = TI->Prev;
95 delete TI;
96
97 II.setFETokenInfo(Next);
98 }
Chris Lattnerffe65b32006-08-14 01:28:29 +000099 }
Chris Lattnera5534f92006-08-14 00:38:06 +0000100}