blob: bd0e30e20491b8bbbf1f3507e86dd50c5012f4f2 [file] [log] [blame]
Chris Lattnerc62b6c22006-11-05 18:44:26 +00001//===--- MinimalAction.cpp - Implement the MinimalAction class ------------===//
Chris Lattnera5534f92006-08-14 00:38:06 +00002//
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//
Chris Lattnerc62b6c22006-11-05 18:44:26 +000010// This file implements the MinimalAction interface.
Chris Lattnera5534f92006-08-14 00:38:06 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner288e86ff12006-11-11 23:03:42 +000015#include "clang/Parse/DeclSpec.h"
Chris Lattnerffe65b32006-08-14 01:28:29 +000016#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;
Chris Lattnerf055d432006-11-28 04:28:12 +000028 Prev = prev;
Steve Naroffb419d3a2006-10-27 23:18:49 +000029 }
Chris Lattnerffe65b32006-08-14 01:28:29 +000030};
31
Chris Lattner64b09ee2006-11-03 07:35:12 +000032/// isTypeName - This looks at the IdentifierInfo::FETokenInfo field to
33/// determine whether the name is a type name (objc class name or typedef) or
34/// not in this scope.
Chris Lattner2ebe4bb2006-11-20 01:29:42 +000035Action::DeclTy *
36MinimalAction::isTypeName(const IdentifierInfo &II, Scope *S) const {
37 if (TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>())
38 if (TI->isTypeName)
39 return TI;
40 return 0;
Chris Lattnera5534f92006-08-14 00:38:06 +000041}
42
43/// ParseDeclarator - If this is a typedef declarator, we modify the
44/// IdentifierInfo::FETokenInfo field to keep track of this fact, until S is
45/// popped.
Chris Lattner2dacc3f2006-10-16 00:33:54 +000046Action::DeclTy *
Chris Lattnerc62b6c22006-11-05 18:44:26 +000047MinimalAction::ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init,
48 DeclTy *LastInGroup) {
Steve Naroffb419d3a2006-10-27 23:18:49 +000049 IdentifierInfo *II = D.getIdentifier();
50
Chris Lattnerffe65b32006-08-14 01:28:29 +000051 // If there is no identifier associated with this declarator, bail out.
Steve Naroffb419d3a2006-10-27 23:18:49 +000052 if (II == 0) return 0;
Chris Lattnerffe65b32006-08-14 01:28:29 +000053
Steve Naroffb419d3a2006-10-27 23:18:49 +000054 TypeNameInfo *weCurrentlyHaveTypeInfo = II->getFETokenInfo<TypeNameInfo>();
Chris Lattnerf055d432006-11-28 04:28:12 +000055 bool isTypeName =
56 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef;
Chris Lattnerffe65b32006-08-14 01:28:29 +000057
Steve Naroffb419d3a2006-10-27 23:18:49 +000058 // this check avoids creating TypeNameInfo objects for the common case.
59 // It does need to handle the uncommon case of shadowing a typedef name with a
60 // non-typedef name. e.g. { typedef int a; a xx; { int a; } }
61 if (weCurrentlyHaveTypeInfo || isTypeName) {
62 TypeNameInfo *TI = new TypeNameInfo(isTypeName, weCurrentlyHaveTypeInfo);
63
64 II->setFETokenInfo(TI);
Chris Lattnerffe65b32006-08-14 01:28:29 +000065
Steve Naroffb419d3a2006-10-27 23:18:49 +000066 // Remember that this needs to be removed when the scope is popped.
67 S->AddDecl(II);
68 }
69 return 0;
70}
71
Chris Lattner94c22cd2006-11-19 02:35:21 +000072/// ParsedObjcClassDeclaration -
Chris Lattner64b09ee2006-11-03 07:35:12 +000073/// Scope will always be top level file scope.
Steve Naroffb419d3a2006-10-27 23:18:49 +000074Action::DeclTy *
Chris Lattner94c22cd2006-11-19 02:35:21 +000075MinimalAction::ParsedObjcClassDeclaration(Scope *S,
76 IdentifierInfo **IdentList,
77 unsigned NumElts) {
Chris Lattner64b09ee2006-11-03 07:35:12 +000078 for (unsigned i = 0; i != NumElts; ++i) {
79 TypeNameInfo *TI =
80 new TypeNameInfo(1, IdentList[i]->getFETokenInfo<TypeNameInfo>());
Steve Naroffb419d3a2006-10-27 23:18:49 +000081
Chris Lattner64b09ee2006-11-03 07:35:12 +000082 IdentList[i]->setFETokenInfo(TI);
Steve Naroffb419d3a2006-10-27 23:18:49 +000083
84 // Remember that this needs to be removed when the scope is popped.
Chris Lattner64b09ee2006-11-03 07:35:12 +000085 S->AddDecl(IdentList[i]);
Steve Naroffb419d3a2006-10-27 23:18:49 +000086 }
Chris Lattner2dacc3f2006-10-16 00:33:54 +000087 return 0;
Chris Lattnera5534f92006-08-14 00:38:06 +000088}
89
90/// PopScope - When a scope is popped, if any typedefs are now out-of-scope,
91/// they are removed from the IdentifierInfo::FETokenInfo field.
Chris Lattnerc62b6c22006-11-05 18:44:26 +000092void MinimalAction::PopScope(SourceLocation Loc, Scope *S) {
Chris Lattnerffe65b32006-08-14 01:28:29 +000093 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
94 I != E; ++I) {
95 IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I);
Steve Naroffb419d3a2006-10-27 23:18:49 +000096 TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>();
Chris Lattnerffe65b32006-08-14 01:28:29 +000097 assert(TI && "This decl didn't get pushed??");
Chris Lattnerffe65b32006-08-14 01:28:29 +000098
Chris Lattnerc62b6c22006-11-05 18:44:26 +000099 if (TI) {
Steve Naroffb419d3a2006-10-27 23:18:49 +0000100 TypeNameInfo *Next = TI->Prev;
101 delete TI;
Chris Lattnerc62b6c22006-11-05 18:44:26 +0000102
Steve Naroffb419d3a2006-10-27 23:18:49 +0000103 II.setFETokenInfo(Next);
Chris Lattnerc62b6c22006-11-05 18:44:26 +0000104 }
Chris Lattnerffe65b32006-08-14 01:28:29 +0000105 }
Chris Lattnera5534f92006-08-14 00:38:06 +0000106}