blob: 8fd453cd0bed84e50cb9aa4cfc821496dfb214e5 [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 Lattner64b09ee2006-11-03 07:35:12 +000033/// isTypeName - This looks at the IdentifierInfo::FETokenInfo field to
34/// determine whether the name is a type name (objc class name or typedef) or
35/// not in this scope.
Steve Naroffb419d3a2006-10-27 23:18:49 +000036bool EmptyAction::isTypeName(const IdentifierInfo &II, Scope *S) const {
37 TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>();
38 return TI != 0 && TI->isTypeName;
Chris Lattnera5534f92006-08-14 00:38:06 +000039}
40
41/// ParseDeclarator - If this is a typedef declarator, we modify the
42/// IdentifierInfo::FETokenInfo field to keep track of this fact, until S is
43/// popped.
Chris Lattner2dacc3f2006-10-16 00:33:54 +000044Action::DeclTy *
45EmptyAction::ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init,
46 DeclTy *LastInGroup) {
Steve Naroffb419d3a2006-10-27 23:18:49 +000047 IdentifierInfo *II = D.getIdentifier();
48
Chris Lattnerffe65b32006-08-14 01:28:29 +000049 // If there is no identifier associated with this declarator, bail out.
Steve Naroffb419d3a2006-10-27 23:18:49 +000050 if (II == 0) return 0;
Chris Lattnerffe65b32006-08-14 01:28:29 +000051
Steve Naroffb419d3a2006-10-27 23:18:49 +000052 TypeNameInfo *weCurrentlyHaveTypeInfo = II->getFETokenInfo<TypeNameInfo>();
53 bool isTypeName = D.getDeclSpec().StorageClassSpec == DeclSpec::SCS_typedef;
Chris Lattnerffe65b32006-08-14 01:28:29 +000054
Steve Naroffb419d3a2006-10-27 23:18:49 +000055 // this check avoids creating TypeNameInfo objects for the common case.
56 // It does need to handle the uncommon case of shadowing a typedef name with a
57 // non-typedef name. e.g. { typedef int a; a xx; { int a; } }
58 if (weCurrentlyHaveTypeInfo || isTypeName) {
59 TypeNameInfo *TI = new TypeNameInfo(isTypeName, weCurrentlyHaveTypeInfo);
60
61 II->setFETokenInfo(TI);
Chris Lattnerffe65b32006-08-14 01:28:29 +000062
Steve Naroffb419d3a2006-10-27 23:18:49 +000063 // Remember that this needs to be removed when the scope is popped.
64 S->AddDecl(II);
65 }
66 return 0;
67}
68
Chris Lattner64b09ee2006-11-03 07:35:12 +000069/// ParsedClassDeclaration -
70/// Scope will always be top level file scope.
Steve Naroffb419d3a2006-10-27 23:18:49 +000071Action::DeclTy *
72EmptyAction::ParsedClassDeclaration(Scope *S,
Chris Lattner64b09ee2006-11-03 07:35:12 +000073 IdentifierInfo **IdentList,
74 unsigned NumElts) {
75 for (unsigned i = 0; i != NumElts; ++i) {
76 TypeNameInfo *TI =
77 new TypeNameInfo(1, IdentList[i]->getFETokenInfo<TypeNameInfo>());
Steve Naroffb419d3a2006-10-27 23:18:49 +000078
Chris Lattner64b09ee2006-11-03 07:35:12 +000079 IdentList[i]->setFETokenInfo(TI);
Steve Naroffb419d3a2006-10-27 23:18:49 +000080
81 // Remember that this needs to be removed when the scope is popped.
Chris Lattner64b09ee2006-11-03 07:35:12 +000082 S->AddDecl(IdentList[i]);
Steve Naroffb419d3a2006-10-27 23:18:49 +000083 }
Chris Lattner2dacc3f2006-10-16 00:33:54 +000084 return 0;
Chris Lattnera5534f92006-08-14 00:38:06 +000085}
86
87/// PopScope - When a scope is popped, if any typedefs are now out-of-scope,
88/// they are removed from the IdentifierInfo::FETokenInfo field.
89void EmptyAction::PopScope(SourceLocation Loc, Scope *S) {
Chris Lattnerffe65b32006-08-14 01:28:29 +000090 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
91 I != E; ++I) {
92 IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I);
Steve Naroffb419d3a2006-10-27 23:18:49 +000093 TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>();
Chris Lattnerffe65b32006-08-14 01:28:29 +000094 assert(TI && "This decl didn't get pushed??");
Chris Lattnerffe65b32006-08-14 01:28:29 +000095
Steve Naroffb419d3a2006-10-27 23:18:49 +000096 if (TI) {
97 TypeNameInfo *Next = TI->Prev;
98 delete TI;
99
100 II.setFETokenInfo(Next);
101 }
Chris Lattnerffe65b32006-08-14 01:28:29 +0000102 }
Chris Lattnera5534f92006-08-14 00:38:06 +0000103}