blob: f4d9da60147a749b9f037260e83a506fd6804fad [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 ||
Chris Lattnerf8396b62008-07-09 17:26:36 +0000208 BID == Builtin::BI__builtin_va_end ||
209 BID == Builtin::BI__builtin_stdarg_start)
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000210 InitBuiltinVaListType();
211
Anders Carlssonb2cf3572007-10-11 01:00:40 +0000212 QualType R = Context.BuiltinInfo.GetBuiltinType(BID, Context);
Argyrios Kyrtzidisff898cd2008-04-17 14:47:13 +0000213 FunctionDecl *New = FunctionDecl::Create(Context,
214 Context.getTranslationUnitDecl(),
Chris Lattner0ed844b2008-04-04 06:12:32 +0000215 SourceLocation(), II, R,
Chris Lattnera98e58d2008-03-15 21:24:04 +0000216 FunctionDecl::Extern, false, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000217
Chris Lattner95e2c712008-05-05 22:18:14 +0000218 // Create Decl objects for each parameter, adding them to the
219 // FunctionDecl.
220 if (FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(R)) {
221 llvm::SmallVector<ParmVarDecl*, 16> Params;
222 for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i)
223 Params.push_back(ParmVarDecl::Create(Context, New, SourceLocation(), 0,
224 FT->getArgType(i), VarDecl::None, 0,
225 0));
226 New->setParams(&Params[0], Params.size());
227 }
228
229
230
Chris Lattner7f925cc2008-04-11 07:00:53 +0000231 // TUScope is the translation-unit scope to insert this function into.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000232 PushOnScopeChains(New, TUScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000233 return New;
234}
235
236/// MergeTypeDefDecl - We just parsed a typedef 'New' which has the same name
237/// and scope as a previous declaration 'Old'. Figure out how to resolve this
238/// situation, merging decls or emitting diagnostics as appropriate.
239///
Steve Naroffe8043c32008-04-01 23:04:06 +0000240TypedefDecl *Sema::MergeTypeDefDecl(TypedefDecl *New, Decl *OldD) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000241 // Verify the old decl was also a typedef.
242 TypedefDecl *Old = dyn_cast<TypedefDecl>(OldD);
243 if (!Old) {
244 Diag(New->getLocation(), diag::err_redefinition_different_kind,
245 New->getName());
246 Diag(OldD->getLocation(), diag::err_previous_definition);
247 return New;
248 }
249
Steve Naroff8ee529b2007-10-31 18:42:27 +0000250 // Allow multiple definitions for ObjC built-in typedefs.
251 // FIXME: Verify the underlying types are equivalent!
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000252 if (getLangOptions().ObjC1 && isBuiltinObjCType(New))
Steve Naroff8ee529b2007-10-31 18:42:27 +0000253 return Old;
Eli Friedman54ecfce2008-06-11 06:20:39 +0000254
255 if (getLangOptions().Microsoft) return New;
256
Steve Naroff4c49a6c2008-01-30 23:46:05 +0000257 // Redeclaration of a type is a constraint violation (6.7.2.3p1).
258 // Apparently GCC, Intel, and Sun all silently ignore the redeclaration if
259 // *either* declaration is in a system header. The code below implements
260 // this adhoc compatibility rule. FIXME: The following code will not
261 // work properly when compiling ".i" files (containing preprocessed output).
262 SourceManager &SrcMgr = Context.getSourceManager();
Steve Naroff4c49a6c2008-01-30 23:46:05 +0000263 HeaderSearch &HdrInfo = PP.getHeaderSearchInfo();
Eli Friedman54ecfce2008-06-11 06:20:39 +0000264 const FileEntry *OldDeclFile = SrcMgr.getFileEntryForLoc(Old->getLocation());
265 if (OldDeclFile) {
266 DirectoryLookup::DirType OldDirType = HdrInfo.getFileDirFlavor(OldDeclFile);
267 // Allow reclarations in both SystemHeaderDir and ExternCSystemHeaderDir.
268 if (OldDirType != DirectoryLookup::NormalHeaderDir)
269 return New;
270 }
271 const FileEntry *NewDeclFile = SrcMgr.getFileEntryForLoc(New->getLocation());
272 if (NewDeclFile) {
273 DirectoryLookup::DirType NewDirType = HdrInfo.getFileDirFlavor(NewDeclFile);
274 // Allow reclarations in both SystemHeaderDir and ExternCSystemHeaderDir.
275 if (NewDirType != DirectoryLookup::NormalHeaderDir)
276 return New;
277 }
278
Ted Kremenek2d05c082008-05-23 21:28:18 +0000279 Diag(New->getLocation(), diag::err_redefinition, New->getName());
280 Diag(Old->getLocation(), diag::err_previous_definition);
Reid Spencer5f016e22007-07-11 17:01:13 +0000281 return New;
282}
283
Chris Lattner6b6b5372008-06-26 18:38:35 +0000284/// DeclhasAttr - returns true if decl Declaration already has the target
285/// attribute.
Chris Lattnerddee4232008-03-03 03:28:21 +0000286static bool DeclHasAttr(const Decl *decl, const Attr *target) {
287 for (const Attr *attr = decl->getAttrs(); attr; attr = attr->getNext())
288 if (attr->getKind() == target->getKind())
289 return true;
290
291 return false;
292}
293
294/// MergeAttributes - append attributes from the Old decl to the New one.
295static void MergeAttributes(Decl *New, Decl *Old) {
296 Attr *attr = const_cast<Attr*>(Old->getAttrs()), *tmp;
297
Chris Lattnerddee4232008-03-03 03:28:21 +0000298 while (attr) {
299 tmp = attr;
300 attr = attr->getNext();
301
302 if (!DeclHasAttr(New, tmp)) {
303 New->addAttr(tmp);
304 } else {
305 tmp->setNext(0);
306 delete(tmp);
307 }
308 }
Nuno Lopes9141bee2008-06-01 22:53:53 +0000309
310 Old->invalidateAttrs();
Chris Lattnerddee4232008-03-03 03:28:21 +0000311}
312
Chris Lattner04421082008-04-08 04:40:51 +0000313/// MergeFunctionDecl - We just parsed a function 'New' from
314/// declarator D which has the same name and scope as a previous
315/// declaration 'Old'. Figure out how to resolve this situation,
316/// merging decls or emitting diagnostics as appropriate.
Douglas Gregorf0097952008-04-21 02:02:58 +0000317/// Redeclaration will be set true if thisNew is a redeclaration OldD.
318FunctionDecl *
319Sema::MergeFunctionDecl(FunctionDecl *New, Decl *OldD, bool &Redeclaration) {
320 Redeclaration = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000321 // Verify the old decl was also a function.
322 FunctionDecl *Old = dyn_cast<FunctionDecl>(OldD);
323 if (!Old) {
324 Diag(New->getLocation(), diag::err_redefinition_different_kind,
325 New->getName());
326 Diag(OldD->getLocation(), diag::err_previous_definition);
327 return New;
328 }
Chris Lattner04421082008-04-08 04:40:51 +0000329
Chris Lattner8bcfc5b2008-04-06 23:10:54 +0000330 QualType OldQType = Context.getCanonicalType(Old->getType());
331 QualType NewQType = Context.getCanonicalType(New->getType());
Chris Lattner55196442007-11-20 19:04:50 +0000332
Chris Lattner04421082008-04-08 04:40:51 +0000333 // C++ [dcl.fct]p3:
334 // All declarations for a function shall agree exactly in both the
335 // return type and the parameter-type-list.
Douglas Gregorf0097952008-04-21 02:02:58 +0000336 if (getLangOptions().CPlusPlus && OldQType == NewQType) {
337 MergeAttributes(New, Old);
338 Redeclaration = true;
Chris Lattner04421082008-04-08 04:40:51 +0000339 return MergeCXXFunctionDecl(New, Old);
Douglas Gregorf0097952008-04-21 02:02:58 +0000340 }
Chris Lattner04421082008-04-08 04:40:51 +0000341
342 // C: Function types need to be compatible, not identical. This handles
Steve Naroffadbbd0c2008-01-14 20:51:29 +0000343 // duplicate function decls like "void f(int); void f(enum X);" properly.
Chris Lattner04421082008-04-08 04:40:51 +0000344 if (!getLangOptions().CPlusPlus &&
345 Context.functionTypesAreCompatible(OldQType, NewQType)) {
Douglas Gregorf0097952008-04-21 02:02:58 +0000346 MergeAttributes(New, Old);
347 Redeclaration = true;
Steve Naroffadbbd0c2008-01-14 20:51:29 +0000348 return New;
Chris Lattner04421082008-04-08 04:40:51 +0000349 }
Chris Lattnere3995fe2007-11-06 06:07:26 +0000350
Steve Naroff837618c2008-01-16 15:01:34 +0000351 // A function that has already been declared has been redeclared or defined
352 // with a different type- show appropriate diagnostic
Steve Naroffe2ef8152008-04-04 14:32:09 +0000353 diag::kind PrevDiag;
Douglas Gregorf0097952008-04-21 02:02:58 +0000354 if (Old->isThisDeclarationADefinition())
Steve Naroffe2ef8152008-04-04 14:32:09 +0000355 PrevDiag = diag::err_previous_definition;
356 else if (Old->isImplicit())
357 PrevDiag = diag::err_previous_implicit_declaration;
Chris Lattner04421082008-04-08 04:40:51 +0000358 else
Steve Naroffe2ef8152008-04-04 14:32:09 +0000359 PrevDiag = diag::err_previous_declaration;
Steve Naroff837618c2008-01-16 15:01:34 +0000360
Reid Spencer5f016e22007-07-11 17:01:13 +0000361 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
362 // TODO: This is totally simplistic. It should handle merging functions
363 // together etc, merging extern int X; int X; ...
Steve Naroff837618c2008-01-16 15:01:34 +0000364 Diag(New->getLocation(), diag::err_conflicting_types, New->getName());
365 Diag(Old->getLocation(), PrevDiag);
Reid Spencer5f016e22007-07-11 17:01:13 +0000366 return New;
367}
368
Chris Lattnerfcc2d262007-11-06 04:28:31 +0000369/// equivalentArrayTypes - Used to determine whether two array types are
370/// equivalent.
371/// We need to check this explicitly as an incomplete array definition is
372/// considered a VariableArrayType, so will not match a complete array
373/// definition that would be otherwise equivalent.
374static bool areEquivalentArrayTypes(QualType NewQType, QualType OldQType) {
375 const ArrayType *NewAT = NewQType->getAsArrayType();
376 const ArrayType *OldAT = OldQType->getAsArrayType();
377
378 if (!NewAT || !OldAT)
379 return false;
380
381 // If either (or both) array types in incomplete we need to strip off the
382 // outer VariableArrayType. Once the outer VAT is removed the remaining
383 // types must be identical if the array types are to be considered
384 // equivalent.
385 // eg. int[][1] and int[1][1] become
386 // VAT(null, CAT(1, int)) and CAT(1, CAT(1, int))
387 // removing the outermost VAT gives
388 // CAT(1, int) and CAT(1, int)
389 // which are equal, therefore the array types are equivalent.
Eli Friedman9db13972008-02-15 12:53:51 +0000390 if (NewAT->isIncompleteArrayType() || OldAT->isIncompleteArrayType()) {
Chris Lattnerfcc2d262007-11-06 04:28:31 +0000391 if (NewAT->getIndexTypeQualifier() != OldAT->getIndexTypeQualifier())
392 return false;
Eli Friedman04930252008-01-29 07:51:12 +0000393 NewQType = NewAT->getElementType().getCanonicalType();
394 OldQType = OldAT->getElementType().getCanonicalType();
Chris Lattnerfcc2d262007-11-06 04:28:31 +0000395 }
396
397 return NewQType == OldQType;
398}
399
Reid Spencer5f016e22007-07-11 17:01:13 +0000400/// MergeVarDecl - We just parsed a variable 'New' which has the same name
401/// and scope as a previous declaration 'Old'. Figure out how to resolve this
402/// situation, merging decls or emitting diagnostics as appropriate.
403///
404/// FIXME: Need to carefully consider tentative definition rules (C99 6.9.2p2).
405/// For example, we incorrectly complain about i1, i4 from C99 6.9.2p4.
406///
Steve Naroffe8043c32008-04-01 23:04:06 +0000407VarDecl *Sema::MergeVarDecl(VarDecl *New, Decl *OldD) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000408 // Verify the old decl was also a variable.
409 VarDecl *Old = dyn_cast<VarDecl>(OldD);
410 if (!Old) {
411 Diag(New->getLocation(), diag::err_redefinition_different_kind,
412 New->getName());
413 Diag(OldD->getLocation(), diag::err_previous_definition);
414 return New;
415 }
Chris Lattnerddee4232008-03-03 03:28:21 +0000416
417 MergeAttributes(New, Old);
418
Reid Spencer5f016e22007-07-11 17:01:13 +0000419 // Verify the types match.
Chris Lattner8bcfc5b2008-04-06 23:10:54 +0000420 QualType OldCType = Context.getCanonicalType(Old->getType());
421 QualType NewCType = Context.getCanonicalType(New->getType());
422 if (OldCType != NewCType && !areEquivalentArrayTypes(NewCType, OldCType)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000423 Diag(New->getLocation(), diag::err_redefinition, New->getName());
424 Diag(Old->getLocation(), diag::err_previous_definition);
425 return New;
426 }
Steve Naroffb7b032e2008-01-30 00:44:01 +0000427 // C99 6.2.2p4: Check if we have a static decl followed by a non-static.
428 if (New->getStorageClass() == VarDecl::Static &&
429 (Old->getStorageClass() == VarDecl::None ||
430 Old->getStorageClass() == VarDecl::Extern)) {
431 Diag(New->getLocation(), diag::err_static_non_static, New->getName());
432 Diag(Old->getLocation(), diag::err_previous_definition);
433 return New;
434 }
435 // C99 6.2.2p4: Check if we have a non-static decl followed by a static.
436 if (New->getStorageClass() != VarDecl::Static &&
437 Old->getStorageClass() == VarDecl::Static) {
438 Diag(New->getLocation(), diag::err_non_static_static, New->getName());
439 Diag(Old->getLocation(), diag::err_previous_definition);
440 return New;
441 }
442 // We've verified the types match, now handle "tentative" definitions.
Steve Naroff248a7532008-04-15 22:42:06 +0000443 if (Old->isFileVarDecl() && New->isFileVarDecl()) {
Steve Naroffb7b032e2008-01-30 00:44:01 +0000444 // Handle C "tentative" external object definitions (C99 6.9.2).
445 bool OldIsTentative = false;
446 bool NewIsTentative = false;
447
Steve Naroff248a7532008-04-15 22:42:06 +0000448 if (!Old->getInit() &&
449 (Old->getStorageClass() == VarDecl::None ||
450 Old->getStorageClass() == VarDecl::Static))
Steve Naroffb7b032e2008-01-30 00:44:01 +0000451 OldIsTentative = true;
452
453 // FIXME: this check doesn't work (since the initializer hasn't been
454 // attached yet). This check should be moved to FinalizeDeclaratorGroup.
455 // Unfortunately, by the time we get to FinializeDeclaratorGroup, we've
456 // thrown out the old decl.
Steve Naroff248a7532008-04-15 22:42:06 +0000457 if (!New->getInit() &&
458 (New->getStorageClass() == VarDecl::None ||
459 New->getStorageClass() == VarDecl::Static))
Steve Naroffb7b032e2008-01-30 00:44:01 +0000460 ; // change to NewIsTentative = true; once the code is moved.
461
462 if (NewIsTentative || OldIsTentative)
463 return New;
464 }
Steve Naroff235549c2008-05-12 22:36:43 +0000465 // Handle __private_extern__ just like extern.
Steve Naroffb7b032e2008-01-30 00:44:01 +0000466 if (Old->getStorageClass() != VarDecl::Extern &&
Steve Naroff235549c2008-05-12 22:36:43 +0000467 Old->getStorageClass() != VarDecl::PrivateExtern &&
468 New->getStorageClass() != VarDecl::Extern &&
469 New->getStorageClass() != VarDecl::PrivateExtern) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000470 Diag(New->getLocation(), diag::err_redefinition, New->getName());
471 Diag(Old->getLocation(), diag::err_previous_definition);
472 }
473 return New;
474}
475
Chris Lattner04421082008-04-08 04:40:51 +0000476/// CheckParmsForFunctionDef - Check that the parameters of the given
477/// function are appropriate for the definition of a function. This
478/// takes care of any checks that cannot be performed on the
479/// declaration itself, e.g., that the types of each of the function
480/// parameters are complete.
481bool Sema::CheckParmsForFunctionDef(FunctionDecl *FD) {
482 bool HasInvalidParm = false;
483 for (unsigned p = 0, NumParams = FD->getNumParams(); p < NumParams; ++p) {
484 ParmVarDecl *Param = FD->getParamDecl(p);
485
486 // C99 6.7.5.3p4: the parameters in a parameter type list in a
487 // function declarator that is part of a function definition of
488 // that function shall not have incomplete type.
489 if (Param->getType()->isIncompleteType() &&
490 !Param->isInvalidDecl()) {
491 Diag(Param->getLocation(), diag::err_typecheck_decl_incomplete_type,
492 Param->getType().getAsString());
493 Param->setInvalidDecl();
494 HasInvalidParm = true;
495 }
496 }
497
498 return HasInvalidParm;
499}
500
501/// CreateImplicitParameter - Creates an implicit function parameter
502/// in the scope S and with the given type. This routine is used, for
503/// example, to create the implicit "self" parameter in an Objective-C
504/// method.
Chris Lattner41110242008-06-17 18:05:57 +0000505ImplicitParamDecl *
Chris Lattner04421082008-04-08 04:40:51 +0000506Sema::CreateImplicitParameter(Scope *S, IdentifierInfo *Id,
507 SourceLocation IdLoc, QualType Type) {
Chris Lattner41110242008-06-17 18:05:57 +0000508 ImplicitParamDecl *New = ImplicitParamDecl::Create(Context, CurContext,
509 IdLoc, Id, Type, 0);
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +0000510 if (Id)
511 PushOnScopeChains(New, S);
Chris Lattner04421082008-04-08 04:40:51 +0000512
513 return New;
514}
515
Reid Spencer5f016e22007-07-11 17:01:13 +0000516/// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with
517/// no declarator (e.g. "struct foo;") is parsed.
518Sema::DeclTy *Sema::ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS) {
519 // TODO: emit error on 'int;' or 'const enum foo;'.
520 // TODO: emit error on 'typedef int;'
521 // if (!DS.isMissingDeclaratorOk()) Diag(...);
522
Steve Naroff92199282007-11-17 21:37:36 +0000523 return dyn_cast_or_null<TagDecl>(static_cast<Decl *>(DS.getTypeRep()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000524}
525
Steve Naroffd0091aa2008-01-10 22:15:12 +0000526bool Sema::CheckSingleInitializer(Expr *&Init, QualType DeclType) {
Steve Narofff0090632007-09-02 02:04:30 +0000527 // Get the type before calling CheckSingleAssignmentConstraints(), since
528 // it can promote the expression.
Chris Lattner5cf216b2008-01-04 18:04:52 +0000529 QualType InitType = Init->getType();
Steve Narofff0090632007-09-02 02:04:30 +0000530
Chris Lattner5cf216b2008-01-04 18:04:52 +0000531 AssignConvertType ConvTy = CheckSingleAssignmentConstraints(DeclType, Init);
532 return DiagnoseAssignmentResult(ConvTy, Init->getLocStart(), DeclType,
533 InitType, Init, "initializing");
Steve Narofff0090632007-09-02 02:04:30 +0000534}
535
Steve Naroffa49e1fa2008-01-22 00:55:40 +0000536bool Sema::CheckStringLiteralInit(StringLiteral *strLiteral, QualType &DeclT) {
Eli Friedmanc5773c42008-02-15 18:16:39 +0000537 if (const IncompleteArrayType *IAT = DeclT->getAsIncompleteArrayType()) {
Steve Naroffa49e1fa2008-01-22 00:55:40 +0000538 // C99 6.7.8p14. We have an array of character type with unknown size
539 // being initialized to a string literal.
540 llvm::APSInt ConstVal(32);
541 ConstVal = strLiteral->getByteLength() + 1;
542 // Return a new array type (C99 6.7.8p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000543 DeclT = Context.getConstantArrayType(IAT->getElementType(), ConstVal,
Steve Naroffa49e1fa2008-01-22 00:55:40 +0000544 ArrayType::Normal, 0);
545 } else if (const ConstantArrayType *CAT = DeclT->getAsConstantArrayType()) {
546 // C99 6.7.8p14. We have an array of character type with known size.
547 if (strLiteral->getByteLength() > (unsigned)CAT->getMaximumElements())
548 Diag(strLiteral->getSourceRange().getBegin(),
549 diag::warn_initializer_string_for_char_array_too_long,
550 strLiteral->getSourceRange());
551 } else {
552 assert(0 && "HandleStringLiteralInit(): Invalid array type");
553 }
554 // Set type from "char *" to "constant array of char".
555 strLiteral->setType(DeclT);
556 // For now, we always return false (meaning success).
557 return false;
558}
559
560StringLiteral *Sema::IsStringLiteralInit(Expr *Init, QualType DeclType) {
Steve Naroffa49e1fa2008-01-22 00:55:40 +0000561 const ArrayType *AT = DeclType->getAsArrayType();
Steve Naroffa9960332008-01-25 00:51:06 +0000562 if (AT && AT->getElementType()->isCharType()) {
563 return dyn_cast<StringLiteral>(Init);
564 }
Steve Naroffa49e1fa2008-01-22 00:55:40 +0000565 return 0;
566}
567
Steve Naroffa9960332008-01-25 00:51:06 +0000568bool Sema::CheckInitializerTypes(Expr *&Init, QualType &DeclType) {
Steve Naroffca107302008-01-21 23:53:58 +0000569 // C99 6.7.8p3: The type of the entity to be initialized shall be an array
570 // of unknown size ("[]") or an object type that is not a variable array type.
Eli Friedmanc5773c42008-02-15 18:16:39 +0000571 if (const VariableArrayType *VAT = DeclType->getAsVariableArrayType())
Steve Naroffca107302008-01-21 23:53:58 +0000572 return Diag(VAT->getSizeExpr()->getLocStart(),
573 diag::err_variable_object_no_init,
574 VAT->getSizeExpr()->getSourceRange());
575
Steve Naroff2fdc3742007-12-10 22:44:33 +0000576 InitListExpr *InitList = dyn_cast<InitListExpr>(Init);
577 if (!InitList) {
Steve Naroffa49e1fa2008-01-22 00:55:40 +0000578 // FIXME: Handle wide strings
579 if (StringLiteral *strLiteral = IsStringLiteralInit(Init, DeclType))
580 return CheckStringLiteralInit(strLiteral, DeclType);
Eli Friedmana312ce22008-02-08 00:48:24 +0000581
582 if (DeclType->isArrayType())
583 return Diag(Init->getLocStart(),
584 diag::err_array_init_list_required,
585 Init->getSourceRange());
586
Steve Naroffd0091aa2008-01-10 22:15:12 +0000587 return CheckSingleInitializer(Init, DeclType);
Steve Naroff2fdc3742007-12-10 22:44:33 +0000588 }
Eli Friedmane6f058f2008-06-06 19:40:52 +0000589
Steve Naroff0cca7492008-05-01 22:18:59 +0000590 InitListChecker CheckInitList(this, InitList, DeclType);
591 return CheckInitList.HadError();
Steve Narofff0090632007-09-02 02:04:30 +0000592}
593
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000594Sema::DeclTy *
Steve Naroff08d92e42007-09-15 18:49:24 +0000595Sema::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *lastDecl) {
Steve Naroff94745042007-09-13 23:52:58 +0000596 ScopedDecl *LastDeclarator = dyn_cast_or_null<ScopedDecl>((Decl *)lastDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000597 IdentifierInfo *II = D.getIdentifier();
598
Chris Lattnere80a59c2007-07-25 00:24:17 +0000599 // All of these full declarators require an identifier. If it doesn't have
600 // one, the ParsedFreeStandingDeclSpec action should be used.
601 if (II == 0) {
Chris Lattner311ff022007-10-16 22:36:42 +0000602 Diag(D.getDeclSpec().getSourceRange().getBegin(),
Chris Lattner98e08632007-08-28 06:17:15 +0000603 diag::err_declarator_need_ident,
Chris Lattnere80a59c2007-07-25 00:24:17 +0000604 D.getDeclSpec().getSourceRange(), D.getSourceRange());
605 return 0;
606 }
607
Chris Lattner31e05722007-08-26 06:24:45 +0000608 // The scope passed in may not be a decl scope. Zip up the scope tree until
609 // we find one that is.
610 while ((S->getFlags() & Scope::DeclScope) == 0)
611 S = S->getParent();
612
Reid Spencer5f016e22007-07-11 17:01:13 +0000613 // See if this is a redefinition of a variable in the same scope.
Steve Naroffb327ce02008-04-02 14:35:35 +0000614 Decl *PrevDecl = LookupDecl(II, Decl::IDNS_Ordinary, S);
Steve Naroffc752d042007-09-13 18:10:37 +0000615 ScopedDecl *New;
Steve Naroff5912a352007-08-28 20:14:24 +0000616 bool InvalidDecl = false;
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000617
618 // In C++, the previous declaration we find might be a tag type
619 // (class or enum). In this case, the new declaration will hide the
620 // tag type.
621 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
622 PrevDecl = 0;
623
Chris Lattner41af0932007-11-14 06:34:38 +0000624 QualType R = GetTypeForDeclarator(D, S);
625 assert(!R.isNull() && "GetTypeForDeclarator() returned null type");
626
Reid Spencer5f016e22007-07-11 17:01:13 +0000627 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
Douglas Gregor6d6eb572008-05-07 04:49:29 +0000628 // Check that there are no default arguments (C++ only).
629 if (getLangOptions().CPlusPlus)
630 CheckExtraCXXDefaultArguments(D);
631
Chris Lattner41af0932007-11-14 06:34:38 +0000632 TypedefDecl *NewTD = ParseTypedefDecl(S, D, R, LastDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +0000633 if (!NewTD) return 0;
634
635 // Handle attributes prior to checking for duplicates in MergeVarDecl
Chris Lattner3ff30c82008-06-29 00:02:00 +0000636 ProcessDeclAttributes(NewTD, D);
Steve Naroffffce4d52008-01-09 23:34:55 +0000637 // Merge the decl with the existing one if appropriate. If the decl is
638 // in an outer scope, it isn't the same thing.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000639 if (PrevDecl && IdResolver.isDeclInScope(PrevDecl, CurContext, S)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000640 NewTD = MergeTypeDefDecl(NewTD, PrevDecl);
641 if (NewTD == 0) return 0;
642 }
643 New = NewTD;
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000644 if (S->getFnParent() == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000645 // C99 6.7.7p2: If a typedef name specifies a variably modified type
646 // then it shall have block scope.
Eli Friedman9db13972008-02-15 12:53:51 +0000647 if (NewTD->getUnderlyingType()->isVariablyModifiedType()) {
648 // FIXME: Diagnostic needs to be fixed.
649 Diag(D.getIdentifierLoc(), diag::err_typecheck_illegal_vla);
Steve Naroffd7444aa2007-08-31 17:20:07 +0000650 InvalidDecl = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000651 }
652 }
Chris Lattner41af0932007-11-14 06:34:38 +0000653 } else if (R.getTypePtr()->isFunctionType()) {
Chris Lattner271f1a62007-09-27 15:15:46 +0000654 FunctionDecl::StorageClass SC = FunctionDecl::None;
Reid Spencer5f016e22007-07-11 17:01:13 +0000655 switch (D.getDeclSpec().getStorageClassSpec()) {
656 default: assert(0 && "Unknown storage class!");
657 case DeclSpec::SCS_auto:
658 case DeclSpec::SCS_register:
659 Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_func,
660 R.getAsString());
Steve Naroff5912a352007-08-28 20:14:24 +0000661 InvalidDecl = true;
662 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000663 case DeclSpec::SCS_unspecified: SC = FunctionDecl::None; break;
664 case DeclSpec::SCS_extern: SC = FunctionDecl::Extern; break;
665 case DeclSpec::SCS_static: SC = FunctionDecl::Static; break;
Steve Naroff7dd0bd42008-01-28 21:57:15 +0000666 case DeclSpec::SCS_private_extern: SC = FunctionDecl::PrivateExtern;break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000667 }
668
Chris Lattnera98e58d2008-03-15 21:24:04 +0000669 bool isInline = D.getDeclSpec().isInlineSpecified();
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000670 FunctionDecl *NewFD;
671 if (D.getContext() == Declarator::MemberContext) {
672 // This is a C++ method declaration.
673 NewFD = CXXMethodDecl::Create(Context, cast<CXXRecordDecl>(CurContext),
674 D.getIdentifierLoc(), II, R,
675 (SC == FunctionDecl::Static), isInline,
676 LastDeclarator);
677 } else {
678 NewFD = FunctionDecl::Create(Context, CurContext,
679 D.getIdentifierLoc(),
680 II, R, SC, isInline,
681 LastDeclarator);
682 }
Ted Kremenekf5c93c12008-02-27 22:18:07 +0000683 // Handle attributes.
Chris Lattner3ff30c82008-06-29 00:02:00 +0000684 ProcessDeclAttributes(NewFD, D);
Chris Lattner04421082008-04-08 04:40:51 +0000685
686 // Copy the parameter declarations from the declarator D to
687 // the function declaration NewFD, if they are available.
688 if (D.getNumTypeObjects() > 0 &&
689 D.getTypeObject(0).Fun.hasPrototype) {
690 DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
691
692 // Create Decl objects for each parameter, adding them to the
693 // FunctionDecl.
694 llvm::SmallVector<ParmVarDecl*, 16> Params;
695
696 // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs
697 // function that takes no arguments, not a function that takes a
Chris Lattner8123a952008-04-10 02:22:51 +0000698 // single void argument.
Eli Friedman6d1e4b52008-05-22 08:54:03 +0000699 // We let through "const void" here because Sema::GetTypeForDeclarator
700 // already checks for that case.
Chris Lattner04421082008-04-08 04:40:51 +0000701 if (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 &&
702 FTI.ArgInfo[0].Param &&
Chris Lattner04421082008-04-08 04:40:51 +0000703 ((ParmVarDecl*)FTI.ArgInfo[0].Param)->getType()->isVoidType()) {
704 // empty arg list, don't push any params.
Chris Lattner8123a952008-04-10 02:22:51 +0000705 ParmVarDecl *Param = (ParmVarDecl*)FTI.ArgInfo[0].Param;
706
Chris Lattnerdef026a2008-04-10 02:26:16 +0000707 // In C++, the empty parameter-type-list must be spelled "void"; a
708 // typedef of void is not permitted.
709 if (getLangOptions().CPlusPlus &&
Eli Friedman6d1e4b52008-05-22 08:54:03 +0000710 Param->getType().getUnqualifiedType() != Context.VoidTy) {
Chris Lattner8123a952008-04-10 02:22:51 +0000711 Diag(Param->getLocation(), diag::ext_param_typedef_of_void);
712 }
713
Chris Lattner04421082008-04-08 04:40:51 +0000714 } else {
715 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i)
716 Params.push_back((ParmVarDecl *)FTI.ArgInfo[i].Param);
717 }
718
719 NewFD->setParams(&Params[0], Params.size());
720 }
721
Steve Naroffffce4d52008-01-09 23:34:55 +0000722 // Merge the decl with the existing one if appropriate. Since C functions
723 // are in a flat namespace, make sure we consider decls in outer scopes.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000724 if (PrevDecl &&
725 (!getLangOptions().CPlusPlus ||
726 IdResolver.isDeclInScope(PrevDecl, CurContext, S)) ) {
Douglas Gregorf0097952008-04-21 02:02:58 +0000727 bool Redeclaration = false;
728 NewFD = MergeFunctionDecl(NewFD, PrevDecl, Redeclaration);
Reid Spencer5f016e22007-07-11 17:01:13 +0000729 if (NewFD == 0) return 0;
Douglas Gregorf0097952008-04-21 02:02:58 +0000730 if (Redeclaration) {
Eli Friedman27424962008-05-27 05:07:37 +0000731 NewFD->setPreviousDeclaration(cast<FunctionDecl>(PrevDecl));
Douglas Gregorf0097952008-04-21 02:02:58 +0000732 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000733 }
734 New = NewFD;
Chris Lattner04421082008-04-08 04:40:51 +0000735
736 // In C++, check default arguments now that we have merged decls.
737 if (getLangOptions().CPlusPlus)
738 CheckCXXDefaultArguments(NewFD);
Reid Spencer5f016e22007-07-11 17:01:13 +0000739 } else {
Douglas Gregor6d6eb572008-05-07 04:49:29 +0000740 // Check that there are no default arguments (C++ only).
741 if (getLangOptions().CPlusPlus)
742 CheckExtraCXXDefaultArguments(D);
743
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000744 if (R.getTypePtr()->isObjCInterfaceType()) {
Fariborz Jahaniane7f64cc2007-10-12 22:10:42 +0000745 Diag(D.getIdentifierLoc(), diag::err_statically_allocated_object,
746 D.getIdentifier()->getName());
747 InvalidDecl = true;
748 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000749
750 VarDecl *NewVD;
751 VarDecl::StorageClass SC;
752 switch (D.getDeclSpec().getStorageClassSpec()) {
Chris Lattner9e151e12008-03-15 21:10:16 +0000753 default: assert(0 && "Unknown storage class!");
754 case DeclSpec::SCS_unspecified: SC = VarDecl::None; break;
755 case DeclSpec::SCS_extern: SC = VarDecl::Extern; break;
756 case DeclSpec::SCS_static: SC = VarDecl::Static; break;
757 case DeclSpec::SCS_auto: SC = VarDecl::Auto; break;
758 case DeclSpec::SCS_register: SC = VarDecl::Register; break;
759 case DeclSpec::SCS_private_extern: SC = VarDecl::PrivateExtern; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000760 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000761 if (D.getContext() == Declarator::MemberContext) {
762 assert(SC == VarDecl::Static && "Invalid storage class for member!");
763 // This is a static data member for a C++ class.
764 NewVD = CXXClassVarDecl::Create(Context, cast<CXXRecordDecl>(CurContext),
765 D.getIdentifierLoc(), II,
766 R, LastDeclarator);
Steve Narofff0090632007-09-02 02:04:30 +0000767 } else {
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000768 if (S->getFnParent() == 0) {
769 // C99 6.9p2: The storage-class specifiers auto and register shall not
770 // appear in the declaration specifiers in an external declaration.
771 if (SC == VarDecl::Auto || SC == VarDecl::Register) {
772 Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_fscope,
773 R.getAsString());
774 InvalidDecl = true;
775 }
776 NewVD = VarDecl::Create(Context, CurContext, D.getIdentifierLoc(),
777 II, R, SC, LastDeclarator);
778 } else {
779 NewVD = VarDecl::Create(Context, CurContext, D.getIdentifierLoc(),
780 II, R, SC, LastDeclarator);
781 }
Steve Naroff53a32342007-08-28 18:45:29 +0000782 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000783 // Handle attributes prior to checking for duplicates in MergeVarDecl
Chris Lattner3ff30c82008-06-29 00:02:00 +0000784 ProcessDeclAttributes(NewVD, D);
Nate Begemanc8e89a82008-03-14 18:07:10 +0000785
786 // Emit an error if an address space was applied to decl with local storage.
787 // This includes arrays of objects with address space qualifiers, but not
788 // automatic variables that point to other address spaces.
789 // ISO/IEC TR 18037 S5.1.2
Nate Begeman8e7dafe2008-03-25 18:36:32 +0000790 if (NewVD->hasLocalStorage() && (NewVD->getType().getAddressSpace() != 0)) {
791 Diag(D.getIdentifierLoc(), diag::err_as_qualified_auto_decl);
792 InvalidDecl = true;
Nate Begeman5af27e02008-03-14 00:22:18 +0000793 }
Steve Naroffffce4d52008-01-09 23:34:55 +0000794 // Merge the decl with the existing one if appropriate. If the decl is
795 // in an outer scope, it isn't the same thing.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000796 if (PrevDecl && IdResolver.isDeclInScope(PrevDecl, CurContext, S)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000797 NewVD = MergeVarDecl(NewVD, PrevDecl);
798 if (NewVD == 0) return 0;
799 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000800 New = NewVD;
801 }
802
803 // If this has an identifier, add it to the scope stack.
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +0000804 if (II)
805 PushOnScopeChains(New, S);
Steve Naroff5912a352007-08-28 20:14:24 +0000806 // If any semantic error occurred, mark the decl as invalid.
807 if (D.getInvalidType() || InvalidDecl)
808 New->setInvalidDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +0000809
810 return New;
811}
812
Eli Friedmanc594b322008-05-20 13:48:25 +0000813bool Sema::CheckAddressConstantExpressionLValue(const Expr* Init) {
814 switch (Init->getStmtClass()) {
815 default:
816 Diag(Init->getExprLoc(),
817 diag::err_init_element_not_constant, Init->getSourceRange());
818 return true;
819 case Expr::ParenExprClass: {
820 const ParenExpr* PE = cast<ParenExpr>(Init);
821 return CheckAddressConstantExpressionLValue(PE->getSubExpr());
822 }
823 case Expr::CompoundLiteralExprClass:
824 return cast<CompoundLiteralExpr>(Init)->isFileScope();
825 case Expr::DeclRefExprClass: {
826 const Decl *D = cast<DeclRefExpr>(Init)->getDecl();
Eli Friedman97c0a392008-05-21 03:39:11 +0000827 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
828 if (VD->hasGlobalStorage())
829 return false;
830 Diag(Init->getExprLoc(),
831 diag::err_init_element_not_constant, Init->getSourceRange());
832 return true;
833 }
Eli Friedmanc594b322008-05-20 13:48:25 +0000834 if (isa<FunctionDecl>(D))
835 return false;
836 Diag(Init->getExprLoc(),
837 diag::err_init_element_not_constant, Init->getSourceRange());
Steve Naroffd0091aa2008-01-10 22:15:12 +0000838 return true;
839 }
Eli Friedmanc594b322008-05-20 13:48:25 +0000840 case Expr::MemberExprClass: {
841 const MemberExpr *M = cast<MemberExpr>(Init);
842 if (M->isArrow())
843 return CheckAddressConstantExpression(M->getBase());
844 return CheckAddressConstantExpressionLValue(M->getBase());
845 }
846 case Expr::ArraySubscriptExprClass: {
847 // FIXME: Should we pedwarn for "x[0+0]" (where x is a pointer)?
848 const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(Init);
849 return CheckAddressConstantExpression(ASE->getBase()) ||
850 CheckArithmeticConstantExpression(ASE->getIdx());
851 }
852 case Expr::StringLiteralClass:
853 case Expr::PreDefinedExprClass:
854 return false;
855 case Expr::UnaryOperatorClass: {
856 const UnaryOperator *Exp = cast<UnaryOperator>(Init);
857
858 // C99 6.6p9
859 if (Exp->getOpcode() == UnaryOperator::Deref)
Eli Friedman97c0a392008-05-21 03:39:11 +0000860 return CheckAddressConstantExpression(Exp->getSubExpr());
Eli Friedmanc594b322008-05-20 13:48:25 +0000861
862 Diag(Init->getExprLoc(),
863 diag::err_init_element_not_constant, Init->getSourceRange());
864 return true;
865 }
866 }
867}
868
869bool Sema::CheckAddressConstantExpression(const Expr* Init) {
870 switch (Init->getStmtClass()) {
871 default:
872 Diag(Init->getExprLoc(),
873 diag::err_init_element_not_constant, Init->getSourceRange());
874 return true;
875 case Expr::ParenExprClass: {
876 const ParenExpr* PE = cast<ParenExpr>(Init);
877 return CheckAddressConstantExpression(PE->getSubExpr());
878 }
879 case Expr::StringLiteralClass:
880 case Expr::ObjCStringLiteralClass:
881 return false;
882 case Expr::CallExprClass: {
883 const CallExpr *CE = cast<CallExpr>(Init);
884 if (CE->isBuiltinConstantExpr())
885 return false;
886 Diag(Init->getExprLoc(),
887 diag::err_init_element_not_constant, Init->getSourceRange());
888 return true;
889 }
890 case Expr::UnaryOperatorClass: {
891 const UnaryOperator *Exp = cast<UnaryOperator>(Init);
892
893 // C99 6.6p9
894 if (Exp->getOpcode() == UnaryOperator::AddrOf)
895 return CheckAddressConstantExpressionLValue(Exp->getSubExpr());
896
897 if (Exp->getOpcode() == UnaryOperator::Extension)
898 return CheckAddressConstantExpression(Exp->getSubExpr());
899
900 Diag(Init->getExprLoc(),
901 diag::err_init_element_not_constant, Init->getSourceRange());
902 return true;
903 }
904 case Expr::BinaryOperatorClass: {
905 // FIXME: Should we pedwarn for expressions like "a + 1 + 2"?
906 const BinaryOperator *Exp = cast<BinaryOperator>(Init);
907
908 Expr *PExp = Exp->getLHS();
909 Expr *IExp = Exp->getRHS();
910 if (IExp->getType()->isPointerType())
911 std::swap(PExp, IExp);
912
913 // FIXME: Should we pedwarn if IExp isn't an integer constant expression?
914 return CheckAddressConstantExpression(PExp) ||
915 CheckArithmeticConstantExpression(IExp);
916 }
917 case Expr::ImplicitCastExprClass: {
918 const Expr* SubExpr = cast<ImplicitCastExpr>(Init)->getSubExpr();
919
920 // Check for implicit promotion
921 if (SubExpr->getType()->isFunctionType() ||
922 SubExpr->getType()->isArrayType())
923 return CheckAddressConstantExpressionLValue(SubExpr);
924
925 // Check for pointer->pointer cast
926 if (SubExpr->getType()->isPointerType())
927 return CheckAddressConstantExpression(SubExpr);
928
929 if (SubExpr->getType()->isArithmeticType())
930 return CheckArithmeticConstantExpression(SubExpr);
931
932 Diag(Init->getExprLoc(),
933 diag::err_init_element_not_constant, Init->getSourceRange());
934 return true;
935 }
936 case Expr::CastExprClass: {
937 const Expr* SubExpr = cast<CastExpr>(Init)->getSubExpr();
938
939 // Check for pointer->pointer cast
940 if (SubExpr->getType()->isPointerType())
941 return CheckAddressConstantExpression(SubExpr);
942
943 // FIXME: Should we pedwarn for (int*)(0+0)?
944 if (SubExpr->getType()->isArithmeticType())
945 return CheckArithmeticConstantExpression(SubExpr);
946
947 Diag(Init->getExprLoc(),
948 diag::err_init_element_not_constant, Init->getSourceRange());
949 return true;
950 }
951 case Expr::ConditionalOperatorClass: {
952 // FIXME: Should we pedwarn here?
953 const ConditionalOperator *Exp = cast<ConditionalOperator>(Init);
954 if (!Exp->getCond()->getType()->isArithmeticType()) {
955 Diag(Init->getExprLoc(),
956 diag::err_init_element_not_constant, Init->getSourceRange());
957 return true;
958 }
959 if (CheckArithmeticConstantExpression(Exp->getCond()))
960 return true;
961 if (Exp->getLHS() &&
962 CheckAddressConstantExpression(Exp->getLHS()))
963 return true;
964 return CheckAddressConstantExpression(Exp->getRHS());
965 }
966 case Expr::AddrLabelExprClass:
967 return false;
968 }
969}
970
Eli Friedman4caf0552008-06-09 05:05:07 +0000971static const Expr* FindExpressionBaseAddress(const Expr* E);
972
973static const Expr* FindExpressionBaseAddressLValue(const Expr* E) {
974 switch (E->getStmtClass()) {
975 default:
976 return E;
977 case Expr::ParenExprClass: {
978 const ParenExpr* PE = cast<ParenExpr>(E);
979 return FindExpressionBaseAddressLValue(PE->getSubExpr());
980 }
981 case Expr::MemberExprClass: {
982 const MemberExpr *M = cast<MemberExpr>(E);
983 if (M->isArrow())
984 return FindExpressionBaseAddress(M->getBase());
985 return FindExpressionBaseAddressLValue(M->getBase());
986 }
987 case Expr::ArraySubscriptExprClass: {
988 const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(E);
989 return FindExpressionBaseAddress(ASE->getBase());
990 }
991 case Expr::UnaryOperatorClass: {
992 const UnaryOperator *Exp = cast<UnaryOperator>(E);
993
994 if (Exp->getOpcode() == UnaryOperator::Deref)
995 return FindExpressionBaseAddress(Exp->getSubExpr());
996
997 return E;
998 }
999 }
1000}
1001
1002static const Expr* FindExpressionBaseAddress(const Expr* E) {
1003 switch (E->getStmtClass()) {
1004 default:
1005 return E;
1006 case Expr::ParenExprClass: {
1007 const ParenExpr* PE = cast<ParenExpr>(E);
1008 return FindExpressionBaseAddress(PE->getSubExpr());
1009 }
1010 case Expr::UnaryOperatorClass: {
1011 const UnaryOperator *Exp = cast<UnaryOperator>(E);
1012
1013 // C99 6.6p9
1014 if (Exp->getOpcode() == UnaryOperator::AddrOf)
1015 return FindExpressionBaseAddressLValue(Exp->getSubExpr());
1016
1017 if (Exp->getOpcode() == UnaryOperator::Extension)
1018 return FindExpressionBaseAddress(Exp->getSubExpr());
1019
1020 return E;
1021 }
1022 case Expr::BinaryOperatorClass: {
1023 const BinaryOperator *Exp = cast<BinaryOperator>(E);
1024
1025 Expr *PExp = Exp->getLHS();
1026 Expr *IExp = Exp->getRHS();
1027 if (IExp->getType()->isPointerType())
1028 std::swap(PExp, IExp);
1029
1030 return FindExpressionBaseAddress(PExp);
1031 }
1032 case Expr::ImplicitCastExprClass: {
1033 const Expr* SubExpr = cast<ImplicitCastExpr>(E)->getSubExpr();
1034
1035 // Check for implicit promotion
1036 if (SubExpr->getType()->isFunctionType() ||
1037 SubExpr->getType()->isArrayType())
1038 return FindExpressionBaseAddressLValue(SubExpr);
1039
1040 // Check for pointer->pointer cast
1041 if (SubExpr->getType()->isPointerType())
1042 return FindExpressionBaseAddress(SubExpr);
1043
1044 // We assume that we have an arithmetic expression here;
1045 // if we don't, we'll figure it out later
1046 return 0;
1047 }
1048 case Expr::CastExprClass: {
1049 const Expr* SubExpr = cast<CastExpr>(E)->getSubExpr();
1050
1051 // Check for pointer->pointer cast
1052 if (SubExpr->getType()->isPointerType())
1053 return FindExpressionBaseAddress(SubExpr);
1054
1055 // We assume that we have an arithmetic expression here;
1056 // if we don't, we'll figure it out later
1057 return 0;
1058 }
1059 }
1060}
1061
Eli Friedmanc594b322008-05-20 13:48:25 +00001062bool Sema::CheckArithmeticConstantExpression(const Expr* Init) {
1063 switch (Init->getStmtClass()) {
1064 default:
1065 Diag(Init->getExprLoc(),
1066 diag::err_init_element_not_constant, Init->getSourceRange());
1067 return true;
1068 case Expr::ParenExprClass: {
1069 const ParenExpr* PE = cast<ParenExpr>(Init);
1070 return CheckArithmeticConstantExpression(PE->getSubExpr());
1071 }
1072 case Expr::FloatingLiteralClass:
1073 case Expr::IntegerLiteralClass:
1074 case Expr::CharacterLiteralClass:
1075 case Expr::ImaginaryLiteralClass:
1076 case Expr::TypesCompatibleExprClass:
1077 case Expr::CXXBoolLiteralExprClass:
1078 return false;
1079 case Expr::CallExprClass: {
1080 const CallExpr *CE = cast<CallExpr>(Init);
1081 if (CE->isBuiltinConstantExpr())
1082 return false;
1083 Diag(Init->getExprLoc(),
1084 diag::err_init_element_not_constant, Init->getSourceRange());
1085 return true;
1086 }
1087 case Expr::DeclRefExprClass: {
1088 const Decl *D = cast<DeclRefExpr>(Init)->getDecl();
1089 if (isa<EnumConstantDecl>(D))
1090 return false;
1091 Diag(Init->getExprLoc(),
1092 diag::err_init_element_not_constant, Init->getSourceRange());
1093 return true;
1094 }
1095 case Expr::CompoundLiteralExprClass:
1096 // Allow "(vector type){2,4}"; normal C constraints don't allow this,
1097 // but vectors are allowed to be magic.
1098 if (Init->getType()->isVectorType())
1099 return false;
1100 Diag(Init->getExprLoc(),
1101 diag::err_init_element_not_constant, Init->getSourceRange());
1102 return true;
1103 case Expr::UnaryOperatorClass: {
1104 const UnaryOperator *Exp = cast<UnaryOperator>(Init);
1105
1106 switch (Exp->getOpcode()) {
1107 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1108 // See C99 6.6p3.
1109 default:
1110 Diag(Init->getExprLoc(),
1111 diag::err_init_element_not_constant, Init->getSourceRange());
1112 return true;
1113 case UnaryOperator::SizeOf:
1114 case UnaryOperator::AlignOf:
1115 case UnaryOperator::OffsetOf:
1116 // sizeof(E) is a constantexpr if and only if E is not evaluted.
1117 // See C99 6.5.3.4p2 and 6.6p3.
1118 if (Exp->getSubExpr()->getType()->isConstantSizeType())
1119 return false;
1120 Diag(Init->getExprLoc(),
1121 diag::err_init_element_not_constant, Init->getSourceRange());
1122 return true;
1123 case UnaryOperator::Extension:
1124 case UnaryOperator::LNot:
1125 case UnaryOperator::Plus:
1126 case UnaryOperator::Minus:
1127 case UnaryOperator::Not:
1128 return CheckArithmeticConstantExpression(Exp->getSubExpr());
1129 }
1130 }
1131 case Expr::SizeOfAlignOfTypeExprClass: {
1132 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(Init);
1133 // Special check for void types, which are allowed as an extension
1134 if (Exp->getArgumentType()->isVoidType())
1135 return false;
1136 // alignof always evaluates to a constant.
1137 // FIXME: is sizeof(int[3.0]) a constant expression?
1138 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType()) {
1139 Diag(Init->getExprLoc(),
1140 diag::err_init_element_not_constant, Init->getSourceRange());
1141 return true;
1142 }
1143 return false;
1144 }
1145 case Expr::BinaryOperatorClass: {
1146 const BinaryOperator *Exp = cast<BinaryOperator>(Init);
1147
1148 if (Exp->getLHS()->getType()->isArithmeticType() &&
1149 Exp->getRHS()->getType()->isArithmeticType()) {
1150 return CheckArithmeticConstantExpression(Exp->getLHS()) ||
1151 CheckArithmeticConstantExpression(Exp->getRHS());
1152 }
1153
Eli Friedman4caf0552008-06-09 05:05:07 +00001154 if (Exp->getLHS()->getType()->isPointerType() &&
1155 Exp->getRHS()->getType()->isPointerType()) {
1156 const Expr* LHSBase = FindExpressionBaseAddress(Exp->getLHS());
1157 const Expr* RHSBase = FindExpressionBaseAddress(Exp->getRHS());
1158
1159 // Only allow a null (constant integer) base; we could
1160 // allow some additional cases if necessary, but this
1161 // is sufficient to cover offsetof-like constructs.
1162 if (!LHSBase && !RHSBase) {
1163 return CheckAddressConstantExpression(Exp->getLHS()) ||
1164 CheckAddressConstantExpression(Exp->getRHS());
1165 }
1166 }
1167
Eli Friedmanc594b322008-05-20 13:48:25 +00001168 Diag(Init->getExprLoc(),
1169 diag::err_init_element_not_constant, Init->getSourceRange());
1170 return true;
1171 }
1172 case Expr::ImplicitCastExprClass:
1173 case Expr::CastExprClass: {
1174 const Expr *SubExpr;
1175 if (const CastExpr *C = dyn_cast<CastExpr>(Init)) {
1176 SubExpr = C->getSubExpr();
1177 } else {
1178 SubExpr = cast<ImplicitCastExpr>(Init)->getSubExpr();
1179 }
1180
1181 if (SubExpr->getType()->isArithmeticType())
1182 return CheckArithmeticConstantExpression(SubExpr);
1183
1184 Diag(Init->getExprLoc(),
1185 diag::err_init_element_not_constant, Init->getSourceRange());
1186 return true;
1187 }
1188 case Expr::ConditionalOperatorClass: {
1189 const ConditionalOperator *Exp = cast<ConditionalOperator>(Init);
1190 if (CheckArithmeticConstantExpression(Exp->getCond()))
1191 return true;
1192 if (Exp->getLHS() &&
1193 CheckArithmeticConstantExpression(Exp->getLHS()))
1194 return true;
1195 return CheckArithmeticConstantExpression(Exp->getRHS());
1196 }
1197 }
1198}
1199
1200bool Sema::CheckForConstantInitializer(Expr *Init, QualType DclT) {
Nuno Lopes9a979c32008-07-07 16:46:50 +00001201 Init = Init->IgnoreParens();
1202
Eli Friedmanc594b322008-05-20 13:48:25 +00001203 // Look through CXXDefaultArgExprs; they have no meaning in this context.
1204 if (CXXDefaultArgExpr* DAE = dyn_cast<CXXDefaultArgExpr>(Init))
1205 return CheckForConstantInitializer(DAE->getExpr(), DclT);
1206
Nuno Lopes9a979c32008-07-07 16:46:50 +00001207 if (CompoundLiteralExpr *e = dyn_cast<CompoundLiteralExpr>(Init))
1208 return CheckForConstantInitializer(e->getInitializer(), DclT);
1209
Eli Friedmanc594b322008-05-20 13:48:25 +00001210 if (Init->getType()->isReferenceType()) {
1211 // FIXME: Work out how the heck reference types work
1212 return false;
1213#if 0
1214 // A reference is constant if the address of the expression
1215 // is constant
1216 // We look through initlists here to simplify
1217 // CheckAddressConstantExpressionLValue.
1218 if (InitListExpr *Exp = dyn_cast<InitListExpr>(Init)) {
1219 assert(Exp->getNumInits() > 0 &&
1220 "Refernce initializer cannot be empty");
1221 Init = Exp->getInit(0);
1222 }
1223 return CheckAddressConstantExpressionLValue(Init);
1224#endif
1225 }
1226
1227 if (InitListExpr *Exp = dyn_cast<InitListExpr>(Init)) {
1228 unsigned numInits = Exp->getNumInits();
1229 for (unsigned i = 0; i < numInits; i++) {
1230 // FIXME: Need to get the type of the declaration for C++,
1231 // because it could be a reference?
1232 if (CheckForConstantInitializer(Exp->getInit(i),
1233 Exp->getInit(i)->getType()))
1234 return true;
1235 }
1236 return false;
1237 }
1238
1239 if (Init->isNullPointerConstant(Context))
1240 return false;
1241 if (Init->getType()->isArithmeticType()) {
Eli Friedmanc1cc6dc2008-05-30 18:14:48 +00001242 QualType InitTy = Init->getType().getCanonicalType().getUnqualifiedType();
1243 if (InitTy == Context.BoolTy) {
1244 // Special handling for pointers implicitly cast to bool;
1245 // (e.g. "_Bool rr = &rr;"). This is only legal at the top level.
1246 if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Init)) {
1247 Expr* SubE = ICE->getSubExpr();
1248 if (SubE->getType()->isPointerType() ||
1249 SubE->getType()->isArrayType() ||
1250 SubE->getType()->isFunctionType()) {
1251 return CheckAddressConstantExpression(Init);
1252 }
1253 }
1254 } else if (InitTy->isIntegralType()) {
1255 Expr* SubE = 0;
1256 if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Init))
1257 SubE = ICE->getSubExpr();
1258 else if (CastExpr* CE = dyn_cast<CastExpr>(Init))
1259 SubE = CE->getSubExpr();
1260 // Special check for pointer cast to int; we allow as an extension
1261 // an address constant cast to an integer if the integer
1262 // is of an appropriate width (this sort of code is apparently used
1263 // in some places).
1264 // FIXME: Add pedwarn?
1265 // FIXME: Don't allow bitfields here! Need the FieldDecl for that.
1266 if (SubE && (SubE->getType()->isPointerType() ||
1267 SubE->getType()->isArrayType() ||
1268 SubE->getType()->isFunctionType())) {
1269 unsigned IntWidth = Context.getTypeSize(Init->getType());
1270 unsigned PointerWidth = Context.getTypeSize(Context.VoidPtrTy);
1271 if (IntWidth >= PointerWidth)
1272 return CheckAddressConstantExpression(Init);
1273 }
Eli Friedmanc594b322008-05-20 13:48:25 +00001274 }
1275
1276 return CheckArithmeticConstantExpression(Init);
1277 }
1278
1279 if (Init->getType()->isPointerType())
1280 return CheckAddressConstantExpression(Init);
1281
Eli Friedmanc1cc6dc2008-05-30 18:14:48 +00001282 // An array type at the top level that isn't an init-list must
1283 // be a string literal
Eli Friedmanc594b322008-05-20 13:48:25 +00001284 if (Init->getType()->isArrayType())
1285 return false;
1286
1287 Diag(Init->getExprLoc(), diag::err_init_element_not_constant,
1288 Init->getSourceRange());
1289 return true;
Steve Naroffd0091aa2008-01-10 22:15:12 +00001290}
1291
Steve Naroffbb204692007-09-12 14:07:44 +00001292void Sema::AddInitializerToDecl(DeclTy *dcl, ExprTy *init) {
Steve Naroff410e3e22007-09-12 20:13:48 +00001293 Decl *RealDecl = static_cast<Decl *>(dcl);
Steve Naroffbb204692007-09-12 14:07:44 +00001294 Expr *Init = static_cast<Expr *>(init);
Chris Lattner9a11b9a2007-10-19 20:10:30 +00001295 assert(Init && "missing initializer");
Steve Naroffbb204692007-09-12 14:07:44 +00001296
Chris Lattner9a11b9a2007-10-19 20:10:30 +00001297 // If there is no declaration, there was an error parsing it. Just ignore
1298 // the initializer.
1299 if (RealDecl == 0) {
1300 delete Init;
1301 return;
1302 }
Steve Naroffbb204692007-09-12 14:07:44 +00001303
Steve Naroff410e3e22007-09-12 20:13:48 +00001304 VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl);
1305 if (!VDecl) {
Steve Naroff8e74c932007-09-13 21:41:19 +00001306 Diag(dyn_cast<ScopedDecl>(RealDecl)->getLocation(),
1307 diag::err_illegal_initializer);
Steve Naroff410e3e22007-09-12 20:13:48 +00001308 RealDecl->setInvalidDecl();
1309 return;
1310 }
Steve Naroffbb204692007-09-12 14:07:44 +00001311 // Get the decls type and save a reference for later, since
Steve Naroffd0091aa2008-01-10 22:15:12 +00001312 // CheckInitializerTypes may change it.
Steve Naroff410e3e22007-09-12 20:13:48 +00001313 QualType DclT = VDecl->getType(), SavT = DclT;
Steve Naroff248a7532008-04-15 22:42:06 +00001314 if (VDecl->isBlockVarDecl()) {
1315 VarDecl::StorageClass SC = VDecl->getStorageClass();
Steve Naroffbb204692007-09-12 14:07:44 +00001316 if (SC == VarDecl::Extern) { // C99 6.7.8p5
Steve Naroff410e3e22007-09-12 20:13:48 +00001317 Diag(VDecl->getLocation(), diag::err_block_extern_cant_init);
Steve Naroff248a7532008-04-15 22:42:06 +00001318 VDecl->setInvalidDecl();
1319 } else if (!VDecl->isInvalidDecl()) {
Steve Naroffa9960332008-01-25 00:51:06 +00001320 if (CheckInitializerTypes(Init, DclT))
Steve Naroff248a7532008-04-15 22:42:06 +00001321 VDecl->setInvalidDecl();
Steve Naroffd0091aa2008-01-10 22:15:12 +00001322 if (SC == VarDecl::Static) // C99 6.7.8p4.
1323 CheckForConstantInitializer(Init, DclT);
Steve Naroffbb204692007-09-12 14:07:44 +00001324 }
Steve Naroff248a7532008-04-15 22:42:06 +00001325 } else if (VDecl->isFileVarDecl()) {
1326 if (VDecl->getStorageClass() == VarDecl::Extern)
Steve Naroff410e3e22007-09-12 20:13:48 +00001327 Diag(VDecl->getLocation(), diag::warn_extern_init);
Steve Naroff248a7532008-04-15 22:42:06 +00001328 if (!VDecl->isInvalidDecl())
Steve Naroffa9960332008-01-25 00:51:06 +00001329 if (CheckInitializerTypes(Init, DclT))
Steve Naroff248a7532008-04-15 22:42:06 +00001330 VDecl->setInvalidDecl();
Steve Naroffd0091aa2008-01-10 22:15:12 +00001331
1332 // C99 6.7.8p4. All file scoped initializers need to be constant.
1333 CheckForConstantInitializer(Init, DclT);
Steve Naroffbb204692007-09-12 14:07:44 +00001334 }
1335 // If the type changed, it means we had an incomplete type that was
1336 // completed by the initializer. For example:
1337 // int ary[] = { 1, 3, 5 };
1338 // "ary" transitions from a VariableArrayType to a ConstantArrayType.
Christopher Lamb48b12392007-11-29 19:09:19 +00001339 if (!VDecl->isInvalidDecl() && (DclT != SavT)) {
Steve Naroff410e3e22007-09-12 20:13:48 +00001340 VDecl->setType(DclT);
Christopher Lamb48b12392007-11-29 19:09:19 +00001341 Init->setType(DclT);
1342 }
Steve Naroffbb204692007-09-12 14:07:44 +00001343
1344 // Attach the initializer to the decl.
Steve Naroff410e3e22007-09-12 20:13:48 +00001345 VDecl->setInit(Init);
Steve Naroffbb204692007-09-12 14:07:44 +00001346 return;
1347}
1348
Reid Spencer5f016e22007-07-11 17:01:13 +00001349/// The declarators are chained together backwards, reverse the list.
1350Sema::DeclTy *Sema::FinalizeDeclaratorGroup(Scope *S, DeclTy *group) {
1351 // Often we have single declarators, handle them quickly.
Steve Naroff94745042007-09-13 23:52:58 +00001352 Decl *GroupDecl = static_cast<Decl*>(group);
1353 if (GroupDecl == 0)
Steve Naroffbb204692007-09-12 14:07:44 +00001354 return 0;
Steve Naroff94745042007-09-13 23:52:58 +00001355
1356 ScopedDecl *Group = dyn_cast<ScopedDecl>(GroupDecl);
1357 ScopedDecl *NewGroup = 0;
Steve Naroffbb204692007-09-12 14:07:44 +00001358 if (Group->getNextDeclarator() == 0)
Reid Spencer5f016e22007-07-11 17:01:13 +00001359 NewGroup = Group;
Steve Naroffbb204692007-09-12 14:07:44 +00001360 else { // reverse the list.
1361 while (Group) {
Steve Naroff94745042007-09-13 23:52:58 +00001362 ScopedDecl *Next = Group->getNextDeclarator();
Steve Naroffbb204692007-09-12 14:07:44 +00001363 Group->setNextDeclarator(NewGroup);
1364 NewGroup = Group;
1365 Group = Next;
1366 }
1367 }
1368 // Perform semantic analysis that depends on having fully processed both
1369 // the declarator and initializer.
Steve Naroff94745042007-09-13 23:52:58 +00001370 for (ScopedDecl *ID = NewGroup; ID; ID = ID->getNextDeclarator()) {
Steve Naroffbb204692007-09-12 14:07:44 +00001371 VarDecl *IDecl = dyn_cast<VarDecl>(ID);
1372 if (!IDecl)
1373 continue;
Steve Naroffbb204692007-09-12 14:07:44 +00001374 QualType T = IDecl->getType();
1375
1376 // C99 6.7.5.2p2: If an identifier is declared to be an object with
1377 // static storage duration, it shall not have a variable length array.
Steve Naroff248a7532008-04-15 22:42:06 +00001378 if ((IDecl->isFileVarDecl() || IDecl->isBlockVarDecl()) &&
1379 IDecl->getStorageClass() == VarDecl::Static) {
Eli Friedman3fe02932008-02-15 19:53:52 +00001380 if (T->getAsVariableArrayType()) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001381 Diag(IDecl->getLocation(), diag::err_typecheck_illegal_vla);
1382 IDecl->setInvalidDecl();
Steve Naroffbb204692007-09-12 14:07:44 +00001383 }
1384 }
1385 // Block scope. C99 6.7p7: If an identifier for an object is declared with
1386 // no linkage (C99 6.2.2p6), the type for the object shall be complete...
Steve Naroff248a7532008-04-15 22:42:06 +00001387 if (IDecl->isBlockVarDecl() &&
1388 IDecl->getStorageClass() != VarDecl::Extern) {
Chris Lattnerfd89bc82008-04-02 01:05:10 +00001389 if (T->isIncompleteType() && !IDecl->isInvalidDecl()) {
Chris Lattner8b1be772007-12-02 07:50:03 +00001390 Diag(IDecl->getLocation(), diag::err_typecheck_decl_incomplete_type,
1391 T.getAsString());
Steve Naroffbb204692007-09-12 14:07:44 +00001392 IDecl->setInvalidDecl();
1393 }
1394 }
1395 // File scope. C99 6.9.2p2: A declaration of an identifier for and
1396 // object that has file scope without an initializer, and without a
1397 // storage-class specifier or with the storage-class specifier "static",
1398 // constitutes a tentative definition. Note: A tentative definition with
1399 // external linkage is valid (C99 6.2.2p5).
Steve Naroff248a7532008-04-15 22:42:06 +00001400 if (IDecl && !IDecl->getInit() &&
1401 (IDecl->getStorageClass() == VarDecl::Static ||
1402 IDecl->getStorageClass() == VarDecl::None)) {
Eli Friedman9db13972008-02-15 12:53:51 +00001403 if (T->isIncompleteArrayType()) {
Steve Naroff9a75f8a2008-01-18 20:40:52 +00001404 // C99 6.9.2 (p2, p5): Implicit initialization causes an incomplete
1405 // array to be completed. Don't issue a diagnostic.
Chris Lattnerfd89bc82008-04-02 01:05:10 +00001406 } else if (T->isIncompleteType() && !IDecl->isInvalidDecl()) {
Steve Naroff9a75f8a2008-01-18 20:40:52 +00001407 // C99 6.9.2p3: If the declaration of an identifier for an object is
1408 // a tentative definition and has internal linkage (C99 6.2.2p3), the
1409 // declared type shall not be an incomplete type.
Chris Lattner8b1be772007-12-02 07:50:03 +00001410 Diag(IDecl->getLocation(), diag::err_typecheck_decl_incomplete_type,
1411 T.getAsString());
Steve Naroffbb204692007-09-12 14:07:44 +00001412 IDecl->setInvalidDecl();
1413 }
1414 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001415 }
1416 return NewGroup;
1417}
Steve Naroffe1223f72007-08-28 03:03:08 +00001418
Chris Lattner04421082008-04-08 04:40:51 +00001419/// ActOnParamDeclarator - Called from Parser::ParseFunctionDeclarator()
1420/// to introduce parameters into function prototype scope.
1421Sema::DeclTy *
1422Sema::ActOnParamDeclarator(Scope *S, Declarator &D) {
Chris Lattner985abd92008-06-26 06:49:43 +00001423 const DeclSpec &DS = D.getDeclSpec();
Chris Lattner04421082008-04-08 04:40:51 +00001424
1425 // Verify C99 6.7.5.3p2: The only SCS allowed is 'register'.
1426 if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
1427 DS.getStorageClassSpec() != DeclSpec::SCS_register) {
1428 Diag(DS.getStorageClassSpecLoc(),
1429 diag::err_invalid_storage_class_in_func_decl);
Chris Lattner985abd92008-06-26 06:49:43 +00001430 D.getMutableDeclSpec().ClearStorageClassSpecs();
Chris Lattner04421082008-04-08 04:40:51 +00001431 }
1432 if (DS.isThreadSpecified()) {
1433 Diag(DS.getThreadSpecLoc(),
1434 diag::err_invalid_storage_class_in_func_decl);
Chris Lattner985abd92008-06-26 06:49:43 +00001435 D.getMutableDeclSpec().ClearStorageClassSpecs();
Chris Lattner04421082008-04-08 04:40:51 +00001436 }
1437
Douglas Gregor6d6eb572008-05-07 04:49:29 +00001438 // Check that there are no default arguments inside the type of this
1439 // parameter (C++ only).
1440 if (getLangOptions().CPlusPlus)
1441 CheckExtraCXXDefaultArguments(D);
1442
Chris Lattner04421082008-04-08 04:40:51 +00001443 // In this context, we *do not* check D.getInvalidType(). If the declarator
1444 // type was invalid, GetTypeForDeclarator() still returns a "valid" type,
1445 // though it will not reflect the user specified type.
1446 QualType parmDeclType = GetTypeForDeclarator(D, S);
1447
1448 assert(!parmDeclType.isNull() && "GetTypeForDeclarator() returned null type");
1449
Reid Spencer5f016e22007-07-11 17:01:13 +00001450 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
1451 // Can this happen for params? We already checked that they don't conflict
1452 // among each other. Here they can only shadow globals, which is ok.
Chris Lattner04421082008-04-08 04:40:51 +00001453 IdentifierInfo *II = D.getIdentifier();
1454 if (Decl *PrevDecl = LookupDecl(II, Decl::IDNS_Ordinary, S)) {
1455 if (S->isDeclScope(PrevDecl)) {
1456 Diag(D.getIdentifierLoc(), diag::err_param_redefinition,
1457 dyn_cast<NamedDecl>(PrevDecl)->getName());
1458
1459 // Recover by removing the name
1460 II = 0;
1461 D.SetIdentifier(0, D.getIdentifierLoc());
1462 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001463 }
Steve Naroff6a9f3e32007-08-07 22:44:21 +00001464
1465 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
1466 // Doing the promotion here has a win and a loss. The win is the type for
1467 // both Decl's and DeclRefExpr's will match (a convenient invariant for the
1468 // code generator). The loss is the orginal type isn't preserved. For example:
1469 //
1470 // void func(int parmvardecl[5]) { // convert "int [5]" to "int *"
1471 // int blockvardecl[5];
1472 // sizeof(parmvardecl); // size == 4
1473 // sizeof(blockvardecl); // size == 20
1474 // }
1475 //
1476 // For expressions, all implicit conversions are captured using the
1477 // ImplicitCastExpr AST node (we have no such mechanism for Decl's).
1478 //
1479 // FIXME: If a source translation tool needs to see the original type, then
1480 // we need to consider storing both types (in ParmVarDecl)...
1481 //
Chris Lattnere6327742008-04-02 05:18:44 +00001482 if (parmDeclType->isArrayType()) {
Chris Lattner529bd022008-01-02 22:50:48 +00001483 // int x[restrict 4] -> int *restrict
Chris Lattnere6327742008-04-02 05:18:44 +00001484 parmDeclType = Context.getArrayDecayedType(parmDeclType);
Chris Lattner529bd022008-01-02 22:50:48 +00001485 } else if (parmDeclType->isFunctionType())
Steve Naroff6a9f3e32007-08-07 22:44:21 +00001486 parmDeclType = Context.getPointerType(parmDeclType);
1487
Chris Lattner04421082008-04-08 04:40:51 +00001488 ParmVarDecl *New = ParmVarDecl::Create(Context, CurContext,
1489 D.getIdentifierLoc(), II,
1490 parmDeclType, VarDecl::None,
1491 0, 0);
Anders Carlssonf78915f2008-02-15 07:04:12 +00001492
Chris Lattner04421082008-04-08 04:40:51 +00001493 if (D.getInvalidType())
Steve Naroff53a32342007-08-28 18:45:29 +00001494 New->setInvalidDecl();
1495
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +00001496 if (II)
1497 PushOnScopeChains(New, S);
Nate Begemanb7894b52008-02-17 21:20:31 +00001498
Chris Lattner3ff30c82008-06-29 00:02:00 +00001499 ProcessDeclAttributes(New, D);
Reid Spencer5f016e22007-07-11 17:01:13 +00001500 return New;
Chris Lattner04421082008-04-08 04:40:51 +00001501
Reid Spencer5f016e22007-07-11 17:01:13 +00001502}
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001503
Chris Lattnerb652cea2007-10-09 17:14:05 +00001504Sema::DeclTy *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Declarator &D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +00001505 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
Reid Spencer5f016e22007-07-11 17:01:13 +00001506 assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function &&
1507 "Not a function declarator!");
1508 DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
Chris Lattner04421082008-04-08 04:40:51 +00001509
Reid Spencer5f016e22007-07-11 17:01:13 +00001510 // Verify 6.9.1p6: 'every identifier in the identifier list shall be declared'
1511 // for a K&R function.
1512 if (!FTI.hasPrototype) {
1513 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Chris Lattner04421082008-04-08 04:40:51 +00001514 if (FTI.ArgInfo[i].Param == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001515 Diag(FTI.ArgInfo[i].IdentLoc, diag::ext_param_not_declared,
1516 FTI.ArgInfo[i].Ident->getName());
1517 // Implicitly declare the argument as type 'int' for lack of a better
1518 // type.
Chris Lattner04421082008-04-08 04:40:51 +00001519 DeclSpec DS;
1520 const char* PrevSpec; // unused
1521 DS.SetTypeSpecType(DeclSpec::TST_int, FTI.ArgInfo[i].IdentLoc,
1522 PrevSpec);
1523 Declarator ParamD(DS, Declarator::KNRTypeListContext);
1524 ParamD.SetIdentifier(FTI.ArgInfo[i].Ident, FTI.ArgInfo[i].IdentLoc);
1525 FTI.ArgInfo[i].Param = ActOnParamDeclarator(FnBodyScope, ParamD);
Reid Spencer5f016e22007-07-11 17:01:13 +00001526 }
1527 }
Chris Lattner52804082008-02-17 19:31:09 +00001528
Reid Spencer5f016e22007-07-11 17:01:13 +00001529 // Since this is a function definition, act as though we have information
1530 // about the arguments.
Chris Lattner52804082008-02-17 19:31:09 +00001531 if (FTI.NumArgs)
1532 FTI.hasPrototype = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001533 } else {
Chris Lattner04421082008-04-08 04:40:51 +00001534 // FIXME: Diagnose arguments without names in C.
Reid Spencer5f016e22007-07-11 17:01:13 +00001535 }
1536
1537 Scope *GlobalScope = FnBodyScope->getParent();
Steve Naroffadbbd0c2008-01-14 20:51:29 +00001538
1539 // See if this is a redefinition.
Steve Naroffe8043c32008-04-01 23:04:06 +00001540 Decl *PrevDcl = LookupDecl(D.getIdentifier(), Decl::IDNS_Ordinary,
Steve Naroffb327ce02008-04-02 14:35:35 +00001541 GlobalScope);
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +00001542 if (PrevDcl && IdResolver.isDeclInScope(PrevDcl, CurContext)) {
1543 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PrevDcl)) {
1544 const FunctionDecl *Definition;
1545 if (FD->getBody(Definition)) {
1546 Diag(D.getIdentifierLoc(), diag::err_redefinition,
1547 D.getIdentifier()->getName());
1548 Diag(Definition->getLocation(), diag::err_previous_definition);
1549 }
Steve Naroffadbbd0c2008-01-14 20:51:29 +00001550 }
1551 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001552
1553 return ActOnStartOfFunctionDef(FnBodyScope,
1554 ActOnDeclarator(GlobalScope, D, 0));
1555}
1556
1557Sema::DeclTy *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, DeclTy *D) {
1558 Decl *decl = static_cast<Decl*>(D);
Chris Lattnere9ba3232008-02-16 01:20:36 +00001559 FunctionDecl *FD = cast<FunctionDecl>(decl);
Chris Lattnerb048c982008-04-06 04:47:34 +00001560 PushDeclContext(FD);
Chris Lattner04421082008-04-08 04:40:51 +00001561
1562 // Check the validity of our function parameters
1563 CheckParmsForFunctionDef(FD);
1564
1565 // Introduce our parameters into the function scope
1566 for (unsigned p = 0, NumParams = FD->getNumParams(); p < NumParams; ++p) {
1567 ParmVarDecl *Param = FD->getParamDecl(p);
1568 // If this has an identifier, add it to the scope stack.
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +00001569 if (Param->getIdentifier())
1570 PushOnScopeChains(Param, FnBodyScope);
Reid Spencer5f016e22007-07-11 17:01:13 +00001571 }
Chris Lattner04421082008-04-08 04:40:51 +00001572
Reid Spencer5f016e22007-07-11 17:01:13 +00001573 return FD;
1574}
1575
Steve Naroffd6d054d2007-11-11 23:20:51 +00001576Sema::DeclTy *Sema::ActOnFinishFunctionBody(DeclTy *D, StmtTy *Body) {
1577 Decl *dcl = static_cast<Decl *>(D);
1578 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(dcl)) {
1579 FD->setBody((Stmt*)Body);
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +00001580 assert(FD == getCurFunctionDecl() && "Function parsing confused");
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001581 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(dcl)) {
Steve Naroffd6d054d2007-11-11 23:20:51 +00001582 MD->setBody((Stmt*)Body);
Steve Naroff4d832202007-12-13 18:18:56 +00001583 }
Chris Lattnerb048c982008-04-06 04:47:34 +00001584 PopDeclContext();
Reid Spencer5f016e22007-07-11 17:01:13 +00001585 // Verify and clean out per-function state.
1586
1587 // Check goto/label use.
1588 for (llvm::DenseMap<IdentifierInfo*, LabelStmt*>::iterator
1589 I = LabelMap.begin(), E = LabelMap.end(); I != E; ++I) {
1590 // Verify that we have no forward references left. If so, there was a goto
1591 // or address of a label taken, but no definition of it. Label fwd
1592 // definitions are indicated with a null substmt.
1593 if (I->second->getSubStmt() == 0) {
1594 LabelStmt *L = I->second;
1595 // Emit error.
1596 Diag(L->getIdentLoc(), diag::err_undeclared_label_use, L->getName());
1597
1598 // At this point, we have gotos that use the bogus label. Stitch it into
1599 // the function body so that they aren't leaked and that the AST is well
1600 // formed.
Chris Lattner0cbc2152008-01-25 00:01:10 +00001601 if (Body) {
1602 L->setSubStmt(new NullStmt(L->getIdentLoc()));
1603 cast<CompoundStmt>((Stmt*)Body)->push_back(L);
1604 } else {
1605 // The whole function wasn't parsed correctly, just delete this.
1606 delete L;
1607 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001608 }
1609 }
1610 LabelMap.clear();
1611
Steve Naroffd6d054d2007-11-11 23:20:51 +00001612 return D;
Fariborz Jahanian60fbca02007-11-10 16:31:34 +00001613}
1614
Reid Spencer5f016e22007-07-11 17:01:13 +00001615/// ImplicitlyDefineFunction - An undeclared identifier was used in a function
1616/// call, forming a call to an implicitly defined function (per C99 6.5.1p2).
Steve Naroff8c9f13e2007-09-16 16:16:00 +00001617ScopedDecl *Sema::ImplicitlyDefineFunction(SourceLocation Loc,
1618 IdentifierInfo &II, Scope *S) {
Chris Lattner37d10842008-05-05 21:18:06 +00001619 // Extension in C99. Legal in C90, but warn about it.
1620 if (getLangOptions().C99)
Reid Spencer5f016e22007-07-11 17:01:13 +00001621 Diag(Loc, diag::ext_implicit_function_decl, II.getName());
Chris Lattner37d10842008-05-05 21:18:06 +00001622 else
Reid Spencer5f016e22007-07-11 17:01:13 +00001623 Diag(Loc, diag::warn_implicit_function_decl, II.getName());
1624
1625 // FIXME: handle stuff like:
1626 // void foo() { extern float X(); }
1627 // void bar() { X(); } <-- implicit decl for X in another scope.
1628
1629 // Set a Declarator for the implicit definition: int foo();
1630 const char *Dummy;
1631 DeclSpec DS;
1632 bool Error = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, Dummy);
1633 Error = Error; // Silence warning.
1634 assert(!Error && "Error setting up implicit decl!");
1635 Declarator D(DS, Declarator::BlockContext);
1636 D.AddTypeInfo(DeclaratorChunk::getFunction(false, false, 0, 0, Loc));
1637 D.SetIdentifier(&II, Loc);
1638
Argyrios Kyrtzidis93213bb2008-05-01 21:04:16 +00001639 // Insert this function into translation-unit scope.
1640
1641 DeclContext *PrevDC = CurContext;
1642 CurContext = Context.getTranslationUnitDecl();
1643
Steve Naroffe2ef8152008-04-04 14:32:09 +00001644 FunctionDecl *FD =
Argyrios Kyrtzidis93213bb2008-05-01 21:04:16 +00001645 dyn_cast<FunctionDecl>(static_cast<Decl*>(ActOnDeclarator(TUScope, D, 0)));
Steve Naroffe2ef8152008-04-04 14:32:09 +00001646 FD->setImplicit();
Argyrios Kyrtzidis93213bb2008-05-01 21:04:16 +00001647
1648 CurContext = PrevDC;
1649
Steve Naroffe2ef8152008-04-04 14:32:09 +00001650 return FD;
Reid Spencer5f016e22007-07-11 17:01:13 +00001651}
1652
1653
Chris Lattner41af0932007-11-14 06:34:38 +00001654TypedefDecl *Sema::ParseTypedefDecl(Scope *S, Declarator &D, QualType T,
Steve Naroff94745042007-09-13 23:52:58 +00001655 ScopedDecl *LastDeclarator) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001656 assert(D.getIdentifier() && "Wrong callback for declspec without declarator");
Steve Naroff5912a352007-08-28 20:14:24 +00001657 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001658
1659 // Scope manipulation handled by caller.
Chris Lattner0ed844b2008-04-04 06:12:32 +00001660 TypedefDecl *NewTD = TypedefDecl::Create(Context, CurContext,
1661 D.getIdentifierLoc(),
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001662 D.getIdentifier(),
Chris Lattnerc63e6602008-03-15 21:32:50 +00001663 T, LastDeclarator);
Steve Naroff5912a352007-08-28 20:14:24 +00001664 if (D.getInvalidType())
1665 NewTD->setInvalidDecl();
1666 return NewTD;
Reid Spencer5f016e22007-07-11 17:01:13 +00001667}
1668
Steve Naroff08d92e42007-09-15 18:49:24 +00001669/// ActOnTag - This is invoked when we see 'struct foo' or 'struct {'. In the
Reid Spencer5f016e22007-07-11 17:01:13 +00001670/// former case, Name will be non-null. In the later case, Name will be null.
1671/// TagType indicates what kind of tag this is. TK indicates whether this is a
1672/// reference/declaration/definition of a tag.
Steve Naroff08d92e42007-09-15 18:49:24 +00001673Sema::DeclTy *Sema::ActOnTag(Scope *S, unsigned TagType, TagKind TK,
Reid Spencer5f016e22007-07-11 17:01:13 +00001674 SourceLocation KWLoc, IdentifierInfo *Name,
1675 SourceLocation NameLoc, AttributeList *Attr) {
1676 // If this is a use of an existing tag, it must have a name.
1677 assert((Name != 0 || TK == TK_Definition) &&
1678 "Nameless record must be a definition!");
1679
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001680 TagDecl::TagKind Kind;
Reid Spencer5f016e22007-07-11 17:01:13 +00001681 switch (TagType) {
1682 default: assert(0 && "Unknown tag type!");
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001683 case DeclSpec::TST_struct: Kind = TagDecl::TK_struct; break;
1684 case DeclSpec::TST_union: Kind = TagDecl::TK_union; break;
1685 case DeclSpec::TST_class: Kind = TagDecl::TK_class; break;
1686 case DeclSpec::TST_enum: Kind = TagDecl::TK_enum; break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001687 }
1688
1689 // If this is a named struct, check to see if there was a previous forward
1690 // declaration or definition.
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +00001691 // Use ScopedDecl instead of TagDecl, because a NamespaceDecl may come up.
1692 if (ScopedDecl *PrevDecl =
1693 dyn_cast_or_null<ScopedDecl>(LookupDecl(Name, Decl::IDNS_Tag, S))) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001694
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +00001695 assert((isa<TagDecl>(PrevDecl) || isa<NamespaceDecl>(PrevDecl)) &&
1696 "unexpected Decl type");
1697 if (TagDecl *PrevTagDecl = dyn_cast<TagDecl>(PrevDecl)) {
Chris Lattner14943b92008-07-03 03:30:58 +00001698 // If this is a use of a previous tag, or if the tag is already declared
1699 // in the same scope (so that the definition/declaration completes or
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +00001700 // rementions the tag), reuse the decl.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +00001701 if (TK == TK_Reference ||
1702 IdResolver.isDeclInScope(PrevDecl, CurContext, S)) {
Chris Lattner14943b92008-07-03 03:30:58 +00001703 // Make sure that this wasn't declared as an enum and now used as a
1704 // struct or something similar.
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001705 if (PrevTagDecl->getTagKind() != Kind) {
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +00001706 Diag(KWLoc, diag::err_use_with_wrong_tag, Name->getName());
1707 Diag(PrevDecl->getLocation(), diag::err_previous_use);
Chris Lattner14943b92008-07-03 03:30:58 +00001708 // Recover by making this an anonymous redefinition.
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +00001709 Name = 0;
Chris Lattner14943b92008-07-03 03:30:58 +00001710 PrevDecl = 0;
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +00001711 } else {
Chris Lattner14943b92008-07-03 03:30:58 +00001712 // If this is a use or a forward declaration, we're good.
1713 if (TK != TK_Definition)
1714 return PrevDecl;
1715
1716 // Diagnose attempts to redefine a tag.
1717 if (PrevTagDecl->isDefinition()) {
1718 Diag(NameLoc, diag::err_redefinition, Name->getName());
1719 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
1720 // If this is a redefinition, recover by making this struct be
1721 // anonymous, which will make any later references get the previous
1722 // definition.
1723 Name = 0;
1724 } else {
1725 // Okay, this is definition of a previously declared or referenced
1726 // tag. Move the location of the decl to be the definition site.
1727 PrevDecl->setLocation(NameLoc);
1728 return PrevDecl;
1729 }
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +00001730 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001731 }
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +00001732 // If we get here, this is a definition of a new struct type in a nested
1733 // scope, e.g. "struct foo; void bar() { struct foo; }", just create a new
1734 // type.
1735 } else {
Argyrios Kyrtzidisb02ef242008-07-16 07:45:46 +00001736 // PrevDecl is a namespace.
1737 if (IdResolver.isDeclInScope(PrevDecl, CurContext, S)) {
1738 // The tag name clashes with a namespace name, issue an error and recover
1739 // by making this tag be anonymous.
1740 Diag(NameLoc, diag::err_redefinition_different_kind, Name->getName());
1741 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
1742 Name = 0;
1743 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001744 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001745 }
1746
1747 // If there is an identifier, use the location of the identifier as the
1748 // location of the decl, otherwise use the location of the struct/union
1749 // keyword.
1750 SourceLocation Loc = NameLoc.isValid() ? NameLoc : KWLoc;
1751
1752 // Otherwise, if this is the first time we've seen this tag, create the decl.
1753 TagDecl *New;
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001754 if (Kind == TagDecl::TK_enum) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001755 // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.:
1756 // enum X { A, B, C } D; D should chain to X.
Chris Lattner0ed844b2008-04-04 06:12:32 +00001757 New = EnumDecl::Create(Context, CurContext, Loc, Name, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +00001758 // If this is an undefined enum, warn.
1759 if (TK != TK_Definition) Diag(Loc, diag::ext_forward_ref_enum);
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001760 } else {
1761 // struct/union/class
1762
Reid Spencer5f016e22007-07-11 17:01:13 +00001763 // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.:
1764 // struct X { int A; } D; D should chain to X.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001765 if (getLangOptions().CPlusPlus)
1766 // FIXME: Look for a way to use RecordDecl for simple structs.
1767 New = CXXRecordDecl::Create(Context, Kind, CurContext, Loc, Name, 0);
1768 else
1769 New = RecordDecl::Create(Context, Kind, CurContext, Loc, Name, 0);
1770 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001771
1772 // If this has an identifier, add it to the scope stack.
1773 if (Name) {
Chris Lattner31e05722007-08-26 06:24:45 +00001774 // The scope passed in may not be a decl scope. Zip up the scope tree until
1775 // we find one that is.
1776 while ((S->getFlags() & Scope::DeclScope) == 0)
1777 S = S->getParent();
1778
1779 // Add it to the decl chain.
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +00001780 PushOnScopeChains(New, S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001781 }
Chris Lattnere1e79852008-02-06 00:51:33 +00001782
Chris Lattnerf2e4bd52008-06-28 23:58:55 +00001783 if (Attr)
1784 ProcessDeclAttributeList(New, Attr);
Reid Spencer5f016e22007-07-11 17:01:13 +00001785 return New;
1786}
1787
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001788/// Collect the instance variables declared in an Objective-C object. Used in
1789/// the creation of structures from objects using the @defs directive.
1790static void CollectIvars(ObjCInterfaceDecl *Class,
1791 llvm::SmallVector<Sema::DeclTy*, 16> &ivars) {
1792 if (Class->getSuperClass())
1793 CollectIvars(Class->getSuperClass(), ivars);
1794 ivars.append(Class->ivar_begin(), Class->ivar_end());
1795}
1796
1797/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1798/// instance variables of ClassName into Decls.
1799void Sema::ActOnDefs(Scope *S, SourceLocation DeclStart,
1800 IdentifierInfo *ClassName,
1801 llvm::SmallVector<DeclTy*, 16> &Decls) {
1802 // Check that ClassName is a valid class
1803 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
1804 if (!Class) {
1805 Diag(DeclStart, diag::err_undef_interface, ClassName->getName());
1806 return;
1807 }
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001808 // Collect the instance variables
1809 CollectIvars(Class, Decls);
1810}
1811
Eli Friedman1b76ada2008-06-03 21:01:11 +00001812QualType Sema::TryFixInvalidVariablyModifiedType(QualType T) {
1813 // This method tries to turn a variable array into a constant
1814 // array even when the size isn't an ICE. This is necessary
1815 // for compatibility with code that depends on gcc's buggy
1816 // constant expression folding, like struct {char x[(int)(char*)2];}
1817 if (const VariableArrayType* VLATy = dyn_cast<VariableArrayType>(T)) {
Anders Carlssonc44eec62008-07-03 04:20:39 +00001818 APValue Result;
Eli Friedman1b76ada2008-06-03 21:01:11 +00001819 if (VLATy->getSizeExpr() &&
Chris Lattnercf0f51d2008-07-11 19:19:21 +00001820 VLATy->getSizeExpr()->tryEvaluate(Result, Context) && Result.isInt()) {
1821 llvm::APSInt &Res = Result.getInt();
1822 if (Res > llvm::APSInt(Res.getBitWidth(), Res.isUnsigned()))
1823 return Context.getConstantArrayType(VLATy->getElementType(),
1824 Res, ArrayType::Normal, 0);
Eli Friedman1b76ada2008-06-03 21:01:11 +00001825 }
1826 }
1827 return QualType();
1828}
1829
Steve Naroff08d92e42007-09-15 18:49:24 +00001830/// ActOnField - Each field of a struct/union/class is passed into this in order
Reid Spencer5f016e22007-07-11 17:01:13 +00001831/// to create a FieldDecl object for it.
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +00001832Sema::DeclTy *Sema::ActOnField(Scope *S,
Reid Spencer5f016e22007-07-11 17:01:13 +00001833 SourceLocation DeclStart,
1834 Declarator &D, ExprTy *BitfieldWidth) {
1835 IdentifierInfo *II = D.getIdentifier();
1836 Expr *BitWidth = (Expr*)BitfieldWidth;
Reid Spencer5f016e22007-07-11 17:01:13 +00001837 SourceLocation Loc = DeclStart;
1838 if (II) Loc = D.getIdentifierLoc();
1839
1840 // FIXME: Unnamed fields can be handled in various different ways, for
1841 // example, unnamed unions inject all members into the struct namespace!
1842
1843
1844 if (BitWidth) {
1845 // TODO: Validate.
1846 //printf("WARNING: BITFIELDS IGNORED!\n");
1847
1848 // 6.7.2.1p3
1849 // 6.7.2.1p4
1850
1851 } else {
1852 // Not a bitfield.
1853
1854 // validate II.
1855
1856 }
1857
1858 QualType T = GetTypeForDeclarator(D, S);
Steve Naroff5912a352007-08-28 20:14:24 +00001859 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
1860 bool InvalidDecl = false;
Steve Naroffd7444aa2007-08-31 17:20:07 +00001861
Reid Spencer5f016e22007-07-11 17:01:13 +00001862 // C99 6.7.2.1p8: A member of a structure or union may have any type other
1863 // than a variably modified type.
Eli Friedman9db13972008-02-15 12:53:51 +00001864 if (T->isVariablyModifiedType()) {
Eli Friedman1b76ada2008-06-03 21:01:11 +00001865 QualType FixedTy = TryFixInvalidVariablyModifiedType(T);
1866 if (!FixedTy.isNull()) {
1867 Diag(Loc, diag::warn_illegal_constant_array_size, Loc);
1868 T = FixedTy;
1869 } else {
1870 // FIXME: This diagnostic needs work
1871 Diag(Loc, diag::err_typecheck_illegal_vla, Loc);
1872 InvalidDecl = true;
1873 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001874 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001875 // FIXME: Chain fielddecls together.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001876 FieldDecl *NewFD;
1877
1878 if (getLangOptions().CPlusPlus) {
1879 // FIXME: Replace CXXFieldDecls with FieldDecls for simple structs.
1880 NewFD = CXXFieldDecl::Create(Context, cast<CXXRecordDecl>(CurContext),
1881 Loc, II, T, BitWidth);
1882 if (II)
1883 PushOnScopeChains(NewFD, S);
1884 }
1885 else
1886 NewFD = FieldDecl::Create(Context, Loc, II, T, BitWidth);
Steve Naroff44739212007-09-11 21:17:26 +00001887
Chris Lattner3ff30c82008-06-29 00:02:00 +00001888 ProcessDeclAttributes(NewFD, D);
Anders Carlssonad148062008-02-16 00:29:18 +00001889
Steve Naroff5912a352007-08-28 20:14:24 +00001890 if (D.getInvalidType() || InvalidDecl)
1891 NewFD->setInvalidDecl();
1892 return NewFD;
Reid Spencer5f016e22007-07-11 17:01:13 +00001893}
1894
Fariborz Jahanian89204a12007-10-01 16:53:59 +00001895/// TranslateIvarVisibility - Translate visibility from a token ID to an
1896/// AST enum value.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001897static ObjCIvarDecl::AccessControl
Fariborz Jahanian89204a12007-10-01 16:53:59 +00001898TranslateIvarVisibility(tok::ObjCKeywordKind ivarVisibility) {
Steve Narofff13271f2007-09-14 23:09:53 +00001899 switch (ivarVisibility) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001900 case tok::objc_private: return ObjCIvarDecl::Private;
1901 case tok::objc_public: return ObjCIvarDecl::Public;
1902 case tok::objc_protected: return ObjCIvarDecl::Protected;
1903 case tok::objc_package: return ObjCIvarDecl::Package;
Fariborz Jahanian89204a12007-10-01 16:53:59 +00001904 default: assert(false && "Unknown visitibility kind");
Steve Narofff13271f2007-09-14 23:09:53 +00001905 }
1906}
1907
Fariborz Jahanian45bc03f2008-04-11 16:55:42 +00001908/// ActOnIvar - Each ivar field of an objective-c class is passed into this
1909/// in order to create an IvarDecl object for it.
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +00001910Sema::DeclTy *Sema::ActOnIvar(Scope *S,
Fariborz Jahanian45bc03f2008-04-11 16:55:42 +00001911 SourceLocation DeclStart,
1912 Declarator &D, ExprTy *BitfieldWidth,
1913 tok::ObjCKeywordKind Visibility) {
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +00001914 IdentifierInfo *II = D.getIdentifier();
1915 Expr *BitWidth = (Expr*)BitfieldWidth;
1916 SourceLocation Loc = DeclStart;
1917 if (II) Loc = D.getIdentifierLoc();
1918
1919 // FIXME: Unnamed fields can be handled in various different ways, for
1920 // example, unnamed unions inject all members into the struct namespace!
1921
1922
1923 if (BitWidth) {
1924 // TODO: Validate.
1925 //printf("WARNING: BITFIELDS IGNORED!\n");
1926
1927 // 6.7.2.1p3
1928 // 6.7.2.1p4
1929
1930 } else {
1931 // Not a bitfield.
1932
1933 // validate II.
1934
1935 }
1936
1937 QualType T = GetTypeForDeclarator(D, S);
1938 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
1939 bool InvalidDecl = false;
1940
1941 // C99 6.7.2.1p8: A member of a structure or union may have any type other
1942 // than a variably modified type.
1943 if (T->isVariablyModifiedType()) {
1944 // FIXME: This diagnostic needs work
1945 Diag(Loc, diag::err_typecheck_illegal_vla, Loc);
1946 InvalidDecl = true;
1947 }
1948
Fariborz Jahanian45bc03f2008-04-11 16:55:42 +00001949 ObjCIvarDecl *NewID = ObjCIvarDecl::Create(Context, Loc, II, T);
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +00001950
Chris Lattner3ff30c82008-06-29 00:02:00 +00001951 ProcessDeclAttributes(NewID, D);
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +00001952
1953 if (D.getInvalidType() || InvalidDecl)
1954 NewID->setInvalidDecl();
1955 // If we have visibility info, make sure the AST is set accordingly.
Fariborz Jahanian45bc03f2008-04-11 16:55:42 +00001956 if (Visibility != tok::objc_not_keyword)
1957 NewID->setAccessControl(TranslateIvarVisibility(Visibility));
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +00001958 return NewID;
1959}
1960
Fariborz Jahanian9d048ff2007-09-29 00:54:24 +00001961void Sema::ActOnFields(Scope* S,
Fariborz Jahanian3f5faf72007-10-04 00:45:27 +00001962 SourceLocation RecLoc, DeclTy *RecDecl,
Steve Naroff08d92e42007-09-15 18:49:24 +00001963 DeclTy **Fields, unsigned NumFields,
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +00001964 SourceLocation LBrac, SourceLocation RBrac) {
Steve Naroff74216642007-09-14 22:20:54 +00001965 Decl *EnclosingDecl = static_cast<Decl*>(RecDecl);
1966 assert(EnclosingDecl && "missing record or interface decl");
1967 RecordDecl *Record = dyn_cast<RecordDecl>(EnclosingDecl);
1968
Fariborz Jahaniane267ab62007-09-14 16:27:55 +00001969 if (Record && Record->isDefinition()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001970 // Diagnose code like:
1971 // struct S { struct S {} X; };
1972 // We discover this when we complete the outer S. Reject and ignore the
1973 // outer S.
1974 Diag(Record->getLocation(), diag::err_nested_redefinition,
1975 Record->getKindName());
1976 Diag(RecLoc, diag::err_previous_definition);
Steve Naroff74216642007-09-14 22:20:54 +00001977 Record->setInvalidDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +00001978 return;
1979 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001980 // Verify that all the fields are okay.
1981 unsigned NumNamedMembers = 0;
1982 llvm::SmallVector<FieldDecl*, 32> RecFields;
1983 llvm::SmallSet<const IdentifierInfo*, 32> FieldIDs;
Steve Naroff74216642007-09-14 22:20:54 +00001984
Reid Spencer5f016e22007-07-11 17:01:13 +00001985 for (unsigned i = 0; i != NumFields; ++i) {
Fariborz Jahaniane267ab62007-09-14 16:27:55 +00001986
Steve Naroff74216642007-09-14 22:20:54 +00001987 FieldDecl *FD = cast_or_null<FieldDecl>(static_cast<Decl*>(Fields[i]));
1988 assert(FD && "missing field decl");
1989
1990 // Remember all fields.
1991 RecFields.push_back(FD);
Reid Spencer5f016e22007-07-11 17:01:13 +00001992
1993 // Get the type for the field.
Chris Lattner02c642e2007-07-31 21:33:24 +00001994 Type *FDTy = FD->getType().getTypePtr();
Steve Narofff13271f2007-09-14 23:09:53 +00001995
Reid Spencer5f016e22007-07-11 17:01:13 +00001996 // C99 6.7.2.1p2 - A field may not be a function type.
Chris Lattner02c642e2007-07-31 21:33:24 +00001997 if (FDTy->isFunctionType()) {
Steve Naroff74216642007-09-14 22:20:54 +00001998 Diag(FD->getLocation(), diag::err_field_declared_as_function,
Reid Spencer5f016e22007-07-11 17:01:13 +00001999 FD->getName());
Steve Naroff74216642007-09-14 22:20:54 +00002000 FD->setInvalidDecl();
2001 EnclosingDecl->setInvalidDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +00002002 continue;
2003 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002004 // C99 6.7.2.1p2 - A field may not be an incomplete type except...
2005 if (FDTy->isIncompleteType()) {
Fariborz Jahaniane267ab62007-09-14 16:27:55 +00002006 if (!Record) { // Incomplete ivar type is always an error.
Fariborz Jahanian3f5faf72007-10-04 00:45:27 +00002007 Diag(FD->getLocation(), diag::err_field_incomplete, FD->getName());
Steve Naroff74216642007-09-14 22:20:54 +00002008 FD->setInvalidDecl();
2009 EnclosingDecl->setInvalidDecl();
Fariborz Jahanian3f5faf72007-10-04 00:45:27 +00002010 continue;
Fariborz Jahaniane267ab62007-09-14 16:27:55 +00002011 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002012 if (i != NumFields-1 || // ... that the last member ...
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00002013 !Record->isStruct() || // ... of a structure ...
Chris Lattner02c642e2007-07-31 21:33:24 +00002014 !FDTy->isArrayType()) { //... may have incomplete array type.
Reid Spencer5f016e22007-07-11 17:01:13 +00002015 Diag(FD->getLocation(), diag::err_field_incomplete, FD->getName());
Steve Naroff74216642007-09-14 22:20:54 +00002016 FD->setInvalidDecl();
2017 EnclosingDecl->setInvalidDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +00002018 continue;
2019 }
Fariborz Jahaniane267ab62007-09-14 16:27:55 +00002020 if (NumNamedMembers < 1) { //... must have more than named member ...
Reid Spencer5f016e22007-07-11 17:01:13 +00002021 Diag(FD->getLocation(), diag::err_flexible_array_empty_struct,
2022 FD->getName());
Steve Naroff74216642007-09-14 22:20:54 +00002023 FD->setInvalidDecl();
2024 EnclosingDecl->setInvalidDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +00002025 continue;
2026 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002027 // Okay, we have a legal flexible array member at the end of the struct.
Fariborz Jahaniane267ab62007-09-14 16:27:55 +00002028 if (Record)
2029 Record->setHasFlexibleArrayMember(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002030 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002031 /// C99 6.7.2.1p2 - a struct ending in a flexible array member cannot be the
2032 /// field of another structure or the element of an array.
Chris Lattner02c642e2007-07-31 21:33:24 +00002033 if (const RecordType *FDTTy = FDTy->getAsRecordType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002034 if (FDTTy->getDecl()->hasFlexibleArrayMember()) {
2035 // If this is a member of a union, then entire union becomes "flexible".
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00002036 if (Record && Record->isUnion()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002037 Record->setHasFlexibleArrayMember(true);
2038 } else {
2039 // If this is a struct/class and this is not the last element, reject
2040 // it. Note that GCC supports variable sized arrays in the middle of
2041 // structures.
2042 if (i != NumFields-1) {
2043 Diag(FD->getLocation(), diag::err_variable_sized_type_in_struct,
2044 FD->getName());
Steve Naroff74216642007-09-14 22:20:54 +00002045 FD->setInvalidDecl();
2046 EnclosingDecl->setInvalidDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +00002047 continue;
2048 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002049 // We support flexible arrays at the end of structs in other structs
2050 // as an extension.
2051 Diag(FD->getLocation(), diag::ext_flexible_array_in_struct,
2052 FD->getName());
Fariborz Jahanian3f5faf72007-10-04 00:45:27 +00002053 if (Record)
Fariborz Jahaniane267ab62007-09-14 16:27:55 +00002054 Record->setHasFlexibleArrayMember(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002055 }
2056 }
2057 }
Fariborz Jahaniane7f64cc2007-10-12 22:10:42 +00002058 /// A field cannot be an Objective-c object
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002059 if (FDTy->isObjCInterfaceType()) {
Fariborz Jahaniane7f64cc2007-10-12 22:10:42 +00002060 Diag(FD->getLocation(), diag::err_statically_allocated_object,
2061 FD->getName());
2062 FD->setInvalidDecl();
2063 EnclosingDecl->setInvalidDecl();
2064 continue;
2065 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002066 // Keep track of the number of named members.
2067 if (IdentifierInfo *II = FD->getIdentifier()) {
2068 // Detect duplicate member names.
2069 if (!FieldIDs.insert(II)) {
2070 Diag(FD->getLocation(), diag::err_duplicate_member, II->getName());
2071 // Find the previous decl.
2072 SourceLocation PrevLoc;
2073 for (unsigned i = 0, e = RecFields.size(); ; ++i) {
2074 assert(i != e && "Didn't find previous def!");
2075 if (RecFields[i]->getIdentifier() == II) {
2076 PrevLoc = RecFields[i]->getLocation();
2077 break;
2078 }
2079 }
2080 Diag(PrevLoc, diag::err_previous_definition);
Steve Naroff74216642007-09-14 22:20:54 +00002081 FD->setInvalidDecl();
2082 EnclosingDecl->setInvalidDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +00002083 continue;
2084 }
2085 ++NumNamedMembers;
2086 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002087 }
2088
Reid Spencer5f016e22007-07-11 17:01:13 +00002089 // Okay, we successfully defined 'Record'.
Chris Lattnere1e79852008-02-06 00:51:33 +00002090 if (Record) {
Fariborz Jahaniane267ab62007-09-14 16:27:55 +00002091 Record->defineBody(&RecFields[0], RecFields.size());
Chris Lattnere1e79852008-02-06 00:51:33 +00002092 Consumer.HandleTagDeclDefinition(Record);
2093 } else {
Chris Lattnera91d3812008-02-05 22:40:55 +00002094 ObjCIvarDecl **ClsFields = reinterpret_cast<ObjCIvarDecl**>(&RecFields[0]);
2095 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(EnclosingDecl))
2096 ID->addInstanceVariablesToClass(ClsFields, RecFields.size(), RBrac);
2097 else if (ObjCImplementationDecl *IMPDecl =
2098 dyn_cast<ObjCImplementationDecl>(EnclosingDecl)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002099 assert(IMPDecl && "ActOnFields - missing ObjCImplementationDecl");
2100 IMPDecl->ObjCAddInstanceVariablesToClassImpl(ClsFields, RecFields.size());
Fariborz Jahanian3a3ca1b2007-10-31 18:48:14 +00002101 CheckImplementationIvars(IMPDecl, ClsFields, RecFields.size(), RBrac);
Fariborz Jahaniand0b90bf2007-09-26 18:27:25 +00002102 }
Fariborz Jahanianb04a0212007-09-14 21:08:27 +00002103 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002104}
2105
Steve Naroff08d92e42007-09-15 18:49:24 +00002106Sema::DeclTy *Sema::ActOnEnumConstant(Scope *S, DeclTy *theEnumDecl,
Reid Spencer5f016e22007-07-11 17:01:13 +00002107 DeclTy *lastEnumConst,
2108 SourceLocation IdLoc, IdentifierInfo *Id,
2109 SourceLocation EqualLoc, ExprTy *val) {
Chris Lattner0ed844b2008-04-04 06:12:32 +00002110 EnumDecl *TheEnumDecl = cast<EnumDecl>(static_cast<Decl*>(theEnumDecl));
Reid Spencer5f016e22007-07-11 17:01:13 +00002111 EnumConstantDecl *LastEnumConst =
2112 cast_or_null<EnumConstantDecl>(static_cast<Decl*>(lastEnumConst));
2113 Expr *Val = static_cast<Expr*>(val);
2114
Chris Lattner31e05722007-08-26 06:24:45 +00002115 // The scope passed in may not be a decl scope. Zip up the scope tree until
2116 // we find one that is.
2117 while ((S->getFlags() & Scope::DeclScope) == 0)
2118 S = S->getParent();
2119
Reid Spencer5f016e22007-07-11 17:01:13 +00002120 // Verify that there isn't already something declared with this name in this
2121 // scope.
Steve Naroffb327ce02008-04-02 14:35:35 +00002122 if (Decl *PrevDecl = LookupDecl(Id, Decl::IDNS_Ordinary, S)) {
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +00002123 if (IdResolver.isDeclInScope(PrevDecl, CurContext, S)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002124 if (isa<EnumConstantDecl>(PrevDecl))
2125 Diag(IdLoc, diag::err_redefinition_of_enumerator, Id->getName());
2126 else
2127 Diag(IdLoc, diag::err_redefinition, Id->getName());
2128 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
Chris Lattnera73349d2008-02-26 00:33:57 +00002129 delete Val;
Reid Spencer5f016e22007-07-11 17:01:13 +00002130 return 0;
2131 }
2132 }
2133
2134 llvm::APSInt EnumVal(32);
2135 QualType EltTy;
2136 if (Val) {
Chris Lattner421a23d2007-08-27 21:16:18 +00002137 // Make sure to promote the operand type to int.
2138 UsualUnaryConversions(Val);
2139
Reid Spencer5f016e22007-07-11 17:01:13 +00002140 // C99 6.7.2.2p2: Make sure we have an integer constant expression.
2141 SourceLocation ExpLoc;
Chris Lattner590b6642007-07-15 23:26:56 +00002142 if (!Val->isIntegerConstantExpr(EnumVal, Context, &ExpLoc)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002143 Diag(ExpLoc, diag::err_enum_value_not_integer_constant_expr,
2144 Id->getName());
Chris Lattnera73349d2008-02-26 00:33:57 +00002145 delete Val;
Chris Lattnerb7416f92007-08-27 17:37:24 +00002146 Val = 0; // Just forget about it.
Chris Lattnere9ca8512007-08-29 16:03:41 +00002147 } else {
2148 EltTy = Val->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +00002149 }
Chris Lattnerb7416f92007-08-27 17:37:24 +00002150 }
2151
2152 if (!Val) {
2153 if (LastEnumConst) {
2154 // Assign the last value + 1.
2155 EnumVal = LastEnumConst->getInitVal();
2156 ++EnumVal;
Chris Lattner421a23d2007-08-27 21:16:18 +00002157
2158 // Check for overflow on increment.
2159 if (EnumVal < LastEnumConst->getInitVal())
2160 Diag(IdLoc, diag::warn_enum_value_overflow);
2161
Chris Lattnerb7416f92007-08-27 17:37:24 +00002162 EltTy = LastEnumConst->getType();
2163 } else {
2164 // First value, set to zero.
2165 EltTy = Context.IntTy;
Chris Lattner98be4942008-03-05 18:54:05 +00002166 EnumVal.zextOrTrunc(static_cast<uint32_t>(Context.getTypeSize(EltTy)));
Chris Lattnerb7416f92007-08-27 17:37:24 +00002167 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002168 }
2169
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00002170 EnumConstantDecl *New =
Chris Lattner0ed844b2008-04-04 06:12:32 +00002171 EnumConstantDecl::Create(Context, TheEnumDecl, IdLoc, Id, EltTy,
2172 Val, EnumVal,
Chris Lattnerc63e6602008-03-15 21:32:50 +00002173 LastEnumConst);
Reid Spencer5f016e22007-07-11 17:01:13 +00002174
2175 // Register this decl in the current scope stack.
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +00002176 PushOnScopeChains(New, S);
Reid Spencer5f016e22007-07-11 17:01:13 +00002177 return New;
2178}
2179
Steve Naroff08d92e42007-09-15 18:49:24 +00002180void Sema::ActOnEnumBody(SourceLocation EnumLoc, DeclTy *EnumDeclX,
Reid Spencer5f016e22007-07-11 17:01:13 +00002181 DeclTy **Elements, unsigned NumElements) {
2182 EnumDecl *Enum = cast<EnumDecl>(static_cast<Decl*>(EnumDeclX));
2183 assert(!Enum->isDefinition() && "Enum redefinitions can't reach here");
2184
Chris Lattnere37f0be2007-08-28 05:10:31 +00002185 // TODO: If the result value doesn't fit in an int, it must be a long or long
2186 // long value. ISO C does not support this, but GCC does as an extension,
2187 // emit a warning.
Chris Lattner98be4942008-03-05 18:54:05 +00002188 unsigned IntWidth = Context.Target.getIntWidth();
Chris Lattnere37f0be2007-08-28 05:10:31 +00002189
Chris Lattnerac609682007-08-28 06:15:15 +00002190 // Verify that all the values are okay, compute the size of the values, and
2191 // reverse the list.
2192 unsigned NumNegativeBits = 0;
2193 unsigned NumPositiveBits = 0;
2194
2195 // Keep track of whether all elements have type int.
2196 bool AllElementsInt = true;
2197
Reid Spencer5f016e22007-07-11 17:01:13 +00002198 EnumConstantDecl *EltList = 0;
2199 for (unsigned i = 0; i != NumElements; ++i) {
2200 EnumConstantDecl *ECD =
2201 cast_or_null<EnumConstantDecl>(static_cast<Decl*>(Elements[i]));
2202 if (!ECD) continue; // Already issued a diagnostic.
Chris Lattner211a30e2007-08-28 05:27:00 +00002203
2204 // If the enum value doesn't fit in an int, emit an extension warning.
Chris Lattner211a30e2007-08-28 05:27:00 +00002205 const llvm::APSInt &InitVal = ECD->getInitVal();
Chris Lattnera73349d2008-02-26 00:33:57 +00002206 assert(InitVal.getBitWidth() >= IntWidth &&
2207 "Should have promoted value to int");
Chris Lattner211a30e2007-08-28 05:27:00 +00002208 if (InitVal.getBitWidth() > IntWidth) {
2209 llvm::APSInt V(InitVal);
2210 V.trunc(IntWidth);
2211 V.extend(InitVal.getBitWidth());
2212 if (V != InitVal)
2213 Diag(ECD->getLocation(), diag::ext_enum_value_not_int,
2214 InitVal.toString());
2215 }
Chris Lattnerac609682007-08-28 06:15:15 +00002216
2217 // Keep track of the size of positive and negative values.
Chris Lattnera73349d2008-02-26 00:33:57 +00002218 if (InitVal.isUnsigned() || InitVal.isNonNegative())
Chris Lattner21dd8212008-01-14 21:47:29 +00002219 NumPositiveBits = std::max(NumPositiveBits,
2220 (unsigned)InitVal.getActiveBits());
Chris Lattnerac609682007-08-28 06:15:15 +00002221 else
Chris Lattner21dd8212008-01-14 21:47:29 +00002222 NumNegativeBits = std::max(NumNegativeBits,
2223 (unsigned)InitVal.getMinSignedBits());
Reid Spencer5f016e22007-07-11 17:01:13 +00002224
Chris Lattnerac609682007-08-28 06:15:15 +00002225 // Keep track of whether every enum element has type int (very commmon).
2226 if (AllElementsInt)
2227 AllElementsInt = ECD->getType() == Context.IntTy;
2228
Reid Spencer5f016e22007-07-11 17:01:13 +00002229 ECD->setNextDeclarator(EltList);
2230 EltList = ECD;
2231 }
2232
Chris Lattnerac609682007-08-28 06:15:15 +00002233 // Figure out the type that should be used for this enum.
2234 // FIXME: Support attribute(packed) on enums and -fshort-enums.
2235 QualType BestType;
Chris Lattnerb7f6e082007-08-29 17:31:48 +00002236 unsigned BestWidth;
Chris Lattnerac609682007-08-28 06:15:15 +00002237
2238 if (NumNegativeBits) {
2239 // If there is a negative value, figure out the smallest integer type (of
2240 // int/long/longlong) that fits.
Chris Lattnerb7f6e082007-08-29 17:31:48 +00002241 if (NumNegativeBits <= IntWidth && NumPositiveBits < IntWidth) {
Chris Lattnerac609682007-08-28 06:15:15 +00002242 BestType = Context.IntTy;
Chris Lattnerb7f6e082007-08-29 17:31:48 +00002243 BestWidth = IntWidth;
2244 } else {
Chris Lattner98be4942008-03-05 18:54:05 +00002245 BestWidth = Context.Target.getLongWidth();
Ted Kremenek9c728dc2007-12-12 22:39:36 +00002246
Chris Lattnerb7f6e082007-08-29 17:31:48 +00002247 if (NumNegativeBits <= BestWidth && NumPositiveBits < BestWidth)
Chris Lattnerac609682007-08-28 06:15:15 +00002248 BestType = Context.LongTy;
2249 else {
Chris Lattner98be4942008-03-05 18:54:05 +00002250 BestWidth = Context.Target.getLongLongWidth();
Ted Kremenek9c728dc2007-12-12 22:39:36 +00002251
Chris Lattnerb7f6e082007-08-29 17:31:48 +00002252 if (NumNegativeBits > BestWidth || NumPositiveBits >= BestWidth)
Chris Lattnerac609682007-08-28 06:15:15 +00002253 Diag(Enum->getLocation(), diag::warn_enum_too_large);
2254 BestType = Context.LongLongTy;
2255 }
2256 }
2257 } else {
2258 // If there is no negative value, figure out which of uint, ulong, ulonglong
2259 // fits.
Chris Lattnerb7f6e082007-08-29 17:31:48 +00002260 if (NumPositiveBits <= IntWidth) {
Chris Lattnerac609682007-08-28 06:15:15 +00002261 BestType = Context.UnsignedIntTy;
Chris Lattnerb7f6e082007-08-29 17:31:48 +00002262 BestWidth = IntWidth;
2263 } else if (NumPositiveBits <=
Chris Lattner98be4942008-03-05 18:54:05 +00002264 (BestWidth = Context.Target.getLongWidth())) {
Chris Lattnerac609682007-08-28 06:15:15 +00002265 BestType = Context.UnsignedLongTy;
Chris Lattner98be4942008-03-05 18:54:05 +00002266 } else {
2267 BestWidth = Context.Target.getLongLongWidth();
Chris Lattnerb7f6e082007-08-29 17:31:48 +00002268 assert(NumPositiveBits <= BestWidth &&
Chris Lattnerac609682007-08-28 06:15:15 +00002269 "How could an initializer get larger than ULL?");
2270 BestType = Context.UnsignedLongLongTy;
2271 }
2272 }
2273
Chris Lattnerb7f6e082007-08-29 17:31:48 +00002274 // Loop over all of the enumerator constants, changing their types to match
2275 // the type of the enum if needed.
2276 for (unsigned i = 0; i != NumElements; ++i) {
2277 EnumConstantDecl *ECD =
2278 cast_or_null<EnumConstantDecl>(static_cast<Decl*>(Elements[i]));
2279 if (!ECD) continue; // Already issued a diagnostic.
2280
2281 // Standard C says the enumerators have int type, but we allow, as an
2282 // extension, the enumerators to be larger than int size. If each
2283 // enumerator value fits in an int, type it as an int, otherwise type it the
2284 // same as the enumerator decl itself. This means that in "enum { X = 1U }"
2285 // that X has type 'int', not 'unsigned'.
Chris Lattnera73349d2008-02-26 00:33:57 +00002286 if (ECD->getType() == Context.IntTy) {
2287 // Make sure the init value is signed.
2288 llvm::APSInt IV = ECD->getInitVal();
2289 IV.setIsSigned(true);
2290 ECD->setInitVal(IV);
Chris Lattnerb7f6e082007-08-29 17:31:48 +00002291 continue; // Already int type.
Chris Lattnera73349d2008-02-26 00:33:57 +00002292 }
Chris Lattnerb7f6e082007-08-29 17:31:48 +00002293
2294 // Determine whether the value fits into an int.
2295 llvm::APSInt InitVal = ECD->getInitVal();
2296 bool FitsInInt;
2297 if (InitVal.isUnsigned() || !InitVal.isNegative())
2298 FitsInInt = InitVal.getActiveBits() < IntWidth;
2299 else
2300 FitsInInt = InitVal.getMinSignedBits() <= IntWidth;
2301
2302 // If it fits into an integer type, force it. Otherwise force it to match
2303 // the enum decl type.
2304 QualType NewTy;
2305 unsigned NewWidth;
2306 bool NewSign;
2307 if (FitsInInt) {
2308 NewTy = Context.IntTy;
2309 NewWidth = IntWidth;
2310 NewSign = true;
2311 } else if (ECD->getType() == BestType) {
2312 // Already the right type!
2313 continue;
2314 } else {
2315 NewTy = BestType;
2316 NewWidth = BestWidth;
2317 NewSign = BestType->isSignedIntegerType();
2318 }
2319
2320 // Adjust the APSInt value.
2321 InitVal.extOrTrunc(NewWidth);
2322 InitVal.setIsSigned(NewSign);
2323 ECD->setInitVal(InitVal);
2324
2325 // Adjust the Expr initializer and type.
2326 ECD->setInitExpr(new ImplicitCastExpr(NewTy, ECD->getInitExpr()));
2327 ECD->setType(NewTy);
2328 }
Chris Lattnerac609682007-08-28 06:15:15 +00002329
Chris Lattnere00b18c2007-08-28 18:24:31 +00002330 Enum->defineElements(EltList, BestType);
Chris Lattnere1e79852008-02-06 00:51:33 +00002331 Consumer.HandleTagDeclDefinition(Enum);
Reid Spencer5f016e22007-07-11 17:01:13 +00002332}
2333
Anders Carlssondfab6cb2008-02-08 00:33:21 +00002334Sema::DeclTy *Sema::ActOnFileScopeAsmDecl(SourceLocation Loc,
2335 ExprTy *expr) {
2336 StringLiteral *AsmString = cast<StringLiteral>((Expr*)expr);
2337
Chris Lattner8e25d862008-03-16 00:16:02 +00002338 return FileScopeAsmDecl::Create(Context, Loc, AsmString);
Anders Carlssondfab6cb2008-02-08 00:33:21 +00002339}
2340
Chris Lattnerc6fdc342008-01-12 07:05:38 +00002341Sema::DeclTy* Sema::ActOnLinkageSpec(SourceLocation Loc,
Chris Lattnerc81c8142008-02-25 21:04:36 +00002342 SourceLocation LBrace,
2343 SourceLocation RBrace,
2344 const char *Lang,
2345 unsigned StrSize,
2346 DeclTy *D) {
Chris Lattnerc6fdc342008-01-12 07:05:38 +00002347 LinkageSpecDecl::LanguageIDs Language;
2348 Decl *dcl = static_cast<Decl *>(D);
2349 if (strncmp(Lang, "\"C\"", StrSize) == 0)
2350 Language = LinkageSpecDecl::lang_c;
2351 else if (strncmp(Lang, "\"C++\"", StrSize) == 0)
2352 Language = LinkageSpecDecl::lang_cxx;
2353 else {
2354 Diag(Loc, diag::err_bad_language);
2355 return 0;
2356 }
2357
2358 // FIXME: Add all the various semantics of linkage specifications
Chris Lattner8e25d862008-03-16 00:16:02 +00002359 return LinkageSpecDecl::Create(Context, Loc, Language, dcl);
Chris Lattnerc6fdc342008-01-12 07:05:38 +00002360}