blob: 80c37b97641ca2578f56e65204d12e88cfeaee99 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- SemaDecl.cpp - Semantic Analysis for Declarations ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for declarations.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Anders Carlssonc44eec62008-07-03 04:20:39 +000015#include "clang/AST/APValue.h"
Chris Lattnere1e79852008-02-06 00:51:33 +000016#include "clang/AST/ASTConsumer.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "clang/AST/ASTContext.h"
Anders Carlssonf78915f2008-02-15 07:04:12 +000018#include "clang/AST/Attr.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "clang/AST/Builtins.h"
20#include "clang/AST/Decl.h"
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +000021#include "clang/AST/DeclCXX.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "clang/AST/Expr.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000023#include "clang/AST/ExprCXX.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000024#include "clang/AST/Type.h"
25#include "clang/Parse/DeclSpec.h"
26#include "clang/Parse/Scope.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000027#include "clang/Basic/LangOptions.h"
28#include "clang/Basic/TargetInfo.h"
Steve Naroff4c49a6c2008-01-30 23:46:05 +000029#include "clang/Basic/SourceManager.h"
30// FIXME: layering (ideally, Sema shouldn't be dependent on Lex API's)
Chris Lattnere1e79852008-02-06 00:51:33 +000031#include "clang/Lex/Preprocessor.h"
Steve Naroff4c49a6c2008-01-30 23:46:05 +000032#include "clang/Lex/HeaderSearch.h"
Steve Naroff563477d2007-09-18 23:55:05 +000033#include "llvm/ADT/SmallString.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000034#include "llvm/ADT/SmallSet.h"
Fariborz Jahanian85ff2642007-10-05 18:00:57 +000035#include "llvm/ADT/DenseSet.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000036using namespace clang;
37
Steve Naroffb327ce02008-04-02 14:35:35 +000038Sema::DeclTy *Sema::isTypeName(const IdentifierInfo &II, Scope *S) {
39 Decl *IIDecl = LookupDecl(&II, Decl::IDNS_Ordinary, S, false);
40
Douglas Gregor2ce52f32008-04-13 21:07:44 +000041 if (IIDecl && (isa<TypedefDecl>(IIDecl) ||
42 isa<ObjCInterfaceDecl>(IIDecl) ||
43 isa<TagDecl>(IIDecl)))
Fariborz Jahanianbece4ac2007-10-12 16:34:10 +000044 return IIDecl;
Steve Naroff3536b442007-09-06 21:24:23 +000045 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000046}
47
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +000048DeclContext *Sema::getDCParent(DeclContext *DC) {
49 // If CurContext is a ObjC method, getParent() will return NULL.
50 if (isa<ObjCMethodDecl>(DC))
51 return Context.getTranslationUnitDecl();
52
53 // A C++ inline method is parsed *after* the topmost class it was declared in
54 // is fully parsed (it's "complete").
55 // The parsing of a C++ inline method happens at the declaration context of
56 // the topmost (non-nested) class it is declared in.
57 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(DC)) {
58 assert(isa<CXXRecordDecl>(MD->getParent()) && "C++ method not in Record.");
59 DC = MD->getParent();
60 while (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC->getParent()))
61 DC = RD;
62
63 // Return the declaration context of the topmost class the inline method is
64 // declared in.
65 return DC;
66 }
67
68 return DC->getParent();
69}
70
Chris Lattner9fdf9c62008-04-22 18:39:57 +000071void Sema::PushDeclContext(DeclContext *DC) {
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +000072 assert(getDCParent(DC) == CurContext &&
73 "The next DeclContext should be directly contained in the current one.");
Chris Lattner9fdf9c62008-04-22 18:39:57 +000074 CurContext = DC;
Chris Lattner0ed844b2008-04-04 06:12:32 +000075}
76
Chris Lattnerb048c982008-04-06 04:47:34 +000077void Sema::PopDeclContext() {
78 assert(CurContext && "DeclContext imbalance!");
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +000079 CurContext = getDCParent(CurContext);
Chris Lattner0ed844b2008-04-04 06:12:32 +000080}
81
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +000082/// Add this decl to the scope shadowed decl chains.
83void Sema::PushOnScopeChains(NamedDecl *D, Scope *S) {
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +000084 S->AddDecl(D);
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000085
86 // C++ [basic.scope]p4:
87 // -- exactly one declaration shall declare a class name or
88 // enumeration name that is not a typedef name and the other
89 // declarations shall all refer to the same object or
90 // enumerator, or all refer to functions and function templates;
91 // in this case the class name or enumeration name is hidden.
92 if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
93 // We are pushing the name of a tag (enum or class).
94 IdentifierResolver::ctx_iterator
95 CIT = IdResolver.ctx_begin(TD->getIdentifier(), TD->getDeclContext());
96 if (CIT != IdResolver.ctx_end(TD->getIdentifier()) &&
97 IdResolver.isDeclInScope(*CIT, TD->getDeclContext(), S)) {
98 // There is already a declaration with the same name in the same
99 // scope. It must be found before we find the new declaration,
100 // so swap the order on the shadowed declaration chain.
101
102 IdResolver.AddShadowedDecl(TD, *CIT);
103 return;
104 }
105 }
106
107 IdResolver.AddDecl(D);
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +0000108}
109
Steve Naroffb216c882007-10-09 22:01:59 +0000110void Sema::ActOnPopScope(SourceLocation Loc, Scope *S) {
Chris Lattner31e05722007-08-26 06:24:45 +0000111 if (S->decl_empty()) return;
112 assert((S->getFlags() & Scope::DeclScope) &&"Scope shouldn't contain decls!");
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000113
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
115 I != E; ++I) {
Steve Naroffc752d042007-09-13 18:10:37 +0000116 Decl *TmpD = static_cast<Decl*>(*I);
117 assert(TmpD && "This decl didn't get pushed??");
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +0000118
119 if (isa<CXXFieldDecl>(TmpD)) continue;
120
121 assert(isa<ScopedDecl>(TmpD) && "Decl isn't ScopedDecl?");
122 ScopedDecl *D = cast<ScopedDecl>(TmpD);
Steve Naroffc752d042007-09-13 18:10:37 +0000123
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 IdentifierInfo *II = D->getIdentifier();
125 if (!II) continue;
126
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +0000127 // We only want to remove the decls from the identifier decl chains for local
128 // scopes, when inside a function/method.
129 if (S->getFnParent() != 0)
130 IdResolver.RemoveDecl(D);
Chris Lattner7f925cc2008-04-11 07:00:53 +0000131
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +0000132 // Chain this decl to the containing DeclContext.
133 D->setNext(CurContext->getDeclChain());
134 CurContext->setDeclChain(D);
Reid Spencer5f016e22007-07-11 17:01:13 +0000135 }
136}
137
Steve Naroffe8043c32008-04-01 23:04:06 +0000138/// getObjCInterfaceDecl - Look up a for a class declaration in the scope.
139/// return 0 if one not found.
Steve Naroffe8043c32008-04-01 23:04:06 +0000140ObjCInterfaceDecl *Sema::getObjCInterfaceDecl(IdentifierInfo *Id) {
Steve Naroff31102512008-04-02 18:30:49 +0000141 // The third "scope" argument is 0 since we aren't enabling lazy built-in
142 // creation from this context.
143 Decl *IDecl = LookupDecl(Id, Decl::IDNS_Ordinary, 0, false);
Fariborz Jahanian4cabdfc2007-10-12 19:38:20 +0000144
Steve Naroffb327ce02008-04-02 14:35:35 +0000145 return dyn_cast_or_null<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanian4cabdfc2007-10-12 19:38:20 +0000146}
147
Steve Naroffe8043c32008-04-01 23:04:06 +0000148/// LookupDecl - Look up the inner-most declaration in the specified
Reid Spencer5f016e22007-07-11 17:01:13 +0000149/// namespace.
Steve Naroffb327ce02008-04-02 14:35:35 +0000150Decl *Sema::LookupDecl(const IdentifierInfo *II, unsigned NSI,
151 Scope *S, bool enableLazyBuiltinCreation) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 if (II == 0) return 0;
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000153 unsigned NS = NSI;
154 if (getLangOptions().CPlusPlus && (NS & Decl::IDNS_Ordinary))
155 NS |= Decl::IDNS_Tag;
Chris Lattner7f925cc2008-04-11 07:00:53 +0000156
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 // Scan up the scope chain looking for a decl that matches this identifier
158 // that is in the appropriate namespace. This search should not take long, as
159 // shadowing of names is uncommon, and deep shadowing is extremely uncommon.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000160 for (IdentifierResolver::iterator
161 I = IdResolver.begin(II, CurContext), E = IdResolver.end(II); I != E; ++I)
162 if ((*I)->getIdentifierNamespace() & NS)
163 return *I;
Chris Lattner7f925cc2008-04-11 07:00:53 +0000164
Reid Spencer5f016e22007-07-11 17:01:13 +0000165 // If we didn't find a use of this identifier, and if the identifier
166 // corresponds to a compiler builtin, create the decl object for the builtin
167 // now, injecting it into translation unit scope, and return it.
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000168 if (NS & Decl::IDNS_Ordinary) {
Steve Naroffb327ce02008-04-02 14:35:35 +0000169 if (enableLazyBuiltinCreation) {
170 // If this is a builtin on this (or all) targets, create the decl.
171 if (unsigned BuiltinID = II->getBuiltinID())
172 return LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID, S);
173 }
Steve Naroffe8043c32008-04-01 23:04:06 +0000174 if (getLangOptions().ObjC1) {
175 // @interface and @compatibility_alias introduce typedef-like names.
176 // Unlike typedef's, they can only be introduced at file-scope (and are
Steve Naroffc822ff42008-04-02 00:39:51 +0000177 // therefore not scoped decls). They can, however, be shadowed by
Steve Naroffe8043c32008-04-01 23:04:06 +0000178 // other names in IDNS_Ordinary.
Steve Naroff31102512008-04-02 18:30:49 +0000179 ObjCInterfaceDeclsTy::iterator IDI = ObjCInterfaceDecls.find(II);
180 if (IDI != ObjCInterfaceDecls.end())
181 return IDI->second;
Steve Naroffe8043c32008-04-01 23:04:06 +0000182 ObjCAliasTy::iterator I = ObjCAliasDecls.find(II);
183 if (I != ObjCAliasDecls.end())
184 return I->second->getClassInterface();
185 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000186 }
187 return 0;
188}
189
Chris Lattner95e2c712008-05-05 22:18:14 +0000190void Sema::InitBuiltinVaListType() {
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000191 if (!Context.getBuiltinVaListType().isNull())
192 return;
193
194 IdentifierInfo *VaIdent = &Context.Idents.get("__builtin_va_list");
Steve Naroffb327ce02008-04-02 14:35:35 +0000195 Decl *VaDecl = LookupDecl(VaIdent, Decl::IDNS_Ordinary, TUScope);
Steve Naroff733002f2007-10-18 22:17:45 +0000196 TypedefDecl *VaTypedef = cast<TypedefDecl>(VaDecl);
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000197 Context.setBuiltinVaListType(Context.getTypedefType(VaTypedef));
198}
199
Reid Spencer5f016e22007-07-11 17:01:13 +0000200/// LazilyCreateBuiltin - The specified Builtin-ID was first used at file scope.
201/// lazily create a decl for it.
Chris Lattner22b73ba2007-10-10 23:42:28 +0000202ScopedDecl *Sema::LazilyCreateBuiltin(IdentifierInfo *II, unsigned bid,
203 Scope *S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000204 Builtin::ID BID = (Builtin::ID)bid;
205
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000206 if (BID == Builtin::BI__builtin_va_start ||
Chris Lattner95e2c712008-05-05 22:18:14 +0000207 BID == Builtin::BI__builtin_va_copy ||
208 BID == Builtin::BI__builtin_va_end)
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000209 InitBuiltinVaListType();
210
Anders Carlssonb2cf3572007-10-11 01:00:40 +0000211 QualType R = Context.BuiltinInfo.GetBuiltinType(BID, Context);
Argyrios Kyrtzidisff898cd2008-04-17 14:47:13 +0000212 FunctionDecl *New = FunctionDecl::Create(Context,
213 Context.getTranslationUnitDecl(),
Chris Lattner0ed844b2008-04-04 06:12:32 +0000214 SourceLocation(), II, R,
Chris Lattnera98e58d2008-03-15 21:24:04 +0000215 FunctionDecl::Extern, false, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000216
Chris Lattner95e2c712008-05-05 22:18:14 +0000217 // Create Decl objects for each parameter, adding them to the
218 // FunctionDecl.
219 if (FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(R)) {
220 llvm::SmallVector<ParmVarDecl*, 16> Params;
221 for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i)
222 Params.push_back(ParmVarDecl::Create(Context, New, SourceLocation(), 0,
223 FT->getArgType(i), VarDecl::None, 0,
224 0));
225 New->setParams(&Params[0], Params.size());
226 }
227
228
229
Chris Lattner7f925cc2008-04-11 07:00:53 +0000230 // TUScope is the translation-unit scope to insert this function into.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000231 PushOnScopeChains(New, TUScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000232 return New;
233}
234
235/// MergeTypeDefDecl - We just parsed a typedef 'New' which has the same name
236/// and scope as a previous declaration 'Old'. Figure out how to resolve this
237/// situation, merging decls or emitting diagnostics as appropriate.
238///
Steve Naroffe8043c32008-04-01 23:04:06 +0000239TypedefDecl *Sema::MergeTypeDefDecl(TypedefDecl *New, Decl *OldD) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000240 // Verify the old decl was also a typedef.
241 TypedefDecl *Old = dyn_cast<TypedefDecl>(OldD);
242 if (!Old) {
243 Diag(New->getLocation(), diag::err_redefinition_different_kind,
244 New->getName());
245 Diag(OldD->getLocation(), diag::err_previous_definition);
246 return New;
247 }
248
Steve Naroff8ee529b2007-10-31 18:42:27 +0000249 // Allow multiple definitions for ObjC built-in typedefs.
250 // FIXME: Verify the underlying types are equivalent!
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000251 if (getLangOptions().ObjC1 && isBuiltinObjCType(New))
Steve Naroff8ee529b2007-10-31 18:42:27 +0000252 return Old;
Eli Friedman54ecfce2008-06-11 06:20:39 +0000253
254 if (getLangOptions().Microsoft) return New;
255
Steve Naroff4c49a6c2008-01-30 23:46:05 +0000256 // Redeclaration of a type is a constraint violation (6.7.2.3p1).
257 // Apparently GCC, Intel, and Sun all silently ignore the redeclaration if
258 // *either* declaration is in a system header. The code below implements
259 // this adhoc compatibility rule. FIXME: The following code will not
260 // work properly when compiling ".i" files (containing preprocessed output).
261 SourceManager &SrcMgr = Context.getSourceManager();
Steve Naroff4c49a6c2008-01-30 23:46:05 +0000262 HeaderSearch &HdrInfo = PP.getHeaderSearchInfo();
Eli Friedman54ecfce2008-06-11 06:20:39 +0000263 const FileEntry *OldDeclFile = SrcMgr.getFileEntryForLoc(Old->getLocation());
264 if (OldDeclFile) {
265 DirectoryLookup::DirType OldDirType = HdrInfo.getFileDirFlavor(OldDeclFile);
266 // Allow reclarations in both SystemHeaderDir and ExternCSystemHeaderDir.
267 if (OldDirType != DirectoryLookup::NormalHeaderDir)
268 return New;
269 }
270 const FileEntry *NewDeclFile = SrcMgr.getFileEntryForLoc(New->getLocation());
271 if (NewDeclFile) {
272 DirectoryLookup::DirType NewDirType = HdrInfo.getFileDirFlavor(NewDeclFile);
273 // Allow reclarations in both SystemHeaderDir and ExternCSystemHeaderDir.
274 if (NewDirType != DirectoryLookup::NormalHeaderDir)
275 return New;
276 }
277
Ted Kremenek2d05c082008-05-23 21:28:18 +0000278 Diag(New->getLocation(), diag::err_redefinition, New->getName());
279 Diag(Old->getLocation(), diag::err_previous_definition);
Reid Spencer5f016e22007-07-11 17:01:13 +0000280 return New;
281}
282
Chris Lattner6b6b5372008-06-26 18:38:35 +0000283/// DeclhasAttr - returns true if decl Declaration already has the target
284/// attribute.
Chris Lattnerddee4232008-03-03 03:28:21 +0000285static bool DeclHasAttr(const Decl *decl, const Attr *target) {
286 for (const Attr *attr = decl->getAttrs(); attr; attr = attr->getNext())
287 if (attr->getKind() == target->getKind())
288 return true;
289
290 return false;
291}
292
293/// MergeAttributes - append attributes from the Old decl to the New one.
294static void MergeAttributes(Decl *New, Decl *Old) {
295 Attr *attr = const_cast<Attr*>(Old->getAttrs()), *tmp;
296
Chris Lattnerddee4232008-03-03 03:28:21 +0000297 while (attr) {
298 tmp = attr;
299 attr = attr->getNext();
300
301 if (!DeclHasAttr(New, tmp)) {
302 New->addAttr(tmp);
303 } else {
304 tmp->setNext(0);
305 delete(tmp);
306 }
307 }
Nuno Lopes9141bee2008-06-01 22:53:53 +0000308
309 Old->invalidateAttrs();
Chris Lattnerddee4232008-03-03 03:28:21 +0000310}
311
Chris Lattner04421082008-04-08 04:40:51 +0000312/// MergeFunctionDecl - We just parsed a function 'New' from
313/// declarator D which has the same name and scope as a previous
314/// declaration 'Old'. Figure out how to resolve this situation,
315/// merging decls or emitting diagnostics as appropriate.
Douglas Gregorf0097952008-04-21 02:02:58 +0000316/// Redeclaration will be set true if thisNew is a redeclaration OldD.
317FunctionDecl *
318Sema::MergeFunctionDecl(FunctionDecl *New, Decl *OldD, bool &Redeclaration) {
319 Redeclaration = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000320 // Verify the old decl was also a function.
321 FunctionDecl *Old = dyn_cast<FunctionDecl>(OldD);
322 if (!Old) {
323 Diag(New->getLocation(), diag::err_redefinition_different_kind,
324 New->getName());
325 Diag(OldD->getLocation(), diag::err_previous_definition);
326 return New;
327 }
Chris Lattner04421082008-04-08 04:40:51 +0000328
Chris Lattner8bcfc5b2008-04-06 23:10:54 +0000329 QualType OldQType = Context.getCanonicalType(Old->getType());
330 QualType NewQType = Context.getCanonicalType(New->getType());
Chris Lattner55196442007-11-20 19:04:50 +0000331
Chris Lattner04421082008-04-08 04:40:51 +0000332 // C++ [dcl.fct]p3:
333 // All declarations for a function shall agree exactly in both the
334 // return type and the parameter-type-list.
Douglas Gregorf0097952008-04-21 02:02:58 +0000335 if (getLangOptions().CPlusPlus && OldQType == NewQType) {
336 MergeAttributes(New, Old);
337 Redeclaration = true;
Chris Lattner04421082008-04-08 04:40:51 +0000338 return MergeCXXFunctionDecl(New, Old);
Douglas Gregorf0097952008-04-21 02:02:58 +0000339 }
Chris Lattner04421082008-04-08 04:40:51 +0000340
341 // C: Function types need to be compatible, not identical. This handles
Steve Naroffadbbd0c2008-01-14 20:51:29 +0000342 // duplicate function decls like "void f(int); void f(enum X);" properly.
Chris Lattner04421082008-04-08 04:40:51 +0000343 if (!getLangOptions().CPlusPlus &&
344 Context.functionTypesAreCompatible(OldQType, NewQType)) {
Douglas Gregorf0097952008-04-21 02:02:58 +0000345 MergeAttributes(New, Old);
346 Redeclaration = true;
Steve Naroffadbbd0c2008-01-14 20:51:29 +0000347 return New;
Chris Lattner04421082008-04-08 04:40:51 +0000348 }
Chris Lattnere3995fe2007-11-06 06:07:26 +0000349
Steve Naroff837618c2008-01-16 15:01:34 +0000350 // A function that has already been declared has been redeclared or defined
351 // with a different type- show appropriate diagnostic
Steve Naroffe2ef8152008-04-04 14:32:09 +0000352 diag::kind PrevDiag;
Douglas Gregorf0097952008-04-21 02:02:58 +0000353 if (Old->isThisDeclarationADefinition())
Steve Naroffe2ef8152008-04-04 14:32:09 +0000354 PrevDiag = diag::err_previous_definition;
355 else if (Old->isImplicit())
356 PrevDiag = diag::err_previous_implicit_declaration;
Chris Lattner04421082008-04-08 04:40:51 +0000357 else
Steve Naroffe2ef8152008-04-04 14:32:09 +0000358 PrevDiag = diag::err_previous_declaration;
Steve Naroff837618c2008-01-16 15:01:34 +0000359
Reid Spencer5f016e22007-07-11 17:01:13 +0000360 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
361 // TODO: This is totally simplistic. It should handle merging functions
362 // together etc, merging extern int X; int X; ...
Steve Naroff837618c2008-01-16 15:01:34 +0000363 Diag(New->getLocation(), diag::err_conflicting_types, New->getName());
364 Diag(Old->getLocation(), PrevDiag);
Reid Spencer5f016e22007-07-11 17:01:13 +0000365 return New;
366}
367
Chris Lattnerfcc2d262007-11-06 04:28:31 +0000368/// equivalentArrayTypes - Used to determine whether two array types are
369/// equivalent.
370/// We need to check this explicitly as an incomplete array definition is
371/// considered a VariableArrayType, so will not match a complete array
372/// definition that would be otherwise equivalent.
373static bool areEquivalentArrayTypes(QualType NewQType, QualType OldQType) {
374 const ArrayType *NewAT = NewQType->getAsArrayType();
375 const ArrayType *OldAT = OldQType->getAsArrayType();
376
377 if (!NewAT || !OldAT)
378 return false;
379
380 // If either (or both) array types in incomplete we need to strip off the
381 // outer VariableArrayType. Once the outer VAT is removed the remaining
382 // types must be identical if the array types are to be considered
383 // equivalent.
384 // eg. int[][1] and int[1][1] become
385 // VAT(null, CAT(1, int)) and CAT(1, CAT(1, int))
386 // removing the outermost VAT gives
387 // CAT(1, int) and CAT(1, int)
388 // which are equal, therefore the array types are equivalent.
Eli Friedman9db13972008-02-15 12:53:51 +0000389 if (NewAT->isIncompleteArrayType() || OldAT->isIncompleteArrayType()) {
Chris Lattnerfcc2d262007-11-06 04:28:31 +0000390 if (NewAT->getIndexTypeQualifier() != OldAT->getIndexTypeQualifier())
391 return false;
Eli Friedman04930252008-01-29 07:51:12 +0000392 NewQType = NewAT->getElementType().getCanonicalType();
393 OldQType = OldAT->getElementType().getCanonicalType();
Chris Lattnerfcc2d262007-11-06 04:28:31 +0000394 }
395
396 return NewQType == OldQType;
397}
398
Reid Spencer5f016e22007-07-11 17:01:13 +0000399/// MergeVarDecl - We just parsed a variable 'New' which has the same name
400/// and scope as a previous declaration 'Old'. Figure out how to resolve this
401/// situation, merging decls or emitting diagnostics as appropriate.
402///
403/// FIXME: Need to carefully consider tentative definition rules (C99 6.9.2p2).
404/// For example, we incorrectly complain about i1, i4 from C99 6.9.2p4.
405///
Steve Naroffe8043c32008-04-01 23:04:06 +0000406VarDecl *Sema::MergeVarDecl(VarDecl *New, Decl *OldD) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000407 // Verify the old decl was also a variable.
408 VarDecl *Old = dyn_cast<VarDecl>(OldD);
409 if (!Old) {
410 Diag(New->getLocation(), diag::err_redefinition_different_kind,
411 New->getName());
412 Diag(OldD->getLocation(), diag::err_previous_definition);
413 return New;
414 }
Chris Lattnerddee4232008-03-03 03:28:21 +0000415
416 MergeAttributes(New, Old);
417
Reid Spencer5f016e22007-07-11 17:01:13 +0000418 // Verify the types match.
Chris Lattner8bcfc5b2008-04-06 23:10:54 +0000419 QualType OldCType = Context.getCanonicalType(Old->getType());
420 QualType NewCType = Context.getCanonicalType(New->getType());
421 if (OldCType != NewCType && !areEquivalentArrayTypes(NewCType, OldCType)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000422 Diag(New->getLocation(), diag::err_redefinition, New->getName());
423 Diag(Old->getLocation(), diag::err_previous_definition);
424 return New;
425 }
Steve Naroffb7b032e2008-01-30 00:44:01 +0000426 // C99 6.2.2p4: Check if we have a static decl followed by a non-static.
427 if (New->getStorageClass() == VarDecl::Static &&
428 (Old->getStorageClass() == VarDecl::None ||
429 Old->getStorageClass() == VarDecl::Extern)) {
430 Diag(New->getLocation(), diag::err_static_non_static, New->getName());
431 Diag(Old->getLocation(), diag::err_previous_definition);
432 return New;
433 }
434 // C99 6.2.2p4: Check if we have a non-static decl followed by a static.
435 if (New->getStorageClass() != VarDecl::Static &&
436 Old->getStorageClass() == VarDecl::Static) {
437 Diag(New->getLocation(), diag::err_non_static_static, New->getName());
438 Diag(Old->getLocation(), diag::err_previous_definition);
439 return New;
440 }
441 // We've verified the types match, now handle "tentative" definitions.
Steve Naroff248a7532008-04-15 22:42:06 +0000442 if (Old->isFileVarDecl() && New->isFileVarDecl()) {
Steve Naroffb7b032e2008-01-30 00:44:01 +0000443 // Handle C "tentative" external object definitions (C99 6.9.2).
444 bool OldIsTentative = false;
445 bool NewIsTentative = false;
446
Steve Naroff248a7532008-04-15 22:42:06 +0000447 if (!Old->getInit() &&
448 (Old->getStorageClass() == VarDecl::None ||
449 Old->getStorageClass() == VarDecl::Static))
Steve Naroffb7b032e2008-01-30 00:44:01 +0000450 OldIsTentative = true;
451
452 // FIXME: this check doesn't work (since the initializer hasn't been
453 // attached yet). This check should be moved to FinalizeDeclaratorGroup.
454 // Unfortunately, by the time we get to FinializeDeclaratorGroup, we've
455 // thrown out the old decl.
Steve Naroff248a7532008-04-15 22:42:06 +0000456 if (!New->getInit() &&
457 (New->getStorageClass() == VarDecl::None ||
458 New->getStorageClass() == VarDecl::Static))
Steve Naroffb7b032e2008-01-30 00:44:01 +0000459 ; // change to NewIsTentative = true; once the code is moved.
460
461 if (NewIsTentative || OldIsTentative)
462 return New;
463 }
Steve Naroff235549c2008-05-12 22:36:43 +0000464 // Handle __private_extern__ just like extern.
Steve Naroffb7b032e2008-01-30 00:44:01 +0000465 if (Old->getStorageClass() != VarDecl::Extern &&
Steve Naroff235549c2008-05-12 22:36:43 +0000466 Old->getStorageClass() != VarDecl::PrivateExtern &&
467 New->getStorageClass() != VarDecl::Extern &&
468 New->getStorageClass() != VarDecl::PrivateExtern) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000469 Diag(New->getLocation(), diag::err_redefinition, New->getName());
470 Diag(Old->getLocation(), diag::err_previous_definition);
471 }
472 return New;
473}
474
Chris Lattner04421082008-04-08 04:40:51 +0000475/// CheckParmsForFunctionDef - Check that the parameters of the given
476/// function are appropriate for the definition of a function. This
477/// takes care of any checks that cannot be performed on the
478/// declaration itself, e.g., that the types of each of the function
479/// parameters are complete.
480bool Sema::CheckParmsForFunctionDef(FunctionDecl *FD) {
481 bool HasInvalidParm = false;
482 for (unsigned p = 0, NumParams = FD->getNumParams(); p < NumParams; ++p) {
483 ParmVarDecl *Param = FD->getParamDecl(p);
484
485 // C99 6.7.5.3p4: the parameters in a parameter type list in a
486 // function declarator that is part of a function definition of
487 // that function shall not have incomplete type.
488 if (Param->getType()->isIncompleteType() &&
489 !Param->isInvalidDecl()) {
490 Diag(Param->getLocation(), diag::err_typecheck_decl_incomplete_type,
491 Param->getType().getAsString());
492 Param->setInvalidDecl();
493 HasInvalidParm = true;
494 }
495 }
496
497 return HasInvalidParm;
498}
499
500/// CreateImplicitParameter - Creates an implicit function parameter
501/// in the scope S and with the given type. This routine is used, for
502/// example, to create the implicit "self" parameter in an Objective-C
503/// method.
Chris Lattner41110242008-06-17 18:05:57 +0000504ImplicitParamDecl *
Chris Lattner04421082008-04-08 04:40:51 +0000505Sema::CreateImplicitParameter(Scope *S, IdentifierInfo *Id,
506 SourceLocation IdLoc, QualType Type) {
Chris Lattner41110242008-06-17 18:05:57 +0000507 ImplicitParamDecl *New = ImplicitParamDecl::Create(Context, CurContext,
508 IdLoc, Id, Type, 0);
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +0000509 if (Id)
510 PushOnScopeChains(New, S);
Chris Lattner04421082008-04-08 04:40:51 +0000511
512 return New;
513}
514
Reid Spencer5f016e22007-07-11 17:01:13 +0000515/// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with
516/// no declarator (e.g. "struct foo;") is parsed.
517Sema::DeclTy *Sema::ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS) {
518 // TODO: emit error on 'int;' or 'const enum foo;'.
519 // TODO: emit error on 'typedef int;'
520 // if (!DS.isMissingDeclaratorOk()) Diag(...);
521
Steve Naroff92199282007-11-17 21:37:36 +0000522 return dyn_cast_or_null<TagDecl>(static_cast<Decl *>(DS.getTypeRep()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000523}
524
Steve Naroffd0091aa2008-01-10 22:15:12 +0000525bool Sema::CheckSingleInitializer(Expr *&Init, QualType DeclType) {
Steve Narofff0090632007-09-02 02:04:30 +0000526 // Get the type before calling CheckSingleAssignmentConstraints(), since
527 // it can promote the expression.
Chris Lattner5cf216b2008-01-04 18:04:52 +0000528 QualType InitType = Init->getType();
Steve Narofff0090632007-09-02 02:04:30 +0000529
Chris Lattner5cf216b2008-01-04 18:04:52 +0000530 AssignConvertType ConvTy = CheckSingleAssignmentConstraints(DeclType, Init);
531 return DiagnoseAssignmentResult(ConvTy, Init->getLocStart(), DeclType,
532 InitType, Init, "initializing");
Steve Narofff0090632007-09-02 02:04:30 +0000533}
534
Steve Naroffa49e1fa2008-01-22 00:55:40 +0000535bool Sema::CheckStringLiteralInit(StringLiteral *strLiteral, QualType &DeclT) {
Eli Friedmanc5773c42008-02-15 18:16:39 +0000536 if (const IncompleteArrayType *IAT = DeclT->getAsIncompleteArrayType()) {
Steve Naroffa49e1fa2008-01-22 00:55:40 +0000537 // C99 6.7.8p14. We have an array of character type with unknown size
538 // being initialized to a string literal.
539 llvm::APSInt ConstVal(32);
540 ConstVal = strLiteral->getByteLength() + 1;
541 // Return a new array type (C99 6.7.8p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000542 DeclT = Context.getConstantArrayType(IAT->getElementType(), ConstVal,
Steve Naroffa49e1fa2008-01-22 00:55:40 +0000543 ArrayType::Normal, 0);
544 } else if (const ConstantArrayType *CAT = DeclT->getAsConstantArrayType()) {
545 // C99 6.7.8p14. We have an array of character type with known size.
546 if (strLiteral->getByteLength() > (unsigned)CAT->getMaximumElements())
547 Diag(strLiteral->getSourceRange().getBegin(),
548 diag::warn_initializer_string_for_char_array_too_long,
549 strLiteral->getSourceRange());
550 } else {
551 assert(0 && "HandleStringLiteralInit(): Invalid array type");
552 }
553 // Set type from "char *" to "constant array of char".
554 strLiteral->setType(DeclT);
555 // For now, we always return false (meaning success).
556 return false;
557}
558
559StringLiteral *Sema::IsStringLiteralInit(Expr *Init, QualType DeclType) {
Steve Naroffa49e1fa2008-01-22 00:55:40 +0000560 const ArrayType *AT = DeclType->getAsArrayType();
Steve Naroffa9960332008-01-25 00:51:06 +0000561 if (AT && AT->getElementType()->isCharType()) {
562 return dyn_cast<StringLiteral>(Init);
563 }
Steve Naroffa49e1fa2008-01-22 00:55:40 +0000564 return 0;
565}
566
Steve Naroffa9960332008-01-25 00:51:06 +0000567bool Sema::CheckInitializerTypes(Expr *&Init, QualType &DeclType) {
Steve Naroffca107302008-01-21 23:53:58 +0000568 // C99 6.7.8p3: The type of the entity to be initialized shall be an array
569 // of unknown size ("[]") or an object type that is not a variable array type.
Eli Friedmanc5773c42008-02-15 18:16:39 +0000570 if (const VariableArrayType *VAT = DeclType->getAsVariableArrayType())
Steve Naroffca107302008-01-21 23:53:58 +0000571 return Diag(VAT->getSizeExpr()->getLocStart(),
572 diag::err_variable_object_no_init,
573 VAT->getSizeExpr()->getSourceRange());
574
Steve Naroff2fdc3742007-12-10 22:44:33 +0000575 InitListExpr *InitList = dyn_cast<InitListExpr>(Init);
576 if (!InitList) {
Steve Naroffa49e1fa2008-01-22 00:55:40 +0000577 // FIXME: Handle wide strings
578 if (StringLiteral *strLiteral = IsStringLiteralInit(Init, DeclType))
579 return CheckStringLiteralInit(strLiteral, DeclType);
Eli Friedmana312ce22008-02-08 00:48:24 +0000580
581 if (DeclType->isArrayType())
582 return Diag(Init->getLocStart(),
583 diag::err_array_init_list_required,
584 Init->getSourceRange());
585
Steve Naroffd0091aa2008-01-10 22:15:12 +0000586 return CheckSingleInitializer(Init, DeclType);
Steve Naroff2fdc3742007-12-10 22:44:33 +0000587 }
Eli Friedmane6f058f2008-06-06 19:40:52 +0000588
Steve Naroff0cca7492008-05-01 22:18:59 +0000589 InitListChecker CheckInitList(this, InitList, DeclType);
590 return CheckInitList.HadError();
Steve Narofff0090632007-09-02 02:04:30 +0000591}
592
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000593Sema::DeclTy *
Steve Naroff08d92e42007-09-15 18:49:24 +0000594Sema::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *lastDecl) {
Steve Naroff94745042007-09-13 23:52:58 +0000595 ScopedDecl *LastDeclarator = dyn_cast_or_null<ScopedDecl>((Decl *)lastDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000596 IdentifierInfo *II = D.getIdentifier();
597
Chris Lattnere80a59c2007-07-25 00:24:17 +0000598 // All of these full declarators require an identifier. If it doesn't have
599 // one, the ParsedFreeStandingDeclSpec action should be used.
600 if (II == 0) {
Chris Lattner311ff022007-10-16 22:36:42 +0000601 Diag(D.getDeclSpec().getSourceRange().getBegin(),
Chris Lattner98e08632007-08-28 06:17:15 +0000602 diag::err_declarator_need_ident,
Chris Lattnere80a59c2007-07-25 00:24:17 +0000603 D.getDeclSpec().getSourceRange(), D.getSourceRange());
604 return 0;
605 }
606
Chris Lattner31e05722007-08-26 06:24:45 +0000607 // The scope passed in may not be a decl scope. Zip up the scope tree until
608 // we find one that is.
609 while ((S->getFlags() & Scope::DeclScope) == 0)
610 S = S->getParent();
611
Reid Spencer5f016e22007-07-11 17:01:13 +0000612 // See if this is a redefinition of a variable in the same scope.
Steve Naroffb327ce02008-04-02 14:35:35 +0000613 Decl *PrevDecl = LookupDecl(II, Decl::IDNS_Ordinary, S);
Steve Naroffc752d042007-09-13 18:10:37 +0000614 ScopedDecl *New;
Steve Naroff5912a352007-08-28 20:14:24 +0000615 bool InvalidDecl = false;
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000616
617 // In C++, the previous declaration we find might be a tag type
618 // (class or enum). In this case, the new declaration will hide the
619 // tag type.
620 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
621 PrevDecl = 0;
622
Chris Lattner41af0932007-11-14 06:34:38 +0000623 QualType R = GetTypeForDeclarator(D, S);
624 assert(!R.isNull() && "GetTypeForDeclarator() returned null type");
625
Reid Spencer5f016e22007-07-11 17:01:13 +0000626 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
Douglas Gregor6d6eb572008-05-07 04:49:29 +0000627 // Check that there are no default arguments (C++ only).
628 if (getLangOptions().CPlusPlus)
629 CheckExtraCXXDefaultArguments(D);
630
Chris Lattner41af0932007-11-14 06:34:38 +0000631 TypedefDecl *NewTD = ParseTypedefDecl(S, D, R, LastDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +0000632 if (!NewTD) return 0;
633
634 // Handle attributes prior to checking for duplicates in MergeVarDecl
Chris Lattner3ff30c82008-06-29 00:02:00 +0000635 ProcessDeclAttributes(NewTD, D);
Steve Naroffffce4d52008-01-09 23:34:55 +0000636 // Merge the decl with the existing one if appropriate. If the decl is
637 // in an outer scope, it isn't the same thing.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000638 if (PrevDecl && IdResolver.isDeclInScope(PrevDecl, CurContext, S)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000639 NewTD = MergeTypeDefDecl(NewTD, PrevDecl);
640 if (NewTD == 0) return 0;
641 }
642 New = NewTD;
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000643 if (S->getFnParent() == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000644 // C99 6.7.7p2: If a typedef name specifies a variably modified type
645 // then it shall have block scope.
Eli Friedman9db13972008-02-15 12:53:51 +0000646 if (NewTD->getUnderlyingType()->isVariablyModifiedType()) {
647 // FIXME: Diagnostic needs to be fixed.
648 Diag(D.getIdentifierLoc(), diag::err_typecheck_illegal_vla);
Steve Naroffd7444aa2007-08-31 17:20:07 +0000649 InvalidDecl = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000650 }
651 }
Chris Lattner41af0932007-11-14 06:34:38 +0000652 } else if (R.getTypePtr()->isFunctionType()) {
Chris Lattner271f1a62007-09-27 15:15:46 +0000653 FunctionDecl::StorageClass SC = FunctionDecl::None;
Reid Spencer5f016e22007-07-11 17:01:13 +0000654 switch (D.getDeclSpec().getStorageClassSpec()) {
655 default: assert(0 && "Unknown storage class!");
656 case DeclSpec::SCS_auto:
657 case DeclSpec::SCS_register:
658 Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_func,
659 R.getAsString());
Steve Naroff5912a352007-08-28 20:14:24 +0000660 InvalidDecl = true;
661 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000662 case DeclSpec::SCS_unspecified: SC = FunctionDecl::None; break;
663 case DeclSpec::SCS_extern: SC = FunctionDecl::Extern; break;
664 case DeclSpec::SCS_static: SC = FunctionDecl::Static; break;
Steve Naroff7dd0bd42008-01-28 21:57:15 +0000665 case DeclSpec::SCS_private_extern: SC = FunctionDecl::PrivateExtern;break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000666 }
667
Chris Lattnera98e58d2008-03-15 21:24:04 +0000668 bool isInline = D.getDeclSpec().isInlineSpecified();
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000669 FunctionDecl *NewFD;
670 if (D.getContext() == Declarator::MemberContext) {
671 // This is a C++ method declaration.
672 NewFD = CXXMethodDecl::Create(Context, cast<CXXRecordDecl>(CurContext),
673 D.getIdentifierLoc(), II, R,
674 (SC == FunctionDecl::Static), isInline,
675 LastDeclarator);
676 } else {
677 NewFD = FunctionDecl::Create(Context, CurContext,
678 D.getIdentifierLoc(),
679 II, R, SC, isInline,
680 LastDeclarator);
681 }
Ted Kremenekf5c93c12008-02-27 22:18:07 +0000682 // Handle attributes.
Chris Lattner3ff30c82008-06-29 00:02:00 +0000683 ProcessDeclAttributes(NewFD, D);
Chris Lattner04421082008-04-08 04:40:51 +0000684
685 // Copy the parameter declarations from the declarator D to
686 // the function declaration NewFD, if they are available.
687 if (D.getNumTypeObjects() > 0 &&
688 D.getTypeObject(0).Fun.hasPrototype) {
689 DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
690
691 // Create Decl objects for each parameter, adding them to the
692 // FunctionDecl.
693 llvm::SmallVector<ParmVarDecl*, 16> Params;
694
695 // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs
696 // function that takes no arguments, not a function that takes a
Chris Lattner8123a952008-04-10 02:22:51 +0000697 // single void argument.
Eli Friedman6d1e4b52008-05-22 08:54:03 +0000698 // We let through "const void" here because Sema::GetTypeForDeclarator
699 // already checks for that case.
Chris Lattner04421082008-04-08 04:40:51 +0000700 if (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 &&
701 FTI.ArgInfo[0].Param &&
Chris Lattner04421082008-04-08 04:40:51 +0000702 ((ParmVarDecl*)FTI.ArgInfo[0].Param)->getType()->isVoidType()) {
703 // empty arg list, don't push any params.
Chris Lattner8123a952008-04-10 02:22:51 +0000704 ParmVarDecl *Param = (ParmVarDecl*)FTI.ArgInfo[0].Param;
705
Chris Lattnerdef026a2008-04-10 02:26:16 +0000706 // In C++, the empty parameter-type-list must be spelled "void"; a
707 // typedef of void is not permitted.
708 if (getLangOptions().CPlusPlus &&
Eli Friedman6d1e4b52008-05-22 08:54:03 +0000709 Param->getType().getUnqualifiedType() != Context.VoidTy) {
Chris Lattner8123a952008-04-10 02:22:51 +0000710 Diag(Param->getLocation(), diag::ext_param_typedef_of_void);
711 }
712
Chris Lattner04421082008-04-08 04:40:51 +0000713 } else {
714 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i)
715 Params.push_back((ParmVarDecl *)FTI.ArgInfo[i].Param);
716 }
717
718 NewFD->setParams(&Params[0], Params.size());
719 }
720
Steve Naroffffce4d52008-01-09 23:34:55 +0000721 // Merge the decl with the existing one if appropriate. Since C functions
722 // are in a flat namespace, make sure we consider decls in outer scopes.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000723 if (PrevDecl &&
724 (!getLangOptions().CPlusPlus ||
725 IdResolver.isDeclInScope(PrevDecl, CurContext, S)) ) {
Douglas Gregorf0097952008-04-21 02:02:58 +0000726 bool Redeclaration = false;
727 NewFD = MergeFunctionDecl(NewFD, PrevDecl, Redeclaration);
Reid Spencer5f016e22007-07-11 17:01:13 +0000728 if (NewFD == 0) return 0;
Douglas Gregorf0097952008-04-21 02:02:58 +0000729 if (Redeclaration) {
Eli Friedman27424962008-05-27 05:07:37 +0000730 NewFD->setPreviousDeclaration(cast<FunctionDecl>(PrevDecl));
Douglas Gregorf0097952008-04-21 02:02:58 +0000731 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000732 }
733 New = NewFD;
Chris Lattner04421082008-04-08 04:40:51 +0000734
735 // In C++, check default arguments now that we have merged decls.
736 if (getLangOptions().CPlusPlus)
737 CheckCXXDefaultArguments(NewFD);
Reid Spencer5f016e22007-07-11 17:01:13 +0000738 } else {
Douglas Gregor6d6eb572008-05-07 04:49:29 +0000739 // Check that there are no default arguments (C++ only).
740 if (getLangOptions().CPlusPlus)
741 CheckExtraCXXDefaultArguments(D);
742
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000743 if (R.getTypePtr()->isObjCInterfaceType()) {
Fariborz Jahaniane7f64cc2007-10-12 22:10:42 +0000744 Diag(D.getIdentifierLoc(), diag::err_statically_allocated_object,
745 D.getIdentifier()->getName());
746 InvalidDecl = true;
747 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000748
749 VarDecl *NewVD;
750 VarDecl::StorageClass SC;
751 switch (D.getDeclSpec().getStorageClassSpec()) {
Chris Lattner9e151e12008-03-15 21:10:16 +0000752 default: assert(0 && "Unknown storage class!");
753 case DeclSpec::SCS_unspecified: SC = VarDecl::None; break;
754 case DeclSpec::SCS_extern: SC = VarDecl::Extern; break;
755 case DeclSpec::SCS_static: SC = VarDecl::Static; break;
756 case DeclSpec::SCS_auto: SC = VarDecl::Auto; break;
757 case DeclSpec::SCS_register: SC = VarDecl::Register; break;
758 case DeclSpec::SCS_private_extern: SC = VarDecl::PrivateExtern; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000759 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000760 if (D.getContext() == Declarator::MemberContext) {
761 assert(SC == VarDecl::Static && "Invalid storage class for member!");
762 // This is a static data member for a C++ class.
763 NewVD = CXXClassVarDecl::Create(Context, cast<CXXRecordDecl>(CurContext),
764 D.getIdentifierLoc(), II,
765 R, LastDeclarator);
Steve Narofff0090632007-09-02 02:04:30 +0000766 } else {
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000767 if (S->getFnParent() == 0) {
768 // C99 6.9p2: The storage-class specifiers auto and register shall not
769 // appear in the declaration specifiers in an external declaration.
770 if (SC == VarDecl::Auto || SC == VarDecl::Register) {
771 Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_fscope,
772 R.getAsString());
773 InvalidDecl = true;
774 }
775 NewVD = VarDecl::Create(Context, CurContext, D.getIdentifierLoc(),
776 II, R, SC, LastDeclarator);
777 } else {
778 NewVD = VarDecl::Create(Context, CurContext, D.getIdentifierLoc(),
779 II, R, SC, LastDeclarator);
780 }
Steve Naroff53a32342007-08-28 18:45:29 +0000781 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000782 // Handle attributes prior to checking for duplicates in MergeVarDecl
Chris Lattner3ff30c82008-06-29 00:02:00 +0000783 ProcessDeclAttributes(NewVD, D);
Nate Begemanc8e89a82008-03-14 18:07:10 +0000784
785 // Emit an error if an address space was applied to decl with local storage.
786 // This includes arrays of objects with address space qualifiers, but not
787 // automatic variables that point to other address spaces.
788 // ISO/IEC TR 18037 S5.1.2
Nate Begeman8e7dafe2008-03-25 18:36:32 +0000789 if (NewVD->hasLocalStorage() && (NewVD->getType().getAddressSpace() != 0)) {
790 Diag(D.getIdentifierLoc(), diag::err_as_qualified_auto_decl);
791 InvalidDecl = true;
Nate Begeman5af27e02008-03-14 00:22:18 +0000792 }
Steve Naroffffce4d52008-01-09 23:34:55 +0000793 // Merge the decl with the existing one if appropriate. If the decl is
794 // in an outer scope, it isn't the same thing.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000795 if (PrevDecl && IdResolver.isDeclInScope(PrevDecl, CurContext, S)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000796 NewVD = MergeVarDecl(NewVD, PrevDecl);
797 if (NewVD == 0) return 0;
798 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000799 New = NewVD;
800 }
801
802 // If this has an identifier, add it to the scope stack.
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +0000803 if (II)
804 PushOnScopeChains(New, S);
Steve Naroff5912a352007-08-28 20:14:24 +0000805 // If any semantic error occurred, mark the decl as invalid.
806 if (D.getInvalidType() || InvalidDecl)
807 New->setInvalidDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +0000808
809 return New;
810}
811
Eli Friedmanc594b322008-05-20 13:48:25 +0000812bool Sema::CheckAddressConstantExpressionLValue(const Expr* Init) {
813 switch (Init->getStmtClass()) {
814 default:
815 Diag(Init->getExprLoc(),
816 diag::err_init_element_not_constant, Init->getSourceRange());
817 return true;
818 case Expr::ParenExprClass: {
819 const ParenExpr* PE = cast<ParenExpr>(Init);
820 return CheckAddressConstantExpressionLValue(PE->getSubExpr());
821 }
822 case Expr::CompoundLiteralExprClass:
823 return cast<CompoundLiteralExpr>(Init)->isFileScope();
824 case Expr::DeclRefExprClass: {
825 const Decl *D = cast<DeclRefExpr>(Init)->getDecl();
Eli Friedman97c0a392008-05-21 03:39:11 +0000826 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
827 if (VD->hasGlobalStorage())
828 return false;
829 Diag(Init->getExprLoc(),
830 diag::err_init_element_not_constant, Init->getSourceRange());
831 return true;
832 }
Eli Friedmanc594b322008-05-20 13:48:25 +0000833 if (isa<FunctionDecl>(D))
834 return false;
835 Diag(Init->getExprLoc(),
836 diag::err_init_element_not_constant, Init->getSourceRange());
Steve Naroffd0091aa2008-01-10 22:15:12 +0000837 return true;
838 }
Eli Friedmanc594b322008-05-20 13:48:25 +0000839 case Expr::MemberExprClass: {
840 const MemberExpr *M = cast<MemberExpr>(Init);
841 if (M->isArrow())
842 return CheckAddressConstantExpression(M->getBase());
843 return CheckAddressConstantExpressionLValue(M->getBase());
844 }
845 case Expr::ArraySubscriptExprClass: {
846 // FIXME: Should we pedwarn for "x[0+0]" (where x is a pointer)?
847 const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(Init);
848 return CheckAddressConstantExpression(ASE->getBase()) ||
849 CheckArithmeticConstantExpression(ASE->getIdx());
850 }
851 case Expr::StringLiteralClass:
852 case Expr::PreDefinedExprClass:
853 return false;
854 case Expr::UnaryOperatorClass: {
855 const UnaryOperator *Exp = cast<UnaryOperator>(Init);
856
857 // C99 6.6p9
858 if (Exp->getOpcode() == UnaryOperator::Deref)
Eli Friedman97c0a392008-05-21 03:39:11 +0000859 return CheckAddressConstantExpression(Exp->getSubExpr());
Eli Friedmanc594b322008-05-20 13:48:25 +0000860
861 Diag(Init->getExprLoc(),
862 diag::err_init_element_not_constant, Init->getSourceRange());
863 return true;
864 }
865 }
866}
867
868bool Sema::CheckAddressConstantExpression(const Expr* Init) {
869 switch (Init->getStmtClass()) {
870 default:
871 Diag(Init->getExprLoc(),
872 diag::err_init_element_not_constant, Init->getSourceRange());
873 return true;
874 case Expr::ParenExprClass: {
875 const ParenExpr* PE = cast<ParenExpr>(Init);
876 return CheckAddressConstantExpression(PE->getSubExpr());
877 }
878 case Expr::StringLiteralClass:
879 case Expr::ObjCStringLiteralClass:
880 return false;
881 case Expr::CallExprClass: {
882 const CallExpr *CE = cast<CallExpr>(Init);
883 if (CE->isBuiltinConstantExpr())
884 return false;
885 Diag(Init->getExprLoc(),
886 diag::err_init_element_not_constant, Init->getSourceRange());
887 return true;
888 }
889 case Expr::UnaryOperatorClass: {
890 const UnaryOperator *Exp = cast<UnaryOperator>(Init);
891
892 // C99 6.6p9
893 if (Exp->getOpcode() == UnaryOperator::AddrOf)
894 return CheckAddressConstantExpressionLValue(Exp->getSubExpr());
895
896 if (Exp->getOpcode() == UnaryOperator::Extension)
897 return CheckAddressConstantExpression(Exp->getSubExpr());
898
899 Diag(Init->getExprLoc(),
900 diag::err_init_element_not_constant, Init->getSourceRange());
901 return true;
902 }
903 case Expr::BinaryOperatorClass: {
904 // FIXME: Should we pedwarn for expressions like "a + 1 + 2"?
905 const BinaryOperator *Exp = cast<BinaryOperator>(Init);
906
907 Expr *PExp = Exp->getLHS();
908 Expr *IExp = Exp->getRHS();
909 if (IExp->getType()->isPointerType())
910 std::swap(PExp, IExp);
911
912 // FIXME: Should we pedwarn if IExp isn't an integer constant expression?
913 return CheckAddressConstantExpression(PExp) ||
914 CheckArithmeticConstantExpression(IExp);
915 }
916 case Expr::ImplicitCastExprClass: {
917 const Expr* SubExpr = cast<ImplicitCastExpr>(Init)->getSubExpr();
918
919 // Check for implicit promotion
920 if (SubExpr->getType()->isFunctionType() ||
921 SubExpr->getType()->isArrayType())
922 return CheckAddressConstantExpressionLValue(SubExpr);
923
924 // Check for pointer->pointer cast
925 if (SubExpr->getType()->isPointerType())
926 return CheckAddressConstantExpression(SubExpr);
927
928 if (SubExpr->getType()->isArithmeticType())
929 return CheckArithmeticConstantExpression(SubExpr);
930
931 Diag(Init->getExprLoc(),
932 diag::err_init_element_not_constant, Init->getSourceRange());
933 return true;
934 }
935 case Expr::CastExprClass: {
936 const Expr* SubExpr = cast<CastExpr>(Init)->getSubExpr();
937
938 // Check for pointer->pointer cast
939 if (SubExpr->getType()->isPointerType())
940 return CheckAddressConstantExpression(SubExpr);
941
942 // FIXME: Should we pedwarn for (int*)(0+0)?
943 if (SubExpr->getType()->isArithmeticType())
944 return CheckArithmeticConstantExpression(SubExpr);
945
946 Diag(Init->getExprLoc(),
947 diag::err_init_element_not_constant, Init->getSourceRange());
948 return true;
949 }
950 case Expr::ConditionalOperatorClass: {
951 // FIXME: Should we pedwarn here?
952 const ConditionalOperator *Exp = cast<ConditionalOperator>(Init);
953 if (!Exp->getCond()->getType()->isArithmeticType()) {
954 Diag(Init->getExprLoc(),
955 diag::err_init_element_not_constant, Init->getSourceRange());
956 return true;
957 }
958 if (CheckArithmeticConstantExpression(Exp->getCond()))
959 return true;
960 if (Exp->getLHS() &&
961 CheckAddressConstantExpression(Exp->getLHS()))
962 return true;
963 return CheckAddressConstantExpression(Exp->getRHS());
964 }
965 case Expr::AddrLabelExprClass:
966 return false;
967 }
968}
969
Eli Friedman4caf0552008-06-09 05:05:07 +0000970static const Expr* FindExpressionBaseAddress(const Expr* E);
971
972static const Expr* FindExpressionBaseAddressLValue(const Expr* E) {
973 switch (E->getStmtClass()) {
974 default:
975 return E;
976 case Expr::ParenExprClass: {
977 const ParenExpr* PE = cast<ParenExpr>(E);
978 return FindExpressionBaseAddressLValue(PE->getSubExpr());
979 }
980 case Expr::MemberExprClass: {
981 const MemberExpr *M = cast<MemberExpr>(E);
982 if (M->isArrow())
983 return FindExpressionBaseAddress(M->getBase());
984 return FindExpressionBaseAddressLValue(M->getBase());
985 }
986 case Expr::ArraySubscriptExprClass: {
987 const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(E);
988 return FindExpressionBaseAddress(ASE->getBase());
989 }
990 case Expr::UnaryOperatorClass: {
991 const UnaryOperator *Exp = cast<UnaryOperator>(E);
992
993 if (Exp->getOpcode() == UnaryOperator::Deref)
994 return FindExpressionBaseAddress(Exp->getSubExpr());
995
996 return E;
997 }
998 }
999}
1000
1001static const Expr* FindExpressionBaseAddress(const Expr* E) {
1002 switch (E->getStmtClass()) {
1003 default:
1004 return E;
1005 case Expr::ParenExprClass: {
1006 const ParenExpr* PE = cast<ParenExpr>(E);
1007 return FindExpressionBaseAddress(PE->getSubExpr());
1008 }
1009 case Expr::UnaryOperatorClass: {
1010 const UnaryOperator *Exp = cast<UnaryOperator>(E);
1011
1012 // C99 6.6p9
1013 if (Exp->getOpcode() == UnaryOperator::AddrOf)
1014 return FindExpressionBaseAddressLValue(Exp->getSubExpr());
1015
1016 if (Exp->getOpcode() == UnaryOperator::Extension)
1017 return FindExpressionBaseAddress(Exp->getSubExpr());
1018
1019 return E;
1020 }
1021 case Expr::BinaryOperatorClass: {
1022 const BinaryOperator *Exp = cast<BinaryOperator>(E);
1023
1024 Expr *PExp = Exp->getLHS();
1025 Expr *IExp = Exp->getRHS();
1026 if (IExp->getType()->isPointerType())
1027 std::swap(PExp, IExp);
1028
1029 return FindExpressionBaseAddress(PExp);
1030 }
1031 case Expr::ImplicitCastExprClass: {
1032 const Expr* SubExpr = cast<ImplicitCastExpr>(E)->getSubExpr();
1033
1034 // Check for implicit promotion
1035 if (SubExpr->getType()->isFunctionType() ||
1036 SubExpr->getType()->isArrayType())
1037 return FindExpressionBaseAddressLValue(SubExpr);
1038
1039 // Check for pointer->pointer cast
1040 if (SubExpr->getType()->isPointerType())
1041 return FindExpressionBaseAddress(SubExpr);
1042
1043 // We assume that we have an arithmetic expression here;
1044 // if we don't, we'll figure it out later
1045 return 0;
1046 }
1047 case Expr::CastExprClass: {
1048 const Expr* SubExpr = cast<CastExpr>(E)->getSubExpr();
1049
1050 // Check for pointer->pointer cast
1051 if (SubExpr->getType()->isPointerType())
1052 return FindExpressionBaseAddress(SubExpr);
1053
1054 // We assume that we have an arithmetic expression here;
1055 // if we don't, we'll figure it out later
1056 return 0;
1057 }
1058 }
1059}
1060
Eli Friedmanc594b322008-05-20 13:48:25 +00001061bool Sema::CheckArithmeticConstantExpression(const Expr* Init) {
1062 switch (Init->getStmtClass()) {
1063 default:
1064 Diag(Init->getExprLoc(),
1065 diag::err_init_element_not_constant, Init->getSourceRange());
1066 return true;
1067 case Expr::ParenExprClass: {
1068 const ParenExpr* PE = cast<ParenExpr>(Init);
1069 return CheckArithmeticConstantExpression(PE->getSubExpr());
1070 }
1071 case Expr::FloatingLiteralClass:
1072 case Expr::IntegerLiteralClass:
1073 case Expr::CharacterLiteralClass:
1074 case Expr::ImaginaryLiteralClass:
1075 case Expr::TypesCompatibleExprClass:
1076 case Expr::CXXBoolLiteralExprClass:
1077 return false;
1078 case Expr::CallExprClass: {
1079 const CallExpr *CE = cast<CallExpr>(Init);
1080 if (CE->isBuiltinConstantExpr())
1081 return false;
1082 Diag(Init->getExprLoc(),
1083 diag::err_init_element_not_constant, Init->getSourceRange());
1084 return true;
1085 }
1086 case Expr::DeclRefExprClass: {
1087 const Decl *D = cast<DeclRefExpr>(Init)->getDecl();
1088 if (isa<EnumConstantDecl>(D))
1089 return false;
1090 Diag(Init->getExprLoc(),
1091 diag::err_init_element_not_constant, Init->getSourceRange());
1092 return true;
1093 }
1094 case Expr::CompoundLiteralExprClass:
1095 // Allow "(vector type){2,4}"; normal C constraints don't allow this,
1096 // but vectors are allowed to be magic.
1097 if (Init->getType()->isVectorType())
1098 return false;
1099 Diag(Init->getExprLoc(),
1100 diag::err_init_element_not_constant, Init->getSourceRange());
1101 return true;
1102 case Expr::UnaryOperatorClass: {
1103 const UnaryOperator *Exp = cast<UnaryOperator>(Init);
1104
1105 switch (Exp->getOpcode()) {
1106 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1107 // See C99 6.6p3.
1108 default:
1109 Diag(Init->getExprLoc(),
1110 diag::err_init_element_not_constant, Init->getSourceRange());
1111 return true;
1112 case UnaryOperator::SizeOf:
1113 case UnaryOperator::AlignOf:
1114 case UnaryOperator::OffsetOf:
1115 // sizeof(E) is a constantexpr if and only if E is not evaluted.
1116 // See C99 6.5.3.4p2 and 6.6p3.
1117 if (Exp->getSubExpr()->getType()->isConstantSizeType())
1118 return false;
1119 Diag(Init->getExprLoc(),
1120 diag::err_init_element_not_constant, Init->getSourceRange());
1121 return true;
1122 case UnaryOperator::Extension:
1123 case UnaryOperator::LNot:
1124 case UnaryOperator::Plus:
1125 case UnaryOperator::Minus:
1126 case UnaryOperator::Not:
1127 return CheckArithmeticConstantExpression(Exp->getSubExpr());
1128 }
1129 }
1130 case Expr::SizeOfAlignOfTypeExprClass: {
1131 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(Init);
1132 // Special check for void types, which are allowed as an extension
1133 if (Exp->getArgumentType()->isVoidType())
1134 return false;
1135 // alignof always evaluates to a constant.
1136 // FIXME: is sizeof(int[3.0]) a constant expression?
1137 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType()) {
1138 Diag(Init->getExprLoc(),
1139 diag::err_init_element_not_constant, Init->getSourceRange());
1140 return true;
1141 }
1142 return false;
1143 }
1144 case Expr::BinaryOperatorClass: {
1145 const BinaryOperator *Exp = cast<BinaryOperator>(Init);
1146
1147 if (Exp->getLHS()->getType()->isArithmeticType() &&
1148 Exp->getRHS()->getType()->isArithmeticType()) {
1149 return CheckArithmeticConstantExpression(Exp->getLHS()) ||
1150 CheckArithmeticConstantExpression(Exp->getRHS());
1151 }
1152
Eli Friedman4caf0552008-06-09 05:05:07 +00001153 if (Exp->getLHS()->getType()->isPointerType() &&
1154 Exp->getRHS()->getType()->isPointerType()) {
1155 const Expr* LHSBase = FindExpressionBaseAddress(Exp->getLHS());
1156 const Expr* RHSBase = FindExpressionBaseAddress(Exp->getRHS());
1157
1158 // Only allow a null (constant integer) base; we could
1159 // allow some additional cases if necessary, but this
1160 // is sufficient to cover offsetof-like constructs.
1161 if (!LHSBase && !RHSBase) {
1162 return CheckAddressConstantExpression(Exp->getLHS()) ||
1163 CheckAddressConstantExpression(Exp->getRHS());
1164 }
1165 }
1166
Eli Friedmanc594b322008-05-20 13:48:25 +00001167 Diag(Init->getExprLoc(),
1168 diag::err_init_element_not_constant, Init->getSourceRange());
1169 return true;
1170 }
1171 case Expr::ImplicitCastExprClass:
1172 case Expr::CastExprClass: {
1173 const Expr *SubExpr;
1174 if (const CastExpr *C = dyn_cast<CastExpr>(Init)) {
1175 SubExpr = C->getSubExpr();
1176 } else {
1177 SubExpr = cast<ImplicitCastExpr>(Init)->getSubExpr();
1178 }
1179
1180 if (SubExpr->getType()->isArithmeticType())
1181 return CheckArithmeticConstantExpression(SubExpr);
1182
1183 Diag(Init->getExprLoc(),
1184 diag::err_init_element_not_constant, Init->getSourceRange());
1185 return true;
1186 }
1187 case Expr::ConditionalOperatorClass: {
1188 const ConditionalOperator *Exp = cast<ConditionalOperator>(Init);
1189 if (CheckArithmeticConstantExpression(Exp->getCond()))
1190 return true;
1191 if (Exp->getLHS() &&
1192 CheckArithmeticConstantExpression(Exp->getLHS()))
1193 return true;
1194 return CheckArithmeticConstantExpression(Exp->getRHS());
1195 }
1196 }
1197}
1198
1199bool Sema::CheckForConstantInitializer(Expr *Init, QualType DclT) {
1200 // Look through CXXDefaultArgExprs; they have no meaning in this context.
1201 if (CXXDefaultArgExpr* DAE = dyn_cast<CXXDefaultArgExpr>(Init))
1202 return CheckForConstantInitializer(DAE->getExpr(), DclT);
1203
1204 if (Init->getType()->isReferenceType()) {
1205 // FIXME: Work out how the heck reference types work
1206 return false;
1207#if 0
1208 // A reference is constant if the address of the expression
1209 // is constant
1210 // We look through initlists here to simplify
1211 // CheckAddressConstantExpressionLValue.
1212 if (InitListExpr *Exp = dyn_cast<InitListExpr>(Init)) {
1213 assert(Exp->getNumInits() > 0 &&
1214 "Refernce initializer cannot be empty");
1215 Init = Exp->getInit(0);
1216 }
1217 return CheckAddressConstantExpressionLValue(Init);
1218#endif
1219 }
1220
1221 if (InitListExpr *Exp = dyn_cast<InitListExpr>(Init)) {
1222 unsigned numInits = Exp->getNumInits();
1223 for (unsigned i = 0; i < numInits; i++) {
1224 // FIXME: Need to get the type of the declaration for C++,
1225 // because it could be a reference?
1226 if (CheckForConstantInitializer(Exp->getInit(i),
1227 Exp->getInit(i)->getType()))
1228 return true;
1229 }
1230 return false;
1231 }
1232
1233 if (Init->isNullPointerConstant(Context))
1234 return false;
1235 if (Init->getType()->isArithmeticType()) {
Eli Friedmanc1cc6dc2008-05-30 18:14:48 +00001236 QualType InitTy = Init->getType().getCanonicalType().getUnqualifiedType();
1237 if (InitTy == Context.BoolTy) {
1238 // Special handling for pointers implicitly cast to bool;
1239 // (e.g. "_Bool rr = &rr;"). This is only legal at the top level.
1240 if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Init)) {
1241 Expr* SubE = ICE->getSubExpr();
1242 if (SubE->getType()->isPointerType() ||
1243 SubE->getType()->isArrayType() ||
1244 SubE->getType()->isFunctionType()) {
1245 return CheckAddressConstantExpression(Init);
1246 }
1247 }
1248 } else if (InitTy->isIntegralType()) {
1249 Expr* SubE = 0;
1250 if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Init))
1251 SubE = ICE->getSubExpr();
1252 else if (CastExpr* CE = dyn_cast<CastExpr>(Init))
1253 SubE = CE->getSubExpr();
1254 // Special check for pointer cast to int; we allow as an extension
1255 // an address constant cast to an integer if the integer
1256 // is of an appropriate width (this sort of code is apparently used
1257 // in some places).
1258 // FIXME: Add pedwarn?
1259 // FIXME: Don't allow bitfields here! Need the FieldDecl for that.
1260 if (SubE && (SubE->getType()->isPointerType() ||
1261 SubE->getType()->isArrayType() ||
1262 SubE->getType()->isFunctionType())) {
1263 unsigned IntWidth = Context.getTypeSize(Init->getType());
1264 unsigned PointerWidth = Context.getTypeSize(Context.VoidPtrTy);
1265 if (IntWidth >= PointerWidth)
1266 return CheckAddressConstantExpression(Init);
1267 }
Eli Friedmanc594b322008-05-20 13:48:25 +00001268 }
1269
1270 return CheckArithmeticConstantExpression(Init);
1271 }
1272
1273 if (Init->getType()->isPointerType())
1274 return CheckAddressConstantExpression(Init);
1275
Eli Friedmanc1cc6dc2008-05-30 18:14:48 +00001276 // An array type at the top level that isn't an init-list must
1277 // be a string literal
Eli Friedmanc594b322008-05-20 13:48:25 +00001278 if (Init->getType()->isArrayType())
1279 return false;
1280
1281 Diag(Init->getExprLoc(), diag::err_init_element_not_constant,
1282 Init->getSourceRange());
1283 return true;
Steve Naroffd0091aa2008-01-10 22:15:12 +00001284}
1285
Steve Naroffbb204692007-09-12 14:07:44 +00001286void Sema::AddInitializerToDecl(DeclTy *dcl, ExprTy *init) {
Steve Naroff410e3e22007-09-12 20:13:48 +00001287 Decl *RealDecl = static_cast<Decl *>(dcl);
Steve Naroffbb204692007-09-12 14:07:44 +00001288 Expr *Init = static_cast<Expr *>(init);
Chris Lattner9a11b9a2007-10-19 20:10:30 +00001289 assert(Init && "missing initializer");
Steve Naroffbb204692007-09-12 14:07:44 +00001290
Chris Lattner9a11b9a2007-10-19 20:10:30 +00001291 // If there is no declaration, there was an error parsing it. Just ignore
1292 // the initializer.
1293 if (RealDecl == 0) {
1294 delete Init;
1295 return;
1296 }
Steve Naroffbb204692007-09-12 14:07:44 +00001297
Steve Naroff410e3e22007-09-12 20:13:48 +00001298 VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl);
1299 if (!VDecl) {
Steve Naroff8e74c932007-09-13 21:41:19 +00001300 Diag(dyn_cast<ScopedDecl>(RealDecl)->getLocation(),
1301 diag::err_illegal_initializer);
Steve Naroff410e3e22007-09-12 20:13:48 +00001302 RealDecl->setInvalidDecl();
1303 return;
1304 }
Steve Naroffbb204692007-09-12 14:07:44 +00001305 // Get the decls type and save a reference for later, since
Steve Naroffd0091aa2008-01-10 22:15:12 +00001306 // CheckInitializerTypes may change it.
Steve Naroff410e3e22007-09-12 20:13:48 +00001307 QualType DclT = VDecl->getType(), SavT = DclT;
Steve Naroff248a7532008-04-15 22:42:06 +00001308 if (VDecl->isBlockVarDecl()) {
1309 VarDecl::StorageClass SC = VDecl->getStorageClass();
Steve Naroffbb204692007-09-12 14:07:44 +00001310 if (SC == VarDecl::Extern) { // C99 6.7.8p5
Steve Naroff410e3e22007-09-12 20:13:48 +00001311 Diag(VDecl->getLocation(), diag::err_block_extern_cant_init);
Steve Naroff248a7532008-04-15 22:42:06 +00001312 VDecl->setInvalidDecl();
1313 } else if (!VDecl->isInvalidDecl()) {
Steve Naroffa9960332008-01-25 00:51:06 +00001314 if (CheckInitializerTypes(Init, DclT))
Steve Naroff248a7532008-04-15 22:42:06 +00001315 VDecl->setInvalidDecl();
Steve Naroffd0091aa2008-01-10 22:15:12 +00001316 if (SC == VarDecl::Static) // C99 6.7.8p4.
1317 CheckForConstantInitializer(Init, DclT);
Steve Naroffbb204692007-09-12 14:07:44 +00001318 }
Steve Naroff248a7532008-04-15 22:42:06 +00001319 } else if (VDecl->isFileVarDecl()) {
1320 if (VDecl->getStorageClass() == VarDecl::Extern)
Steve Naroff410e3e22007-09-12 20:13:48 +00001321 Diag(VDecl->getLocation(), diag::warn_extern_init);
Steve Naroff248a7532008-04-15 22:42:06 +00001322 if (!VDecl->isInvalidDecl())
Steve Naroffa9960332008-01-25 00:51:06 +00001323 if (CheckInitializerTypes(Init, DclT))
Steve Naroff248a7532008-04-15 22:42:06 +00001324 VDecl->setInvalidDecl();
Steve Naroffd0091aa2008-01-10 22:15:12 +00001325
1326 // C99 6.7.8p4. All file scoped initializers need to be constant.
1327 CheckForConstantInitializer(Init, DclT);
Steve Naroffbb204692007-09-12 14:07:44 +00001328 }
1329 // If the type changed, it means we had an incomplete type that was
1330 // completed by the initializer. For example:
1331 // int ary[] = { 1, 3, 5 };
1332 // "ary" transitions from a VariableArrayType to a ConstantArrayType.
Christopher Lamb48b12392007-11-29 19:09:19 +00001333 if (!VDecl->isInvalidDecl() && (DclT != SavT)) {
Steve Naroff410e3e22007-09-12 20:13:48 +00001334 VDecl->setType(DclT);
Christopher Lamb48b12392007-11-29 19:09:19 +00001335 Init->setType(DclT);
1336 }
Steve Naroffbb204692007-09-12 14:07:44 +00001337
1338 // Attach the initializer to the decl.
Steve Naroff410e3e22007-09-12 20:13:48 +00001339 VDecl->setInit(Init);
Steve Naroffbb204692007-09-12 14:07:44 +00001340 return;
1341}
1342
Reid Spencer5f016e22007-07-11 17:01:13 +00001343/// The declarators are chained together backwards, reverse the list.
1344Sema::DeclTy *Sema::FinalizeDeclaratorGroup(Scope *S, DeclTy *group) {
1345 // Often we have single declarators, handle them quickly.
Steve Naroff94745042007-09-13 23:52:58 +00001346 Decl *GroupDecl = static_cast<Decl*>(group);
1347 if (GroupDecl == 0)
Steve Naroffbb204692007-09-12 14:07:44 +00001348 return 0;
Steve Naroff94745042007-09-13 23:52:58 +00001349
1350 ScopedDecl *Group = dyn_cast<ScopedDecl>(GroupDecl);
1351 ScopedDecl *NewGroup = 0;
Steve Naroffbb204692007-09-12 14:07:44 +00001352 if (Group->getNextDeclarator() == 0)
Reid Spencer5f016e22007-07-11 17:01:13 +00001353 NewGroup = Group;
Steve Naroffbb204692007-09-12 14:07:44 +00001354 else { // reverse the list.
1355 while (Group) {
Steve Naroff94745042007-09-13 23:52:58 +00001356 ScopedDecl *Next = Group->getNextDeclarator();
Steve Naroffbb204692007-09-12 14:07:44 +00001357 Group->setNextDeclarator(NewGroup);
1358 NewGroup = Group;
1359 Group = Next;
1360 }
1361 }
1362 // Perform semantic analysis that depends on having fully processed both
1363 // the declarator and initializer.
Steve Naroff94745042007-09-13 23:52:58 +00001364 for (ScopedDecl *ID = NewGroup; ID; ID = ID->getNextDeclarator()) {
Steve Naroffbb204692007-09-12 14:07:44 +00001365 VarDecl *IDecl = dyn_cast<VarDecl>(ID);
1366 if (!IDecl)
1367 continue;
Steve Naroffbb204692007-09-12 14:07:44 +00001368 QualType T = IDecl->getType();
1369
1370 // C99 6.7.5.2p2: If an identifier is declared to be an object with
1371 // static storage duration, it shall not have a variable length array.
Steve Naroff248a7532008-04-15 22:42:06 +00001372 if ((IDecl->isFileVarDecl() || IDecl->isBlockVarDecl()) &&
1373 IDecl->getStorageClass() == VarDecl::Static) {
Eli Friedman3fe02932008-02-15 19:53:52 +00001374 if (T->getAsVariableArrayType()) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001375 Diag(IDecl->getLocation(), diag::err_typecheck_illegal_vla);
1376 IDecl->setInvalidDecl();
Steve Naroffbb204692007-09-12 14:07:44 +00001377 }
1378 }
1379 // Block scope. C99 6.7p7: If an identifier for an object is declared with
1380 // no linkage (C99 6.2.2p6), the type for the object shall be complete...
Steve Naroff248a7532008-04-15 22:42:06 +00001381 if (IDecl->isBlockVarDecl() &&
1382 IDecl->getStorageClass() != VarDecl::Extern) {
Chris Lattnerfd89bc82008-04-02 01:05:10 +00001383 if (T->isIncompleteType() && !IDecl->isInvalidDecl()) {
Chris Lattner8b1be772007-12-02 07:50:03 +00001384 Diag(IDecl->getLocation(), diag::err_typecheck_decl_incomplete_type,
1385 T.getAsString());
Steve Naroffbb204692007-09-12 14:07:44 +00001386 IDecl->setInvalidDecl();
1387 }
1388 }
1389 // File scope. C99 6.9.2p2: A declaration of an identifier for and
1390 // object that has file scope without an initializer, and without a
1391 // storage-class specifier or with the storage-class specifier "static",
1392 // constitutes a tentative definition. Note: A tentative definition with
1393 // external linkage is valid (C99 6.2.2p5).
Steve Naroff248a7532008-04-15 22:42:06 +00001394 if (IDecl && !IDecl->getInit() &&
1395 (IDecl->getStorageClass() == VarDecl::Static ||
1396 IDecl->getStorageClass() == VarDecl::None)) {
Eli Friedman9db13972008-02-15 12:53:51 +00001397 if (T->isIncompleteArrayType()) {
Steve Naroff9a75f8a2008-01-18 20:40:52 +00001398 // C99 6.9.2 (p2, p5): Implicit initialization causes an incomplete
1399 // array to be completed. Don't issue a diagnostic.
Chris Lattnerfd89bc82008-04-02 01:05:10 +00001400 } else if (T->isIncompleteType() && !IDecl->isInvalidDecl()) {
Steve Naroff9a75f8a2008-01-18 20:40:52 +00001401 // C99 6.9.2p3: If the declaration of an identifier for an object is
1402 // a tentative definition and has internal linkage (C99 6.2.2p3), the
1403 // declared type shall not be an incomplete type.
Chris Lattner8b1be772007-12-02 07:50:03 +00001404 Diag(IDecl->getLocation(), diag::err_typecheck_decl_incomplete_type,
1405 T.getAsString());
Steve Naroffbb204692007-09-12 14:07:44 +00001406 IDecl->setInvalidDecl();
1407 }
1408 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001409 }
1410 return NewGroup;
1411}
Steve Naroffe1223f72007-08-28 03:03:08 +00001412
Chris Lattner04421082008-04-08 04:40:51 +00001413/// ActOnParamDeclarator - Called from Parser::ParseFunctionDeclarator()
1414/// to introduce parameters into function prototype scope.
1415Sema::DeclTy *
1416Sema::ActOnParamDeclarator(Scope *S, Declarator &D) {
Chris Lattner985abd92008-06-26 06:49:43 +00001417 const DeclSpec &DS = D.getDeclSpec();
Chris Lattner04421082008-04-08 04:40:51 +00001418
1419 // Verify C99 6.7.5.3p2: The only SCS allowed is 'register'.
1420 if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
1421 DS.getStorageClassSpec() != DeclSpec::SCS_register) {
1422 Diag(DS.getStorageClassSpecLoc(),
1423 diag::err_invalid_storage_class_in_func_decl);
Chris Lattner985abd92008-06-26 06:49:43 +00001424 D.getMutableDeclSpec().ClearStorageClassSpecs();
Chris Lattner04421082008-04-08 04:40:51 +00001425 }
1426 if (DS.isThreadSpecified()) {
1427 Diag(DS.getThreadSpecLoc(),
1428 diag::err_invalid_storage_class_in_func_decl);
Chris Lattner985abd92008-06-26 06:49:43 +00001429 D.getMutableDeclSpec().ClearStorageClassSpecs();
Chris Lattner04421082008-04-08 04:40:51 +00001430 }
1431
Douglas Gregor6d6eb572008-05-07 04:49:29 +00001432 // Check that there are no default arguments inside the type of this
1433 // parameter (C++ only).
1434 if (getLangOptions().CPlusPlus)
1435 CheckExtraCXXDefaultArguments(D);
1436
Chris Lattner04421082008-04-08 04:40:51 +00001437 // In this context, we *do not* check D.getInvalidType(). If the declarator
1438 // type was invalid, GetTypeForDeclarator() still returns a "valid" type,
1439 // though it will not reflect the user specified type.
1440 QualType parmDeclType = GetTypeForDeclarator(D, S);
1441
1442 assert(!parmDeclType.isNull() && "GetTypeForDeclarator() returned null type");
1443
Reid Spencer5f016e22007-07-11 17:01:13 +00001444 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
1445 // Can this happen for params? We already checked that they don't conflict
1446 // among each other. Here they can only shadow globals, which is ok.
Chris Lattner04421082008-04-08 04:40:51 +00001447 IdentifierInfo *II = D.getIdentifier();
1448 if (Decl *PrevDecl = LookupDecl(II, Decl::IDNS_Ordinary, S)) {
1449 if (S->isDeclScope(PrevDecl)) {
1450 Diag(D.getIdentifierLoc(), diag::err_param_redefinition,
1451 dyn_cast<NamedDecl>(PrevDecl)->getName());
1452
1453 // Recover by removing the name
1454 II = 0;
1455 D.SetIdentifier(0, D.getIdentifierLoc());
1456 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001457 }
Steve Naroff6a9f3e32007-08-07 22:44:21 +00001458
1459 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
1460 // Doing the promotion here has a win and a loss. The win is the type for
1461 // both Decl's and DeclRefExpr's will match (a convenient invariant for the
1462 // code generator). The loss is the orginal type isn't preserved. For example:
1463 //
1464 // void func(int parmvardecl[5]) { // convert "int [5]" to "int *"
1465 // int blockvardecl[5];
1466 // sizeof(parmvardecl); // size == 4
1467 // sizeof(blockvardecl); // size == 20
1468 // }
1469 //
1470 // For expressions, all implicit conversions are captured using the
1471 // ImplicitCastExpr AST node (we have no such mechanism for Decl's).
1472 //
1473 // FIXME: If a source translation tool needs to see the original type, then
1474 // we need to consider storing both types (in ParmVarDecl)...
1475 //
Chris Lattnere6327742008-04-02 05:18:44 +00001476 if (parmDeclType->isArrayType()) {
Chris Lattner529bd022008-01-02 22:50:48 +00001477 // int x[restrict 4] -> int *restrict
Chris Lattnere6327742008-04-02 05:18:44 +00001478 parmDeclType = Context.getArrayDecayedType(parmDeclType);
Chris Lattner529bd022008-01-02 22:50:48 +00001479 } else if (parmDeclType->isFunctionType())
Steve Naroff6a9f3e32007-08-07 22:44:21 +00001480 parmDeclType = Context.getPointerType(parmDeclType);
1481
Chris Lattner04421082008-04-08 04:40:51 +00001482 ParmVarDecl *New = ParmVarDecl::Create(Context, CurContext,
1483 D.getIdentifierLoc(), II,
1484 parmDeclType, VarDecl::None,
1485 0, 0);
Anders Carlssonf78915f2008-02-15 07:04:12 +00001486
Chris Lattner04421082008-04-08 04:40:51 +00001487 if (D.getInvalidType())
Steve Naroff53a32342007-08-28 18:45:29 +00001488 New->setInvalidDecl();
1489
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +00001490 if (II)
1491 PushOnScopeChains(New, S);
Nate Begemanb7894b52008-02-17 21:20:31 +00001492
Chris Lattner3ff30c82008-06-29 00:02:00 +00001493 ProcessDeclAttributes(New, D);
Reid Spencer5f016e22007-07-11 17:01:13 +00001494 return New;
Chris Lattner04421082008-04-08 04:40:51 +00001495
Reid Spencer5f016e22007-07-11 17:01:13 +00001496}
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001497
Chris Lattnerb652cea2007-10-09 17:14:05 +00001498Sema::DeclTy *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Declarator &D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +00001499 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
Reid Spencer5f016e22007-07-11 17:01:13 +00001500 assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function &&
1501 "Not a function declarator!");
1502 DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
Chris Lattner04421082008-04-08 04:40:51 +00001503
Reid Spencer5f016e22007-07-11 17:01:13 +00001504 // Verify 6.9.1p6: 'every identifier in the identifier list shall be declared'
1505 // for a K&R function.
1506 if (!FTI.hasPrototype) {
1507 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Chris Lattner04421082008-04-08 04:40:51 +00001508 if (FTI.ArgInfo[i].Param == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001509 Diag(FTI.ArgInfo[i].IdentLoc, diag::ext_param_not_declared,
1510 FTI.ArgInfo[i].Ident->getName());
1511 // Implicitly declare the argument as type 'int' for lack of a better
1512 // type.
Chris Lattner04421082008-04-08 04:40:51 +00001513 DeclSpec DS;
1514 const char* PrevSpec; // unused
1515 DS.SetTypeSpecType(DeclSpec::TST_int, FTI.ArgInfo[i].IdentLoc,
1516 PrevSpec);
1517 Declarator ParamD(DS, Declarator::KNRTypeListContext);
1518 ParamD.SetIdentifier(FTI.ArgInfo[i].Ident, FTI.ArgInfo[i].IdentLoc);
1519 FTI.ArgInfo[i].Param = ActOnParamDeclarator(FnBodyScope, ParamD);
Reid Spencer5f016e22007-07-11 17:01:13 +00001520 }
1521 }
Chris Lattner52804082008-02-17 19:31:09 +00001522
Reid Spencer5f016e22007-07-11 17:01:13 +00001523 // Since this is a function definition, act as though we have information
1524 // about the arguments.
Chris Lattner52804082008-02-17 19:31:09 +00001525 if (FTI.NumArgs)
1526 FTI.hasPrototype = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001527 } else {
Chris Lattner04421082008-04-08 04:40:51 +00001528 // FIXME: Diagnose arguments without names in C.
Reid Spencer5f016e22007-07-11 17:01:13 +00001529 }
1530
1531 Scope *GlobalScope = FnBodyScope->getParent();
Steve Naroffadbbd0c2008-01-14 20:51:29 +00001532
1533 // See if this is a redefinition.
Steve Naroffe8043c32008-04-01 23:04:06 +00001534 Decl *PrevDcl = LookupDecl(D.getIdentifier(), Decl::IDNS_Ordinary,
Steve Naroffb327ce02008-04-02 14:35:35 +00001535 GlobalScope);
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +00001536 if (PrevDcl && IdResolver.isDeclInScope(PrevDcl, CurContext)) {
1537 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PrevDcl)) {
1538 const FunctionDecl *Definition;
1539 if (FD->getBody(Definition)) {
1540 Diag(D.getIdentifierLoc(), diag::err_redefinition,
1541 D.getIdentifier()->getName());
1542 Diag(Definition->getLocation(), diag::err_previous_definition);
1543 }
Steve Naroffadbbd0c2008-01-14 20:51:29 +00001544 }
1545 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001546
1547 return ActOnStartOfFunctionDef(FnBodyScope,
1548 ActOnDeclarator(GlobalScope, D, 0));
1549}
1550
1551Sema::DeclTy *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, DeclTy *D) {
1552 Decl *decl = static_cast<Decl*>(D);
Chris Lattnere9ba3232008-02-16 01:20:36 +00001553 FunctionDecl *FD = cast<FunctionDecl>(decl);
Chris Lattnerb048c982008-04-06 04:47:34 +00001554 PushDeclContext(FD);
Chris Lattner04421082008-04-08 04:40:51 +00001555
1556 // Check the validity of our function parameters
1557 CheckParmsForFunctionDef(FD);
1558
1559 // Introduce our parameters into the function scope
1560 for (unsigned p = 0, NumParams = FD->getNumParams(); p < NumParams; ++p) {
1561 ParmVarDecl *Param = FD->getParamDecl(p);
1562 // If this has an identifier, add it to the scope stack.
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +00001563 if (Param->getIdentifier())
1564 PushOnScopeChains(Param, FnBodyScope);
Reid Spencer5f016e22007-07-11 17:01:13 +00001565 }
Chris Lattner04421082008-04-08 04:40:51 +00001566
Reid Spencer5f016e22007-07-11 17:01:13 +00001567 return FD;
1568}
1569
Steve Naroffd6d054d2007-11-11 23:20:51 +00001570Sema::DeclTy *Sema::ActOnFinishFunctionBody(DeclTy *D, StmtTy *Body) {
1571 Decl *dcl = static_cast<Decl *>(D);
1572 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(dcl)) {
1573 FD->setBody((Stmt*)Body);
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +00001574 assert(FD == getCurFunctionDecl() && "Function parsing confused");
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001575 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(dcl)) {
Steve Naroffd6d054d2007-11-11 23:20:51 +00001576 MD->setBody((Stmt*)Body);
Steve Naroff4d832202007-12-13 18:18:56 +00001577 }
Chris Lattnerb048c982008-04-06 04:47:34 +00001578 PopDeclContext();
Reid Spencer5f016e22007-07-11 17:01:13 +00001579 // Verify and clean out per-function state.
1580
1581 // Check goto/label use.
1582 for (llvm::DenseMap<IdentifierInfo*, LabelStmt*>::iterator
1583 I = LabelMap.begin(), E = LabelMap.end(); I != E; ++I) {
1584 // Verify that we have no forward references left. If so, there was a goto
1585 // or address of a label taken, but no definition of it. Label fwd
1586 // definitions are indicated with a null substmt.
1587 if (I->second->getSubStmt() == 0) {
1588 LabelStmt *L = I->second;
1589 // Emit error.
1590 Diag(L->getIdentLoc(), diag::err_undeclared_label_use, L->getName());
1591
1592 // At this point, we have gotos that use the bogus label. Stitch it into
1593 // the function body so that they aren't leaked and that the AST is well
1594 // formed.
Chris Lattner0cbc2152008-01-25 00:01:10 +00001595 if (Body) {
1596 L->setSubStmt(new NullStmt(L->getIdentLoc()));
1597 cast<CompoundStmt>((Stmt*)Body)->push_back(L);
1598 } else {
1599 // The whole function wasn't parsed correctly, just delete this.
1600 delete L;
1601 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001602 }
1603 }
1604 LabelMap.clear();
1605
Steve Naroffd6d054d2007-11-11 23:20:51 +00001606 return D;
Fariborz Jahanian60fbca02007-11-10 16:31:34 +00001607}
1608
Reid Spencer5f016e22007-07-11 17:01:13 +00001609/// ImplicitlyDefineFunction - An undeclared identifier was used in a function
1610/// call, forming a call to an implicitly defined function (per C99 6.5.1p2).
Steve Naroff8c9f13e2007-09-16 16:16:00 +00001611ScopedDecl *Sema::ImplicitlyDefineFunction(SourceLocation Loc,
1612 IdentifierInfo &II, Scope *S) {
Chris Lattner37d10842008-05-05 21:18:06 +00001613 // Extension in C99. Legal in C90, but warn about it.
1614 if (getLangOptions().C99)
Reid Spencer5f016e22007-07-11 17:01:13 +00001615 Diag(Loc, diag::ext_implicit_function_decl, II.getName());
Chris Lattner37d10842008-05-05 21:18:06 +00001616 else
Reid Spencer5f016e22007-07-11 17:01:13 +00001617 Diag(Loc, diag::warn_implicit_function_decl, II.getName());
1618
1619 // FIXME: handle stuff like:
1620 // void foo() { extern float X(); }
1621 // void bar() { X(); } <-- implicit decl for X in another scope.
1622
1623 // Set a Declarator for the implicit definition: int foo();
1624 const char *Dummy;
1625 DeclSpec DS;
1626 bool Error = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, Dummy);
1627 Error = Error; // Silence warning.
1628 assert(!Error && "Error setting up implicit decl!");
1629 Declarator D(DS, Declarator::BlockContext);
1630 D.AddTypeInfo(DeclaratorChunk::getFunction(false, false, 0, 0, Loc));
1631 D.SetIdentifier(&II, Loc);
1632
Argyrios Kyrtzidis93213bb2008-05-01 21:04:16 +00001633 // Insert this function into translation-unit scope.
1634
1635 DeclContext *PrevDC = CurContext;
1636 CurContext = Context.getTranslationUnitDecl();
1637
Steve Naroffe2ef8152008-04-04 14:32:09 +00001638 FunctionDecl *FD =
Argyrios Kyrtzidis93213bb2008-05-01 21:04:16 +00001639 dyn_cast<FunctionDecl>(static_cast<Decl*>(ActOnDeclarator(TUScope, D, 0)));
Steve Naroffe2ef8152008-04-04 14:32:09 +00001640 FD->setImplicit();
Argyrios Kyrtzidis93213bb2008-05-01 21:04:16 +00001641
1642 CurContext = PrevDC;
1643
Steve Naroffe2ef8152008-04-04 14:32:09 +00001644 return FD;
Reid Spencer5f016e22007-07-11 17:01:13 +00001645}
1646
1647
Chris Lattner41af0932007-11-14 06:34:38 +00001648TypedefDecl *Sema::ParseTypedefDecl(Scope *S, Declarator &D, QualType T,
Steve Naroff94745042007-09-13 23:52:58 +00001649 ScopedDecl *LastDeclarator) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001650 assert(D.getIdentifier() && "Wrong callback for declspec without declarator");
Steve Naroff5912a352007-08-28 20:14:24 +00001651 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001652
1653 // Scope manipulation handled by caller.
Chris Lattner0ed844b2008-04-04 06:12:32 +00001654 TypedefDecl *NewTD = TypedefDecl::Create(Context, CurContext,
1655 D.getIdentifierLoc(),
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001656 D.getIdentifier(),
Chris Lattnerc63e6602008-03-15 21:32:50 +00001657 T, LastDeclarator);
Steve Naroff5912a352007-08-28 20:14:24 +00001658 if (D.getInvalidType())
1659 NewTD->setInvalidDecl();
1660 return NewTD;
Reid Spencer5f016e22007-07-11 17:01:13 +00001661}
1662
Steve Naroff08d92e42007-09-15 18:49:24 +00001663/// ActOnTag - This is invoked when we see 'struct foo' or 'struct {'. In the
Reid Spencer5f016e22007-07-11 17:01:13 +00001664/// former case, Name will be non-null. In the later case, Name will be null.
1665/// TagType indicates what kind of tag this is. TK indicates whether this is a
1666/// reference/declaration/definition of a tag.
Steve Naroff08d92e42007-09-15 18:49:24 +00001667Sema::DeclTy *Sema::ActOnTag(Scope *S, unsigned TagType, TagKind TK,
Reid Spencer5f016e22007-07-11 17:01:13 +00001668 SourceLocation KWLoc, IdentifierInfo *Name,
1669 SourceLocation NameLoc, AttributeList *Attr) {
1670 // If this is a use of an existing tag, it must have a name.
1671 assert((Name != 0 || TK == TK_Definition) &&
1672 "Nameless record must be a definition!");
1673
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001674 TagDecl::TagKind Kind;
Reid Spencer5f016e22007-07-11 17:01:13 +00001675 switch (TagType) {
1676 default: assert(0 && "Unknown tag type!");
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001677 case DeclSpec::TST_struct: Kind = TagDecl::TK_struct; break;
1678 case DeclSpec::TST_union: Kind = TagDecl::TK_union; break;
1679 case DeclSpec::TST_class: Kind = TagDecl::TK_class; break;
1680 case DeclSpec::TST_enum: Kind = TagDecl::TK_enum; break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001681 }
1682
1683 // If this is a named struct, check to see if there was a previous forward
1684 // declaration or definition.
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +00001685 // Use ScopedDecl instead of TagDecl, because a NamespaceDecl may come up.
1686 if (ScopedDecl *PrevDecl =
1687 dyn_cast_or_null<ScopedDecl>(LookupDecl(Name, Decl::IDNS_Tag, S))) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001688
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +00001689 assert((isa<TagDecl>(PrevDecl) || isa<NamespaceDecl>(PrevDecl)) &&
1690 "unexpected Decl type");
1691 if (TagDecl *PrevTagDecl = dyn_cast<TagDecl>(PrevDecl)) {
Chris Lattner14943b92008-07-03 03:30:58 +00001692 // If this is a use of a previous tag, or if the tag is already declared
1693 // in the same scope (so that the definition/declaration completes or
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +00001694 // rementions the tag), reuse the decl.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +00001695 if (TK == TK_Reference ||
1696 IdResolver.isDeclInScope(PrevDecl, CurContext, S)) {
Chris Lattner14943b92008-07-03 03:30:58 +00001697 // Make sure that this wasn't declared as an enum and now used as a
1698 // struct or something similar.
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001699 if (PrevTagDecl->getTagKind() != Kind) {
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +00001700 Diag(KWLoc, diag::err_use_with_wrong_tag, Name->getName());
1701 Diag(PrevDecl->getLocation(), diag::err_previous_use);
Chris Lattner14943b92008-07-03 03:30:58 +00001702 // Recover by making this an anonymous redefinition.
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +00001703 Name = 0;
Chris Lattner14943b92008-07-03 03:30:58 +00001704 PrevDecl = 0;
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +00001705 } else {
Chris Lattner14943b92008-07-03 03:30:58 +00001706 // If this is a use or a forward declaration, we're good.
1707 if (TK != TK_Definition)
1708 return PrevDecl;
1709
1710 // Diagnose attempts to redefine a tag.
1711 if (PrevTagDecl->isDefinition()) {
1712 Diag(NameLoc, diag::err_redefinition, Name->getName());
1713 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
1714 // If this is a redefinition, recover by making this struct be
1715 // anonymous, which will make any later references get the previous
1716 // definition.
1717 Name = 0;
1718 } else {
1719 // Okay, this is definition of a previously declared or referenced
1720 // tag. Move the location of the decl to be the definition site.
1721 PrevDecl->setLocation(NameLoc);
1722 return PrevDecl;
1723 }
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +00001724 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001725 }
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +00001726 // If we get here, this is a definition of a new struct type in a nested
1727 // scope, e.g. "struct foo; void bar() { struct foo; }", just create a new
1728 // type.
1729 } else {
1730 // The tag name clashes with a namespace name, issue an error and recover
1731 // by making this tag be anonymous.
1732 Diag(NameLoc, diag::err_redefinition_different_kind, Name->getName());
1733 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
1734 Name = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001735 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001736 }
1737
1738 // If there is an identifier, use the location of the identifier as the
1739 // location of the decl, otherwise use the location of the struct/union
1740 // keyword.
1741 SourceLocation Loc = NameLoc.isValid() ? NameLoc : KWLoc;
1742
1743 // Otherwise, if this is the first time we've seen this tag, create the decl.
1744 TagDecl *New;
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001745 if (Kind == TagDecl::TK_enum) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001746 // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.:
1747 // enum X { A, B, C } D; D should chain to X.
Chris Lattner0ed844b2008-04-04 06:12:32 +00001748 New = EnumDecl::Create(Context, CurContext, Loc, Name, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +00001749 // If this is an undefined enum, warn.
1750 if (TK != TK_Definition) Diag(Loc, diag::ext_forward_ref_enum);
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001751 } else {
1752 // struct/union/class
1753
Reid Spencer5f016e22007-07-11 17:01:13 +00001754 // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.:
1755 // struct X { int A; } D; D should chain to X.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001756 if (getLangOptions().CPlusPlus)
1757 // FIXME: Look for a way to use RecordDecl for simple structs.
1758 New = CXXRecordDecl::Create(Context, Kind, CurContext, Loc, Name, 0);
1759 else
1760 New = RecordDecl::Create(Context, Kind, CurContext, Loc, Name, 0);
1761 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001762
1763 // If this has an identifier, add it to the scope stack.
1764 if (Name) {
Chris Lattner31e05722007-08-26 06:24:45 +00001765 // The scope passed in may not be a decl scope. Zip up the scope tree until
1766 // we find one that is.
1767 while ((S->getFlags() & Scope::DeclScope) == 0)
1768 S = S->getParent();
1769
1770 // Add it to the decl chain.
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +00001771 PushOnScopeChains(New, S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001772 }
Chris Lattnere1e79852008-02-06 00:51:33 +00001773
Chris Lattnerf2e4bd52008-06-28 23:58:55 +00001774 if (Attr)
1775 ProcessDeclAttributeList(New, Attr);
Reid Spencer5f016e22007-07-11 17:01:13 +00001776 return New;
1777}
1778
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001779/// Collect the instance variables declared in an Objective-C object. Used in
1780/// the creation of structures from objects using the @defs directive.
1781static void CollectIvars(ObjCInterfaceDecl *Class,
1782 llvm::SmallVector<Sema::DeclTy*, 16> &ivars) {
1783 if (Class->getSuperClass())
1784 CollectIvars(Class->getSuperClass(), ivars);
1785 ivars.append(Class->ivar_begin(), Class->ivar_end());
1786}
1787
1788/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1789/// instance variables of ClassName into Decls.
1790void Sema::ActOnDefs(Scope *S, SourceLocation DeclStart,
1791 IdentifierInfo *ClassName,
1792 llvm::SmallVector<DeclTy*, 16> &Decls) {
1793 // Check that ClassName is a valid class
1794 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
1795 if (!Class) {
1796 Diag(DeclStart, diag::err_undef_interface, ClassName->getName());
1797 return;
1798 }
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001799 // Collect the instance variables
1800 CollectIvars(Class, Decls);
1801}
1802
Eli Friedman1b76ada2008-06-03 21:01:11 +00001803QualType Sema::TryFixInvalidVariablyModifiedType(QualType T) {
1804 // This method tries to turn a variable array into a constant
1805 // array even when the size isn't an ICE. This is necessary
1806 // for compatibility with code that depends on gcc's buggy
1807 // constant expression folding, like struct {char x[(int)(char*)2];}
1808 if (const VariableArrayType* VLATy = dyn_cast<VariableArrayType>(T)) {
Anders Carlssonc44eec62008-07-03 04:20:39 +00001809 APValue Result;
Eli Friedman1b76ada2008-06-03 21:01:11 +00001810 if (VLATy->getSizeExpr() &&
Anders Carlssonc44eec62008-07-03 04:20:39 +00001811 VLATy->getSizeExpr()->tryEvaluate(Result, Context) && Result.isSInt() &&
1812 Result.getSInt() > llvm::APSInt(Result.getSInt().getBitWidth(),
1813 Result.getSInt().isUnsigned())) {
Eli Friedman1b76ada2008-06-03 21:01:11 +00001814 return Context.getConstantArrayType(VLATy->getElementType(),
Anders Carlssonc44eec62008-07-03 04:20:39 +00001815 Result.getSInt(),
1816 ArrayType::Normal, 0);
Eli Friedman1b76ada2008-06-03 21:01:11 +00001817 }
1818 }
1819 return QualType();
1820}
1821
Steve Naroff08d92e42007-09-15 18:49:24 +00001822/// ActOnField - Each field of a struct/union/class is passed into this in order
Reid Spencer5f016e22007-07-11 17:01:13 +00001823/// to create a FieldDecl object for it.
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +00001824Sema::DeclTy *Sema::ActOnField(Scope *S,
Reid Spencer5f016e22007-07-11 17:01:13 +00001825 SourceLocation DeclStart,
1826 Declarator &D, ExprTy *BitfieldWidth) {
1827 IdentifierInfo *II = D.getIdentifier();
1828 Expr *BitWidth = (Expr*)BitfieldWidth;
Reid Spencer5f016e22007-07-11 17:01:13 +00001829 SourceLocation Loc = DeclStart;
1830 if (II) Loc = D.getIdentifierLoc();
1831
1832 // FIXME: Unnamed fields can be handled in various different ways, for
1833 // example, unnamed unions inject all members into the struct namespace!
1834
1835
1836 if (BitWidth) {
1837 // TODO: Validate.
1838 //printf("WARNING: BITFIELDS IGNORED!\n");
1839
1840 // 6.7.2.1p3
1841 // 6.7.2.1p4
1842
1843 } else {
1844 // Not a bitfield.
1845
1846 // validate II.
1847
1848 }
1849
1850 QualType T = GetTypeForDeclarator(D, S);
Steve Naroff5912a352007-08-28 20:14:24 +00001851 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
1852 bool InvalidDecl = false;
Steve Naroffd7444aa2007-08-31 17:20:07 +00001853
Reid Spencer5f016e22007-07-11 17:01:13 +00001854 // C99 6.7.2.1p8: A member of a structure or union may have any type other
1855 // than a variably modified type.
Eli Friedman9db13972008-02-15 12:53:51 +00001856 if (T->isVariablyModifiedType()) {
Eli Friedman1b76ada2008-06-03 21:01:11 +00001857 QualType FixedTy = TryFixInvalidVariablyModifiedType(T);
1858 if (!FixedTy.isNull()) {
1859 Diag(Loc, diag::warn_illegal_constant_array_size, Loc);
1860 T = FixedTy;
1861 } else {
1862 // FIXME: This diagnostic needs work
1863 Diag(Loc, diag::err_typecheck_illegal_vla, Loc);
1864 InvalidDecl = true;
1865 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001866 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001867 // FIXME: Chain fielddecls together.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001868 FieldDecl *NewFD;
1869
1870 if (getLangOptions().CPlusPlus) {
1871 // FIXME: Replace CXXFieldDecls with FieldDecls for simple structs.
1872 NewFD = CXXFieldDecl::Create(Context, cast<CXXRecordDecl>(CurContext),
1873 Loc, II, T, BitWidth);
1874 if (II)
1875 PushOnScopeChains(NewFD, S);
1876 }
1877 else
1878 NewFD = FieldDecl::Create(Context, Loc, II, T, BitWidth);
Steve Naroff44739212007-09-11 21:17:26 +00001879
Chris Lattner3ff30c82008-06-29 00:02:00 +00001880 ProcessDeclAttributes(NewFD, D);
Anders Carlssonad148062008-02-16 00:29:18 +00001881
Steve Naroff5912a352007-08-28 20:14:24 +00001882 if (D.getInvalidType() || InvalidDecl)
1883 NewFD->setInvalidDecl();
1884 return NewFD;
Reid Spencer5f016e22007-07-11 17:01:13 +00001885}
1886
Fariborz Jahanian89204a12007-10-01 16:53:59 +00001887/// TranslateIvarVisibility - Translate visibility from a token ID to an
1888/// AST enum value.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001889static ObjCIvarDecl::AccessControl
Fariborz Jahanian89204a12007-10-01 16:53:59 +00001890TranslateIvarVisibility(tok::ObjCKeywordKind ivarVisibility) {
Steve Narofff13271f2007-09-14 23:09:53 +00001891 switch (ivarVisibility) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001892 case tok::objc_private: return ObjCIvarDecl::Private;
1893 case tok::objc_public: return ObjCIvarDecl::Public;
1894 case tok::objc_protected: return ObjCIvarDecl::Protected;
1895 case tok::objc_package: return ObjCIvarDecl::Package;
Fariborz Jahanian89204a12007-10-01 16:53:59 +00001896 default: assert(false && "Unknown visitibility kind");
Steve Narofff13271f2007-09-14 23:09:53 +00001897 }
1898}
1899
Fariborz Jahanian45bc03f2008-04-11 16:55:42 +00001900/// ActOnIvar - Each ivar field of an objective-c class is passed into this
1901/// in order to create an IvarDecl object for it.
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +00001902Sema::DeclTy *Sema::ActOnIvar(Scope *S,
Fariborz Jahanian45bc03f2008-04-11 16:55:42 +00001903 SourceLocation DeclStart,
1904 Declarator &D, ExprTy *BitfieldWidth,
1905 tok::ObjCKeywordKind Visibility) {
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +00001906 IdentifierInfo *II = D.getIdentifier();
1907 Expr *BitWidth = (Expr*)BitfieldWidth;
1908 SourceLocation Loc = DeclStart;
1909 if (II) Loc = D.getIdentifierLoc();
1910
1911 // FIXME: Unnamed fields can be handled in various different ways, for
1912 // example, unnamed unions inject all members into the struct namespace!
1913
1914
1915 if (BitWidth) {
1916 // TODO: Validate.
1917 //printf("WARNING: BITFIELDS IGNORED!\n");
1918
1919 // 6.7.2.1p3
1920 // 6.7.2.1p4
1921
1922 } else {
1923 // Not a bitfield.
1924
1925 // validate II.
1926
1927 }
1928
1929 QualType T = GetTypeForDeclarator(D, S);
1930 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
1931 bool InvalidDecl = false;
1932
1933 // C99 6.7.2.1p8: A member of a structure or union may have any type other
1934 // than a variably modified type.
1935 if (T->isVariablyModifiedType()) {
1936 // FIXME: This diagnostic needs work
1937 Diag(Loc, diag::err_typecheck_illegal_vla, Loc);
1938 InvalidDecl = true;
1939 }
1940
Fariborz Jahanian45bc03f2008-04-11 16:55:42 +00001941 ObjCIvarDecl *NewID = ObjCIvarDecl::Create(Context, Loc, II, T);
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +00001942
Chris Lattner3ff30c82008-06-29 00:02:00 +00001943 ProcessDeclAttributes(NewID, D);
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +00001944
1945 if (D.getInvalidType() || InvalidDecl)
1946 NewID->setInvalidDecl();
1947 // If we have visibility info, make sure the AST is set accordingly.
Fariborz Jahanian45bc03f2008-04-11 16:55:42 +00001948 if (Visibility != tok::objc_not_keyword)
1949 NewID->setAccessControl(TranslateIvarVisibility(Visibility));
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +00001950 return NewID;
1951}
1952
Fariborz Jahanian9d048ff2007-09-29 00:54:24 +00001953void Sema::ActOnFields(Scope* S,
Fariborz Jahanian3f5faf72007-10-04 00:45:27 +00001954 SourceLocation RecLoc, DeclTy *RecDecl,
Steve Naroff08d92e42007-09-15 18:49:24 +00001955 DeclTy **Fields, unsigned NumFields,
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +00001956 SourceLocation LBrac, SourceLocation RBrac) {
Steve Naroff74216642007-09-14 22:20:54 +00001957 Decl *EnclosingDecl = static_cast<Decl*>(RecDecl);
1958 assert(EnclosingDecl && "missing record or interface decl");
1959 RecordDecl *Record = dyn_cast<RecordDecl>(EnclosingDecl);
1960
Fariborz Jahaniane267ab62007-09-14 16:27:55 +00001961 if (Record && Record->isDefinition()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001962 // Diagnose code like:
1963 // struct S { struct S {} X; };
1964 // We discover this when we complete the outer S. Reject and ignore the
1965 // outer S.
1966 Diag(Record->getLocation(), diag::err_nested_redefinition,
1967 Record->getKindName());
1968 Diag(RecLoc, diag::err_previous_definition);
Steve Naroff74216642007-09-14 22:20:54 +00001969 Record->setInvalidDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +00001970 return;
1971 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001972 // Verify that all the fields are okay.
1973 unsigned NumNamedMembers = 0;
1974 llvm::SmallVector<FieldDecl*, 32> RecFields;
1975 llvm::SmallSet<const IdentifierInfo*, 32> FieldIDs;
Steve Naroff74216642007-09-14 22:20:54 +00001976
Reid Spencer5f016e22007-07-11 17:01:13 +00001977 for (unsigned i = 0; i != NumFields; ++i) {
Fariborz Jahaniane267ab62007-09-14 16:27:55 +00001978
Steve Naroff74216642007-09-14 22:20:54 +00001979 FieldDecl *FD = cast_or_null<FieldDecl>(static_cast<Decl*>(Fields[i]));
1980 assert(FD && "missing field decl");
1981
1982 // Remember all fields.
1983 RecFields.push_back(FD);
Reid Spencer5f016e22007-07-11 17:01:13 +00001984
1985 // Get the type for the field.
Chris Lattner02c642e2007-07-31 21:33:24 +00001986 Type *FDTy = FD->getType().getTypePtr();
Steve Narofff13271f2007-09-14 23:09:53 +00001987
Reid Spencer5f016e22007-07-11 17:01:13 +00001988 // C99 6.7.2.1p2 - A field may not be a function type.
Chris Lattner02c642e2007-07-31 21:33:24 +00001989 if (FDTy->isFunctionType()) {
Steve Naroff74216642007-09-14 22:20:54 +00001990 Diag(FD->getLocation(), diag::err_field_declared_as_function,
Reid Spencer5f016e22007-07-11 17:01:13 +00001991 FD->getName());
Steve Naroff74216642007-09-14 22:20:54 +00001992 FD->setInvalidDecl();
1993 EnclosingDecl->setInvalidDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +00001994 continue;
1995 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001996 // C99 6.7.2.1p2 - A field may not be an incomplete type except...
1997 if (FDTy->isIncompleteType()) {
Fariborz Jahaniane267ab62007-09-14 16:27:55 +00001998 if (!Record) { // Incomplete ivar type is always an error.
Fariborz Jahanian3f5faf72007-10-04 00:45:27 +00001999 Diag(FD->getLocation(), diag::err_field_incomplete, FD->getName());
Steve Naroff74216642007-09-14 22:20:54 +00002000 FD->setInvalidDecl();
2001 EnclosingDecl->setInvalidDecl();
Fariborz Jahanian3f5faf72007-10-04 00:45:27 +00002002 continue;
Fariborz Jahaniane267ab62007-09-14 16:27:55 +00002003 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002004 if (i != NumFields-1 || // ... that the last member ...
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00002005 !Record->isStruct() || // ... of a structure ...
Chris Lattner02c642e2007-07-31 21:33:24 +00002006 !FDTy->isArrayType()) { //... may have incomplete array type.
Reid Spencer5f016e22007-07-11 17:01:13 +00002007 Diag(FD->getLocation(), diag::err_field_incomplete, FD->getName());
Steve Naroff74216642007-09-14 22:20:54 +00002008 FD->setInvalidDecl();
2009 EnclosingDecl->setInvalidDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +00002010 continue;
2011 }
Fariborz Jahaniane267ab62007-09-14 16:27:55 +00002012 if (NumNamedMembers < 1) { //... must have more than named member ...
Reid Spencer5f016e22007-07-11 17:01:13 +00002013 Diag(FD->getLocation(), diag::err_flexible_array_empty_struct,
2014 FD->getName());
Steve Naroff74216642007-09-14 22:20:54 +00002015 FD->setInvalidDecl();
2016 EnclosingDecl->setInvalidDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +00002017 continue;
2018 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002019 // Okay, we have a legal flexible array member at the end of the struct.
Fariborz Jahaniane267ab62007-09-14 16:27:55 +00002020 if (Record)
2021 Record->setHasFlexibleArrayMember(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002022 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002023 /// C99 6.7.2.1p2 - a struct ending in a flexible array member cannot be the
2024 /// field of another structure or the element of an array.
Chris Lattner02c642e2007-07-31 21:33:24 +00002025 if (const RecordType *FDTTy = FDTy->getAsRecordType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002026 if (FDTTy->getDecl()->hasFlexibleArrayMember()) {
2027 // If this is a member of a union, then entire union becomes "flexible".
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00002028 if (Record && Record->isUnion()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002029 Record->setHasFlexibleArrayMember(true);
2030 } else {
2031 // If this is a struct/class and this is not the last element, reject
2032 // it. Note that GCC supports variable sized arrays in the middle of
2033 // structures.
2034 if (i != NumFields-1) {
2035 Diag(FD->getLocation(), diag::err_variable_sized_type_in_struct,
2036 FD->getName());
Steve Naroff74216642007-09-14 22:20:54 +00002037 FD->setInvalidDecl();
2038 EnclosingDecl->setInvalidDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +00002039 continue;
2040 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002041 // We support flexible arrays at the end of structs in other structs
2042 // as an extension.
2043 Diag(FD->getLocation(), diag::ext_flexible_array_in_struct,
2044 FD->getName());
Fariborz Jahanian3f5faf72007-10-04 00:45:27 +00002045 if (Record)
Fariborz Jahaniane267ab62007-09-14 16:27:55 +00002046 Record->setHasFlexibleArrayMember(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002047 }
2048 }
2049 }
Fariborz Jahaniane7f64cc2007-10-12 22:10:42 +00002050 /// A field cannot be an Objective-c object
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002051 if (FDTy->isObjCInterfaceType()) {
Fariborz Jahaniane7f64cc2007-10-12 22:10:42 +00002052 Diag(FD->getLocation(), diag::err_statically_allocated_object,
2053 FD->getName());
2054 FD->setInvalidDecl();
2055 EnclosingDecl->setInvalidDecl();
2056 continue;
2057 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002058 // Keep track of the number of named members.
2059 if (IdentifierInfo *II = FD->getIdentifier()) {
2060 // Detect duplicate member names.
2061 if (!FieldIDs.insert(II)) {
2062 Diag(FD->getLocation(), diag::err_duplicate_member, II->getName());
2063 // Find the previous decl.
2064 SourceLocation PrevLoc;
2065 for (unsigned i = 0, e = RecFields.size(); ; ++i) {
2066 assert(i != e && "Didn't find previous def!");
2067 if (RecFields[i]->getIdentifier() == II) {
2068 PrevLoc = RecFields[i]->getLocation();
2069 break;
2070 }
2071 }
2072 Diag(PrevLoc, diag::err_previous_definition);
Steve Naroff74216642007-09-14 22:20:54 +00002073 FD->setInvalidDecl();
2074 EnclosingDecl->setInvalidDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +00002075 continue;
2076 }
2077 ++NumNamedMembers;
2078 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002079 }
2080
Reid Spencer5f016e22007-07-11 17:01:13 +00002081 // Okay, we successfully defined 'Record'.
Chris Lattnere1e79852008-02-06 00:51:33 +00002082 if (Record) {
Fariborz Jahaniane267ab62007-09-14 16:27:55 +00002083 Record->defineBody(&RecFields[0], RecFields.size());
Chris Lattnere1e79852008-02-06 00:51:33 +00002084 Consumer.HandleTagDeclDefinition(Record);
2085 } else {
Chris Lattnera91d3812008-02-05 22:40:55 +00002086 ObjCIvarDecl **ClsFields = reinterpret_cast<ObjCIvarDecl**>(&RecFields[0]);
2087 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(EnclosingDecl))
2088 ID->addInstanceVariablesToClass(ClsFields, RecFields.size(), RBrac);
2089 else if (ObjCImplementationDecl *IMPDecl =
2090 dyn_cast<ObjCImplementationDecl>(EnclosingDecl)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002091 assert(IMPDecl && "ActOnFields - missing ObjCImplementationDecl");
2092 IMPDecl->ObjCAddInstanceVariablesToClassImpl(ClsFields, RecFields.size());
Fariborz Jahanian3a3ca1b2007-10-31 18:48:14 +00002093 CheckImplementationIvars(IMPDecl, ClsFields, RecFields.size(), RBrac);
Fariborz Jahaniand0b90bf2007-09-26 18:27:25 +00002094 }
Fariborz Jahanianb04a0212007-09-14 21:08:27 +00002095 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002096}
2097
Steve Naroff08d92e42007-09-15 18:49:24 +00002098Sema::DeclTy *Sema::ActOnEnumConstant(Scope *S, DeclTy *theEnumDecl,
Reid Spencer5f016e22007-07-11 17:01:13 +00002099 DeclTy *lastEnumConst,
2100 SourceLocation IdLoc, IdentifierInfo *Id,
2101 SourceLocation EqualLoc, ExprTy *val) {
Chris Lattner0ed844b2008-04-04 06:12:32 +00002102 EnumDecl *TheEnumDecl = cast<EnumDecl>(static_cast<Decl*>(theEnumDecl));
Reid Spencer5f016e22007-07-11 17:01:13 +00002103 EnumConstantDecl *LastEnumConst =
2104 cast_or_null<EnumConstantDecl>(static_cast<Decl*>(lastEnumConst));
2105 Expr *Val = static_cast<Expr*>(val);
2106
Chris Lattner31e05722007-08-26 06:24:45 +00002107 // The scope passed in may not be a decl scope. Zip up the scope tree until
2108 // we find one that is.
2109 while ((S->getFlags() & Scope::DeclScope) == 0)
2110 S = S->getParent();
2111
Reid Spencer5f016e22007-07-11 17:01:13 +00002112 // Verify that there isn't already something declared with this name in this
2113 // scope.
Steve Naroffb327ce02008-04-02 14:35:35 +00002114 if (Decl *PrevDecl = LookupDecl(Id, Decl::IDNS_Ordinary, S)) {
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +00002115 if (IdResolver.isDeclInScope(PrevDecl, CurContext, S)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002116 if (isa<EnumConstantDecl>(PrevDecl))
2117 Diag(IdLoc, diag::err_redefinition_of_enumerator, Id->getName());
2118 else
2119 Diag(IdLoc, diag::err_redefinition, Id->getName());
2120 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
Chris Lattnera73349d2008-02-26 00:33:57 +00002121 delete Val;
Reid Spencer5f016e22007-07-11 17:01:13 +00002122 return 0;
2123 }
2124 }
2125
2126 llvm::APSInt EnumVal(32);
2127 QualType EltTy;
2128 if (Val) {
Chris Lattner421a23d2007-08-27 21:16:18 +00002129 // Make sure to promote the operand type to int.
2130 UsualUnaryConversions(Val);
2131
Reid Spencer5f016e22007-07-11 17:01:13 +00002132 // C99 6.7.2.2p2: Make sure we have an integer constant expression.
2133 SourceLocation ExpLoc;
Chris Lattner590b6642007-07-15 23:26:56 +00002134 if (!Val->isIntegerConstantExpr(EnumVal, Context, &ExpLoc)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002135 Diag(ExpLoc, diag::err_enum_value_not_integer_constant_expr,
2136 Id->getName());
Chris Lattnera73349d2008-02-26 00:33:57 +00002137 delete Val;
Chris Lattnerb7416f92007-08-27 17:37:24 +00002138 Val = 0; // Just forget about it.
Chris Lattnere9ca8512007-08-29 16:03:41 +00002139 } else {
2140 EltTy = Val->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +00002141 }
Chris Lattnerb7416f92007-08-27 17:37:24 +00002142 }
2143
2144 if (!Val) {
2145 if (LastEnumConst) {
2146 // Assign the last value + 1.
2147 EnumVal = LastEnumConst->getInitVal();
2148 ++EnumVal;
Chris Lattner421a23d2007-08-27 21:16:18 +00002149
2150 // Check for overflow on increment.
2151 if (EnumVal < LastEnumConst->getInitVal())
2152 Diag(IdLoc, diag::warn_enum_value_overflow);
2153
Chris Lattnerb7416f92007-08-27 17:37:24 +00002154 EltTy = LastEnumConst->getType();
2155 } else {
2156 // First value, set to zero.
2157 EltTy = Context.IntTy;
Chris Lattner98be4942008-03-05 18:54:05 +00002158 EnumVal.zextOrTrunc(static_cast<uint32_t>(Context.getTypeSize(EltTy)));
Chris Lattnerb7416f92007-08-27 17:37:24 +00002159 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002160 }
2161
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00002162 EnumConstantDecl *New =
Chris Lattner0ed844b2008-04-04 06:12:32 +00002163 EnumConstantDecl::Create(Context, TheEnumDecl, IdLoc, Id, EltTy,
2164 Val, EnumVal,
Chris Lattnerc63e6602008-03-15 21:32:50 +00002165 LastEnumConst);
Reid Spencer5f016e22007-07-11 17:01:13 +00002166
2167 // Register this decl in the current scope stack.
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +00002168 PushOnScopeChains(New, S);
Reid Spencer5f016e22007-07-11 17:01:13 +00002169 return New;
2170}
2171
Steve Naroff08d92e42007-09-15 18:49:24 +00002172void Sema::ActOnEnumBody(SourceLocation EnumLoc, DeclTy *EnumDeclX,
Reid Spencer5f016e22007-07-11 17:01:13 +00002173 DeclTy **Elements, unsigned NumElements) {
2174 EnumDecl *Enum = cast<EnumDecl>(static_cast<Decl*>(EnumDeclX));
2175 assert(!Enum->isDefinition() && "Enum redefinitions can't reach here");
2176
Chris Lattnere37f0be2007-08-28 05:10:31 +00002177 // TODO: If the result value doesn't fit in an int, it must be a long or long
2178 // long value. ISO C does not support this, but GCC does as an extension,
2179 // emit a warning.
Chris Lattner98be4942008-03-05 18:54:05 +00002180 unsigned IntWidth = Context.Target.getIntWidth();
Chris Lattnere37f0be2007-08-28 05:10:31 +00002181
Chris Lattnerac609682007-08-28 06:15:15 +00002182 // Verify that all the values are okay, compute the size of the values, and
2183 // reverse the list.
2184 unsigned NumNegativeBits = 0;
2185 unsigned NumPositiveBits = 0;
2186
2187 // Keep track of whether all elements have type int.
2188 bool AllElementsInt = true;
2189
Reid Spencer5f016e22007-07-11 17:01:13 +00002190 EnumConstantDecl *EltList = 0;
2191 for (unsigned i = 0; i != NumElements; ++i) {
2192 EnumConstantDecl *ECD =
2193 cast_or_null<EnumConstantDecl>(static_cast<Decl*>(Elements[i]));
2194 if (!ECD) continue; // Already issued a diagnostic.
Chris Lattner211a30e2007-08-28 05:27:00 +00002195
2196 // If the enum value doesn't fit in an int, emit an extension warning.
Chris Lattner211a30e2007-08-28 05:27:00 +00002197 const llvm::APSInt &InitVal = ECD->getInitVal();
Chris Lattnera73349d2008-02-26 00:33:57 +00002198 assert(InitVal.getBitWidth() >= IntWidth &&
2199 "Should have promoted value to int");
Chris Lattner211a30e2007-08-28 05:27:00 +00002200 if (InitVal.getBitWidth() > IntWidth) {
2201 llvm::APSInt V(InitVal);
2202 V.trunc(IntWidth);
2203 V.extend(InitVal.getBitWidth());
2204 if (V != InitVal)
2205 Diag(ECD->getLocation(), diag::ext_enum_value_not_int,
2206 InitVal.toString());
2207 }
Chris Lattnerac609682007-08-28 06:15:15 +00002208
2209 // Keep track of the size of positive and negative values.
Chris Lattnera73349d2008-02-26 00:33:57 +00002210 if (InitVal.isUnsigned() || InitVal.isNonNegative())
Chris Lattner21dd8212008-01-14 21:47:29 +00002211 NumPositiveBits = std::max(NumPositiveBits,
2212 (unsigned)InitVal.getActiveBits());
Chris Lattnerac609682007-08-28 06:15:15 +00002213 else
Chris Lattner21dd8212008-01-14 21:47:29 +00002214 NumNegativeBits = std::max(NumNegativeBits,
2215 (unsigned)InitVal.getMinSignedBits());
Reid Spencer5f016e22007-07-11 17:01:13 +00002216
Chris Lattnerac609682007-08-28 06:15:15 +00002217 // Keep track of whether every enum element has type int (very commmon).
2218 if (AllElementsInt)
2219 AllElementsInt = ECD->getType() == Context.IntTy;
2220
Reid Spencer5f016e22007-07-11 17:01:13 +00002221 ECD->setNextDeclarator(EltList);
2222 EltList = ECD;
2223 }
2224
Chris Lattnerac609682007-08-28 06:15:15 +00002225 // Figure out the type that should be used for this enum.
2226 // FIXME: Support attribute(packed) on enums and -fshort-enums.
2227 QualType BestType;
Chris Lattnerb7f6e082007-08-29 17:31:48 +00002228 unsigned BestWidth;
Chris Lattnerac609682007-08-28 06:15:15 +00002229
2230 if (NumNegativeBits) {
2231 // If there is a negative value, figure out the smallest integer type (of
2232 // int/long/longlong) that fits.
Chris Lattnerb7f6e082007-08-29 17:31:48 +00002233 if (NumNegativeBits <= IntWidth && NumPositiveBits < IntWidth) {
Chris Lattnerac609682007-08-28 06:15:15 +00002234 BestType = Context.IntTy;
Chris Lattnerb7f6e082007-08-29 17:31:48 +00002235 BestWidth = IntWidth;
2236 } else {
Chris Lattner98be4942008-03-05 18:54:05 +00002237 BestWidth = Context.Target.getLongWidth();
Ted Kremenek9c728dc2007-12-12 22:39:36 +00002238
Chris Lattnerb7f6e082007-08-29 17:31:48 +00002239 if (NumNegativeBits <= BestWidth && NumPositiveBits < BestWidth)
Chris Lattnerac609682007-08-28 06:15:15 +00002240 BestType = Context.LongTy;
2241 else {
Chris Lattner98be4942008-03-05 18:54:05 +00002242 BestWidth = Context.Target.getLongLongWidth();
Ted Kremenek9c728dc2007-12-12 22:39:36 +00002243
Chris Lattnerb7f6e082007-08-29 17:31:48 +00002244 if (NumNegativeBits > BestWidth || NumPositiveBits >= BestWidth)
Chris Lattnerac609682007-08-28 06:15:15 +00002245 Diag(Enum->getLocation(), diag::warn_enum_too_large);
2246 BestType = Context.LongLongTy;
2247 }
2248 }
2249 } else {
2250 // If there is no negative value, figure out which of uint, ulong, ulonglong
2251 // fits.
Chris Lattnerb7f6e082007-08-29 17:31:48 +00002252 if (NumPositiveBits <= IntWidth) {
Chris Lattnerac609682007-08-28 06:15:15 +00002253 BestType = Context.UnsignedIntTy;
Chris Lattnerb7f6e082007-08-29 17:31:48 +00002254 BestWidth = IntWidth;
2255 } else if (NumPositiveBits <=
Chris Lattner98be4942008-03-05 18:54:05 +00002256 (BestWidth = Context.Target.getLongWidth())) {
Chris Lattnerac609682007-08-28 06:15:15 +00002257 BestType = Context.UnsignedLongTy;
Chris Lattner98be4942008-03-05 18:54:05 +00002258 } else {
2259 BestWidth = Context.Target.getLongLongWidth();
Chris Lattnerb7f6e082007-08-29 17:31:48 +00002260 assert(NumPositiveBits <= BestWidth &&
Chris Lattnerac609682007-08-28 06:15:15 +00002261 "How could an initializer get larger than ULL?");
2262 BestType = Context.UnsignedLongLongTy;
2263 }
2264 }
2265
Chris Lattnerb7f6e082007-08-29 17:31:48 +00002266 // Loop over all of the enumerator constants, changing their types to match
2267 // the type of the enum if needed.
2268 for (unsigned i = 0; i != NumElements; ++i) {
2269 EnumConstantDecl *ECD =
2270 cast_or_null<EnumConstantDecl>(static_cast<Decl*>(Elements[i]));
2271 if (!ECD) continue; // Already issued a diagnostic.
2272
2273 // Standard C says the enumerators have int type, but we allow, as an
2274 // extension, the enumerators to be larger than int size. If each
2275 // enumerator value fits in an int, type it as an int, otherwise type it the
2276 // same as the enumerator decl itself. This means that in "enum { X = 1U }"
2277 // that X has type 'int', not 'unsigned'.
Chris Lattnera73349d2008-02-26 00:33:57 +00002278 if (ECD->getType() == Context.IntTy) {
2279 // Make sure the init value is signed.
2280 llvm::APSInt IV = ECD->getInitVal();
2281 IV.setIsSigned(true);
2282 ECD->setInitVal(IV);
Chris Lattnerb7f6e082007-08-29 17:31:48 +00002283 continue; // Already int type.
Chris Lattnera73349d2008-02-26 00:33:57 +00002284 }
Chris Lattnerb7f6e082007-08-29 17:31:48 +00002285
2286 // Determine whether the value fits into an int.
2287 llvm::APSInt InitVal = ECD->getInitVal();
2288 bool FitsInInt;
2289 if (InitVal.isUnsigned() || !InitVal.isNegative())
2290 FitsInInt = InitVal.getActiveBits() < IntWidth;
2291 else
2292 FitsInInt = InitVal.getMinSignedBits() <= IntWidth;
2293
2294 // If it fits into an integer type, force it. Otherwise force it to match
2295 // the enum decl type.
2296 QualType NewTy;
2297 unsigned NewWidth;
2298 bool NewSign;
2299 if (FitsInInt) {
2300 NewTy = Context.IntTy;
2301 NewWidth = IntWidth;
2302 NewSign = true;
2303 } else if (ECD->getType() == BestType) {
2304 // Already the right type!
2305 continue;
2306 } else {
2307 NewTy = BestType;
2308 NewWidth = BestWidth;
2309 NewSign = BestType->isSignedIntegerType();
2310 }
2311
2312 // Adjust the APSInt value.
2313 InitVal.extOrTrunc(NewWidth);
2314 InitVal.setIsSigned(NewSign);
2315 ECD->setInitVal(InitVal);
2316
2317 // Adjust the Expr initializer and type.
2318 ECD->setInitExpr(new ImplicitCastExpr(NewTy, ECD->getInitExpr()));
2319 ECD->setType(NewTy);
2320 }
Chris Lattnerac609682007-08-28 06:15:15 +00002321
Chris Lattnere00b18c2007-08-28 18:24:31 +00002322 Enum->defineElements(EltList, BestType);
Chris Lattnere1e79852008-02-06 00:51:33 +00002323 Consumer.HandleTagDeclDefinition(Enum);
Reid Spencer5f016e22007-07-11 17:01:13 +00002324}
2325
Anders Carlssondfab6cb2008-02-08 00:33:21 +00002326Sema::DeclTy *Sema::ActOnFileScopeAsmDecl(SourceLocation Loc,
2327 ExprTy *expr) {
2328 StringLiteral *AsmString = cast<StringLiteral>((Expr*)expr);
2329
Chris Lattner8e25d862008-03-16 00:16:02 +00002330 return FileScopeAsmDecl::Create(Context, Loc, AsmString);
Anders Carlssondfab6cb2008-02-08 00:33:21 +00002331}
2332
Chris Lattnerc6fdc342008-01-12 07:05:38 +00002333Sema::DeclTy* Sema::ActOnLinkageSpec(SourceLocation Loc,
Chris Lattnerc81c8142008-02-25 21:04:36 +00002334 SourceLocation LBrace,
2335 SourceLocation RBrace,
2336 const char *Lang,
2337 unsigned StrSize,
2338 DeclTy *D) {
Chris Lattnerc6fdc342008-01-12 07:05:38 +00002339 LinkageSpecDecl::LanguageIDs Language;
2340 Decl *dcl = static_cast<Decl *>(D);
2341 if (strncmp(Lang, "\"C\"", StrSize) == 0)
2342 Language = LinkageSpecDecl::lang_c;
2343 else if (strncmp(Lang, "\"C++\"", StrSize) == 0)
2344 Language = LinkageSpecDecl::lang_cxx;
2345 else {
2346 Diag(Loc, diag::err_bad_language);
2347 return 0;
2348 }
2349
2350 // FIXME: Add all the various semantics of linkage specifications
Chris Lattner8e25d862008-03-16 00:16:02 +00002351 return LinkageSpecDecl::Create(Context, Loc, Language, dcl);
Chris Lattnerc6fdc342008-01-12 07:05:38 +00002352}