blob: 583781056e0b91212dde12e92363dbea075f399b [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//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnera5534f92006-08-14 00:38:06 +00007//
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 clang;
18
Steve Naroffb419d3a2006-10-27 23:18:49 +000019/// TypeNameInfo - A link exists here for each scope that an identifier is
Chris Lattnerffe65b32006-08-14 01:28:29 +000020/// defined.
Steve Naroffb419d3a2006-10-27 23:18:49 +000021struct TypeNameInfo {
22 TypeNameInfo *Prev;
23 bool isTypeName;
24
25 TypeNameInfo(bool istypename, TypeNameInfo *prev) {
26 isTypeName = istypename;
Chris Lattnerf055d432006-11-28 04:28:12 +000027 Prev = prev;
Steve Naroffb419d3a2006-10-27 23:18:49 +000028 }
Chris Lattnerffe65b32006-08-14 01:28:29 +000029};
30
Daniel Dunbarf8362f92008-10-31 08:56:51 +000031MinimalAction::MinimalAction(Preprocessor &pp)
32 : Idents(pp.getIdentifierTable()), PP(pp) {}
33
34void MinimalAction::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
Steve Naroff6d40db02007-10-31 18:42:27 +000035 TUScope = S;
Daniel Dunbarf8362f92008-10-31 08:56:51 +000036 if (!PP.getLangOptions().ObjC1) return;
37
38 // recognize the ObjC built-in type identifiers.
Steve Naroffaaba0272007-10-31 20:55:39 +000039 IdentifierInfo *II;
40 TypeNameInfo *TI;
Steve Naroffaaba0272007-10-31 20:55:39 +000041 II = &Idents.get("id");
42 TI = new TypeNameInfo(1, II->getFETokenInfo<TypeNameInfo>());
43 II->setFETokenInfo(TI);
44 II = &Idents.get("SEL");
45 TI = new TypeNameInfo(1, II->getFETokenInfo<TypeNameInfo>());
46 II->setFETokenInfo(TI);
47 II = &Idents.get("Class");
48 TI = new TypeNameInfo(1, II->getFETokenInfo<TypeNameInfo>());
49 II->setFETokenInfo(TI);
Fariborz Jahanian2bbd03a2007-12-07 00:18:54 +000050 II = &Idents.get("Protocol");
51 TI = new TypeNameInfo(1, II->getFETokenInfo<TypeNameInfo>());
52 II->setFETokenInfo(TI);
Steve Naroff6d40db02007-10-31 18:42:27 +000053}
54
Chris Lattner64b09ee2006-11-03 07:35:12 +000055/// isTypeName - This looks at the IdentifierInfo::FETokenInfo field to
56/// determine whether the name is a type name (objc class name or typedef) or
57/// not in this scope.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +000058///
59/// FIXME: Use the passed CXXScopeSpec for accurate C++ type checking.
Argyrios Kyrtzidis25d05e82008-08-01 10:35:27 +000060Action::TypeTy *
Douglas Gregorae2fbad2008-11-17 20:34:05 +000061MinimalAction::isTypeName(IdentifierInfo &II, Scope *S,
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +000062 const CXXScopeSpec *SS) {
Chris Lattner2ebe4bb2006-11-20 01:29:42 +000063 if (TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>())
64 if (TI->isTypeName)
65 return TI;
66 return 0;
Chris Lattnera5534f92006-08-14 00:38:06 +000067}
68
Douglas Gregor61956c42008-10-31 09:07:45 +000069/// isCurrentClassName - Always returns false, because MinimalAction
70/// does not support C++ classes with constructors.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +000071bool MinimalAction::isCurrentClassName(const IdentifierInfo &, Scope *,
72 const CXXScopeSpec *) {
Douglas Gregor61956c42008-10-31 09:07:45 +000073 return false;
74}
75
Steve Naroff30d242c2007-09-15 18:49:24 +000076/// ActOnDeclarator - If this is a typedef declarator, we modify the
Chris Lattnera5534f92006-08-14 00:38:06 +000077/// IdentifierInfo::FETokenInfo field to keep track of this fact, until S is
78/// popped.
Chris Lattner2dacc3f2006-10-16 00:33:54 +000079Action::DeclTy *
Daniel Dunbar1ff1d1f2008-08-05 16:28:08 +000080MinimalAction::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *LastInGroup) {
Steve Naroffb419d3a2006-10-27 23:18:49 +000081 IdentifierInfo *II = D.getIdentifier();
82
Chris Lattnerffe65b32006-08-14 01:28:29 +000083 // If there is no identifier associated with this declarator, bail out.
Steve Naroffb419d3a2006-10-27 23:18:49 +000084 if (II == 0) return 0;
Chris Lattnerffe65b32006-08-14 01:28:29 +000085
Steve Naroffb419d3a2006-10-27 23:18:49 +000086 TypeNameInfo *weCurrentlyHaveTypeInfo = II->getFETokenInfo<TypeNameInfo>();
Chris Lattnerf055d432006-11-28 04:28:12 +000087 bool isTypeName =
88 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef;
Chris Lattnerffe65b32006-08-14 01:28:29 +000089
Steve Naroffb419d3a2006-10-27 23:18:49 +000090 // this check avoids creating TypeNameInfo objects for the common case.
Ted Kremenekbe9b33b2008-08-04 22:51:42 +000091 // It does need to handle the uncommon case of shadowing a typedef name with a
Steve Naroffb419d3a2006-10-27 23:18:49 +000092 // non-typedef name. e.g. { typedef int a; a xx; { int a; } }
93 if (weCurrentlyHaveTypeInfo || isTypeName) {
94 TypeNameInfo *TI = new TypeNameInfo(isTypeName, weCurrentlyHaveTypeInfo);
95
96 II->setFETokenInfo(TI);
Chris Lattnerffe65b32006-08-14 01:28:29 +000097
Steve Naroffb419d3a2006-10-27 23:18:49 +000098 // Remember that this needs to be removed when the scope is popped.
99 S->AddDecl(II);
100 }
101 return 0;
102}
103
Steve Naroff09bf8152007-09-06 21:24:23 +0000104Action::DeclTy *
Chris Lattner04648562008-07-21 07:13:18 +0000105MinimalAction::ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000106 IdentifierInfo *ClassName,
107 SourceLocation ClassLoc,
108 IdentifierInfo *SuperName,
109 SourceLocation SuperLoc,
110 DeclTy * const *ProtoRefs,
111 unsigned NumProtocols,
112 SourceLocation EndProtoLoc,
113 AttributeList *AttrList) {
Steve Naroff09bf8152007-09-06 21:24:23 +0000114 TypeNameInfo *TI =
115 new TypeNameInfo(1, ClassName->getFETokenInfo<TypeNameInfo>());
116
117 ClassName->setFETokenInfo(TI);
118 return 0;
119}
120
Steve Naroff0c37b0c2007-10-02 22:39:18 +0000121/// ActOnForwardClassDeclaration -
Chris Lattner64b09ee2006-11-03 07:35:12 +0000122/// Scope will always be top level file scope.
Steve Naroffb419d3a2006-10-27 23:18:49 +0000123Action::DeclTy *
Steve Naroff93eb5f12007-10-10 17:32:04 +0000124MinimalAction::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Steve Naroff09bf8152007-09-06 21:24:23 +0000125 IdentifierInfo **IdentList, unsigned NumElts) {
Chris Lattner64b09ee2006-11-03 07:35:12 +0000126 for (unsigned i = 0; i != NumElts; ++i) {
127 TypeNameInfo *TI =
128 new TypeNameInfo(1, IdentList[i]->getFETokenInfo<TypeNameInfo>());
Steve Naroffb419d3a2006-10-27 23:18:49 +0000129
Chris Lattner64b09ee2006-11-03 07:35:12 +0000130 IdentList[i]->setFETokenInfo(TI);
Steve Naroffb419d3a2006-10-27 23:18:49 +0000131
132 // Remember that this needs to be removed when the scope is popped.
Steve Naroff93eb5f12007-10-10 17:32:04 +0000133 TUScope->AddDecl(IdentList[i]);
Steve Naroffb419d3a2006-10-27 23:18:49 +0000134 }
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000135 return 0;
Chris Lattnera5534f92006-08-14 00:38:06 +0000136}
137
Ted Kremenekbe9b33b2008-08-04 22:51:42 +0000138/// ActOnPopScope - When a scope is popped, if any typedefs are now
139/// out-of-scope, they are removed from the IdentifierInfo::FETokenInfo field.
Steve Naroff8308f602007-10-10 17:45:44 +0000140void MinimalAction::ActOnPopScope(SourceLocation Loc, Scope *S) {
Chris Lattnerffe65b32006-08-14 01:28:29 +0000141 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
142 I != E; ++I) {
143 IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I);
Steve Naroffb419d3a2006-10-27 23:18:49 +0000144 TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>();
Chris Lattnerffe65b32006-08-14 01:28:29 +0000145 assert(TI && "This decl didn't get pushed??");
Chris Lattnerffe65b32006-08-14 01:28:29 +0000146
Chris Lattnerc62b6c22006-11-05 18:44:26 +0000147 if (TI) {
Steve Naroffb419d3a2006-10-27 23:18:49 +0000148 TypeNameInfo *Next = TI->Prev;
149 delete TI;
Chris Lattnerc62b6c22006-11-05 18:44:26 +0000150
Steve Naroffb419d3a2006-10-27 23:18:49 +0000151 II.setFETokenInfo(Next);
Chris Lattnerc62b6c22006-11-05 18:44:26 +0000152 }
Chris Lattnerffe65b32006-08-14 01:28:29 +0000153 }
Chris Lattnera5534f92006-08-14 00:38:06 +0000154}