blob: 8b98cbad112bdaf044a8589498e5f646ef88acfb [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- SemaDecl.cpp - Semantic Analysis for Declarations ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for declarations.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Anders Carlssonc44eec62008-07-03 04:20:39 +000015#include "clang/AST/APValue.h"
Chris Lattnere1e79852008-02-06 00:51:33 +000016#include "clang/AST/ASTConsumer.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "clang/AST/ASTContext.h"
Anders Carlssonf78915f2008-02-15 07:04:12 +000018#include "clang/AST/Attr.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "clang/AST/Builtins.h"
20#include "clang/AST/Decl.h"
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +000021#include "clang/AST/DeclCXX.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "clang/AST/Expr.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000023#include "clang/AST/ExprCXX.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000024#include "clang/AST/Type.h"
25#include "clang/Parse/DeclSpec.h"
26#include "clang/Parse/Scope.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000027#include "clang/Basic/LangOptions.h"
28#include "clang/Basic/TargetInfo.h"
Steve Naroff4c49a6c2008-01-30 23:46:05 +000029#include "clang/Basic/SourceManager.h"
30// FIXME: layering (ideally, Sema shouldn't be dependent on Lex API's)
Chris Lattnere1e79852008-02-06 00:51:33 +000031#include "clang/Lex/Preprocessor.h"
Steve Naroff4c49a6c2008-01-30 23:46:05 +000032#include "clang/Lex/HeaderSearch.h"
Steve Naroff563477d2007-09-18 23:55:05 +000033#include "llvm/ADT/SmallString.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000034#include "llvm/ADT/SmallSet.h"
Fariborz Jahanian85ff2642007-10-05 18:00:57 +000035#include "llvm/ADT/DenseSet.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000036using namespace clang;
37
Steve Naroffb327ce02008-04-02 14:35:35 +000038Sema::DeclTy *Sema::isTypeName(const IdentifierInfo &II, Scope *S) {
39 Decl *IIDecl = LookupDecl(&II, Decl::IDNS_Ordinary, S, false);
40
Douglas Gregor2ce52f32008-04-13 21:07:44 +000041 if (IIDecl && (isa<TypedefDecl>(IIDecl) ||
42 isa<ObjCInterfaceDecl>(IIDecl) ||
43 isa<TagDecl>(IIDecl)))
Fariborz Jahanianbece4ac2007-10-12 16:34:10 +000044 return IIDecl;
Steve Naroff3536b442007-09-06 21:24:23 +000045 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000046}
47
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +000048DeclContext *Sema::getDCParent(DeclContext *DC) {
49 // If CurContext is a ObjC method, getParent() will return NULL.
50 if (isa<ObjCMethodDecl>(DC))
51 return Context.getTranslationUnitDecl();
52
53 // A C++ inline method is parsed *after* the topmost class it was declared in
54 // is fully parsed (it's "complete").
55 // The parsing of a C++ inline method happens at the declaration context of
56 // the topmost (non-nested) class it is declared in.
57 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(DC)) {
58 assert(isa<CXXRecordDecl>(MD->getParent()) && "C++ method not in Record.");
59 DC = MD->getParent();
60 while (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC->getParent()))
61 DC = RD;
62
63 // Return the declaration context of the topmost class the inline method is
64 // declared in.
65 return DC;
66 }
67
68 return DC->getParent();
69}
70
Chris Lattner9fdf9c62008-04-22 18:39:57 +000071void Sema::PushDeclContext(DeclContext *DC) {
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +000072 assert(getDCParent(DC) == CurContext &&
73 "The next DeclContext should be directly contained in the current one.");
Chris Lattner9fdf9c62008-04-22 18:39:57 +000074 CurContext = DC;
Chris Lattner0ed844b2008-04-04 06:12:32 +000075}
76
Chris Lattnerb048c982008-04-06 04:47:34 +000077void Sema::PopDeclContext() {
78 assert(CurContext && "DeclContext imbalance!");
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +000079 CurContext = getDCParent(CurContext);
Chris Lattner0ed844b2008-04-04 06:12:32 +000080}
81
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +000082/// Add this decl to the scope shadowed decl chains.
83void Sema::PushOnScopeChains(NamedDecl *D, Scope *S) {
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +000084 S->AddDecl(D);
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000085
86 // C++ [basic.scope]p4:
87 // -- exactly one declaration shall declare a class name or
88 // enumeration name that is not a typedef name and the other
89 // declarations shall all refer to the same object or
90 // enumerator, or all refer to functions and function templates;
91 // in this case the class name or enumeration name is hidden.
92 if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
93 // We are pushing the name of a tag (enum or class).
94 IdentifierResolver::ctx_iterator
95 CIT = IdResolver.ctx_begin(TD->getIdentifier(), TD->getDeclContext());
96 if (CIT != IdResolver.ctx_end(TD->getIdentifier()) &&
97 IdResolver.isDeclInScope(*CIT, TD->getDeclContext(), S)) {
98 // There is already a declaration with the same name in the same
99 // scope. It must be found before we find the new declaration,
100 // so swap the order on the shadowed declaration chain.
101
102 IdResolver.AddShadowedDecl(TD, *CIT);
103 return;
104 }
105 }
106
107 IdResolver.AddDecl(D);
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +0000108}
109
Steve Naroffb216c882007-10-09 22:01:59 +0000110void Sema::ActOnPopScope(SourceLocation Loc, Scope *S) {
Chris Lattner31e05722007-08-26 06:24:45 +0000111 if (S->decl_empty()) return;
112 assert((S->getFlags() & Scope::DeclScope) &&"Scope shouldn't contain decls!");
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000113
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
115 I != E; ++I) {
Steve Naroffc752d042007-09-13 18:10:37 +0000116 Decl *TmpD = static_cast<Decl*>(*I);
117 assert(TmpD && "This decl didn't get pushed??");
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +0000118
119 if (isa<CXXFieldDecl>(TmpD)) continue;
120
121 assert(isa<ScopedDecl>(TmpD) && "Decl isn't ScopedDecl?");
122 ScopedDecl *D = cast<ScopedDecl>(TmpD);
Steve Naroffc752d042007-09-13 18:10:37 +0000123
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 IdentifierInfo *II = D->getIdentifier();
125 if (!II) continue;
126
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +0000127 // We only want to remove the decls from the identifier decl chains for local
128 // scopes, when inside a function/method.
129 if (S->getFnParent() != 0)
130 IdResolver.RemoveDecl(D);
Chris Lattner7f925cc2008-04-11 07:00:53 +0000131
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +0000132 // Chain this decl to the containing DeclContext.
133 D->setNext(CurContext->getDeclChain());
134 CurContext->setDeclChain(D);
Reid Spencer5f016e22007-07-11 17:01:13 +0000135 }
136}
137
Steve Naroffe8043c32008-04-01 23:04:06 +0000138/// getObjCInterfaceDecl - Look up a for a class declaration in the scope.
139/// return 0 if one not found.
Steve Naroffe8043c32008-04-01 23:04:06 +0000140ObjCInterfaceDecl *Sema::getObjCInterfaceDecl(IdentifierInfo *Id) {
Steve Naroff31102512008-04-02 18:30:49 +0000141 // The third "scope" argument is 0 since we aren't enabling lazy built-in
142 // creation from this context.
143 Decl *IDecl = LookupDecl(Id, Decl::IDNS_Ordinary, 0, false);
Fariborz Jahanian4cabdfc2007-10-12 19:38:20 +0000144
Steve Naroffb327ce02008-04-02 14:35:35 +0000145 return dyn_cast_or_null<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanian4cabdfc2007-10-12 19:38:20 +0000146}
147
Steve Naroffe8043c32008-04-01 23:04:06 +0000148/// LookupDecl - Look up the inner-most declaration in the specified
Reid Spencer5f016e22007-07-11 17:01:13 +0000149/// namespace.
Steve Naroffb327ce02008-04-02 14:35:35 +0000150Decl *Sema::LookupDecl(const IdentifierInfo *II, unsigned NSI,
151 Scope *S, bool enableLazyBuiltinCreation) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 if (II == 0) return 0;
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000153 unsigned NS = NSI;
154 if (getLangOptions().CPlusPlus && (NS & Decl::IDNS_Ordinary))
155 NS |= Decl::IDNS_Tag;
Chris Lattner7f925cc2008-04-11 07:00:53 +0000156
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 // Scan up the scope chain looking for a decl that matches this identifier
158 // that is in the appropriate namespace. This search should not take long, as
159 // shadowing of names is uncommon, and deep shadowing is extremely uncommon.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000160 for (IdentifierResolver::iterator
161 I = IdResolver.begin(II, CurContext), E = IdResolver.end(II); I != E; ++I)
162 if ((*I)->getIdentifierNamespace() & NS)
163 return *I;
Chris Lattner7f925cc2008-04-11 07:00:53 +0000164
Reid Spencer5f016e22007-07-11 17:01:13 +0000165 // If we didn't find a use of this identifier, and if the identifier
166 // corresponds to a compiler builtin, create the decl object for the builtin
167 // now, injecting it into translation unit scope, and return it.
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000168 if (NS & Decl::IDNS_Ordinary) {
Steve Naroffb327ce02008-04-02 14:35:35 +0000169 if (enableLazyBuiltinCreation) {
170 // If this is a builtin on this (or all) targets, create the decl.
171 if (unsigned BuiltinID = II->getBuiltinID())
172 return LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID, S);
173 }
Steve Naroffe8043c32008-04-01 23:04:06 +0000174 if (getLangOptions().ObjC1) {
175 // @interface and @compatibility_alias introduce typedef-like names.
176 // Unlike typedef's, they can only be introduced at file-scope (and are
Steve Naroffc822ff42008-04-02 00:39:51 +0000177 // therefore not scoped decls). They can, however, be shadowed by
Steve Naroffe8043c32008-04-01 23:04:06 +0000178 // other names in IDNS_Ordinary.
Steve Naroff31102512008-04-02 18:30:49 +0000179 ObjCInterfaceDeclsTy::iterator IDI = ObjCInterfaceDecls.find(II);
180 if (IDI != ObjCInterfaceDecls.end())
181 return IDI->second;
Steve Naroffe8043c32008-04-01 23:04:06 +0000182 ObjCAliasTy::iterator I = ObjCAliasDecls.find(II);
183 if (I != ObjCAliasDecls.end())
184 return I->second->getClassInterface();
185 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000186 }
187 return 0;
188}
189
Chris Lattner95e2c712008-05-05 22:18:14 +0000190void Sema::InitBuiltinVaListType() {
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000191 if (!Context.getBuiltinVaListType().isNull())
192 return;
193
194 IdentifierInfo *VaIdent = &Context.Idents.get("__builtin_va_list");
Steve Naroffb327ce02008-04-02 14:35:35 +0000195 Decl *VaDecl = LookupDecl(VaIdent, Decl::IDNS_Ordinary, TUScope);
Steve Naroff733002f2007-10-18 22:17:45 +0000196 TypedefDecl *VaTypedef = cast<TypedefDecl>(VaDecl);
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000197 Context.setBuiltinVaListType(Context.getTypedefType(VaTypedef));
198}
199
Reid Spencer5f016e22007-07-11 17:01:13 +0000200/// LazilyCreateBuiltin - The specified Builtin-ID was first used at file scope.
201/// lazily create a decl for it.
Chris Lattner22b73ba2007-10-10 23:42:28 +0000202ScopedDecl *Sema::LazilyCreateBuiltin(IdentifierInfo *II, unsigned bid,
203 Scope *S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000204 Builtin::ID BID = (Builtin::ID)bid;
205
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000206 if (BID == Builtin::BI__builtin_va_start ||
Chris Lattner95e2c712008-05-05 22:18:14 +0000207 BID == Builtin::BI__builtin_va_copy ||
208 BID == Builtin::BI__builtin_va_end)
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000209 InitBuiltinVaListType();
210
Anders Carlssonb2cf3572007-10-11 01:00:40 +0000211 QualType R = Context.BuiltinInfo.GetBuiltinType(BID, Context);
Argyrios Kyrtzidisff898cd2008-04-17 14:47:13 +0000212 FunctionDecl *New = FunctionDecl::Create(Context,
213 Context.getTranslationUnitDecl(),
Chris Lattner0ed844b2008-04-04 06:12:32 +0000214 SourceLocation(), II, R,
Chris Lattnera98e58d2008-03-15 21:24:04 +0000215 FunctionDecl::Extern, false, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000216
Chris Lattner95e2c712008-05-05 22:18:14 +0000217 // Create Decl objects for each parameter, adding them to the
218 // FunctionDecl.
219 if (FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(R)) {
220 llvm::SmallVector<ParmVarDecl*, 16> Params;
221 for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i)
222 Params.push_back(ParmVarDecl::Create(Context, New, SourceLocation(), 0,
223 FT->getArgType(i), VarDecl::None, 0,
224 0));
225 New->setParams(&Params[0], Params.size());
226 }
227
228
229
Chris Lattner7f925cc2008-04-11 07:00:53 +0000230 // TUScope is the translation-unit scope to insert this function into.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000231 PushOnScopeChains(New, TUScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000232 return New;
233}
234
235/// MergeTypeDefDecl - We just parsed a typedef 'New' which has the same name
236/// and scope as a previous declaration 'Old'. Figure out how to resolve this
237/// situation, merging decls or emitting diagnostics as appropriate.
238///
Steve Naroffe8043c32008-04-01 23:04:06 +0000239TypedefDecl *Sema::MergeTypeDefDecl(TypedefDecl *New, Decl *OldD) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000240 // Verify the old decl was also a typedef.
241 TypedefDecl *Old = dyn_cast<TypedefDecl>(OldD);
242 if (!Old) {
243 Diag(New->getLocation(), diag::err_redefinition_different_kind,
244 New->getName());
245 Diag(OldD->getLocation(), diag::err_previous_definition);
246 return New;
247 }
248
Steve Naroff8ee529b2007-10-31 18:42:27 +0000249 // Allow multiple definitions for ObjC built-in typedefs.
250 // FIXME: Verify the underlying types are equivalent!
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000251 if (getLangOptions().ObjC1 && isBuiltinObjCType(New))
Steve Naroff8ee529b2007-10-31 18:42:27 +0000252 return Old;
Eli Friedman54ecfce2008-06-11 06:20:39 +0000253
254 if (getLangOptions().Microsoft) return New;
255
Steve Naroff4c49a6c2008-01-30 23:46:05 +0000256 // Redeclaration of a type is a constraint violation (6.7.2.3p1).
257 // Apparently GCC, Intel, and Sun all silently ignore the redeclaration if
258 // *either* declaration is in a system header. The code below implements
259 // this adhoc compatibility rule. FIXME: The following code will not
260 // work properly when compiling ".i" files (containing preprocessed output).
261 SourceManager &SrcMgr = Context.getSourceManager();
Steve Naroff4c49a6c2008-01-30 23:46:05 +0000262 HeaderSearch &HdrInfo = PP.getHeaderSearchInfo();
Eli Friedman54ecfce2008-06-11 06:20:39 +0000263 const FileEntry *OldDeclFile = SrcMgr.getFileEntryForLoc(Old->getLocation());
264 if (OldDeclFile) {
265 DirectoryLookup::DirType OldDirType = HdrInfo.getFileDirFlavor(OldDeclFile);
266 // Allow reclarations in both SystemHeaderDir and ExternCSystemHeaderDir.
267 if (OldDirType != DirectoryLookup::NormalHeaderDir)
268 return New;
269 }
270 const FileEntry *NewDeclFile = SrcMgr.getFileEntryForLoc(New->getLocation());
271 if (NewDeclFile) {
272 DirectoryLookup::DirType NewDirType = HdrInfo.getFileDirFlavor(NewDeclFile);
273 // Allow reclarations in both SystemHeaderDir and ExternCSystemHeaderDir.
274 if (NewDirType != DirectoryLookup::NormalHeaderDir)
275 return New;
276 }
277
Ted Kremenek2d05c082008-05-23 21:28:18 +0000278 Diag(New->getLocation(), diag::err_redefinition, New->getName());
279 Diag(Old->getLocation(), diag::err_previous_definition);
Reid Spencer5f016e22007-07-11 17:01:13 +0000280 return New;
281}
282
Chris Lattner6b6b5372008-06-26 18:38:35 +0000283/// DeclhasAttr - returns true if decl Declaration already has the target
284/// attribute.
Chris Lattnerddee4232008-03-03 03:28:21 +0000285static bool DeclHasAttr(const Decl *decl, const Attr *target) {
286 for (const Attr *attr = decl->getAttrs(); attr; attr = attr->getNext())
287 if (attr->getKind() == target->getKind())
288 return true;
289
290 return false;
291}
292
293/// MergeAttributes - append attributes from the Old decl to the New one.
294static void MergeAttributes(Decl *New, Decl *Old) {
295 Attr *attr = const_cast<Attr*>(Old->getAttrs()), *tmp;
296
Chris Lattnerddee4232008-03-03 03:28:21 +0000297 while (attr) {
298 tmp = attr;
299 attr = attr->getNext();
300
301 if (!DeclHasAttr(New, tmp)) {
302 New->addAttr(tmp);
303 } else {
304 tmp->setNext(0);
305 delete(tmp);
306 }
307 }
Nuno Lopes9141bee2008-06-01 22:53:53 +0000308
309 Old->invalidateAttrs();
Chris Lattnerddee4232008-03-03 03:28:21 +0000310}
311
Chris Lattner04421082008-04-08 04:40:51 +0000312/// MergeFunctionDecl - We just parsed a function 'New' from
313/// declarator D which has the same name and scope as a previous
314/// declaration 'Old'. Figure out how to resolve this situation,
315/// merging decls or emitting diagnostics as appropriate.
Douglas Gregorf0097952008-04-21 02:02:58 +0000316/// Redeclaration will be set true if thisNew is a redeclaration OldD.
317FunctionDecl *
318Sema::MergeFunctionDecl(FunctionDecl *New, Decl *OldD, bool &Redeclaration) {
319 Redeclaration = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000320 // Verify the old decl was also a function.
321 FunctionDecl *Old = dyn_cast<FunctionDecl>(OldD);
322 if (!Old) {
323 Diag(New->getLocation(), diag::err_redefinition_different_kind,
324 New->getName());
325 Diag(OldD->getLocation(), diag::err_previous_definition);
326 return New;
327 }
Chris Lattner04421082008-04-08 04:40:51 +0000328
Chris Lattner8bcfc5b2008-04-06 23:10:54 +0000329 QualType OldQType = Context.getCanonicalType(Old->getType());
330 QualType NewQType = Context.getCanonicalType(New->getType());
Chris Lattner55196442007-11-20 19:04:50 +0000331
Chris Lattner04421082008-04-08 04:40:51 +0000332 // C++ [dcl.fct]p3:
333 // All declarations for a function shall agree exactly in both the
334 // return type and the parameter-type-list.
Douglas Gregorf0097952008-04-21 02:02:58 +0000335 if (getLangOptions().CPlusPlus && OldQType == NewQType) {
336 MergeAttributes(New, Old);
337 Redeclaration = true;
Chris Lattner04421082008-04-08 04:40:51 +0000338 return MergeCXXFunctionDecl(New, Old);
Douglas Gregorf0097952008-04-21 02:02:58 +0000339 }
Chris Lattner04421082008-04-08 04:40:51 +0000340
341 // C: Function types need to be compatible, not identical. This handles
Steve Naroffadbbd0c2008-01-14 20:51:29 +0000342 // duplicate function decls like "void f(int); void f(enum X);" properly.
Chris Lattner04421082008-04-08 04:40:51 +0000343 if (!getLangOptions().CPlusPlus &&
344 Context.functionTypesAreCompatible(OldQType, NewQType)) {
Douglas Gregorf0097952008-04-21 02:02:58 +0000345 MergeAttributes(New, Old);
346 Redeclaration = true;
Steve Naroffadbbd0c2008-01-14 20:51:29 +0000347 return New;
Chris Lattner04421082008-04-08 04:40:51 +0000348 }
Chris Lattnere3995fe2007-11-06 06:07:26 +0000349
Steve Naroff837618c2008-01-16 15:01:34 +0000350 // A function that has already been declared has been redeclared or defined
351 // with a different type- show appropriate diagnostic
Steve Naroffe2ef8152008-04-04 14:32:09 +0000352 diag::kind PrevDiag;
Douglas Gregorf0097952008-04-21 02:02:58 +0000353 if (Old->isThisDeclarationADefinition())
Steve Naroffe2ef8152008-04-04 14:32:09 +0000354 PrevDiag = diag::err_previous_definition;
355 else if (Old->isImplicit())
356 PrevDiag = diag::err_previous_implicit_declaration;
Chris Lattner04421082008-04-08 04:40:51 +0000357 else
Steve Naroffe2ef8152008-04-04 14:32:09 +0000358 PrevDiag = diag::err_previous_declaration;
Steve Naroff837618c2008-01-16 15:01:34 +0000359
Reid Spencer5f016e22007-07-11 17:01:13 +0000360 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
361 // TODO: This is totally simplistic. It should handle merging functions
362 // together etc, merging extern int X; int X; ...
Steve Naroff837618c2008-01-16 15:01:34 +0000363 Diag(New->getLocation(), diag::err_conflicting_types, New->getName());
364 Diag(Old->getLocation(), PrevDiag);
Reid Spencer5f016e22007-07-11 17:01:13 +0000365 return New;
366}
367
Chris Lattnerfcc2d262007-11-06 04:28:31 +0000368/// equivalentArrayTypes - Used to determine whether two array types are
369/// equivalent.
370/// We need to check this explicitly as an incomplete array definition is
371/// considered a VariableArrayType, so will not match a complete array
372/// definition that would be otherwise equivalent.
373static bool areEquivalentArrayTypes(QualType NewQType, QualType OldQType) {
374 const ArrayType *NewAT = NewQType->getAsArrayType();
375 const ArrayType *OldAT = OldQType->getAsArrayType();
376
377 if (!NewAT || !OldAT)
378 return false;
379
380 // If either (or both) array types in incomplete we need to strip off the
381 // outer VariableArrayType. Once the outer VAT is removed the remaining
382 // types must be identical if the array types are to be considered
383 // equivalent.
384 // eg. int[][1] and int[1][1] become
385 // VAT(null, CAT(1, int)) and CAT(1, CAT(1, int))
386 // removing the outermost VAT gives
387 // CAT(1, int) and CAT(1, int)
388 // which are equal, therefore the array types are equivalent.
Eli Friedman9db13972008-02-15 12:53:51 +0000389 if (NewAT->isIncompleteArrayType() || OldAT->isIncompleteArrayType()) {
Chris Lattnerfcc2d262007-11-06 04:28:31 +0000390 if (NewAT->getIndexTypeQualifier() != OldAT->getIndexTypeQualifier())
391 return false;
Eli Friedman04930252008-01-29 07:51:12 +0000392 NewQType = NewAT->getElementType().getCanonicalType();
393 OldQType = OldAT->getElementType().getCanonicalType();
Chris Lattnerfcc2d262007-11-06 04:28:31 +0000394 }
395
396 return NewQType == OldQType;
397}
398
Reid Spencer5f016e22007-07-11 17:01:13 +0000399/// MergeVarDecl - We just parsed a variable 'New' which has the same name
400/// and scope as a previous declaration 'Old'. Figure out how to resolve this
401/// situation, merging decls or emitting diagnostics as appropriate.
402///
403/// FIXME: Need to carefully consider tentative definition rules (C99 6.9.2p2).
404/// For example, we incorrectly complain about i1, i4 from C99 6.9.2p4.
405///
Steve Naroffe8043c32008-04-01 23:04:06 +0000406VarDecl *Sema::MergeVarDecl(VarDecl *New, Decl *OldD) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000407 // Verify the old decl was also a variable.
408 VarDecl *Old = dyn_cast<VarDecl>(OldD);
409 if (!Old) {
410 Diag(New->getLocation(), diag::err_redefinition_different_kind,
411 New->getName());
412 Diag(OldD->getLocation(), diag::err_previous_definition);
413 return New;
414 }
Chris Lattnerddee4232008-03-03 03:28:21 +0000415
416 MergeAttributes(New, Old);
417
Reid Spencer5f016e22007-07-11 17:01:13 +0000418 // Verify the types match.
Chris Lattner8bcfc5b2008-04-06 23:10:54 +0000419 QualType OldCType = Context.getCanonicalType(Old->getType());
420 QualType NewCType = Context.getCanonicalType(New->getType());
421 if (OldCType != NewCType && !areEquivalentArrayTypes(NewCType, OldCType)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000422 Diag(New->getLocation(), diag::err_redefinition, New->getName());
423 Diag(Old->getLocation(), diag::err_previous_definition);
424 return New;
425 }
Steve Naroffb7b032e2008-01-30 00:44:01 +0000426 // C99 6.2.2p4: Check if we have a static decl followed by a non-static.
427 if (New->getStorageClass() == VarDecl::Static &&
428 (Old->getStorageClass() == VarDecl::None ||
429 Old->getStorageClass() == VarDecl::Extern)) {
430 Diag(New->getLocation(), diag::err_static_non_static, New->getName());
431 Diag(Old->getLocation(), diag::err_previous_definition);
432 return New;
433 }
434 // C99 6.2.2p4: Check if we have a non-static decl followed by a static.
435 if (New->getStorageClass() != VarDecl::Static &&
436 Old->getStorageClass() == VarDecl::Static) {
437 Diag(New->getLocation(), diag::err_non_static_static, New->getName());
438 Diag(Old->getLocation(), diag::err_previous_definition);
439 return New;
440 }
441 // We've verified the types match, now handle "tentative" definitions.
Steve Naroff248a7532008-04-15 22:42:06 +0000442 if (Old->isFileVarDecl() && New->isFileVarDecl()) {
Steve Naroffb7b032e2008-01-30 00:44:01 +0000443 // Handle C "tentative" external object definitions (C99 6.9.2).
444 bool OldIsTentative = false;
445 bool NewIsTentative = false;
446
Steve Naroff248a7532008-04-15 22:42:06 +0000447 if (!Old->getInit() &&
448 (Old->getStorageClass() == VarDecl::None ||
449 Old->getStorageClass() == VarDecl::Static))
Steve Naroffb7b032e2008-01-30 00:44:01 +0000450 OldIsTentative = true;
451
452 // FIXME: this check doesn't work (since the initializer hasn't been
453 // attached yet). This check should be moved to FinalizeDeclaratorGroup.
454 // Unfortunately, by the time we get to FinializeDeclaratorGroup, we've
455 // thrown out the old decl.
Steve Naroff248a7532008-04-15 22:42:06 +0000456 if (!New->getInit() &&
457 (New->getStorageClass() == VarDecl::None ||
458 New->getStorageClass() == VarDecl::Static))
Steve Naroffb7b032e2008-01-30 00:44:01 +0000459 ; // change to NewIsTentative = true; once the code is moved.
460
461 if (NewIsTentative || OldIsTentative)
462 return New;
463 }
Steve Naroff235549c2008-05-12 22:36:43 +0000464 // Handle __private_extern__ just like extern.
Steve Naroffb7b032e2008-01-30 00:44:01 +0000465 if (Old->getStorageClass() != VarDecl::Extern &&
Steve Naroff235549c2008-05-12 22:36:43 +0000466 Old->getStorageClass() != VarDecl::PrivateExtern &&
467 New->getStorageClass() != VarDecl::Extern &&
468 New->getStorageClass() != VarDecl::PrivateExtern) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000469 Diag(New->getLocation(), diag::err_redefinition, New->getName());
470 Diag(Old->getLocation(), diag::err_previous_definition);
471 }
472 return New;
473}
474
Chris Lattner04421082008-04-08 04:40:51 +0000475/// CheckParmsForFunctionDef - Check that the parameters of the given
476/// function are appropriate for the definition of a function. This
477/// takes care of any checks that cannot be performed on the
478/// declaration itself, e.g., that the types of each of the function
479/// parameters are complete.
480bool Sema::CheckParmsForFunctionDef(FunctionDecl *FD) {
481 bool HasInvalidParm = false;
482 for (unsigned p = 0, NumParams = FD->getNumParams(); p < NumParams; ++p) {
483 ParmVarDecl *Param = FD->getParamDecl(p);
484
485 // C99 6.7.5.3p4: the parameters in a parameter type list in a
486 // function declarator that is part of a function definition of
487 // that function shall not have incomplete type.
488 if (Param->getType()->isIncompleteType() &&
489 !Param->isInvalidDecl()) {
490 Diag(Param->getLocation(), diag::err_typecheck_decl_incomplete_type,
491 Param->getType().getAsString());
492 Param->setInvalidDecl();
493 HasInvalidParm = true;
494 }
495 }
496
497 return HasInvalidParm;
498}
499
500/// CreateImplicitParameter - Creates an implicit function parameter
501/// in the scope S and with the given type. This routine is used, for
502/// example, to create the implicit "self" parameter in an Objective-C
503/// method.
Chris Lattner41110242008-06-17 18:05:57 +0000504ImplicitParamDecl *
Chris Lattner04421082008-04-08 04:40:51 +0000505Sema::CreateImplicitParameter(Scope *S, IdentifierInfo *Id,
506 SourceLocation IdLoc, QualType Type) {
Chris Lattner41110242008-06-17 18:05:57 +0000507 ImplicitParamDecl *New = ImplicitParamDecl::Create(Context, CurContext,
508 IdLoc, Id, Type, 0);
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +0000509 if (Id)
510 PushOnScopeChains(New, S);
Chris Lattner04421082008-04-08 04:40:51 +0000511
512 return New;
513}
514
Reid Spencer5f016e22007-07-11 17:01:13 +0000515/// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with
516/// no declarator (e.g. "struct foo;") is parsed.
517Sema::DeclTy *Sema::ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS) {
518 // TODO: emit error on 'int;' or 'const enum foo;'.
519 // TODO: emit error on 'typedef int;'
520 // if (!DS.isMissingDeclaratorOk()) Diag(...);
521
Steve Naroff92199282007-11-17 21:37:36 +0000522 return dyn_cast_or_null<TagDecl>(static_cast<Decl *>(DS.getTypeRep()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000523}
524
Steve Naroffd0091aa2008-01-10 22:15:12 +0000525bool Sema::CheckSingleInitializer(Expr *&Init, QualType DeclType) {
Steve Narofff0090632007-09-02 02:04:30 +0000526 // Get the type before calling CheckSingleAssignmentConstraints(), since
527 // it can promote the expression.
Chris Lattner5cf216b2008-01-04 18:04:52 +0000528 QualType InitType = Init->getType();
Steve Narofff0090632007-09-02 02:04:30 +0000529
Chris Lattner5cf216b2008-01-04 18:04:52 +0000530 AssignConvertType ConvTy = CheckSingleAssignmentConstraints(DeclType, Init);
531 return DiagnoseAssignmentResult(ConvTy, Init->getLocStart(), DeclType,
532 InitType, Init, "initializing");
Steve Narofff0090632007-09-02 02:04:30 +0000533}
534
Steve Naroffa49e1fa2008-01-22 00:55:40 +0000535bool Sema::CheckStringLiteralInit(StringLiteral *strLiteral, QualType &DeclT) {
Eli Friedmanc5773c42008-02-15 18:16:39 +0000536 if (const IncompleteArrayType *IAT = DeclT->getAsIncompleteArrayType()) {
Steve Naroffa49e1fa2008-01-22 00:55:40 +0000537 // C99 6.7.8p14. We have an array of character type with unknown size
538 // being initialized to a string literal.
539 llvm::APSInt ConstVal(32);
540 ConstVal = strLiteral->getByteLength() + 1;
541 // Return a new array type (C99 6.7.8p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000542 DeclT = Context.getConstantArrayType(IAT->getElementType(), ConstVal,
Steve Naroffa49e1fa2008-01-22 00:55:40 +0000543 ArrayType::Normal, 0);
544 } else if (const ConstantArrayType *CAT = DeclT->getAsConstantArrayType()) {
545 // C99 6.7.8p14. We have an array of character type with known size.
546 if (strLiteral->getByteLength() > (unsigned)CAT->getMaximumElements())
547 Diag(strLiteral->getSourceRange().getBegin(),
548 diag::warn_initializer_string_for_char_array_too_long,
549 strLiteral->getSourceRange());
550 } else {
551 assert(0 && "HandleStringLiteralInit(): Invalid array type");
552 }
553 // Set type from "char *" to "constant array of char".
554 strLiteral->setType(DeclT);
555 // For now, we always return false (meaning success).
556 return false;
557}
558
559StringLiteral *Sema::IsStringLiteralInit(Expr *Init, QualType DeclType) {
Steve Naroffa49e1fa2008-01-22 00:55:40 +0000560 const ArrayType *AT = DeclType->getAsArrayType();
Steve Naroffa9960332008-01-25 00:51:06 +0000561 if (AT && AT->getElementType()->isCharType()) {
562 return dyn_cast<StringLiteral>(Init);
563 }
Steve Naroffa49e1fa2008-01-22 00:55:40 +0000564 return 0;
565}
566
Steve Naroffa9960332008-01-25 00:51:06 +0000567bool Sema::CheckInitializerTypes(Expr *&Init, QualType &DeclType) {
Steve Naroffca107302008-01-21 23:53:58 +0000568 // C99 6.7.8p3: The type of the entity to be initialized shall be an array
569 // of unknown size ("[]") or an object type that is not a variable array type.
Eli Friedmanc5773c42008-02-15 18:16:39 +0000570 if (const VariableArrayType *VAT = DeclType->getAsVariableArrayType())
Steve Naroffca107302008-01-21 23:53:58 +0000571 return Diag(VAT->getSizeExpr()->getLocStart(),
572 diag::err_variable_object_no_init,
573 VAT->getSizeExpr()->getSourceRange());
574
Steve Naroff2fdc3742007-12-10 22:44:33 +0000575 InitListExpr *InitList = dyn_cast<InitListExpr>(Init);
576 if (!InitList) {
Steve Naroffa49e1fa2008-01-22 00:55:40 +0000577 // FIXME: Handle wide strings
578 if (StringLiteral *strLiteral = IsStringLiteralInit(Init, DeclType))
579 return CheckStringLiteralInit(strLiteral, DeclType);
Eli Friedmana312ce22008-02-08 00:48:24 +0000580
581 if (DeclType->isArrayType())
582 return Diag(Init->getLocStart(),
583 diag::err_array_init_list_required,
584 Init->getSourceRange());
585
Steve Naroffd0091aa2008-01-10 22:15:12 +0000586 return CheckSingleInitializer(Init, DeclType);
Steve Naroff2fdc3742007-12-10 22:44:33 +0000587 }
Eli Friedmane6f058f2008-06-06 19:40:52 +0000588
Steve Naroff0cca7492008-05-01 22:18:59 +0000589 InitListChecker CheckInitList(this, InitList, DeclType);
590 return CheckInitList.HadError();
Steve Narofff0090632007-09-02 02:04:30 +0000591}
592
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000593Sema::DeclTy *
Steve Naroff08d92e42007-09-15 18:49:24 +0000594Sema::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *lastDecl) {
Steve Naroff94745042007-09-13 23:52:58 +0000595 ScopedDecl *LastDeclarator = dyn_cast_or_null<ScopedDecl>((Decl *)lastDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000596 IdentifierInfo *II = D.getIdentifier();
597
Chris Lattnere80a59c2007-07-25 00:24:17 +0000598 // All of these full declarators require an identifier. If it doesn't have
599 // one, the ParsedFreeStandingDeclSpec action should be used.
600 if (II == 0) {
Chris Lattner311ff022007-10-16 22:36:42 +0000601 Diag(D.getDeclSpec().getSourceRange().getBegin(),
Chris Lattner98e08632007-08-28 06:17:15 +0000602 diag::err_declarator_need_ident,
Chris Lattnere80a59c2007-07-25 00:24:17 +0000603 D.getDeclSpec().getSourceRange(), D.getSourceRange());
604 return 0;
605 }
606
Chris Lattner31e05722007-08-26 06:24:45 +0000607 // The scope passed in may not be a decl scope. Zip up the scope tree until
608 // we find one that is.
609 while ((S->getFlags() & Scope::DeclScope) == 0)
610 S = S->getParent();
611
Reid Spencer5f016e22007-07-11 17:01:13 +0000612 // See if this is a redefinition of a variable in the same scope.
Steve Naroffb327ce02008-04-02 14:35:35 +0000613 Decl *PrevDecl = LookupDecl(II, Decl::IDNS_Ordinary, S);
Steve Naroffc752d042007-09-13 18:10:37 +0000614 ScopedDecl *New;
Steve Naroff5912a352007-08-28 20:14:24 +0000615 bool InvalidDecl = false;
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000616
617 // In C++, the previous declaration we find might be a tag type
618 // (class or enum). In this case, the new declaration will hide the
619 // tag type.
620 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
621 PrevDecl = 0;
622
Chris Lattner41af0932007-11-14 06:34:38 +0000623 QualType R = GetTypeForDeclarator(D, S);
624 assert(!R.isNull() && "GetTypeForDeclarator() returned null type");
625
Reid Spencer5f016e22007-07-11 17:01:13 +0000626 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
Douglas Gregor6d6eb572008-05-07 04:49:29 +0000627 // Check that there are no default arguments (C++ only).
628 if (getLangOptions().CPlusPlus)
629 CheckExtraCXXDefaultArguments(D);
630
Chris Lattner41af0932007-11-14 06:34:38 +0000631 TypedefDecl *NewTD = ParseTypedefDecl(S, D, R, LastDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +0000632 if (!NewTD) return 0;
633
634 // Handle attributes prior to checking for duplicates in MergeVarDecl
Chris Lattner3ff30c82008-06-29 00:02:00 +0000635 ProcessDeclAttributes(NewTD, D);
Steve Naroffffce4d52008-01-09 23:34:55 +0000636 // Merge the decl with the existing one if appropriate. If the decl is
637 // in an outer scope, it isn't the same thing.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000638 if (PrevDecl && IdResolver.isDeclInScope(PrevDecl, CurContext, S)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000639 NewTD = MergeTypeDefDecl(NewTD, PrevDecl);
640 if (NewTD == 0) return 0;
641 }
642 New = NewTD;
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000643 if (S->getFnParent() == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000644 // C99 6.7.7p2: If a typedef name specifies a variably modified type
645 // then it shall have block scope.
Eli Friedman9db13972008-02-15 12:53:51 +0000646 if (NewTD->getUnderlyingType()->isVariablyModifiedType()) {
647 // FIXME: Diagnostic needs to be fixed.
648 Diag(D.getIdentifierLoc(), diag::err_typecheck_illegal_vla);
Steve Naroffd7444aa2007-08-31 17:20:07 +0000649 InvalidDecl = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000650 }
651 }
Chris Lattner41af0932007-11-14 06:34:38 +0000652 } else if (R.getTypePtr()->isFunctionType()) {
Chris Lattner271f1a62007-09-27 15:15:46 +0000653 FunctionDecl::StorageClass SC = FunctionDecl::None;
Reid Spencer5f016e22007-07-11 17:01:13 +0000654 switch (D.getDeclSpec().getStorageClassSpec()) {
655 default: assert(0 && "Unknown storage class!");
656 case DeclSpec::SCS_auto:
657 case DeclSpec::SCS_register:
658 Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_func,
659 R.getAsString());
Steve Naroff5912a352007-08-28 20:14:24 +0000660 InvalidDecl = true;
661 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000662 case DeclSpec::SCS_unspecified: SC = FunctionDecl::None; break;
663 case DeclSpec::SCS_extern: SC = FunctionDecl::Extern; break;
664 case DeclSpec::SCS_static: SC = FunctionDecl::Static; break;
Steve Naroff7dd0bd42008-01-28 21:57:15 +0000665 case DeclSpec::SCS_private_extern: SC = FunctionDecl::PrivateExtern;break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000666 }
667
Chris Lattnera98e58d2008-03-15 21:24:04 +0000668 bool isInline = D.getDeclSpec().isInlineSpecified();
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000669 FunctionDecl *NewFD;
670 if (D.getContext() == Declarator::MemberContext) {
671 // This is a C++ method declaration.
672 NewFD = CXXMethodDecl::Create(Context, cast<CXXRecordDecl>(CurContext),
673 D.getIdentifierLoc(), II, R,
674 (SC == FunctionDecl::Static), isInline,
675 LastDeclarator);
676 } else {
677 NewFD = FunctionDecl::Create(Context, CurContext,
678 D.getIdentifierLoc(),
679 II, R, SC, isInline,
680 LastDeclarator);
681 }
Ted Kremenekf5c93c12008-02-27 22:18:07 +0000682 // Handle attributes.
Chris Lattner3ff30c82008-06-29 00:02:00 +0000683 ProcessDeclAttributes(NewFD, D);
Chris Lattner04421082008-04-08 04:40:51 +0000684
685 // Copy the parameter declarations from the declarator D to
686 // the function declaration NewFD, if they are available.
687 if (D.getNumTypeObjects() > 0 &&
688 D.getTypeObject(0).Fun.hasPrototype) {
689 DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
690
691 // Create Decl objects for each parameter, adding them to the
692 // FunctionDecl.
693 llvm::SmallVector<ParmVarDecl*, 16> Params;
694
695 // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs
696 // function that takes no arguments, not a function that takes a
Chris Lattner8123a952008-04-10 02:22:51 +0000697 // single void argument.
Eli Friedman6d1e4b52008-05-22 08:54:03 +0000698 // We let through "const void" here because Sema::GetTypeForDeclarator
699 // already checks for that case.
Chris Lattner04421082008-04-08 04:40:51 +0000700 if (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 &&
701 FTI.ArgInfo[0].Param &&
Chris Lattner04421082008-04-08 04:40:51 +0000702 ((ParmVarDecl*)FTI.ArgInfo[0].Param)->getType()->isVoidType()) {
703 // empty arg list, don't push any params.
Chris Lattner8123a952008-04-10 02:22:51 +0000704 ParmVarDecl *Param = (ParmVarDecl*)FTI.ArgInfo[0].Param;
705
Chris Lattnerdef026a2008-04-10 02:26:16 +0000706 // In C++, the empty parameter-type-list must be spelled "void"; a
707 // typedef of void is not permitted.
708 if (getLangOptions().CPlusPlus &&
Eli Friedman6d1e4b52008-05-22 08:54:03 +0000709 Param->getType().getUnqualifiedType() != Context.VoidTy) {
Chris Lattner8123a952008-04-10 02:22:51 +0000710 Diag(Param->getLocation(), diag::ext_param_typedef_of_void);
711 }
712
Chris Lattner04421082008-04-08 04:40:51 +0000713 } else {
714 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i)
715 Params.push_back((ParmVarDecl *)FTI.ArgInfo[i].Param);
716 }
717
718 NewFD->setParams(&Params[0], Params.size());
719 }
720
Steve Naroffffce4d52008-01-09 23:34:55 +0000721 // Merge the decl with the existing one if appropriate. Since C functions
722 // are in a flat namespace, make sure we consider decls in outer scopes.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000723 if (PrevDecl &&
724 (!getLangOptions().CPlusPlus ||
725 IdResolver.isDeclInScope(PrevDecl, CurContext, S)) ) {
Douglas Gregorf0097952008-04-21 02:02:58 +0000726 bool Redeclaration = false;
727 NewFD = MergeFunctionDecl(NewFD, PrevDecl, Redeclaration);
Reid Spencer5f016e22007-07-11 17:01:13 +0000728 if (NewFD == 0) return 0;
Douglas Gregorf0097952008-04-21 02:02:58 +0000729 if (Redeclaration) {
Eli Friedman27424962008-05-27 05:07:37 +0000730 NewFD->setPreviousDeclaration(cast<FunctionDecl>(PrevDecl));
Douglas Gregorf0097952008-04-21 02:02:58 +0000731 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000732 }
733 New = NewFD;
Chris Lattner04421082008-04-08 04:40:51 +0000734
735 // In C++, check default arguments now that we have merged decls.
736 if (getLangOptions().CPlusPlus)
737 CheckCXXDefaultArguments(NewFD);
Reid Spencer5f016e22007-07-11 17:01:13 +0000738 } else {
Douglas Gregor6d6eb572008-05-07 04:49:29 +0000739 // Check that there are no default arguments (C++ only).
740 if (getLangOptions().CPlusPlus)
741 CheckExtraCXXDefaultArguments(D);
742
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000743 if (R.getTypePtr()->isObjCInterfaceType()) {
Fariborz Jahaniane7f64cc2007-10-12 22:10:42 +0000744 Diag(D.getIdentifierLoc(), diag::err_statically_allocated_object,
745 D.getIdentifier()->getName());
746 InvalidDecl = true;
747 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000748
749 VarDecl *NewVD;
750 VarDecl::StorageClass SC;
751 switch (D.getDeclSpec().getStorageClassSpec()) {
Chris Lattner9e151e12008-03-15 21:10:16 +0000752 default: assert(0 && "Unknown storage class!");
753 case DeclSpec::SCS_unspecified: SC = VarDecl::None; break;
754 case DeclSpec::SCS_extern: SC = VarDecl::Extern; break;
755 case DeclSpec::SCS_static: SC = VarDecl::Static; break;
756 case DeclSpec::SCS_auto: SC = VarDecl::Auto; break;
757 case DeclSpec::SCS_register: SC = VarDecl::Register; break;
758 case DeclSpec::SCS_private_extern: SC = VarDecl::PrivateExtern; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000759 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000760 if (D.getContext() == Declarator::MemberContext) {
761 assert(SC == VarDecl::Static && "Invalid storage class for member!");
762 // This is a static data member for a C++ class.
763 NewVD = CXXClassVarDecl::Create(Context, cast<CXXRecordDecl>(CurContext),
764 D.getIdentifierLoc(), II,
765 R, LastDeclarator);
Steve Narofff0090632007-09-02 02:04:30 +0000766 } else {
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000767 if (S->getFnParent() == 0) {
768 // C99 6.9p2: The storage-class specifiers auto and register shall not
769 // appear in the declaration specifiers in an external declaration.
770 if (SC == VarDecl::Auto || SC == VarDecl::Register) {
771 Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_fscope,
772 R.getAsString());
773 InvalidDecl = true;
774 }
775 NewVD = VarDecl::Create(Context, CurContext, D.getIdentifierLoc(),
776 II, R, SC, LastDeclarator);
777 } else {
778 NewVD = VarDecl::Create(Context, CurContext, D.getIdentifierLoc(),
779 II, R, SC, LastDeclarator);
780 }
Steve Naroff53a32342007-08-28 18:45:29 +0000781 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000782 // Handle attributes prior to checking for duplicates in MergeVarDecl
Chris Lattner3ff30c82008-06-29 00:02:00 +0000783 ProcessDeclAttributes(NewVD, D);
Nate Begemanc8e89a82008-03-14 18:07:10 +0000784
785 // Emit an error if an address space was applied to decl with local storage.
786 // This includes arrays of objects with address space qualifiers, but not
787 // automatic variables that point to other address spaces.
788 // ISO/IEC TR 18037 S5.1.2
Nate Begeman8e7dafe2008-03-25 18:36:32 +0000789 if (NewVD->hasLocalStorage() && (NewVD->getType().getAddressSpace() != 0)) {
790 Diag(D.getIdentifierLoc(), diag::err_as_qualified_auto_decl);
791 InvalidDecl = true;
Nate Begeman5af27e02008-03-14 00:22:18 +0000792 }
Steve Naroffffce4d52008-01-09 23:34:55 +0000793 // Merge the decl with the existing one if appropriate. If the decl is
794 // in an outer scope, it isn't the same thing.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000795 if (PrevDecl && IdResolver.isDeclInScope(PrevDecl, CurContext, S)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000796 NewVD = MergeVarDecl(NewVD, PrevDecl);
797 if (NewVD == 0) return 0;
798 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000799 New = NewVD;
800 }
801
802 // If this has an identifier, add it to the scope stack.
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +0000803 if (II)
804 PushOnScopeChains(New, S);
Steve Naroff5912a352007-08-28 20:14:24 +0000805 // If any semantic error occurred, mark the decl as invalid.
806 if (D.getInvalidType() || InvalidDecl)
807 New->setInvalidDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +0000808
809 return New;
810}
811
Eli Friedmanc594b322008-05-20 13:48:25 +0000812bool Sema::CheckAddressConstantExpressionLValue(const Expr* Init) {
813 switch (Init->getStmtClass()) {
814 default:
815 Diag(Init->getExprLoc(),
816 diag::err_init_element_not_constant, Init->getSourceRange());
817 return true;
818 case Expr::ParenExprClass: {
819 const ParenExpr* PE = cast<ParenExpr>(Init);
820 return CheckAddressConstantExpressionLValue(PE->getSubExpr());
821 }
822 case Expr::CompoundLiteralExprClass:
823 return cast<CompoundLiteralExpr>(Init)->isFileScope();
824 case Expr::DeclRefExprClass: {
825 const Decl *D = cast<DeclRefExpr>(Init)->getDecl();
Eli Friedman97c0a392008-05-21 03:39:11 +0000826 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
827 if (VD->hasGlobalStorage())
828 return false;
829 Diag(Init->getExprLoc(),
830 diag::err_init_element_not_constant, Init->getSourceRange());
831 return true;
832 }
Eli Friedmanc594b322008-05-20 13:48:25 +0000833 if (isa<FunctionDecl>(D))
834 return false;
835 Diag(Init->getExprLoc(),
836 diag::err_init_element_not_constant, Init->getSourceRange());
Steve Naroffd0091aa2008-01-10 22:15:12 +0000837 return true;
838 }
Eli Friedmanc594b322008-05-20 13:48:25 +0000839 case Expr::MemberExprClass: {
840 const MemberExpr *M = cast<MemberExpr>(Init);
841 if (M->isArrow())
842 return CheckAddressConstantExpression(M->getBase());
843 return CheckAddressConstantExpressionLValue(M->getBase());
844 }
845 case Expr::ArraySubscriptExprClass: {
846 // FIXME: Should we pedwarn for "x[0+0]" (where x is a pointer)?
847 const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(Init);
848 return CheckAddressConstantExpression(ASE->getBase()) ||
849 CheckArithmeticConstantExpression(ASE->getIdx());
850 }
851 case Expr::StringLiteralClass:
852 case Expr::PreDefinedExprClass:
853 return false;
854 case Expr::UnaryOperatorClass: {
855 const UnaryOperator *Exp = cast<UnaryOperator>(Init);
856
857 // C99 6.6p9
858 if (Exp->getOpcode() == UnaryOperator::Deref)
Eli Friedman97c0a392008-05-21 03:39:11 +0000859 return CheckAddressConstantExpression(Exp->getSubExpr());
Eli Friedmanc594b322008-05-20 13:48:25 +0000860
861 Diag(Init->getExprLoc(),
862 diag::err_init_element_not_constant, Init->getSourceRange());
863 return true;
864 }
865 }
866}
867
868bool Sema::CheckAddressConstantExpression(const Expr* Init) {
869 switch (Init->getStmtClass()) {
870 default:
871 Diag(Init->getExprLoc(),
872 diag::err_init_element_not_constant, Init->getSourceRange());
873 return true;
874 case Expr::ParenExprClass: {
875 const ParenExpr* PE = cast<ParenExpr>(Init);
876 return CheckAddressConstantExpression(PE->getSubExpr());
877 }
878 case Expr::StringLiteralClass:
879 case Expr::ObjCStringLiteralClass:
880 return false;
881 case Expr::CallExprClass: {
882 const CallExpr *CE = cast<CallExpr>(Init);
883 if (CE->isBuiltinConstantExpr())
884 return false;
885 Diag(Init->getExprLoc(),
886 diag::err_init_element_not_constant, Init->getSourceRange());
887 return true;
888 }
889 case Expr::UnaryOperatorClass: {
890 const UnaryOperator *Exp = cast<UnaryOperator>(Init);
891
892 // C99 6.6p9
893 if (Exp->getOpcode() == UnaryOperator::AddrOf)
894 return CheckAddressConstantExpressionLValue(Exp->getSubExpr());
895
896 if (Exp->getOpcode() == UnaryOperator::Extension)
897 return CheckAddressConstantExpression(Exp->getSubExpr());
898
899 Diag(Init->getExprLoc(),
900 diag::err_init_element_not_constant, Init->getSourceRange());
901 return true;
902 }
903 case Expr::BinaryOperatorClass: {
904 // FIXME: Should we pedwarn for expressions like "a + 1 + 2"?
905 const BinaryOperator *Exp = cast<BinaryOperator>(Init);
906
907 Expr *PExp = Exp->getLHS();
908 Expr *IExp = Exp->getRHS();
909 if (IExp->getType()->isPointerType())
910 std::swap(PExp, IExp);
911
912 // FIXME: Should we pedwarn if IExp isn't an integer constant expression?
913 return CheckAddressConstantExpression(PExp) ||
914 CheckArithmeticConstantExpression(IExp);
915 }
916 case Expr::ImplicitCastExprClass: {
917 const Expr* SubExpr = cast<ImplicitCastExpr>(Init)->getSubExpr();
918
919 // Check for implicit promotion
920 if (SubExpr->getType()->isFunctionType() ||
921 SubExpr->getType()->isArrayType())
922 return CheckAddressConstantExpressionLValue(SubExpr);
923
924 // Check for pointer->pointer cast
925 if (SubExpr->getType()->isPointerType())
926 return CheckAddressConstantExpression(SubExpr);
927
928 if (SubExpr->getType()->isArithmeticType())
929 return CheckArithmeticConstantExpression(SubExpr);
930
931 Diag(Init->getExprLoc(),
932 diag::err_init_element_not_constant, Init->getSourceRange());
933 return true;
934 }
935 case Expr::CastExprClass: {
936 const Expr* SubExpr = cast<CastExpr>(Init)->getSubExpr();
937
938 // Check for pointer->pointer cast
939 if (SubExpr->getType()->isPointerType())
940 return CheckAddressConstantExpression(SubExpr);
941
942 // FIXME: Should we pedwarn for (int*)(0+0)?
943 if (SubExpr->getType()->isArithmeticType())
944 return CheckArithmeticConstantExpression(SubExpr);
945
946 Diag(Init->getExprLoc(),
947 diag::err_init_element_not_constant, Init->getSourceRange());
948 return true;
949 }
950 case Expr::ConditionalOperatorClass: {
951 // FIXME: Should we pedwarn here?
952 const ConditionalOperator *Exp = cast<ConditionalOperator>(Init);
953 if (!Exp->getCond()->getType()->isArithmeticType()) {
954 Diag(Init->getExprLoc(),
955 diag::err_init_element_not_constant, Init->getSourceRange());
956 return true;
957 }
958 if (CheckArithmeticConstantExpression(Exp->getCond()))
959 return true;
960 if (Exp->getLHS() &&
961 CheckAddressConstantExpression(Exp->getLHS()))
962 return true;
963 return CheckAddressConstantExpression(Exp->getRHS());
964 }
965 case Expr::AddrLabelExprClass:
966 return false;
967 }
968}
969
Eli Friedman4caf0552008-06-09 05:05:07 +0000970static const Expr* FindExpressionBaseAddress(const Expr* E);
971
972static const Expr* FindExpressionBaseAddressLValue(const Expr* E) {
973 switch (E->getStmtClass()) {
974 default:
975 return E;
976 case Expr::ParenExprClass: {
977 const ParenExpr* PE = cast<ParenExpr>(E);
978 return FindExpressionBaseAddressLValue(PE->getSubExpr());
979 }
980 case Expr::MemberExprClass: {
981 const MemberExpr *M = cast<MemberExpr>(E);
982 if (M->isArrow())
983 return FindExpressionBaseAddress(M->getBase());
984 return FindExpressionBaseAddressLValue(M->getBase());
985 }
986 case Expr::ArraySubscriptExprClass: {
987 const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(E);
988 return FindExpressionBaseAddress(ASE->getBase());
989 }
990 case Expr::UnaryOperatorClass: {
991 const UnaryOperator *Exp = cast<UnaryOperator>(E);
992
993 if (Exp->getOpcode() == UnaryOperator::Deref)
994 return FindExpressionBaseAddress(Exp->getSubExpr());
995
996 return E;
997 }
998 }
999}
1000
1001static const Expr* FindExpressionBaseAddress(const Expr* E) {
1002 switch (E->getStmtClass()) {
1003 default:
1004 return E;
1005 case Expr::ParenExprClass: {
1006 const ParenExpr* PE = cast<ParenExpr>(E);
1007 return FindExpressionBaseAddress(PE->getSubExpr());
1008 }
1009 case Expr::UnaryOperatorClass: {
1010 const UnaryOperator *Exp = cast<UnaryOperator>(E);
1011
1012 // C99 6.6p9
1013 if (Exp->getOpcode() == UnaryOperator::AddrOf)
1014 return FindExpressionBaseAddressLValue(Exp->getSubExpr());
1015
1016 if (Exp->getOpcode() == UnaryOperator::Extension)
1017 return FindExpressionBaseAddress(Exp->getSubExpr());
1018
1019 return E;
1020 }
1021 case Expr::BinaryOperatorClass: {
1022 const BinaryOperator *Exp = cast<BinaryOperator>(E);
1023
1024 Expr *PExp = Exp->getLHS();
1025 Expr *IExp = Exp->getRHS();
1026 if (IExp->getType()->isPointerType())
1027 std::swap(PExp, IExp);
1028
1029 return FindExpressionBaseAddress(PExp);
1030 }
1031 case Expr::ImplicitCastExprClass: {
1032 const Expr* SubExpr = cast<ImplicitCastExpr>(E)->getSubExpr();
1033
1034 // Check for implicit promotion
1035 if (SubExpr->getType()->isFunctionType() ||
1036 SubExpr->getType()->isArrayType())
1037 return FindExpressionBaseAddressLValue(SubExpr);
1038
1039 // Check for pointer->pointer cast
1040 if (SubExpr->getType()->isPointerType())
1041 return FindExpressionBaseAddress(SubExpr);
1042
1043 // We assume that we have an arithmetic expression here;
1044 // if we don't, we'll figure it out later
1045 return 0;
1046 }
1047 case Expr::CastExprClass: {
1048 const Expr* SubExpr = cast<CastExpr>(E)->getSubExpr();
1049
1050 // Check for pointer->pointer cast
1051 if (SubExpr->getType()->isPointerType())
1052 return FindExpressionBaseAddress(SubExpr);
1053
1054 // We assume that we have an arithmetic expression here;
1055 // if we don't, we'll figure it out later
1056 return 0;
1057 }
1058 }
1059}
1060
Eli Friedmanc594b322008-05-20 13:48:25 +00001061bool Sema::CheckArithmeticConstantExpression(const Expr* Init) {
1062 switch (Init->getStmtClass()) {
1063 default:
1064 Diag(Init->getExprLoc(),
1065 diag::err_init_element_not_constant, Init->getSourceRange());
1066 return true;
1067 case Expr::ParenExprClass: {
1068 const ParenExpr* PE = cast<ParenExpr>(Init);
1069 return CheckArithmeticConstantExpression(PE->getSubExpr());
1070 }
1071 case Expr::FloatingLiteralClass:
1072 case Expr::IntegerLiteralClass:
1073 case Expr::CharacterLiteralClass:
1074 case Expr::ImaginaryLiteralClass:
1075 case Expr::TypesCompatibleExprClass:
1076 case Expr::CXXBoolLiteralExprClass:
1077 return false;
1078 case Expr::CallExprClass: {
1079 const CallExpr *CE = cast<CallExpr>(Init);
1080 if (CE->isBuiltinConstantExpr())
1081 return false;
1082 Diag(Init->getExprLoc(),
1083 diag::err_init_element_not_constant, Init->getSourceRange());
1084 return true;
1085 }
1086 case Expr::DeclRefExprClass: {
1087 const Decl *D = cast<DeclRefExpr>(Init)->getDecl();
1088 if (isa<EnumConstantDecl>(D))
1089 return false;
1090 Diag(Init->getExprLoc(),
1091 diag::err_init_element_not_constant, Init->getSourceRange());
1092 return true;
1093 }
1094 case Expr::CompoundLiteralExprClass:
1095 // Allow "(vector type){2,4}"; normal C constraints don't allow this,
1096 // but vectors are allowed to be magic.
1097 if (Init->getType()->isVectorType())
1098 return false;
1099 Diag(Init->getExprLoc(),
1100 diag::err_init_element_not_constant, Init->getSourceRange());
1101 return true;
1102 case Expr::UnaryOperatorClass: {
1103 const UnaryOperator *Exp = cast<UnaryOperator>(Init);
1104
1105 switch (Exp->getOpcode()) {
1106 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1107 // See C99 6.6p3.
1108 default:
1109 Diag(Init->getExprLoc(),
1110 diag::err_init_element_not_constant, Init->getSourceRange());
1111 return true;
1112 case UnaryOperator::SizeOf:
1113 case UnaryOperator::AlignOf:
1114 case UnaryOperator::OffsetOf:
1115 // sizeof(E) is a constantexpr if and only if E is not evaluted.
1116 // See C99 6.5.3.4p2 and 6.6p3.
1117 if (Exp->getSubExpr()->getType()->isConstantSizeType())
1118 return false;
1119 Diag(Init->getExprLoc(),
1120 diag::err_init_element_not_constant, Init->getSourceRange());
1121 return true;
1122 case UnaryOperator::Extension:
1123 case UnaryOperator::LNot:
1124 case UnaryOperator::Plus:
1125 case UnaryOperator::Minus:
1126 case UnaryOperator::Not:
1127 return CheckArithmeticConstantExpression(Exp->getSubExpr());
1128 }
1129 }
1130 case Expr::SizeOfAlignOfTypeExprClass: {
1131 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(Init);
1132 // Special check for void types, which are allowed as an extension
1133 if (Exp->getArgumentType()->isVoidType())
1134 return false;
1135 // alignof always evaluates to a constant.
1136 // FIXME: is sizeof(int[3.0]) a constant expression?
1137 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType()) {
1138 Diag(Init->getExprLoc(),
1139 diag::err_init_element_not_constant, Init->getSourceRange());
1140 return true;
1141 }
1142 return false;
1143 }
1144 case Expr::BinaryOperatorClass: {
1145 const BinaryOperator *Exp = cast<BinaryOperator>(Init);
1146
1147 if (Exp->getLHS()->getType()->isArithmeticType() &&
1148 Exp->getRHS()->getType()->isArithmeticType()) {
1149 return CheckArithmeticConstantExpression(Exp->getLHS()) ||
1150 CheckArithmeticConstantExpression(Exp->getRHS());
1151 }
1152
Eli Friedman4caf0552008-06-09 05:05:07 +00001153 if (Exp->getLHS()->getType()->isPointerType() &&
1154 Exp->getRHS()->getType()->isPointerType()) {
1155 const Expr* LHSBase = FindExpressionBaseAddress(Exp->getLHS());
1156 const Expr* RHSBase = FindExpressionBaseAddress(Exp->getRHS());
1157
1158 // Only allow a null (constant integer) base; we could
1159 // allow some additional cases if necessary, but this
1160 // is sufficient to cover offsetof-like constructs.
1161 if (!LHSBase && !RHSBase) {
1162 return CheckAddressConstantExpression(Exp->getLHS()) ||
1163 CheckAddressConstantExpression(Exp->getRHS());
1164 }
1165 }
1166
Eli Friedmanc594b322008-05-20 13:48:25 +00001167 Diag(Init->getExprLoc(),
1168 diag::err_init_element_not_constant, Init->getSourceRange());
1169 return true;
1170 }
1171 case Expr::ImplicitCastExprClass:
1172 case Expr::CastExprClass: {
1173 const Expr *SubExpr;
1174 if (const CastExpr *C = dyn_cast<CastExpr>(Init)) {
1175 SubExpr = C->getSubExpr();
1176 } else {
1177 SubExpr = cast<ImplicitCastExpr>(Init)->getSubExpr();
1178 }
1179
1180 if (SubExpr->getType()->isArithmeticType())
1181 return CheckArithmeticConstantExpression(SubExpr);
1182
1183 Diag(Init->getExprLoc(),
1184 diag::err_init_element_not_constant, Init->getSourceRange());
1185 return true;
1186 }
1187 case Expr::ConditionalOperatorClass: {
1188 const ConditionalOperator *Exp = cast<ConditionalOperator>(Init);
1189 if (CheckArithmeticConstantExpression(Exp->getCond()))
1190 return true;
1191 if (Exp->getLHS() &&
1192 CheckArithmeticConstantExpression(Exp->getLHS()))
1193 return true;
1194 return CheckArithmeticConstantExpression(Exp->getRHS());
1195 }
1196 }
1197}
1198
1199bool Sema::CheckForConstantInitializer(Expr *Init, QualType DclT) {
Nuno Lopes9a979c32008-07-07 16:46:50 +00001200 Init = Init->IgnoreParens();
1201
Eli Friedmanc594b322008-05-20 13:48:25 +00001202 // Look through CXXDefaultArgExprs; they have no meaning in this context.
1203 if (CXXDefaultArgExpr* DAE = dyn_cast<CXXDefaultArgExpr>(Init))
1204 return CheckForConstantInitializer(DAE->getExpr(), DclT);
1205
Nuno Lopes9a979c32008-07-07 16:46:50 +00001206 if (CompoundLiteralExpr *e = dyn_cast<CompoundLiteralExpr>(Init))
1207 return CheckForConstantInitializer(e->getInitializer(), DclT);
1208
Eli Friedmanc594b322008-05-20 13:48:25 +00001209 if (Init->getType()->isReferenceType()) {
1210 // FIXME: Work out how the heck reference types work
1211 return false;
1212#if 0
1213 // A reference is constant if the address of the expression
1214 // is constant
1215 // We look through initlists here to simplify
1216 // CheckAddressConstantExpressionLValue.
1217 if (InitListExpr *Exp = dyn_cast<InitListExpr>(Init)) {
1218 assert(Exp->getNumInits() > 0 &&
1219 "Refernce initializer cannot be empty");
1220 Init = Exp->getInit(0);
1221 }
1222 return CheckAddressConstantExpressionLValue(Init);
1223#endif
1224 }
1225
1226 if (InitListExpr *Exp = dyn_cast<InitListExpr>(Init)) {
1227 unsigned numInits = Exp->getNumInits();
1228 for (unsigned i = 0; i < numInits; i++) {
1229 // FIXME: Need to get the type of the declaration for C++,
1230 // because it could be a reference?
1231 if (CheckForConstantInitializer(Exp->getInit(i),
1232 Exp->getInit(i)->getType()))
1233 return true;
1234 }
1235 return false;
1236 }
1237
1238 if (Init->isNullPointerConstant(Context))
1239 return false;
1240 if (Init->getType()->isArithmeticType()) {
Eli Friedmanc1cc6dc2008-05-30 18:14:48 +00001241 QualType InitTy = Init->getType().getCanonicalType().getUnqualifiedType();
1242 if (InitTy == Context.BoolTy) {
1243 // Special handling for pointers implicitly cast to bool;
1244 // (e.g. "_Bool rr = &rr;"). This is only legal at the top level.
1245 if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Init)) {
1246 Expr* SubE = ICE->getSubExpr();
1247 if (SubE->getType()->isPointerType() ||
1248 SubE->getType()->isArrayType() ||
1249 SubE->getType()->isFunctionType()) {
1250 return CheckAddressConstantExpression(Init);
1251 }
1252 }
1253 } else if (InitTy->isIntegralType()) {
1254 Expr* SubE = 0;
1255 if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Init))
1256 SubE = ICE->getSubExpr();
1257 else if (CastExpr* CE = dyn_cast<CastExpr>(Init))
1258 SubE = CE->getSubExpr();
1259 // Special check for pointer cast to int; we allow as an extension
1260 // an address constant cast to an integer if the integer
1261 // is of an appropriate width (this sort of code is apparently used
1262 // in some places).
1263 // FIXME: Add pedwarn?
1264 // FIXME: Don't allow bitfields here! Need the FieldDecl for that.
1265 if (SubE && (SubE->getType()->isPointerType() ||
1266 SubE->getType()->isArrayType() ||
1267 SubE->getType()->isFunctionType())) {
1268 unsigned IntWidth = Context.getTypeSize(Init->getType());
1269 unsigned PointerWidth = Context.getTypeSize(Context.VoidPtrTy);
1270 if (IntWidth >= PointerWidth)
1271 return CheckAddressConstantExpression(Init);
1272 }
Eli Friedmanc594b322008-05-20 13:48:25 +00001273 }
1274
1275 return CheckArithmeticConstantExpression(Init);
1276 }
1277
1278 if (Init->getType()->isPointerType())
1279 return CheckAddressConstantExpression(Init);
1280
Eli Friedmanc1cc6dc2008-05-30 18:14:48 +00001281 // An array type at the top level that isn't an init-list must
1282 // be a string literal
Eli Friedmanc594b322008-05-20 13:48:25 +00001283 if (Init->getType()->isArrayType())
1284 return false;
1285
1286 Diag(Init->getExprLoc(), diag::err_init_element_not_constant,
1287 Init->getSourceRange());
1288 return true;
Steve Naroffd0091aa2008-01-10 22:15:12 +00001289}
1290
Steve Naroffbb204692007-09-12 14:07:44 +00001291void Sema::AddInitializerToDecl(DeclTy *dcl, ExprTy *init) {
Steve Naroff410e3e22007-09-12 20:13:48 +00001292 Decl *RealDecl = static_cast<Decl *>(dcl);
Steve Naroffbb204692007-09-12 14:07:44 +00001293 Expr *Init = static_cast<Expr *>(init);
Chris Lattner9a11b9a2007-10-19 20:10:30 +00001294 assert(Init && "missing initializer");
Steve Naroffbb204692007-09-12 14:07:44 +00001295
Chris Lattner9a11b9a2007-10-19 20:10:30 +00001296 // If there is no declaration, there was an error parsing it. Just ignore
1297 // the initializer.
1298 if (RealDecl == 0) {
1299 delete Init;
1300 return;
1301 }
Steve Naroffbb204692007-09-12 14:07:44 +00001302
Steve Naroff410e3e22007-09-12 20:13:48 +00001303 VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl);
1304 if (!VDecl) {
Steve Naroff8e74c932007-09-13 21:41:19 +00001305 Diag(dyn_cast<ScopedDecl>(RealDecl)->getLocation(),
1306 diag::err_illegal_initializer);
Steve Naroff410e3e22007-09-12 20:13:48 +00001307 RealDecl->setInvalidDecl();
1308 return;
1309 }
Steve Naroffbb204692007-09-12 14:07:44 +00001310 // Get the decls type and save a reference for later, since
Steve Naroffd0091aa2008-01-10 22:15:12 +00001311 // CheckInitializerTypes may change it.
Steve Naroff410e3e22007-09-12 20:13:48 +00001312 QualType DclT = VDecl->getType(), SavT = DclT;
Steve Naroff248a7532008-04-15 22:42:06 +00001313 if (VDecl->isBlockVarDecl()) {
1314 VarDecl::StorageClass SC = VDecl->getStorageClass();
Steve Naroffbb204692007-09-12 14:07:44 +00001315 if (SC == VarDecl::Extern) { // C99 6.7.8p5
Steve Naroff410e3e22007-09-12 20:13:48 +00001316 Diag(VDecl->getLocation(), diag::err_block_extern_cant_init);
Steve Naroff248a7532008-04-15 22:42:06 +00001317 VDecl->setInvalidDecl();
1318 } else if (!VDecl->isInvalidDecl()) {
Steve Naroffa9960332008-01-25 00:51:06 +00001319 if (CheckInitializerTypes(Init, DclT))
Steve Naroff248a7532008-04-15 22:42:06 +00001320 VDecl->setInvalidDecl();
Steve Naroffd0091aa2008-01-10 22:15:12 +00001321 if (SC == VarDecl::Static) // C99 6.7.8p4.
1322 CheckForConstantInitializer(Init, DclT);
Steve Naroffbb204692007-09-12 14:07:44 +00001323 }
Steve Naroff248a7532008-04-15 22:42:06 +00001324 } else if (VDecl->isFileVarDecl()) {
1325 if (VDecl->getStorageClass() == VarDecl::Extern)
Steve Naroff410e3e22007-09-12 20:13:48 +00001326 Diag(VDecl->getLocation(), diag::warn_extern_init);
Steve Naroff248a7532008-04-15 22:42:06 +00001327 if (!VDecl->isInvalidDecl())
Steve Naroffa9960332008-01-25 00:51:06 +00001328 if (CheckInitializerTypes(Init, DclT))
Steve Naroff248a7532008-04-15 22:42:06 +00001329 VDecl->setInvalidDecl();
Steve Naroffd0091aa2008-01-10 22:15:12 +00001330
1331 // C99 6.7.8p4. All file scoped initializers need to be constant.
1332 CheckForConstantInitializer(Init, DclT);
Steve Naroffbb204692007-09-12 14:07:44 +00001333 }
1334 // If the type changed, it means we had an incomplete type that was
1335 // completed by the initializer. For example:
1336 // int ary[] = { 1, 3, 5 };
1337 // "ary" transitions from a VariableArrayType to a ConstantArrayType.
Christopher Lamb48b12392007-11-29 19:09:19 +00001338 if (!VDecl->isInvalidDecl() && (DclT != SavT)) {
Steve Naroff410e3e22007-09-12 20:13:48 +00001339 VDecl->setType(DclT);
Christopher Lamb48b12392007-11-29 19:09:19 +00001340 Init->setType(DclT);
1341 }
Steve Naroffbb204692007-09-12 14:07:44 +00001342
1343 // Attach the initializer to the decl.
Steve Naroff410e3e22007-09-12 20:13:48 +00001344 VDecl->setInit(Init);
Steve Naroffbb204692007-09-12 14:07:44 +00001345 return;
1346}
1347
Reid Spencer5f016e22007-07-11 17:01:13 +00001348/// The declarators are chained together backwards, reverse the list.
1349Sema::DeclTy *Sema::FinalizeDeclaratorGroup(Scope *S, DeclTy *group) {
1350 // Often we have single declarators, handle them quickly.
Steve Naroff94745042007-09-13 23:52:58 +00001351 Decl *GroupDecl = static_cast<Decl*>(group);
1352 if (GroupDecl == 0)
Steve Naroffbb204692007-09-12 14:07:44 +00001353 return 0;
Steve Naroff94745042007-09-13 23:52:58 +00001354
1355 ScopedDecl *Group = dyn_cast<ScopedDecl>(GroupDecl);
1356 ScopedDecl *NewGroup = 0;
Steve Naroffbb204692007-09-12 14:07:44 +00001357 if (Group->getNextDeclarator() == 0)
Reid Spencer5f016e22007-07-11 17:01:13 +00001358 NewGroup = Group;
Steve Naroffbb204692007-09-12 14:07:44 +00001359 else { // reverse the list.
1360 while (Group) {
Steve Naroff94745042007-09-13 23:52:58 +00001361 ScopedDecl *Next = Group->getNextDeclarator();
Steve Naroffbb204692007-09-12 14:07:44 +00001362 Group->setNextDeclarator(NewGroup);
1363 NewGroup = Group;
1364 Group = Next;
1365 }
1366 }
1367 // Perform semantic analysis that depends on having fully processed both
1368 // the declarator and initializer.
Steve Naroff94745042007-09-13 23:52:58 +00001369 for (ScopedDecl *ID = NewGroup; ID; ID = ID->getNextDeclarator()) {
Steve Naroffbb204692007-09-12 14:07:44 +00001370 VarDecl *IDecl = dyn_cast<VarDecl>(ID);
1371 if (!IDecl)
1372 continue;
Steve Naroffbb204692007-09-12 14:07:44 +00001373 QualType T = IDecl->getType();
1374
1375 // C99 6.7.5.2p2: If an identifier is declared to be an object with
1376 // static storage duration, it shall not have a variable length array.
Steve Naroff248a7532008-04-15 22:42:06 +00001377 if ((IDecl->isFileVarDecl() || IDecl->isBlockVarDecl()) &&
1378 IDecl->getStorageClass() == VarDecl::Static) {
Eli Friedman3fe02932008-02-15 19:53:52 +00001379 if (T->getAsVariableArrayType()) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001380 Diag(IDecl->getLocation(), diag::err_typecheck_illegal_vla);
1381 IDecl->setInvalidDecl();
Steve Naroffbb204692007-09-12 14:07:44 +00001382 }
1383 }
1384 // Block scope. C99 6.7p7: If an identifier for an object is declared with
1385 // no linkage (C99 6.2.2p6), the type for the object shall be complete...
Steve Naroff248a7532008-04-15 22:42:06 +00001386 if (IDecl->isBlockVarDecl() &&
1387 IDecl->getStorageClass() != VarDecl::Extern) {
Chris Lattnerfd89bc82008-04-02 01:05:10 +00001388 if (T->isIncompleteType() && !IDecl->isInvalidDecl()) {
Chris Lattner8b1be772007-12-02 07:50:03 +00001389 Diag(IDecl->getLocation(), diag::err_typecheck_decl_incomplete_type,
1390 T.getAsString());
Steve Naroffbb204692007-09-12 14:07:44 +00001391 IDecl->setInvalidDecl();
1392 }
1393 }
1394 // File scope. C99 6.9.2p2: A declaration of an identifier for and
1395 // object that has file scope without an initializer, and without a
1396 // storage-class specifier or with the storage-class specifier "static",
1397 // constitutes a tentative definition. Note: A tentative definition with
1398 // external linkage is valid (C99 6.2.2p5).
Steve Naroff248a7532008-04-15 22:42:06 +00001399 if (IDecl && !IDecl->getInit() &&
1400 (IDecl->getStorageClass() == VarDecl::Static ||
1401 IDecl->getStorageClass() == VarDecl::None)) {
Eli Friedman9db13972008-02-15 12:53:51 +00001402 if (T->isIncompleteArrayType()) {
Steve Naroff9a75f8a2008-01-18 20:40:52 +00001403 // C99 6.9.2 (p2, p5): Implicit initialization causes an incomplete
1404 // array to be completed. Don't issue a diagnostic.
Chris Lattnerfd89bc82008-04-02 01:05:10 +00001405 } else if (T->isIncompleteType() && !IDecl->isInvalidDecl()) {
Steve Naroff9a75f8a2008-01-18 20:40:52 +00001406 // C99 6.9.2p3: If the declaration of an identifier for an object is
1407 // a tentative definition and has internal linkage (C99 6.2.2p3), the
1408 // declared type shall not be an incomplete type.
Chris Lattner8b1be772007-12-02 07:50:03 +00001409 Diag(IDecl->getLocation(), diag::err_typecheck_decl_incomplete_type,
1410 T.getAsString());
Steve Naroffbb204692007-09-12 14:07:44 +00001411 IDecl->setInvalidDecl();
1412 }
1413 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001414 }
1415 return NewGroup;
1416}
Steve Naroffe1223f72007-08-28 03:03:08 +00001417
Chris Lattner04421082008-04-08 04:40:51 +00001418/// ActOnParamDeclarator - Called from Parser::ParseFunctionDeclarator()
1419/// to introduce parameters into function prototype scope.
1420Sema::DeclTy *
1421Sema::ActOnParamDeclarator(Scope *S, Declarator &D) {
Chris Lattner985abd92008-06-26 06:49:43 +00001422 const DeclSpec &DS = D.getDeclSpec();
Chris Lattner04421082008-04-08 04:40:51 +00001423
1424 // Verify C99 6.7.5.3p2: The only SCS allowed is 'register'.
1425 if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
1426 DS.getStorageClassSpec() != DeclSpec::SCS_register) {
1427 Diag(DS.getStorageClassSpecLoc(),
1428 diag::err_invalid_storage_class_in_func_decl);
Chris Lattner985abd92008-06-26 06:49:43 +00001429 D.getMutableDeclSpec().ClearStorageClassSpecs();
Chris Lattner04421082008-04-08 04:40:51 +00001430 }
1431 if (DS.isThreadSpecified()) {
1432 Diag(DS.getThreadSpecLoc(),
1433 diag::err_invalid_storage_class_in_func_decl);
Chris Lattner985abd92008-06-26 06:49:43 +00001434 D.getMutableDeclSpec().ClearStorageClassSpecs();
Chris Lattner04421082008-04-08 04:40:51 +00001435 }
1436
Douglas Gregor6d6eb572008-05-07 04:49:29 +00001437 // Check that there are no default arguments inside the type of this
1438 // parameter (C++ only).
1439 if (getLangOptions().CPlusPlus)
1440 CheckExtraCXXDefaultArguments(D);
1441
Chris Lattner04421082008-04-08 04:40:51 +00001442 // In this context, we *do not* check D.getInvalidType(). If the declarator
1443 // type was invalid, GetTypeForDeclarator() still returns a "valid" type,
1444 // though it will not reflect the user specified type.
1445 QualType parmDeclType = GetTypeForDeclarator(D, S);
1446
1447 assert(!parmDeclType.isNull() && "GetTypeForDeclarator() returned null type");
1448
Reid Spencer5f016e22007-07-11 17:01:13 +00001449 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
1450 // Can this happen for params? We already checked that they don't conflict
1451 // among each other. Here they can only shadow globals, which is ok.
Chris Lattner04421082008-04-08 04:40:51 +00001452 IdentifierInfo *II = D.getIdentifier();
1453 if (Decl *PrevDecl = LookupDecl(II, Decl::IDNS_Ordinary, S)) {
1454 if (S->isDeclScope(PrevDecl)) {
1455 Diag(D.getIdentifierLoc(), diag::err_param_redefinition,
1456 dyn_cast<NamedDecl>(PrevDecl)->getName());
1457
1458 // Recover by removing the name
1459 II = 0;
1460 D.SetIdentifier(0, D.getIdentifierLoc());
1461 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001462 }
Steve Naroff6a9f3e32007-08-07 22:44:21 +00001463
1464 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
1465 // Doing the promotion here has a win and a loss. The win is the type for
1466 // both Decl's and DeclRefExpr's will match (a convenient invariant for the
1467 // code generator). The loss is the orginal type isn't preserved. For example:
1468 //
1469 // void func(int parmvardecl[5]) { // convert "int [5]" to "int *"
1470 // int blockvardecl[5];
1471 // sizeof(parmvardecl); // size == 4
1472 // sizeof(blockvardecl); // size == 20
1473 // }
1474 //
1475 // For expressions, all implicit conversions are captured using the
1476 // ImplicitCastExpr AST node (we have no such mechanism for Decl's).
1477 //
1478 // FIXME: If a source translation tool needs to see the original type, then
1479 // we need to consider storing both types (in ParmVarDecl)...
1480 //
Chris Lattnere6327742008-04-02 05:18:44 +00001481 if (parmDeclType->isArrayType()) {
Chris Lattner529bd022008-01-02 22:50:48 +00001482 // int x[restrict 4] -> int *restrict
Chris Lattnere6327742008-04-02 05:18:44 +00001483 parmDeclType = Context.getArrayDecayedType(parmDeclType);
Chris Lattner529bd022008-01-02 22:50:48 +00001484 } else if (parmDeclType->isFunctionType())
Steve Naroff6a9f3e32007-08-07 22:44:21 +00001485 parmDeclType = Context.getPointerType(parmDeclType);
1486
Chris Lattner04421082008-04-08 04:40:51 +00001487 ParmVarDecl *New = ParmVarDecl::Create(Context, CurContext,
1488 D.getIdentifierLoc(), II,
1489 parmDeclType, VarDecl::None,
1490 0, 0);
Anders Carlssonf78915f2008-02-15 07:04:12 +00001491
Chris Lattner04421082008-04-08 04:40:51 +00001492 if (D.getInvalidType())
Steve Naroff53a32342007-08-28 18:45:29 +00001493 New->setInvalidDecl();
1494
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +00001495 if (II)
1496 PushOnScopeChains(New, S);
Nate Begemanb7894b52008-02-17 21:20:31 +00001497
Chris Lattner3ff30c82008-06-29 00:02:00 +00001498 ProcessDeclAttributes(New, D);
Reid Spencer5f016e22007-07-11 17:01:13 +00001499 return New;
Chris Lattner04421082008-04-08 04:40:51 +00001500
Reid Spencer5f016e22007-07-11 17:01:13 +00001501}
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001502
Chris Lattnerb652cea2007-10-09 17:14:05 +00001503Sema::DeclTy *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Declarator &D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +00001504 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
Reid Spencer5f016e22007-07-11 17:01:13 +00001505 assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function &&
1506 "Not a function declarator!");
1507 DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
Chris Lattner04421082008-04-08 04:40:51 +00001508
Reid Spencer5f016e22007-07-11 17:01:13 +00001509 // Verify 6.9.1p6: 'every identifier in the identifier list shall be declared'
1510 // for a K&R function.
1511 if (!FTI.hasPrototype) {
1512 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Chris Lattner04421082008-04-08 04:40:51 +00001513 if (FTI.ArgInfo[i].Param == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001514 Diag(FTI.ArgInfo[i].IdentLoc, diag::ext_param_not_declared,
1515 FTI.ArgInfo[i].Ident->getName());
1516 // Implicitly declare the argument as type 'int' for lack of a better
1517 // type.
Chris Lattner04421082008-04-08 04:40:51 +00001518 DeclSpec DS;
1519 const char* PrevSpec; // unused
1520 DS.SetTypeSpecType(DeclSpec::TST_int, FTI.ArgInfo[i].IdentLoc,
1521 PrevSpec);
1522 Declarator ParamD(DS, Declarator::KNRTypeListContext);
1523 ParamD.SetIdentifier(FTI.ArgInfo[i].Ident, FTI.ArgInfo[i].IdentLoc);
1524 FTI.ArgInfo[i].Param = ActOnParamDeclarator(FnBodyScope, ParamD);
Reid Spencer5f016e22007-07-11 17:01:13 +00001525 }
1526 }
Chris Lattner52804082008-02-17 19:31:09 +00001527
Reid Spencer5f016e22007-07-11 17:01:13 +00001528 // Since this is a function definition, act as though we have information
1529 // about the arguments.
Chris Lattner52804082008-02-17 19:31:09 +00001530 if (FTI.NumArgs)
1531 FTI.hasPrototype = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001532 } else {
Chris Lattner04421082008-04-08 04:40:51 +00001533 // FIXME: Diagnose arguments without names in C.
Reid Spencer5f016e22007-07-11 17:01:13 +00001534 }
1535
1536 Scope *GlobalScope = FnBodyScope->getParent();
Steve Naroffadbbd0c2008-01-14 20:51:29 +00001537
1538 // See if this is a redefinition.
Steve Naroffe8043c32008-04-01 23:04:06 +00001539 Decl *PrevDcl = LookupDecl(D.getIdentifier(), Decl::IDNS_Ordinary,
Steve Naroffb327ce02008-04-02 14:35:35 +00001540 GlobalScope);
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +00001541 if (PrevDcl && IdResolver.isDeclInScope(PrevDcl, CurContext)) {
1542 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PrevDcl)) {
1543 const FunctionDecl *Definition;
1544 if (FD->getBody(Definition)) {
1545 Diag(D.getIdentifierLoc(), diag::err_redefinition,
1546 D.getIdentifier()->getName());
1547 Diag(Definition->getLocation(), diag::err_previous_definition);
1548 }
Steve Naroffadbbd0c2008-01-14 20:51:29 +00001549 }
1550 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001551
1552 return ActOnStartOfFunctionDef(FnBodyScope,
1553 ActOnDeclarator(GlobalScope, D, 0));
1554}
1555
1556Sema::DeclTy *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, DeclTy *D) {
1557 Decl *decl = static_cast<Decl*>(D);
Chris Lattnere9ba3232008-02-16 01:20:36 +00001558 FunctionDecl *FD = cast<FunctionDecl>(decl);
Chris Lattnerb048c982008-04-06 04:47:34 +00001559 PushDeclContext(FD);
Chris Lattner04421082008-04-08 04:40:51 +00001560
1561 // Check the validity of our function parameters
1562 CheckParmsForFunctionDef(FD);
1563
1564 // Introduce our parameters into the function scope
1565 for (unsigned p = 0, NumParams = FD->getNumParams(); p < NumParams; ++p) {
1566 ParmVarDecl *Param = FD->getParamDecl(p);
1567 // If this has an identifier, add it to the scope stack.
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +00001568 if (Param->getIdentifier())
1569 PushOnScopeChains(Param, FnBodyScope);
Reid Spencer5f016e22007-07-11 17:01:13 +00001570 }
Chris Lattner04421082008-04-08 04:40:51 +00001571
Reid Spencer5f016e22007-07-11 17:01:13 +00001572 return FD;
1573}
1574
Steve Naroffd6d054d2007-11-11 23:20:51 +00001575Sema::DeclTy *Sema::ActOnFinishFunctionBody(DeclTy *D, StmtTy *Body) {
1576 Decl *dcl = static_cast<Decl *>(D);
1577 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(dcl)) {
1578 FD->setBody((Stmt*)Body);
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +00001579 assert(FD == getCurFunctionDecl() && "Function parsing confused");
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001580 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(dcl)) {
Steve Naroffd6d054d2007-11-11 23:20:51 +00001581 MD->setBody((Stmt*)Body);
Steve Naroff4d832202007-12-13 18:18:56 +00001582 }
Chris Lattnerb048c982008-04-06 04:47:34 +00001583 PopDeclContext();
Reid Spencer5f016e22007-07-11 17:01:13 +00001584 // Verify and clean out per-function state.
1585
1586 // Check goto/label use.
1587 for (llvm::DenseMap<IdentifierInfo*, LabelStmt*>::iterator
1588 I = LabelMap.begin(), E = LabelMap.end(); I != E; ++I) {
1589 // Verify that we have no forward references left. If so, there was a goto
1590 // or address of a label taken, but no definition of it. Label fwd
1591 // definitions are indicated with a null substmt.
1592 if (I->second->getSubStmt() == 0) {
1593 LabelStmt *L = I->second;
1594 // Emit error.
1595 Diag(L->getIdentLoc(), diag::err_undeclared_label_use, L->getName());
1596
1597 // At this point, we have gotos that use the bogus label. Stitch it into
1598 // the function body so that they aren't leaked and that the AST is well
1599 // formed.
Chris Lattner0cbc2152008-01-25 00:01:10 +00001600 if (Body) {
1601 L->setSubStmt(new NullStmt(L->getIdentLoc()));
1602 cast<CompoundStmt>((Stmt*)Body)->push_back(L);
1603 } else {
1604 // The whole function wasn't parsed correctly, just delete this.
1605 delete L;
1606 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001607 }
1608 }
1609 LabelMap.clear();
1610
Steve Naroffd6d054d2007-11-11 23:20:51 +00001611 return D;
Fariborz Jahanian60fbca02007-11-10 16:31:34 +00001612}
1613
Reid Spencer5f016e22007-07-11 17:01:13 +00001614/// ImplicitlyDefineFunction - An undeclared identifier was used in a function
1615/// call, forming a call to an implicitly defined function (per C99 6.5.1p2).
Steve Naroff8c9f13e2007-09-16 16:16:00 +00001616ScopedDecl *Sema::ImplicitlyDefineFunction(SourceLocation Loc,
1617 IdentifierInfo &II, Scope *S) {
Chris Lattner37d10842008-05-05 21:18:06 +00001618 // Extension in C99. Legal in C90, but warn about it.
1619 if (getLangOptions().C99)
Reid Spencer5f016e22007-07-11 17:01:13 +00001620 Diag(Loc, diag::ext_implicit_function_decl, II.getName());
Chris Lattner37d10842008-05-05 21:18:06 +00001621 else
Reid Spencer5f016e22007-07-11 17:01:13 +00001622 Diag(Loc, diag::warn_implicit_function_decl, II.getName());
1623
1624 // FIXME: handle stuff like:
1625 // void foo() { extern float X(); }
1626 // void bar() { X(); } <-- implicit decl for X in another scope.
1627
1628 // Set a Declarator for the implicit definition: int foo();
1629 const char *Dummy;
1630 DeclSpec DS;
1631 bool Error = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, Dummy);
1632 Error = Error; // Silence warning.
1633 assert(!Error && "Error setting up implicit decl!");
1634 Declarator D(DS, Declarator::BlockContext);
1635 D.AddTypeInfo(DeclaratorChunk::getFunction(false, false, 0, 0, Loc));
1636 D.SetIdentifier(&II, Loc);
1637
Argyrios Kyrtzidis93213bb2008-05-01 21:04:16 +00001638 // Insert this function into translation-unit scope.
1639
1640 DeclContext *PrevDC = CurContext;
1641 CurContext = Context.getTranslationUnitDecl();
1642
Steve Naroffe2ef8152008-04-04 14:32:09 +00001643 FunctionDecl *FD =
Argyrios Kyrtzidis93213bb2008-05-01 21:04:16 +00001644 dyn_cast<FunctionDecl>(static_cast<Decl*>(ActOnDeclarator(TUScope, D, 0)));
Steve Naroffe2ef8152008-04-04 14:32:09 +00001645 FD->setImplicit();
Argyrios Kyrtzidis93213bb2008-05-01 21:04:16 +00001646
1647 CurContext = PrevDC;
1648
Steve Naroffe2ef8152008-04-04 14:32:09 +00001649 return FD;
Reid Spencer5f016e22007-07-11 17:01:13 +00001650}
1651
1652
Chris Lattner41af0932007-11-14 06:34:38 +00001653TypedefDecl *Sema::ParseTypedefDecl(Scope *S, Declarator &D, QualType T,
Steve Naroff94745042007-09-13 23:52:58 +00001654 ScopedDecl *LastDeclarator) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001655 assert(D.getIdentifier() && "Wrong callback for declspec without declarator");
Steve Naroff5912a352007-08-28 20:14:24 +00001656 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001657
1658 // Scope manipulation handled by caller.
Chris Lattner0ed844b2008-04-04 06:12:32 +00001659 TypedefDecl *NewTD = TypedefDecl::Create(Context, CurContext,
1660 D.getIdentifierLoc(),
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001661 D.getIdentifier(),
Chris Lattnerc63e6602008-03-15 21:32:50 +00001662 T, LastDeclarator);
Steve Naroff5912a352007-08-28 20:14:24 +00001663 if (D.getInvalidType())
1664 NewTD->setInvalidDecl();
1665 return NewTD;
Reid Spencer5f016e22007-07-11 17:01:13 +00001666}
1667
Steve Naroff08d92e42007-09-15 18:49:24 +00001668/// ActOnTag - This is invoked when we see 'struct foo' or 'struct {'. In the
Reid Spencer5f016e22007-07-11 17:01:13 +00001669/// former case, Name will be non-null. In the later case, Name will be null.
1670/// TagType indicates what kind of tag this is. TK indicates whether this is a
1671/// reference/declaration/definition of a tag.
Steve Naroff08d92e42007-09-15 18:49:24 +00001672Sema::DeclTy *Sema::ActOnTag(Scope *S, unsigned TagType, TagKind TK,
Reid Spencer5f016e22007-07-11 17:01:13 +00001673 SourceLocation KWLoc, IdentifierInfo *Name,
1674 SourceLocation NameLoc, AttributeList *Attr) {
1675 // If this is a use of an existing tag, it must have a name.
1676 assert((Name != 0 || TK == TK_Definition) &&
1677 "Nameless record must be a definition!");
1678
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001679 TagDecl::TagKind Kind;
Reid Spencer5f016e22007-07-11 17:01:13 +00001680 switch (TagType) {
1681 default: assert(0 && "Unknown tag type!");
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001682 case DeclSpec::TST_struct: Kind = TagDecl::TK_struct; break;
1683 case DeclSpec::TST_union: Kind = TagDecl::TK_union; break;
1684 case DeclSpec::TST_class: Kind = TagDecl::TK_class; break;
1685 case DeclSpec::TST_enum: Kind = TagDecl::TK_enum; break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001686 }
1687
1688 // If this is a named struct, check to see if there was a previous forward
1689 // declaration or definition.
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +00001690 // Use ScopedDecl instead of TagDecl, because a NamespaceDecl may come up.
1691 if (ScopedDecl *PrevDecl =
1692 dyn_cast_or_null<ScopedDecl>(LookupDecl(Name, Decl::IDNS_Tag, S))) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001693
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +00001694 assert((isa<TagDecl>(PrevDecl) || isa<NamespaceDecl>(PrevDecl)) &&
1695 "unexpected Decl type");
1696 if (TagDecl *PrevTagDecl = dyn_cast<TagDecl>(PrevDecl)) {
Chris Lattner14943b92008-07-03 03:30:58 +00001697 // If this is a use of a previous tag, or if the tag is already declared
1698 // in the same scope (so that the definition/declaration completes or
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +00001699 // rementions the tag), reuse the decl.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +00001700 if (TK == TK_Reference ||
1701 IdResolver.isDeclInScope(PrevDecl, CurContext, S)) {
Chris Lattner14943b92008-07-03 03:30:58 +00001702 // Make sure that this wasn't declared as an enum and now used as a
1703 // struct or something similar.
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001704 if (PrevTagDecl->getTagKind() != Kind) {
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +00001705 Diag(KWLoc, diag::err_use_with_wrong_tag, Name->getName());
1706 Diag(PrevDecl->getLocation(), diag::err_previous_use);
Chris Lattner14943b92008-07-03 03:30:58 +00001707 // Recover by making this an anonymous redefinition.
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +00001708 Name = 0;
Chris Lattner14943b92008-07-03 03:30:58 +00001709 PrevDecl = 0;
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +00001710 } else {
Chris Lattner14943b92008-07-03 03:30:58 +00001711 // If this is a use or a forward declaration, we're good.
1712 if (TK != TK_Definition)
1713 return PrevDecl;
1714
1715 // Diagnose attempts to redefine a tag.
1716 if (PrevTagDecl->isDefinition()) {
1717 Diag(NameLoc, diag::err_redefinition, Name->getName());
1718 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
1719 // If this is a redefinition, recover by making this struct be
1720 // anonymous, which will make any later references get the previous
1721 // definition.
1722 Name = 0;
1723 } else {
1724 // Okay, this is definition of a previously declared or referenced
1725 // tag. Move the location of the decl to be the definition site.
1726 PrevDecl->setLocation(NameLoc);
1727 return PrevDecl;
1728 }
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +00001729 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001730 }
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +00001731 // If we get here, this is a definition of a new struct type in a nested
1732 // scope, e.g. "struct foo; void bar() { struct foo; }", just create a new
1733 // type.
1734 } else {
1735 // The tag name clashes with a namespace name, issue an error and recover
1736 // by making this tag be anonymous.
1737 Diag(NameLoc, diag::err_redefinition_different_kind, Name->getName());
1738 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
1739 Name = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001740 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001741 }
1742
1743 // If there is an identifier, use the location of the identifier as the
1744 // location of the decl, otherwise use the location of the struct/union
1745 // keyword.
1746 SourceLocation Loc = NameLoc.isValid() ? NameLoc : KWLoc;
1747
1748 // Otherwise, if this is the first time we've seen this tag, create the decl.
1749 TagDecl *New;
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001750 if (Kind == TagDecl::TK_enum) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001751 // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.:
1752 // enum X { A, B, C } D; D should chain to X.
Chris Lattner0ed844b2008-04-04 06:12:32 +00001753 New = EnumDecl::Create(Context, CurContext, Loc, Name, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +00001754 // If this is an undefined enum, warn.
1755 if (TK != TK_Definition) Diag(Loc, diag::ext_forward_ref_enum);
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001756 } else {
1757 // struct/union/class
1758
Reid Spencer5f016e22007-07-11 17:01:13 +00001759 // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.:
1760 // struct X { int A; } D; D should chain to X.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001761 if (getLangOptions().CPlusPlus)
1762 // FIXME: Look for a way to use RecordDecl for simple structs.
1763 New = CXXRecordDecl::Create(Context, Kind, CurContext, Loc, Name, 0);
1764 else
1765 New = RecordDecl::Create(Context, Kind, CurContext, Loc, Name, 0);
1766 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001767
1768 // If this has an identifier, add it to the scope stack.
1769 if (Name) {
Chris Lattner31e05722007-08-26 06:24:45 +00001770 // The scope passed in may not be a decl scope. Zip up the scope tree until
1771 // we find one that is.
1772 while ((S->getFlags() & Scope::DeclScope) == 0)
1773 S = S->getParent();
1774
1775 // Add it to the decl chain.
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +00001776 PushOnScopeChains(New, S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001777 }
Chris Lattnere1e79852008-02-06 00:51:33 +00001778
Chris Lattnerf2e4bd52008-06-28 23:58:55 +00001779 if (Attr)
1780 ProcessDeclAttributeList(New, Attr);
Reid Spencer5f016e22007-07-11 17:01:13 +00001781 return New;
1782}
1783
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001784/// Collect the instance variables declared in an Objective-C object. Used in
1785/// the creation of structures from objects using the @defs directive.
1786static void CollectIvars(ObjCInterfaceDecl *Class,
1787 llvm::SmallVector<Sema::DeclTy*, 16> &ivars) {
1788 if (Class->getSuperClass())
1789 CollectIvars(Class->getSuperClass(), ivars);
1790 ivars.append(Class->ivar_begin(), Class->ivar_end());
1791}
1792
1793/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1794/// instance variables of ClassName into Decls.
1795void Sema::ActOnDefs(Scope *S, SourceLocation DeclStart,
1796 IdentifierInfo *ClassName,
1797 llvm::SmallVector<DeclTy*, 16> &Decls) {
1798 // Check that ClassName is a valid class
1799 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
1800 if (!Class) {
1801 Diag(DeclStart, diag::err_undef_interface, ClassName->getName());
1802 return;
1803 }
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001804 // Collect the instance variables
1805 CollectIvars(Class, Decls);
1806}
1807
Eli Friedman1b76ada2008-06-03 21:01:11 +00001808QualType Sema::TryFixInvalidVariablyModifiedType(QualType T) {
1809 // This method tries to turn a variable array into a constant
1810 // array even when the size isn't an ICE. This is necessary
1811 // for compatibility with code that depends on gcc's buggy
1812 // constant expression folding, like struct {char x[(int)(char*)2];}
1813 if (const VariableArrayType* VLATy = dyn_cast<VariableArrayType>(T)) {
Anders Carlssonc44eec62008-07-03 04:20:39 +00001814 APValue Result;
Eli Friedman1b76ada2008-06-03 21:01:11 +00001815 if (VLATy->getSizeExpr() &&
Anders Carlssonc44eec62008-07-03 04:20:39 +00001816 VLATy->getSizeExpr()->tryEvaluate(Result, Context) && Result.isSInt() &&
1817 Result.getSInt() > llvm::APSInt(Result.getSInt().getBitWidth(),
1818 Result.getSInt().isUnsigned())) {
Eli Friedman1b76ada2008-06-03 21:01:11 +00001819 return Context.getConstantArrayType(VLATy->getElementType(),
Anders Carlssonc44eec62008-07-03 04:20:39 +00001820 Result.getSInt(),
1821 ArrayType::Normal, 0);
Eli Friedman1b76ada2008-06-03 21:01:11 +00001822 }
1823 }
1824 return QualType();
1825}
1826
Steve Naroff08d92e42007-09-15 18:49:24 +00001827/// ActOnField - Each field of a struct/union/class is passed into this in order
Reid Spencer5f016e22007-07-11 17:01:13 +00001828/// to create a FieldDecl object for it.
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +00001829Sema::DeclTy *Sema::ActOnField(Scope *S,
Reid Spencer5f016e22007-07-11 17:01:13 +00001830 SourceLocation DeclStart,
1831 Declarator &D, ExprTy *BitfieldWidth) {
1832 IdentifierInfo *II = D.getIdentifier();
1833 Expr *BitWidth = (Expr*)BitfieldWidth;
Reid Spencer5f016e22007-07-11 17:01:13 +00001834 SourceLocation Loc = DeclStart;
1835 if (II) Loc = D.getIdentifierLoc();
1836
1837 // FIXME: Unnamed fields can be handled in various different ways, for
1838 // example, unnamed unions inject all members into the struct namespace!
1839
1840
1841 if (BitWidth) {
1842 // TODO: Validate.
1843 //printf("WARNING: BITFIELDS IGNORED!\n");
1844
1845 // 6.7.2.1p3
1846 // 6.7.2.1p4
1847
1848 } else {
1849 // Not a bitfield.
1850
1851 // validate II.
1852
1853 }
1854
1855 QualType T = GetTypeForDeclarator(D, S);
Steve Naroff5912a352007-08-28 20:14:24 +00001856 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
1857 bool InvalidDecl = false;
Steve Naroffd7444aa2007-08-31 17:20:07 +00001858
Reid Spencer5f016e22007-07-11 17:01:13 +00001859 // C99 6.7.2.1p8: A member of a structure or union may have any type other
1860 // than a variably modified type.
Eli Friedman9db13972008-02-15 12:53:51 +00001861 if (T->isVariablyModifiedType()) {
Eli Friedman1b76ada2008-06-03 21:01:11 +00001862 QualType FixedTy = TryFixInvalidVariablyModifiedType(T);
1863 if (!FixedTy.isNull()) {
1864 Diag(Loc, diag::warn_illegal_constant_array_size, Loc);
1865 T = FixedTy;
1866 } else {
1867 // FIXME: This diagnostic needs work
1868 Diag(Loc, diag::err_typecheck_illegal_vla, Loc);
1869 InvalidDecl = true;
1870 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001871 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001872 // FIXME: Chain fielddecls together.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001873 FieldDecl *NewFD;
1874
1875 if (getLangOptions().CPlusPlus) {
1876 // FIXME: Replace CXXFieldDecls with FieldDecls for simple structs.
1877 NewFD = CXXFieldDecl::Create(Context, cast<CXXRecordDecl>(CurContext),
1878 Loc, II, T, BitWidth);
1879 if (II)
1880 PushOnScopeChains(NewFD, S);
1881 }
1882 else
1883 NewFD = FieldDecl::Create(Context, Loc, II, T, BitWidth);
Steve Naroff44739212007-09-11 21:17:26 +00001884
Chris Lattner3ff30c82008-06-29 00:02:00 +00001885 ProcessDeclAttributes(NewFD, D);
Anders Carlssonad148062008-02-16 00:29:18 +00001886
Steve Naroff5912a352007-08-28 20:14:24 +00001887 if (D.getInvalidType() || InvalidDecl)
1888 NewFD->setInvalidDecl();
1889 return NewFD;
Reid Spencer5f016e22007-07-11 17:01:13 +00001890}
1891
Fariborz Jahanian89204a12007-10-01 16:53:59 +00001892/// TranslateIvarVisibility - Translate visibility from a token ID to an
1893/// AST enum value.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001894static ObjCIvarDecl::AccessControl
Fariborz Jahanian89204a12007-10-01 16:53:59 +00001895TranslateIvarVisibility(tok::ObjCKeywordKind ivarVisibility) {
Steve Narofff13271f2007-09-14 23:09:53 +00001896 switch (ivarVisibility) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001897 case tok::objc_private: return ObjCIvarDecl::Private;
1898 case tok::objc_public: return ObjCIvarDecl::Public;
1899 case tok::objc_protected: return ObjCIvarDecl::Protected;
1900 case tok::objc_package: return ObjCIvarDecl::Package;
Fariborz Jahanian89204a12007-10-01 16:53:59 +00001901 default: assert(false && "Unknown visitibility kind");
Steve Narofff13271f2007-09-14 23:09:53 +00001902 }
1903}
1904
Fariborz Jahanian45bc03f2008-04-11 16:55:42 +00001905/// ActOnIvar - Each ivar field of an objective-c class is passed into this
1906/// in order to create an IvarDecl object for it.
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +00001907Sema::DeclTy *Sema::ActOnIvar(Scope *S,
Fariborz Jahanian45bc03f2008-04-11 16:55:42 +00001908 SourceLocation DeclStart,
1909 Declarator &D, ExprTy *BitfieldWidth,
1910 tok::ObjCKeywordKind Visibility) {
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +00001911 IdentifierInfo *II = D.getIdentifier();
1912 Expr *BitWidth = (Expr*)BitfieldWidth;
1913 SourceLocation Loc = DeclStart;
1914 if (II) Loc = D.getIdentifierLoc();
1915
1916 // FIXME: Unnamed fields can be handled in various different ways, for
1917 // example, unnamed unions inject all members into the struct namespace!
1918
1919
1920 if (BitWidth) {
1921 // TODO: Validate.
1922 //printf("WARNING: BITFIELDS IGNORED!\n");
1923
1924 // 6.7.2.1p3
1925 // 6.7.2.1p4
1926
1927 } else {
1928 // Not a bitfield.
1929
1930 // validate II.
1931
1932 }
1933
1934 QualType T = GetTypeForDeclarator(D, S);
1935 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
1936 bool InvalidDecl = false;
1937
1938 // C99 6.7.2.1p8: A member of a structure or union may have any type other
1939 // than a variably modified type.
1940 if (T->isVariablyModifiedType()) {
1941 // FIXME: This diagnostic needs work
1942 Diag(Loc, diag::err_typecheck_illegal_vla, Loc);
1943 InvalidDecl = true;
1944 }
1945
Fariborz Jahanian45bc03f2008-04-11 16:55:42 +00001946 ObjCIvarDecl *NewID = ObjCIvarDecl::Create(Context, Loc, II, T);
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +00001947
Chris Lattner3ff30c82008-06-29 00:02:00 +00001948 ProcessDeclAttributes(NewID, D);
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +00001949
1950 if (D.getInvalidType() || InvalidDecl)
1951 NewID->setInvalidDecl();
1952 // If we have visibility info, make sure the AST is set accordingly.
Fariborz Jahanian45bc03f2008-04-11 16:55:42 +00001953 if (Visibility != tok::objc_not_keyword)
1954 NewID->setAccessControl(TranslateIvarVisibility(Visibility));
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +00001955 return NewID;
1956}
1957
Fariborz Jahanian9d048ff2007-09-29 00:54:24 +00001958void Sema::ActOnFields(Scope* S,
Fariborz Jahanian3f5faf72007-10-04 00:45:27 +00001959 SourceLocation RecLoc, DeclTy *RecDecl,
Steve Naroff08d92e42007-09-15 18:49:24 +00001960 DeclTy **Fields, unsigned NumFields,
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +00001961 SourceLocation LBrac, SourceLocation RBrac) {
Steve Naroff74216642007-09-14 22:20:54 +00001962 Decl *EnclosingDecl = static_cast<Decl*>(RecDecl);
1963 assert(EnclosingDecl && "missing record or interface decl");
1964 RecordDecl *Record = dyn_cast<RecordDecl>(EnclosingDecl);
1965
Fariborz Jahaniane267ab62007-09-14 16:27:55 +00001966 if (Record && Record->isDefinition()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001967 // Diagnose code like:
1968 // struct S { struct S {} X; };
1969 // We discover this when we complete the outer S. Reject and ignore the
1970 // outer S.
1971 Diag(Record->getLocation(), diag::err_nested_redefinition,
1972 Record->getKindName());
1973 Diag(RecLoc, diag::err_previous_definition);
Steve Naroff74216642007-09-14 22:20:54 +00001974 Record->setInvalidDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +00001975 return;
1976 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001977 // Verify that all the fields are okay.
1978 unsigned NumNamedMembers = 0;
1979 llvm::SmallVector<FieldDecl*, 32> RecFields;
1980 llvm::SmallSet<const IdentifierInfo*, 32> FieldIDs;
Steve Naroff74216642007-09-14 22:20:54 +00001981
Reid Spencer5f016e22007-07-11 17:01:13 +00001982 for (unsigned i = 0; i != NumFields; ++i) {
Fariborz Jahaniane267ab62007-09-14 16:27:55 +00001983
Steve Naroff74216642007-09-14 22:20:54 +00001984 FieldDecl *FD = cast_or_null<FieldDecl>(static_cast<Decl*>(Fields[i]));
1985 assert(FD && "missing field decl");
1986
1987 // Remember all fields.
1988 RecFields.push_back(FD);
Reid Spencer5f016e22007-07-11 17:01:13 +00001989
1990 // Get the type for the field.
Chris Lattner02c642e2007-07-31 21:33:24 +00001991 Type *FDTy = FD->getType().getTypePtr();
Steve Narofff13271f2007-09-14 23:09:53 +00001992
Reid Spencer5f016e22007-07-11 17:01:13 +00001993 // C99 6.7.2.1p2 - A field may not be a function type.
Chris Lattner02c642e2007-07-31 21:33:24 +00001994 if (FDTy->isFunctionType()) {
Steve Naroff74216642007-09-14 22:20:54 +00001995 Diag(FD->getLocation(), diag::err_field_declared_as_function,
Reid Spencer5f016e22007-07-11 17:01:13 +00001996 FD->getName());
Steve Naroff74216642007-09-14 22:20:54 +00001997 FD->setInvalidDecl();
1998 EnclosingDecl->setInvalidDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +00001999 continue;
2000 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002001 // C99 6.7.2.1p2 - A field may not be an incomplete type except...
2002 if (FDTy->isIncompleteType()) {
Fariborz Jahaniane267ab62007-09-14 16:27:55 +00002003 if (!Record) { // Incomplete ivar type is always an error.
Fariborz Jahanian3f5faf72007-10-04 00:45:27 +00002004 Diag(FD->getLocation(), diag::err_field_incomplete, FD->getName());
Steve Naroff74216642007-09-14 22:20:54 +00002005 FD->setInvalidDecl();
2006 EnclosingDecl->setInvalidDecl();
Fariborz Jahanian3f5faf72007-10-04 00:45:27 +00002007 continue;
Fariborz Jahaniane267ab62007-09-14 16:27:55 +00002008 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002009 if (i != NumFields-1 || // ... that the last member ...
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00002010 !Record->isStruct() || // ... of a structure ...
Chris Lattner02c642e2007-07-31 21:33:24 +00002011 !FDTy->isArrayType()) { //... may have incomplete array type.
Reid Spencer5f016e22007-07-11 17:01:13 +00002012 Diag(FD->getLocation(), diag::err_field_incomplete, FD->getName());
Steve Naroff74216642007-09-14 22:20:54 +00002013 FD->setInvalidDecl();
2014 EnclosingDecl->setInvalidDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +00002015 continue;
2016 }
Fariborz Jahaniane267ab62007-09-14 16:27:55 +00002017 if (NumNamedMembers < 1) { //... must have more than named member ...
Reid Spencer5f016e22007-07-11 17:01:13 +00002018 Diag(FD->getLocation(), diag::err_flexible_array_empty_struct,
2019 FD->getName());
Steve Naroff74216642007-09-14 22:20:54 +00002020 FD->setInvalidDecl();
2021 EnclosingDecl->setInvalidDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +00002022 continue;
2023 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002024 // Okay, we have a legal flexible array member at the end of the struct.
Fariborz Jahaniane267ab62007-09-14 16:27:55 +00002025 if (Record)
2026 Record->setHasFlexibleArrayMember(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002027 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002028 /// C99 6.7.2.1p2 - a struct ending in a flexible array member cannot be the
2029 /// field of another structure or the element of an array.
Chris Lattner02c642e2007-07-31 21:33:24 +00002030 if (const RecordType *FDTTy = FDTy->getAsRecordType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002031 if (FDTTy->getDecl()->hasFlexibleArrayMember()) {
2032 // If this is a member of a union, then entire union becomes "flexible".
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00002033 if (Record && Record->isUnion()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002034 Record->setHasFlexibleArrayMember(true);
2035 } else {
2036 // If this is a struct/class and this is not the last element, reject
2037 // it. Note that GCC supports variable sized arrays in the middle of
2038 // structures.
2039 if (i != NumFields-1) {
2040 Diag(FD->getLocation(), diag::err_variable_sized_type_in_struct,
2041 FD->getName());
Steve Naroff74216642007-09-14 22:20:54 +00002042 FD->setInvalidDecl();
2043 EnclosingDecl->setInvalidDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +00002044 continue;
2045 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002046 // We support flexible arrays at the end of structs in other structs
2047 // as an extension.
2048 Diag(FD->getLocation(), diag::ext_flexible_array_in_struct,
2049 FD->getName());
Fariborz Jahanian3f5faf72007-10-04 00:45:27 +00002050 if (Record)
Fariborz Jahaniane267ab62007-09-14 16:27:55 +00002051 Record->setHasFlexibleArrayMember(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002052 }
2053 }
2054 }
Fariborz Jahaniane7f64cc2007-10-12 22:10:42 +00002055 /// A field cannot be an Objective-c object
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002056 if (FDTy->isObjCInterfaceType()) {
Fariborz Jahaniane7f64cc2007-10-12 22:10:42 +00002057 Diag(FD->getLocation(), diag::err_statically_allocated_object,
2058 FD->getName());
2059 FD->setInvalidDecl();
2060 EnclosingDecl->setInvalidDecl();
2061 continue;
2062 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002063 // Keep track of the number of named members.
2064 if (IdentifierInfo *II = FD->getIdentifier()) {
2065 // Detect duplicate member names.
2066 if (!FieldIDs.insert(II)) {
2067 Diag(FD->getLocation(), diag::err_duplicate_member, II->getName());
2068 // Find the previous decl.
2069 SourceLocation PrevLoc;
2070 for (unsigned i = 0, e = RecFields.size(); ; ++i) {
2071 assert(i != e && "Didn't find previous def!");
2072 if (RecFields[i]->getIdentifier() == II) {
2073 PrevLoc = RecFields[i]->getLocation();
2074 break;
2075 }
2076 }
2077 Diag(PrevLoc, diag::err_previous_definition);
Steve Naroff74216642007-09-14 22:20:54 +00002078 FD->setInvalidDecl();
2079 EnclosingDecl->setInvalidDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +00002080 continue;
2081 }
2082 ++NumNamedMembers;
2083 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002084 }
2085
Reid Spencer5f016e22007-07-11 17:01:13 +00002086 // Okay, we successfully defined 'Record'.
Chris Lattnere1e79852008-02-06 00:51:33 +00002087 if (Record) {
Fariborz Jahaniane267ab62007-09-14 16:27:55 +00002088 Record->defineBody(&RecFields[0], RecFields.size());
Chris Lattnere1e79852008-02-06 00:51:33 +00002089 Consumer.HandleTagDeclDefinition(Record);
2090 } else {
Chris Lattnera91d3812008-02-05 22:40:55 +00002091 ObjCIvarDecl **ClsFields = reinterpret_cast<ObjCIvarDecl**>(&RecFields[0]);
2092 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(EnclosingDecl))
2093 ID->addInstanceVariablesToClass(ClsFields, RecFields.size(), RBrac);
2094 else if (ObjCImplementationDecl *IMPDecl =
2095 dyn_cast<ObjCImplementationDecl>(EnclosingDecl)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002096 assert(IMPDecl && "ActOnFields - missing ObjCImplementationDecl");
2097 IMPDecl->ObjCAddInstanceVariablesToClassImpl(ClsFields, RecFields.size());
Fariborz Jahanian3a3ca1b2007-10-31 18:48:14 +00002098 CheckImplementationIvars(IMPDecl, ClsFields, RecFields.size(), RBrac);
Fariborz Jahaniand0b90bf2007-09-26 18:27:25 +00002099 }
Fariborz Jahanianb04a0212007-09-14 21:08:27 +00002100 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002101}
2102
Steve Naroff08d92e42007-09-15 18:49:24 +00002103Sema::DeclTy *Sema::ActOnEnumConstant(Scope *S, DeclTy *theEnumDecl,
Reid Spencer5f016e22007-07-11 17:01:13 +00002104 DeclTy *lastEnumConst,
2105 SourceLocation IdLoc, IdentifierInfo *Id,
2106 SourceLocation EqualLoc, ExprTy *val) {
Chris Lattner0ed844b2008-04-04 06:12:32 +00002107 EnumDecl *TheEnumDecl = cast<EnumDecl>(static_cast<Decl*>(theEnumDecl));
Reid Spencer5f016e22007-07-11 17:01:13 +00002108 EnumConstantDecl *LastEnumConst =
2109 cast_or_null<EnumConstantDecl>(static_cast<Decl*>(lastEnumConst));
2110 Expr *Val = static_cast<Expr*>(val);
2111
Chris Lattner31e05722007-08-26 06:24:45 +00002112 // The scope passed in may not be a decl scope. Zip up the scope tree until
2113 // we find one that is.
2114 while ((S->getFlags() & Scope::DeclScope) == 0)
2115 S = S->getParent();
2116
Reid Spencer5f016e22007-07-11 17:01:13 +00002117 // Verify that there isn't already something declared with this name in this
2118 // scope.
Steve Naroffb327ce02008-04-02 14:35:35 +00002119 if (Decl *PrevDecl = LookupDecl(Id, Decl::IDNS_Ordinary, S)) {
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +00002120 if (IdResolver.isDeclInScope(PrevDecl, CurContext, S)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002121 if (isa<EnumConstantDecl>(PrevDecl))
2122 Diag(IdLoc, diag::err_redefinition_of_enumerator, Id->getName());
2123 else
2124 Diag(IdLoc, diag::err_redefinition, Id->getName());
2125 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
Chris Lattnera73349d2008-02-26 00:33:57 +00002126 delete Val;
Reid Spencer5f016e22007-07-11 17:01:13 +00002127 return 0;
2128 }
2129 }
2130
2131 llvm::APSInt EnumVal(32);
2132 QualType EltTy;
2133 if (Val) {
Chris Lattner421a23d2007-08-27 21:16:18 +00002134 // Make sure to promote the operand type to int.
2135 UsualUnaryConversions(Val);
2136
Reid Spencer5f016e22007-07-11 17:01:13 +00002137 // C99 6.7.2.2p2: Make sure we have an integer constant expression.
2138 SourceLocation ExpLoc;
Chris Lattner590b6642007-07-15 23:26:56 +00002139 if (!Val->isIntegerConstantExpr(EnumVal, Context, &ExpLoc)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002140 Diag(ExpLoc, diag::err_enum_value_not_integer_constant_expr,
2141 Id->getName());
Chris Lattnera73349d2008-02-26 00:33:57 +00002142 delete Val;
Chris Lattnerb7416f92007-08-27 17:37:24 +00002143 Val = 0; // Just forget about it.
Chris Lattnere9ca8512007-08-29 16:03:41 +00002144 } else {
2145 EltTy = Val->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +00002146 }
Chris Lattnerb7416f92007-08-27 17:37:24 +00002147 }
2148
2149 if (!Val) {
2150 if (LastEnumConst) {
2151 // Assign the last value + 1.
2152 EnumVal = LastEnumConst->getInitVal();
2153 ++EnumVal;
Chris Lattner421a23d2007-08-27 21:16:18 +00002154
2155 // Check for overflow on increment.
2156 if (EnumVal < LastEnumConst->getInitVal())
2157 Diag(IdLoc, diag::warn_enum_value_overflow);
2158
Chris Lattnerb7416f92007-08-27 17:37:24 +00002159 EltTy = LastEnumConst->getType();
2160 } else {
2161 // First value, set to zero.
2162 EltTy = Context.IntTy;
Chris Lattner98be4942008-03-05 18:54:05 +00002163 EnumVal.zextOrTrunc(static_cast<uint32_t>(Context.getTypeSize(EltTy)));
Chris Lattnerb7416f92007-08-27 17:37:24 +00002164 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002165 }
2166
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00002167 EnumConstantDecl *New =
Chris Lattner0ed844b2008-04-04 06:12:32 +00002168 EnumConstantDecl::Create(Context, TheEnumDecl, IdLoc, Id, EltTy,
2169 Val, EnumVal,
Chris Lattnerc63e6602008-03-15 21:32:50 +00002170 LastEnumConst);
Reid Spencer5f016e22007-07-11 17:01:13 +00002171
2172 // Register this decl in the current scope stack.
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +00002173 PushOnScopeChains(New, S);
Reid Spencer5f016e22007-07-11 17:01:13 +00002174 return New;
2175}
2176
Steve Naroff08d92e42007-09-15 18:49:24 +00002177void Sema::ActOnEnumBody(SourceLocation EnumLoc, DeclTy *EnumDeclX,
Reid Spencer5f016e22007-07-11 17:01:13 +00002178 DeclTy **Elements, unsigned NumElements) {
2179 EnumDecl *Enum = cast<EnumDecl>(static_cast<Decl*>(EnumDeclX));
2180 assert(!Enum->isDefinition() && "Enum redefinitions can't reach here");
2181
Chris Lattnere37f0be2007-08-28 05:10:31 +00002182 // TODO: If the result value doesn't fit in an int, it must be a long or long
2183 // long value. ISO C does not support this, but GCC does as an extension,
2184 // emit a warning.
Chris Lattner98be4942008-03-05 18:54:05 +00002185 unsigned IntWidth = Context.Target.getIntWidth();
Chris Lattnere37f0be2007-08-28 05:10:31 +00002186
Chris Lattnerac609682007-08-28 06:15:15 +00002187 // Verify that all the values are okay, compute the size of the values, and
2188 // reverse the list.
2189 unsigned NumNegativeBits = 0;
2190 unsigned NumPositiveBits = 0;
2191
2192 // Keep track of whether all elements have type int.
2193 bool AllElementsInt = true;
2194
Reid Spencer5f016e22007-07-11 17:01:13 +00002195 EnumConstantDecl *EltList = 0;
2196 for (unsigned i = 0; i != NumElements; ++i) {
2197 EnumConstantDecl *ECD =
2198 cast_or_null<EnumConstantDecl>(static_cast<Decl*>(Elements[i]));
2199 if (!ECD) continue; // Already issued a diagnostic.
Chris Lattner211a30e2007-08-28 05:27:00 +00002200
2201 // If the enum value doesn't fit in an int, emit an extension warning.
Chris Lattner211a30e2007-08-28 05:27:00 +00002202 const llvm::APSInt &InitVal = ECD->getInitVal();
Chris Lattnera73349d2008-02-26 00:33:57 +00002203 assert(InitVal.getBitWidth() >= IntWidth &&
2204 "Should have promoted value to int");
Chris Lattner211a30e2007-08-28 05:27:00 +00002205 if (InitVal.getBitWidth() > IntWidth) {
2206 llvm::APSInt V(InitVal);
2207 V.trunc(IntWidth);
2208 V.extend(InitVal.getBitWidth());
2209 if (V != InitVal)
2210 Diag(ECD->getLocation(), diag::ext_enum_value_not_int,
2211 InitVal.toString());
2212 }
Chris Lattnerac609682007-08-28 06:15:15 +00002213
2214 // Keep track of the size of positive and negative values.
Chris Lattnera73349d2008-02-26 00:33:57 +00002215 if (InitVal.isUnsigned() || InitVal.isNonNegative())
Chris Lattner21dd8212008-01-14 21:47:29 +00002216 NumPositiveBits = std::max(NumPositiveBits,
2217 (unsigned)InitVal.getActiveBits());
Chris Lattnerac609682007-08-28 06:15:15 +00002218 else
Chris Lattner21dd8212008-01-14 21:47:29 +00002219 NumNegativeBits = std::max(NumNegativeBits,
2220 (unsigned)InitVal.getMinSignedBits());
Reid Spencer5f016e22007-07-11 17:01:13 +00002221
Chris Lattnerac609682007-08-28 06:15:15 +00002222 // Keep track of whether every enum element has type int (very commmon).
2223 if (AllElementsInt)
2224 AllElementsInt = ECD->getType() == Context.IntTy;
2225
Reid Spencer5f016e22007-07-11 17:01:13 +00002226 ECD->setNextDeclarator(EltList);
2227 EltList = ECD;
2228 }
2229
Chris Lattnerac609682007-08-28 06:15:15 +00002230 // Figure out the type that should be used for this enum.
2231 // FIXME: Support attribute(packed) on enums and -fshort-enums.
2232 QualType BestType;
Chris Lattnerb7f6e082007-08-29 17:31:48 +00002233 unsigned BestWidth;
Chris Lattnerac609682007-08-28 06:15:15 +00002234
2235 if (NumNegativeBits) {
2236 // If there is a negative value, figure out the smallest integer type (of
2237 // int/long/longlong) that fits.
Chris Lattnerb7f6e082007-08-29 17:31:48 +00002238 if (NumNegativeBits <= IntWidth && NumPositiveBits < IntWidth) {
Chris Lattnerac609682007-08-28 06:15:15 +00002239 BestType = Context.IntTy;
Chris Lattnerb7f6e082007-08-29 17:31:48 +00002240 BestWidth = IntWidth;
2241 } else {
Chris Lattner98be4942008-03-05 18:54:05 +00002242 BestWidth = Context.Target.getLongWidth();
Ted Kremenek9c728dc2007-12-12 22:39:36 +00002243
Chris Lattnerb7f6e082007-08-29 17:31:48 +00002244 if (NumNegativeBits <= BestWidth && NumPositiveBits < BestWidth)
Chris Lattnerac609682007-08-28 06:15:15 +00002245 BestType = Context.LongTy;
2246 else {
Chris Lattner98be4942008-03-05 18:54:05 +00002247 BestWidth = Context.Target.getLongLongWidth();
Ted Kremenek9c728dc2007-12-12 22:39:36 +00002248
Chris Lattnerb7f6e082007-08-29 17:31:48 +00002249 if (NumNegativeBits > BestWidth || NumPositiveBits >= BestWidth)
Chris Lattnerac609682007-08-28 06:15:15 +00002250 Diag(Enum->getLocation(), diag::warn_enum_too_large);
2251 BestType = Context.LongLongTy;
2252 }
2253 }
2254 } else {
2255 // If there is no negative value, figure out which of uint, ulong, ulonglong
2256 // fits.
Chris Lattnerb7f6e082007-08-29 17:31:48 +00002257 if (NumPositiveBits <= IntWidth) {
Chris Lattnerac609682007-08-28 06:15:15 +00002258 BestType = Context.UnsignedIntTy;
Chris Lattnerb7f6e082007-08-29 17:31:48 +00002259 BestWidth = IntWidth;
2260 } else if (NumPositiveBits <=
Chris Lattner98be4942008-03-05 18:54:05 +00002261 (BestWidth = Context.Target.getLongWidth())) {
Chris Lattnerac609682007-08-28 06:15:15 +00002262 BestType = Context.UnsignedLongTy;
Chris Lattner98be4942008-03-05 18:54:05 +00002263 } else {
2264 BestWidth = Context.Target.getLongLongWidth();
Chris Lattnerb7f6e082007-08-29 17:31:48 +00002265 assert(NumPositiveBits <= BestWidth &&
Chris Lattnerac609682007-08-28 06:15:15 +00002266 "How could an initializer get larger than ULL?");
2267 BestType = Context.UnsignedLongLongTy;
2268 }
2269 }
2270
Chris Lattnerb7f6e082007-08-29 17:31:48 +00002271 // Loop over all of the enumerator constants, changing their types to match
2272 // the type of the enum if needed.
2273 for (unsigned i = 0; i != NumElements; ++i) {
2274 EnumConstantDecl *ECD =
2275 cast_or_null<EnumConstantDecl>(static_cast<Decl*>(Elements[i]));
2276 if (!ECD) continue; // Already issued a diagnostic.
2277
2278 // Standard C says the enumerators have int type, but we allow, as an
2279 // extension, the enumerators to be larger than int size. If each
2280 // enumerator value fits in an int, type it as an int, otherwise type it the
2281 // same as the enumerator decl itself. This means that in "enum { X = 1U }"
2282 // that X has type 'int', not 'unsigned'.
Chris Lattnera73349d2008-02-26 00:33:57 +00002283 if (ECD->getType() == Context.IntTy) {
2284 // Make sure the init value is signed.
2285 llvm::APSInt IV = ECD->getInitVal();
2286 IV.setIsSigned(true);
2287 ECD->setInitVal(IV);
Chris Lattnerb7f6e082007-08-29 17:31:48 +00002288 continue; // Already int type.
Chris Lattnera73349d2008-02-26 00:33:57 +00002289 }
Chris Lattnerb7f6e082007-08-29 17:31:48 +00002290
2291 // Determine whether the value fits into an int.
2292 llvm::APSInt InitVal = ECD->getInitVal();
2293 bool FitsInInt;
2294 if (InitVal.isUnsigned() || !InitVal.isNegative())
2295 FitsInInt = InitVal.getActiveBits() < IntWidth;
2296 else
2297 FitsInInt = InitVal.getMinSignedBits() <= IntWidth;
2298
2299 // If it fits into an integer type, force it. Otherwise force it to match
2300 // the enum decl type.
2301 QualType NewTy;
2302 unsigned NewWidth;
2303 bool NewSign;
2304 if (FitsInInt) {
2305 NewTy = Context.IntTy;
2306 NewWidth = IntWidth;
2307 NewSign = true;
2308 } else if (ECD->getType() == BestType) {
2309 // Already the right type!
2310 continue;
2311 } else {
2312 NewTy = BestType;
2313 NewWidth = BestWidth;
2314 NewSign = BestType->isSignedIntegerType();
2315 }
2316
2317 // Adjust the APSInt value.
2318 InitVal.extOrTrunc(NewWidth);
2319 InitVal.setIsSigned(NewSign);
2320 ECD->setInitVal(InitVal);
2321
2322 // Adjust the Expr initializer and type.
2323 ECD->setInitExpr(new ImplicitCastExpr(NewTy, ECD->getInitExpr()));
2324 ECD->setType(NewTy);
2325 }
Chris Lattnerac609682007-08-28 06:15:15 +00002326
Chris Lattnere00b18c2007-08-28 18:24:31 +00002327 Enum->defineElements(EltList, BestType);
Chris Lattnere1e79852008-02-06 00:51:33 +00002328 Consumer.HandleTagDeclDefinition(Enum);
Reid Spencer5f016e22007-07-11 17:01:13 +00002329}
2330
Anders Carlssondfab6cb2008-02-08 00:33:21 +00002331Sema::DeclTy *Sema::ActOnFileScopeAsmDecl(SourceLocation Loc,
2332 ExprTy *expr) {
2333 StringLiteral *AsmString = cast<StringLiteral>((Expr*)expr);
2334
Chris Lattner8e25d862008-03-16 00:16:02 +00002335 return FileScopeAsmDecl::Create(Context, Loc, AsmString);
Anders Carlssondfab6cb2008-02-08 00:33:21 +00002336}
2337
Chris Lattnerc6fdc342008-01-12 07:05:38 +00002338Sema::DeclTy* Sema::ActOnLinkageSpec(SourceLocation Loc,
Chris Lattnerc81c8142008-02-25 21:04:36 +00002339 SourceLocation LBrace,
2340 SourceLocation RBrace,
2341 const char *Lang,
2342 unsigned StrSize,
2343 DeclTy *D) {
Chris Lattnerc6fdc342008-01-12 07:05:38 +00002344 LinkageSpecDecl::LanguageIDs Language;
2345 Decl *dcl = static_cast<Decl *>(D);
2346 if (strncmp(Lang, "\"C\"", StrSize) == 0)
2347 Language = LinkageSpecDecl::lang_c;
2348 else if (strncmp(Lang, "\"C++\"", StrSize) == 0)
2349 Language = LinkageSpecDecl::lang_cxx;
2350 else {
2351 Diag(Loc, diag::err_bad_language);
2352 return 0;
2353 }
2354
2355 // FIXME: Add all the various semantics of linkage specifications
Chris Lattner8e25d862008-03-16 00:16:02 +00002356 return LinkageSpecDecl::Create(Context, Loc, Language, dcl);
Chris Lattnerc6fdc342008-01-12 07:05:38 +00002357}