blob: 6c8d75c2d6dbf014171f75da90d51d6ed9a2dbfc [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 Lattnera020b072009-05-01 16:33:20 +000017#include "clang/Basic/TargetInfo.h"
Chris Lattnerc671e062009-01-18 09:39:41 +000018#include "llvm/Support/Allocator.h"
19#include "llvm/Support/RecyclingAllocator.h"
Chris Lattner03b53942009-03-05 01:25:28 +000020#include "llvm/Support/raw_ostream.h"
Chris Lattnera5534f92006-08-14 00:38:06 +000021using namespace clang;
22
Sebastian Redl6d4256c2009-03-15 17:47:39 +000023/// Out-of-line virtual destructor to provide home for ActionBase class.
Chris Lattnerbcfe4f72009-03-05 07:24:28 +000024ActionBase::~ActionBase() {}
25
26/// Out-of-line virtual destructor to provide home for Action class.
27Action::~Action() {}
28
29// Defined out-of-line here because of dependecy on AttributeList
Chris Lattner83f095c2009-03-28 19:18:32 +000030Action::DeclPtrTy Action::ActOnUsingDirective(Scope *CurScope,
31 SourceLocation UsingLoc,
32 SourceLocation NamespcLoc,
33 const CXXScopeSpec &SS,
34 SourceLocation IdentLoc,
35 IdentifierInfo *NamespcName,
36 AttributeList *AttrList) {
Chris Lattnerbcfe4f72009-03-05 07:24:28 +000037
38 // FIXME: Parser seems to assume that Action::ActOn* takes ownership over
39 // passed AttributeList, however other actions don't free it, is it
40 // temporary state or bug?
41 delete AttrList;
Chris Lattner83f095c2009-03-28 19:18:32 +000042 return DeclPtrTy();
Chris Lattnerbcfe4f72009-03-05 07:24:28 +000043}
44
Douglas Gregorfec52632009-06-20 00:51:54 +000045// Defined out-of-line here because of dependecy on AttributeList
46Action::DeclPtrTy Action::ActOnUsingDeclaration(Scope *CurScope,
47 SourceLocation UsingLoc,
48 const CXXScopeSpec &SS,
49 SourceLocation IdentLoc,
50 IdentifierInfo *TargetName,
Anders Carlsson74d7f0d2009-06-27 00:27:47 +000051 OverloadedOperatorKind Op,
Douglas Gregorfec52632009-06-20 00:51:54 +000052 AttributeList *AttrList,
53 bool IsTypeName) {
54
55 // FIXME: Parser seems to assume that Action::ActOn* takes ownership over
56 // passed AttributeList, however other actions don't free it, is it
57 // temporary state or bug?
58 delete AttrList;
59 return DeclPtrTy();
60}
61
Chris Lattnerbcfe4f72009-03-05 07:24:28 +000062
Chris Lattnereae6cb62009-03-05 08:00:35 +000063void PrettyStackTraceActionsDecl::print(llvm::raw_ostream &OS) const {
Chris Lattner03b53942009-03-05 01:25:28 +000064 if (Loc.isValid()) {
65 Loc.print(OS, SM);
66 OS << ": ";
67 }
68 OS << Message;
69
70 std::string Name = Actions.getDeclName(TheDecl);
71 if (!Name.empty())
72 OS << " '" << Name << '\'';
73
74 OS << '\n';
75}
76
Steve Naroffb419d3a2006-10-27 23:18:49 +000077/// TypeNameInfo - A link exists here for each scope that an identifier is
Chris Lattnerffe65b32006-08-14 01:28:29 +000078/// defined.
Chris Lattnerc671e062009-01-18 09:39:41 +000079namespace {
80 struct TypeNameInfo {
81 TypeNameInfo *Prev;
82 bool isTypeName;
83
84 TypeNameInfo(bool istypename, TypeNameInfo *prev) {
85 isTypeName = istypename;
86 Prev = prev;
87 }
88 };
89
90 struct TypeNameInfoTable {
91 llvm::RecyclingAllocator<llvm::BumpPtrAllocator, TypeNameInfo> Allocator;
92
93 void AddEntry(bool isTypename, IdentifierInfo *II) {
94 TypeNameInfo *TI = Allocator.Allocate<TypeNameInfo>();
Chris Lattnerdeab7252009-03-24 17:05:27 +000095 new (TI) TypeNameInfo(isTypename, II->getFETokenInfo<TypeNameInfo>());
Chris Lattnerc671e062009-01-18 09:39:41 +000096 II->setFETokenInfo(TI);
97 }
98
99 void DeleteEntry(TypeNameInfo *Entry) {
100 Entry->~TypeNameInfo();
101 Allocator.Deallocate(Entry);
102 }
103 };
104}
105
106static TypeNameInfoTable *getTable(void *TP) {
107 return static_cast<TypeNameInfoTable*>(TP);
108}
Chris Lattnerffe65b32006-08-14 01:28:29 +0000109
Daniel Dunbarf8362f92008-10-31 08:56:51 +0000110MinimalAction::MinimalAction(Preprocessor &pp)
Chris Lattnerc671e062009-01-18 09:39:41 +0000111 : Idents(pp.getIdentifierTable()), PP(pp) {
112 TypeNameInfoTablePtr = new TypeNameInfoTable();
113}
114
115MinimalAction::~MinimalAction() {
116 delete getTable(TypeNameInfoTablePtr);
117}
Daniel Dunbarf8362f92008-10-31 08:56:51 +0000118
119void MinimalAction::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
Steve Naroff6d40db02007-10-31 18:42:27 +0000120 TUScope = S;
Chris Lattnerc671e062009-01-18 09:39:41 +0000121
122 TypeNameInfoTable &TNIT = *getTable(TypeNameInfoTablePtr);
Chris Lattnera020b072009-05-01 16:33:20 +0000123
124 if (PP.getTargetInfo().getPointerWidth(0) >= 64) {
125 // Install [u]int128_t for 64-bit targets.
126 TNIT.AddEntry(true, &Idents.get("__int128_t"));
127 TNIT.AddEntry(true, &Idents.get("__uint128_t"));
128 }
Chris Lattnerc671e062009-01-18 09:39:41 +0000129
Chris Lattnera020b072009-05-01 16:33:20 +0000130 if (PP.getLangOptions().ObjC1) {
131 // Recognize the ObjC built-in type identifiers as types.
132 TNIT.AddEntry(true, &Idents.get("id"));
133 TNIT.AddEntry(true, &Idents.get("SEL"));
134 TNIT.AddEntry(true, &Idents.get("Class"));
135 TNIT.AddEntry(true, &Idents.get("Protocol"));
136 }
Steve Naroff6d40db02007-10-31 18:42:27 +0000137}
138
Chris Lattner64b09ee2006-11-03 07:35:12 +0000139/// isTypeName - This looks at the IdentifierInfo::FETokenInfo field to
140/// determine whether the name is a type name (objc class name or typedef) or
141/// not in this scope.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000142///
143/// FIXME: Use the passed CXXScopeSpec for accurate C++ type checking.
Argyrios Kyrtzidis25d05e82008-08-01 10:35:27 +0000144Action::TypeTy *
Douglas Gregor8a6be5e2009-02-04 17:00:24 +0000145MinimalAction::getTypeName(IdentifierInfo &II, SourceLocation Loc,
146 Scope *S, const CXXScopeSpec *SS) {
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000147 if (TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>())
148 if (TI->isTypeName)
149 return TI;
150 return 0;
Chris Lattnera5534f92006-08-14 00:38:06 +0000151}
152
Douglas Gregor61956c42008-10-31 09:07:45 +0000153/// isCurrentClassName - Always returns false, because MinimalAction
154/// does not support C++ classes with constructors.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000155bool MinimalAction::isCurrentClassName(const IdentifierInfo &, Scope *,
156 const CXXScopeSpec *) {
Douglas Gregor61956c42008-10-31 09:07:45 +0000157 return false;
158}
159
Douglas Gregor7f741122009-02-25 19:37:18 +0000160TemplateNameKind
Douglas Gregorb67535d2009-03-31 00:43:58 +0000161MinimalAction::isTemplateName(const IdentifierInfo &II, Scope *S,
Douglas Gregore861bac2009-08-25 22:51:20 +0000162 const CXXScopeSpec *SS,
163 bool EnteringScope,
164 TemplateTy &TemplateDecl) {
Douglas Gregor8bf42052009-02-09 18:46:07 +0000165 return TNK_Non_template;
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000166}
167
Steve Naroff30d242c2007-09-15 18:49:24 +0000168/// ActOnDeclarator - If this is a typedef declarator, we modify the
Chris Lattnera5534f92006-08-14 00:38:06 +0000169/// IdentifierInfo::FETokenInfo field to keep track of this fact, until S is
170/// popped.
Chris Lattner83f095c2009-03-28 19:18:32 +0000171Action::DeclPtrTy
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000172MinimalAction::ActOnDeclarator(Scope *S, Declarator &D) {
Steve Naroffb419d3a2006-10-27 23:18:49 +0000173 IdentifierInfo *II = D.getIdentifier();
174
Chris Lattnerffe65b32006-08-14 01:28:29 +0000175 // If there is no identifier associated with this declarator, bail out.
Chris Lattner83f095c2009-03-28 19:18:32 +0000176 if (II == 0) return DeclPtrTy();
Chris Lattnerffe65b32006-08-14 01:28:29 +0000177
Steve Naroffb419d3a2006-10-27 23:18:49 +0000178 TypeNameInfo *weCurrentlyHaveTypeInfo = II->getFETokenInfo<TypeNameInfo>();
Chris Lattnerf055d432006-11-28 04:28:12 +0000179 bool isTypeName =
180 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef;
Chris Lattnerffe65b32006-08-14 01:28:29 +0000181
Steve Naroffb419d3a2006-10-27 23:18:49 +0000182 // this check avoids creating TypeNameInfo objects for the common case.
Ted Kremenekbe9b33b2008-08-04 22:51:42 +0000183 // It does need to handle the uncommon case of shadowing a typedef name with a
Steve Naroffb419d3a2006-10-27 23:18:49 +0000184 // non-typedef name. e.g. { typedef int a; a xx; { int a; } }
185 if (weCurrentlyHaveTypeInfo || isTypeName) {
Chris Lattnerc671e062009-01-18 09:39:41 +0000186 // Allocate and add the 'TypeNameInfo' "decl".
187 getTable(TypeNameInfoTablePtr)->AddEntry(isTypeName, II);
Chris Lattnerffe65b32006-08-14 01:28:29 +0000188
Steve Naroffb419d3a2006-10-27 23:18:49 +0000189 // Remember that this needs to be removed when the scope is popped.
Chris Lattner83f095c2009-03-28 19:18:32 +0000190 S->AddDecl(DeclPtrTy::make(II));
Steve Naroffb419d3a2006-10-27 23:18:49 +0000191 }
Chris Lattner83f095c2009-03-28 19:18:32 +0000192 return DeclPtrTy();
Steve Naroffb419d3a2006-10-27 23:18:49 +0000193}
194
Chris Lattner83f095c2009-03-28 19:18:32 +0000195Action::DeclPtrTy
Chris Lattner04648562008-07-21 07:13:18 +0000196MinimalAction::ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000197 IdentifierInfo *ClassName,
198 SourceLocation ClassLoc,
199 IdentifierInfo *SuperName,
200 SourceLocation SuperLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +0000201 const DeclPtrTy *ProtoRefs,
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000202 unsigned NumProtocols,
203 SourceLocation EndProtoLoc,
204 AttributeList *AttrList) {
Chris Lattnerc671e062009-01-18 09:39:41 +0000205 // Allocate and add the 'TypeNameInfo' "decl".
206 getTable(TypeNameInfoTablePtr)->AddEntry(true, ClassName);
Chris Lattner83f095c2009-03-28 19:18:32 +0000207 return DeclPtrTy();
Steve Naroff09bf8152007-09-06 21:24:23 +0000208}
209
Steve Naroff0c37b0c2007-10-02 22:39:18 +0000210/// ActOnForwardClassDeclaration -
Chris Lattner64b09ee2006-11-03 07:35:12 +0000211/// Scope will always be top level file scope.
Chris Lattner83f095c2009-03-28 19:18:32 +0000212Action::DeclPtrTy
Steve Naroff93eb5f12007-10-10 17:32:04 +0000213MinimalAction::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Steve Naroff09bf8152007-09-06 21:24:23 +0000214 IdentifierInfo **IdentList, unsigned NumElts) {
Chris Lattner64b09ee2006-11-03 07:35:12 +0000215 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnerc671e062009-01-18 09:39:41 +0000216 // Allocate and add the 'TypeNameInfo' "decl".
217 getTable(TypeNameInfoTablePtr)->AddEntry(true, IdentList[i]);
Steve Naroffb419d3a2006-10-27 23:18:49 +0000218
219 // Remember that this needs to be removed when the scope is popped.
Chris Lattner83f095c2009-03-28 19:18:32 +0000220 TUScope->AddDecl(DeclPtrTy::make(IdentList[i]));
Steve Naroffb419d3a2006-10-27 23:18:49 +0000221 }
Chris Lattner83f095c2009-03-28 19:18:32 +0000222 return DeclPtrTy();
Chris Lattnera5534f92006-08-14 00:38:06 +0000223}
224
Ted Kremenekbe9b33b2008-08-04 22:51:42 +0000225/// ActOnPopScope - When a scope is popped, if any typedefs are now
226/// out-of-scope, they are removed from the IdentifierInfo::FETokenInfo field.
Steve Naroff8308f602007-10-10 17:45:44 +0000227void MinimalAction::ActOnPopScope(SourceLocation Loc, Scope *S) {
Chris Lattnerc671e062009-01-18 09:39:41 +0000228 TypeNameInfoTable &Table = *getTable(TypeNameInfoTablePtr);
229
Chris Lattnerffe65b32006-08-14 01:28:29 +0000230 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
231 I != E; ++I) {
Chris Lattner83f095c2009-03-28 19:18:32 +0000232 IdentifierInfo &II = *(*I).getAs<IdentifierInfo>();
Steve Naroffb419d3a2006-10-27 23:18:49 +0000233 TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>();
Chris Lattnerffe65b32006-08-14 01:28:29 +0000234 assert(TI && "This decl didn't get pushed??");
Chris Lattnerffe65b32006-08-14 01:28:29 +0000235
Chris Lattnerc62b6c22006-11-05 18:44:26 +0000236 if (TI) {
Steve Naroffb419d3a2006-10-27 23:18:49 +0000237 TypeNameInfo *Next = TI->Prev;
Chris Lattnerc671e062009-01-18 09:39:41 +0000238 Table.DeleteEntry(TI);
Chris Lattnerc62b6c22006-11-05 18:44:26 +0000239
Steve Naroffb419d3a2006-10-27 23:18:49 +0000240 II.setFETokenInfo(Next);
Chris Lattnerc62b6c22006-11-05 18:44:26 +0000241 }
Chris Lattnerffe65b32006-08-14 01:28:29 +0000242 }
Chris Lattnera5534f92006-08-14 00:38:06 +0000243}