blob: c0c3f09b8c901b257aee36a3d26704a17ddb3d58 [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"
Chris Lattnere1e79852008-02-06 00:51:33 +000015#include "clang/AST/ASTConsumer.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000016#include "clang/AST/ASTContext.h"
Anders Carlssonf78915f2008-02-15 07:04:12 +000017#include "clang/AST/Attr.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "clang/AST/Builtins.h"
19#include "clang/AST/Decl.h"
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +000020#include "clang/AST/DeclCXX.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "clang/AST/Expr.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000022#include "clang/AST/ExprCXX.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023#include "clang/AST/Type.h"
24#include "clang/Parse/DeclSpec.h"
25#include "clang/Parse/Scope.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000026#include "clang/Basic/LangOptions.h"
27#include "clang/Basic/TargetInfo.h"
Steve Naroff4c49a6c2008-01-30 23:46:05 +000028#include "clang/Basic/SourceManager.h"
29// FIXME: layering (ideally, Sema shouldn't be dependent on Lex API's)
Chris Lattnere1e79852008-02-06 00:51:33 +000030#include "clang/Lex/Preprocessor.h"
Steve Naroff4c49a6c2008-01-30 23:46:05 +000031#include "clang/Lex/HeaderSearch.h"
Steve Naroff563477d2007-09-18 23:55:05 +000032#include "llvm/ADT/SmallString.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000033#include "llvm/ADT/SmallSet.h"
Fariborz Jahanian85ff2642007-10-05 18:00:57 +000034#include "llvm/ADT/DenseSet.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000035using namespace clang;
36
Steve Naroffb327ce02008-04-02 14:35:35 +000037Sema::DeclTy *Sema::isTypeName(const IdentifierInfo &II, Scope *S) {
38 Decl *IIDecl = LookupDecl(&II, Decl::IDNS_Ordinary, S, false);
39
Douglas Gregor2ce52f32008-04-13 21:07:44 +000040 if (IIDecl && (isa<TypedefDecl>(IIDecl) ||
41 isa<ObjCInterfaceDecl>(IIDecl) ||
42 isa<TagDecl>(IIDecl)))
Fariborz Jahanianbece4ac2007-10-12 16:34:10 +000043 return IIDecl;
Steve Naroff3536b442007-09-06 21:24:23 +000044 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000045}
46
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +000047DeclContext *Sema::getDCParent(DeclContext *DC) {
48 // If CurContext is a ObjC method, getParent() will return NULL.
49 if (isa<ObjCMethodDecl>(DC))
50 return Context.getTranslationUnitDecl();
51
52 // A C++ inline method is parsed *after* the topmost class it was declared in
53 // is fully parsed (it's "complete").
54 // The parsing of a C++ inline method happens at the declaration context of
55 // the topmost (non-nested) class it is declared in.
56 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(DC)) {
57 assert(isa<CXXRecordDecl>(MD->getParent()) && "C++ method not in Record.");
58 DC = MD->getParent();
59 while (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC->getParent()))
60 DC = RD;
61
62 // Return the declaration context of the topmost class the inline method is
63 // declared in.
64 return DC;
65 }
66
67 return DC->getParent();
68}
69
Chris Lattner9fdf9c62008-04-22 18:39:57 +000070void Sema::PushDeclContext(DeclContext *DC) {
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +000071 assert(getDCParent(DC) == CurContext &&
72 "The next DeclContext should be directly contained in the current one.");
Chris Lattner9fdf9c62008-04-22 18:39:57 +000073 CurContext = DC;
Chris Lattner0ed844b2008-04-04 06:12:32 +000074}
75
Chris Lattnerb048c982008-04-06 04:47:34 +000076void Sema::PopDeclContext() {
77 assert(CurContext && "DeclContext imbalance!");
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +000078 CurContext = getDCParent(CurContext);
Chris Lattner0ed844b2008-04-04 06:12:32 +000079}
80
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +000081/// Add this decl to the scope shadowed decl chains.
82void Sema::PushOnScopeChains(NamedDecl *D, Scope *S) {
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +000083 S->AddDecl(D);
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000084
85 // C++ [basic.scope]p4:
86 // -- exactly one declaration shall declare a class name or
87 // enumeration name that is not a typedef name and the other
88 // declarations shall all refer to the same object or
89 // enumerator, or all refer to functions and function templates;
90 // in this case the class name or enumeration name is hidden.
91 if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
92 // We are pushing the name of a tag (enum or class).
93 IdentifierResolver::ctx_iterator
94 CIT = IdResolver.ctx_begin(TD->getIdentifier(), TD->getDeclContext());
95 if (CIT != IdResolver.ctx_end(TD->getIdentifier()) &&
96 IdResolver.isDeclInScope(*CIT, TD->getDeclContext(), S)) {
97 // There is already a declaration with the same name in the same
98 // scope. It must be found before we find the new declaration,
99 // so swap the order on the shadowed declaration chain.
100
101 IdResolver.AddShadowedDecl(TD, *CIT);
102 return;
103 }
104 }
105
106 IdResolver.AddDecl(D);
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +0000107}
108
Steve Naroffb216c882007-10-09 22:01:59 +0000109void Sema::ActOnPopScope(SourceLocation Loc, Scope *S) {
Chris Lattner31e05722007-08-26 06:24:45 +0000110 if (S->decl_empty()) return;
111 assert((S->getFlags() & Scope::DeclScope) &&"Scope shouldn't contain decls!");
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000112
Reid Spencer5f016e22007-07-11 17:01:13 +0000113 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
114 I != E; ++I) {
Steve Naroffc752d042007-09-13 18:10:37 +0000115 Decl *TmpD = static_cast<Decl*>(*I);
116 assert(TmpD && "This decl didn't get pushed??");
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +0000117
118 if (isa<CXXFieldDecl>(TmpD)) continue;
119
120 assert(isa<ScopedDecl>(TmpD) && "Decl isn't ScopedDecl?");
121 ScopedDecl *D = cast<ScopedDecl>(TmpD);
Steve Naroffc752d042007-09-13 18:10:37 +0000122
Reid Spencer5f016e22007-07-11 17:01:13 +0000123 IdentifierInfo *II = D->getIdentifier();
124 if (!II) continue;
125
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +0000126 // We only want to remove the decls from the identifier decl chains for local
127 // scopes, when inside a function/method.
128 if (S->getFnParent() != 0)
129 IdResolver.RemoveDecl(D);
Chris Lattner7f925cc2008-04-11 07:00:53 +0000130
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +0000131 // Chain this decl to the containing DeclContext.
132 D->setNext(CurContext->getDeclChain());
133 CurContext->setDeclChain(D);
Reid Spencer5f016e22007-07-11 17:01:13 +0000134 }
135}
136
Steve Naroffe8043c32008-04-01 23:04:06 +0000137/// getObjCInterfaceDecl - Look up a for a class declaration in the scope.
138/// return 0 if one not found.
Steve Naroffe8043c32008-04-01 23:04:06 +0000139ObjCInterfaceDecl *Sema::getObjCInterfaceDecl(IdentifierInfo *Id) {
Steve Naroff31102512008-04-02 18:30:49 +0000140 // The third "scope" argument is 0 since we aren't enabling lazy built-in
141 // creation from this context.
142 Decl *IDecl = LookupDecl(Id, Decl::IDNS_Ordinary, 0, false);
Fariborz Jahanian4cabdfc2007-10-12 19:38:20 +0000143
Steve Naroffb327ce02008-04-02 14:35:35 +0000144 return dyn_cast_or_null<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanian4cabdfc2007-10-12 19:38:20 +0000145}
146
Steve Naroffe8043c32008-04-01 23:04:06 +0000147/// LookupDecl - Look up the inner-most declaration in the specified
Reid Spencer5f016e22007-07-11 17:01:13 +0000148/// namespace.
Steve Naroffb327ce02008-04-02 14:35:35 +0000149Decl *Sema::LookupDecl(const IdentifierInfo *II, unsigned NSI,
150 Scope *S, bool enableLazyBuiltinCreation) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000151 if (II == 0) return 0;
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000152 unsigned NS = NSI;
153 if (getLangOptions().CPlusPlus && (NS & Decl::IDNS_Ordinary))
154 NS |= Decl::IDNS_Tag;
Chris Lattner7f925cc2008-04-11 07:00:53 +0000155
Reid Spencer5f016e22007-07-11 17:01:13 +0000156 // Scan up the scope chain looking for a decl that matches this identifier
157 // that is in the appropriate namespace. This search should not take long, as
158 // shadowing of names is uncommon, and deep shadowing is extremely uncommon.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000159 for (IdentifierResolver::iterator
160 I = IdResolver.begin(II, CurContext), E = IdResolver.end(II); I != E; ++I)
161 if ((*I)->getIdentifierNamespace() & NS)
162 return *I;
Chris Lattner7f925cc2008-04-11 07:00:53 +0000163
Reid Spencer5f016e22007-07-11 17:01:13 +0000164 // If we didn't find a use of this identifier, and if the identifier
165 // corresponds to a compiler builtin, create the decl object for the builtin
166 // now, injecting it into translation unit scope, and return it.
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000167 if (NS & Decl::IDNS_Ordinary) {
Steve Naroffb327ce02008-04-02 14:35:35 +0000168 if (enableLazyBuiltinCreation) {
169 // If this is a builtin on this (or all) targets, create the decl.
170 if (unsigned BuiltinID = II->getBuiltinID())
171 return LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID, S);
172 }
Steve Naroffe8043c32008-04-01 23:04:06 +0000173 if (getLangOptions().ObjC1) {
174 // @interface and @compatibility_alias introduce typedef-like names.
175 // Unlike typedef's, they can only be introduced at file-scope (and are
Steve Naroffc822ff42008-04-02 00:39:51 +0000176 // therefore not scoped decls). They can, however, be shadowed by
Steve Naroffe8043c32008-04-01 23:04:06 +0000177 // other names in IDNS_Ordinary.
Steve Naroff31102512008-04-02 18:30:49 +0000178 ObjCInterfaceDeclsTy::iterator IDI = ObjCInterfaceDecls.find(II);
179 if (IDI != ObjCInterfaceDecls.end())
180 return IDI->second;
Steve Naroffe8043c32008-04-01 23:04:06 +0000181 ObjCAliasTy::iterator I = ObjCAliasDecls.find(II);
182 if (I != ObjCAliasDecls.end())
183 return I->second->getClassInterface();
184 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000185 }
186 return 0;
187}
188
Chris Lattner95e2c712008-05-05 22:18:14 +0000189void Sema::InitBuiltinVaListType() {
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000190 if (!Context.getBuiltinVaListType().isNull())
191 return;
192
193 IdentifierInfo *VaIdent = &Context.Idents.get("__builtin_va_list");
Steve Naroffb327ce02008-04-02 14:35:35 +0000194 Decl *VaDecl = LookupDecl(VaIdent, Decl::IDNS_Ordinary, TUScope);
Steve Naroff733002f2007-10-18 22:17:45 +0000195 TypedefDecl *VaTypedef = cast<TypedefDecl>(VaDecl);
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000196 Context.setBuiltinVaListType(Context.getTypedefType(VaTypedef));
197}
198
Reid Spencer5f016e22007-07-11 17:01:13 +0000199/// LazilyCreateBuiltin - The specified Builtin-ID was first used at file scope.
200/// lazily create a decl for it.
Chris Lattner22b73ba2007-10-10 23:42:28 +0000201ScopedDecl *Sema::LazilyCreateBuiltin(IdentifierInfo *II, unsigned bid,
202 Scope *S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000203 Builtin::ID BID = (Builtin::ID)bid;
204
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000205 if (BID == Builtin::BI__builtin_va_start ||
Chris Lattner95e2c712008-05-05 22:18:14 +0000206 BID == Builtin::BI__builtin_va_copy ||
207 BID == Builtin::BI__builtin_va_end)
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000208 InitBuiltinVaListType();
209
Anders Carlssonb2cf3572007-10-11 01:00:40 +0000210 QualType R = Context.BuiltinInfo.GetBuiltinType(BID, Context);
Argyrios Kyrtzidisff898cd2008-04-17 14:47:13 +0000211 FunctionDecl *New = FunctionDecl::Create(Context,
212 Context.getTranslationUnitDecl(),
Chris Lattner0ed844b2008-04-04 06:12:32 +0000213 SourceLocation(), II, R,
Chris Lattnera98e58d2008-03-15 21:24:04 +0000214 FunctionDecl::Extern, false, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000215
Chris Lattner95e2c712008-05-05 22:18:14 +0000216 // Create Decl objects for each parameter, adding them to the
217 // FunctionDecl.
218 if (FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(R)) {
219 llvm::SmallVector<ParmVarDecl*, 16> Params;
220 for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i)
221 Params.push_back(ParmVarDecl::Create(Context, New, SourceLocation(), 0,
222 FT->getArgType(i), VarDecl::None, 0,
223 0));
224 New->setParams(&Params[0], Params.size());
225 }
226
227
228
Chris Lattner7f925cc2008-04-11 07:00:53 +0000229 // TUScope is the translation-unit scope to insert this function into.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000230 PushOnScopeChains(New, TUScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000231 return New;
232}
233
234/// MergeTypeDefDecl - We just parsed a typedef 'New' which has the same name
235/// and scope as a previous declaration 'Old'. Figure out how to resolve this
236/// situation, merging decls or emitting diagnostics as appropriate.
237///
Steve Naroffe8043c32008-04-01 23:04:06 +0000238TypedefDecl *Sema::MergeTypeDefDecl(TypedefDecl *New, Decl *OldD) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000239 // Verify the old decl was also a typedef.
240 TypedefDecl *Old = dyn_cast<TypedefDecl>(OldD);
241 if (!Old) {
242 Diag(New->getLocation(), diag::err_redefinition_different_kind,
243 New->getName());
244 Diag(OldD->getLocation(), diag::err_previous_definition);
245 return New;
246 }
247
Steve Naroff8ee529b2007-10-31 18:42:27 +0000248 // Allow multiple definitions for ObjC built-in typedefs.
249 // FIXME: Verify the underlying types are equivalent!
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000250 if (getLangOptions().ObjC1 && isBuiltinObjCType(New))
Steve Naroff8ee529b2007-10-31 18:42:27 +0000251 return Old;
Eli Friedman54ecfce2008-06-11 06:20:39 +0000252
253 if (getLangOptions().Microsoft) return New;
254
Steve Naroff4c49a6c2008-01-30 23:46:05 +0000255 // Redeclaration of a type is a constraint violation (6.7.2.3p1).
256 // Apparently GCC, Intel, and Sun all silently ignore the redeclaration if
257 // *either* declaration is in a system header. The code below implements
258 // this adhoc compatibility rule. FIXME: The following code will not
259 // work properly when compiling ".i" files (containing preprocessed output).
260 SourceManager &SrcMgr = Context.getSourceManager();
Steve Naroff4c49a6c2008-01-30 23:46:05 +0000261 HeaderSearch &HdrInfo = PP.getHeaderSearchInfo();
Eli Friedman54ecfce2008-06-11 06:20:39 +0000262 const FileEntry *OldDeclFile = SrcMgr.getFileEntryForLoc(Old->getLocation());
263 if (OldDeclFile) {
264 DirectoryLookup::DirType OldDirType = HdrInfo.getFileDirFlavor(OldDeclFile);
265 // Allow reclarations in both SystemHeaderDir and ExternCSystemHeaderDir.
266 if (OldDirType != DirectoryLookup::NormalHeaderDir)
267 return New;
268 }
269 const FileEntry *NewDeclFile = SrcMgr.getFileEntryForLoc(New->getLocation());
270 if (NewDeclFile) {
271 DirectoryLookup::DirType NewDirType = HdrInfo.getFileDirFlavor(NewDeclFile);
272 // Allow reclarations in both SystemHeaderDir and ExternCSystemHeaderDir.
273 if (NewDirType != DirectoryLookup::NormalHeaderDir)
274 return New;
275 }
276
Ted Kremenek2d05c082008-05-23 21:28:18 +0000277 Diag(New->getLocation(), diag::err_redefinition, New->getName());
278 Diag(Old->getLocation(), diag::err_previous_definition);
Reid Spencer5f016e22007-07-11 17:01:13 +0000279 return New;
280}
281
Chris Lattner6b6b5372008-06-26 18:38:35 +0000282/// DeclhasAttr - returns true if decl Declaration already has the target
283/// attribute.
Chris Lattnerddee4232008-03-03 03:28:21 +0000284static bool DeclHasAttr(const Decl *decl, const Attr *target) {
285 for (const Attr *attr = decl->getAttrs(); attr; attr = attr->getNext())
286 if (attr->getKind() == target->getKind())
287 return true;
288
289 return false;
290}
291
292/// MergeAttributes - append attributes from the Old decl to the New one.
293static void MergeAttributes(Decl *New, Decl *Old) {
294 Attr *attr = const_cast<Attr*>(Old->getAttrs()), *tmp;
295
Chris Lattnerddee4232008-03-03 03:28:21 +0000296 while (attr) {
297 tmp = attr;
298 attr = attr->getNext();
299
300 if (!DeclHasAttr(New, tmp)) {
301 New->addAttr(tmp);
302 } else {
303 tmp->setNext(0);
304 delete(tmp);
305 }
306 }
Nuno Lopes9141bee2008-06-01 22:53:53 +0000307
308 Old->invalidateAttrs();
Chris Lattnerddee4232008-03-03 03:28:21 +0000309}
310
Chris Lattner04421082008-04-08 04:40:51 +0000311/// MergeFunctionDecl - We just parsed a function 'New' from
312/// declarator D which has the same name and scope as a previous
313/// declaration 'Old'. Figure out how to resolve this situation,
314/// merging decls or emitting diagnostics as appropriate.
Douglas Gregorf0097952008-04-21 02:02:58 +0000315/// Redeclaration will be set true if thisNew is a redeclaration OldD.
316FunctionDecl *
317Sema::MergeFunctionDecl(FunctionDecl *New, Decl *OldD, bool &Redeclaration) {
318 Redeclaration = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000319 // Verify the old decl was also a function.
320 FunctionDecl *Old = dyn_cast<FunctionDecl>(OldD);
321 if (!Old) {
322 Diag(New->getLocation(), diag::err_redefinition_different_kind,
323 New->getName());
324 Diag(OldD->getLocation(), diag::err_previous_definition);
325 return New;
326 }
Chris Lattner04421082008-04-08 04:40:51 +0000327
Chris Lattner8bcfc5b2008-04-06 23:10:54 +0000328 QualType OldQType = Context.getCanonicalType(Old->getType());
329 QualType NewQType = Context.getCanonicalType(New->getType());
Chris Lattner55196442007-11-20 19:04:50 +0000330
Chris Lattner04421082008-04-08 04:40:51 +0000331 // C++ [dcl.fct]p3:
332 // All declarations for a function shall agree exactly in both the
333 // return type and the parameter-type-list.
Douglas Gregorf0097952008-04-21 02:02:58 +0000334 if (getLangOptions().CPlusPlus && OldQType == NewQType) {
335 MergeAttributes(New, Old);
336 Redeclaration = true;
Chris Lattner04421082008-04-08 04:40:51 +0000337 return MergeCXXFunctionDecl(New, Old);
Douglas Gregorf0097952008-04-21 02:02:58 +0000338 }
Chris Lattner04421082008-04-08 04:40:51 +0000339
340 // C: Function types need to be compatible, not identical. This handles
Steve Naroffadbbd0c2008-01-14 20:51:29 +0000341 // duplicate function decls like "void f(int); void f(enum X);" properly.
Chris Lattner04421082008-04-08 04:40:51 +0000342 if (!getLangOptions().CPlusPlus &&
343 Context.functionTypesAreCompatible(OldQType, NewQType)) {
Douglas Gregorf0097952008-04-21 02:02:58 +0000344 MergeAttributes(New, Old);
345 Redeclaration = true;
Steve Naroffadbbd0c2008-01-14 20:51:29 +0000346 return New;
Chris Lattner04421082008-04-08 04:40:51 +0000347 }
Chris Lattnere3995fe2007-11-06 06:07:26 +0000348
Steve Naroff837618c2008-01-16 15:01:34 +0000349 // A function that has already been declared has been redeclared or defined
350 // with a different type- show appropriate diagnostic
Steve Naroffe2ef8152008-04-04 14:32:09 +0000351 diag::kind PrevDiag;
Douglas Gregorf0097952008-04-21 02:02:58 +0000352 if (Old->isThisDeclarationADefinition())
Steve Naroffe2ef8152008-04-04 14:32:09 +0000353 PrevDiag = diag::err_previous_definition;
354 else if (Old->isImplicit())
355 PrevDiag = diag::err_previous_implicit_declaration;
Chris Lattner04421082008-04-08 04:40:51 +0000356 else
Steve Naroffe2ef8152008-04-04 14:32:09 +0000357 PrevDiag = diag::err_previous_declaration;
Steve Naroff837618c2008-01-16 15:01:34 +0000358
Reid Spencer5f016e22007-07-11 17:01:13 +0000359 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
360 // TODO: This is totally simplistic. It should handle merging functions
361 // together etc, merging extern int X; int X; ...
Steve Naroff837618c2008-01-16 15:01:34 +0000362 Diag(New->getLocation(), diag::err_conflicting_types, New->getName());
363 Diag(Old->getLocation(), PrevDiag);
Reid Spencer5f016e22007-07-11 17:01:13 +0000364 return New;
365}
366
Chris Lattnerfcc2d262007-11-06 04:28:31 +0000367/// equivalentArrayTypes - Used to determine whether two array types are
368/// equivalent.
369/// We need to check this explicitly as an incomplete array definition is
370/// considered a VariableArrayType, so will not match a complete array
371/// definition that would be otherwise equivalent.
372static bool areEquivalentArrayTypes(QualType NewQType, QualType OldQType) {
373 const ArrayType *NewAT = NewQType->getAsArrayType();
374 const ArrayType *OldAT = OldQType->getAsArrayType();
375
376 if (!NewAT || !OldAT)
377 return false;
378
379 // If either (or both) array types in incomplete we need to strip off the
380 // outer VariableArrayType. Once the outer VAT is removed the remaining
381 // types must be identical if the array types are to be considered
382 // equivalent.
383 // eg. int[][1] and int[1][1] become
384 // VAT(null, CAT(1, int)) and CAT(1, CAT(1, int))
385 // removing the outermost VAT gives
386 // CAT(1, int) and CAT(1, int)
387 // which are equal, therefore the array types are equivalent.
Eli Friedman9db13972008-02-15 12:53:51 +0000388 if (NewAT->isIncompleteArrayType() || OldAT->isIncompleteArrayType()) {
Chris Lattnerfcc2d262007-11-06 04:28:31 +0000389 if (NewAT->getIndexTypeQualifier() != OldAT->getIndexTypeQualifier())
390 return false;
Eli Friedman04930252008-01-29 07:51:12 +0000391 NewQType = NewAT->getElementType().getCanonicalType();
392 OldQType = OldAT->getElementType().getCanonicalType();
Chris Lattnerfcc2d262007-11-06 04:28:31 +0000393 }
394
395 return NewQType == OldQType;
396}
397
Reid Spencer5f016e22007-07-11 17:01:13 +0000398/// MergeVarDecl - We just parsed a variable 'New' which has the same name
399/// and scope as a previous declaration 'Old'. Figure out how to resolve this
400/// situation, merging decls or emitting diagnostics as appropriate.
401///
402/// FIXME: Need to carefully consider tentative definition rules (C99 6.9.2p2).
403/// For example, we incorrectly complain about i1, i4 from C99 6.9.2p4.
404///
Steve Naroffe8043c32008-04-01 23:04:06 +0000405VarDecl *Sema::MergeVarDecl(VarDecl *New, Decl *OldD) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000406 // Verify the old decl was also a variable.
407 VarDecl *Old = dyn_cast<VarDecl>(OldD);
408 if (!Old) {
409 Diag(New->getLocation(), diag::err_redefinition_different_kind,
410 New->getName());
411 Diag(OldD->getLocation(), diag::err_previous_definition);
412 return New;
413 }
Chris Lattnerddee4232008-03-03 03:28:21 +0000414
415 MergeAttributes(New, Old);
416
Reid Spencer5f016e22007-07-11 17:01:13 +0000417 // Verify the types match.
Chris Lattner8bcfc5b2008-04-06 23:10:54 +0000418 QualType OldCType = Context.getCanonicalType(Old->getType());
419 QualType NewCType = Context.getCanonicalType(New->getType());
420 if (OldCType != NewCType && !areEquivalentArrayTypes(NewCType, OldCType)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000421 Diag(New->getLocation(), diag::err_redefinition, New->getName());
422 Diag(Old->getLocation(), diag::err_previous_definition);
423 return New;
424 }
Steve Naroffb7b032e2008-01-30 00:44:01 +0000425 // C99 6.2.2p4: Check if we have a static decl followed by a non-static.
426 if (New->getStorageClass() == VarDecl::Static &&
427 (Old->getStorageClass() == VarDecl::None ||
428 Old->getStorageClass() == VarDecl::Extern)) {
429 Diag(New->getLocation(), diag::err_static_non_static, New->getName());
430 Diag(Old->getLocation(), diag::err_previous_definition);
431 return New;
432 }
433 // C99 6.2.2p4: Check if we have a non-static decl followed by a static.
434 if (New->getStorageClass() != VarDecl::Static &&
435 Old->getStorageClass() == VarDecl::Static) {
436 Diag(New->getLocation(), diag::err_non_static_static, New->getName());
437 Diag(Old->getLocation(), diag::err_previous_definition);
438 return New;
439 }
440 // We've verified the types match, now handle "tentative" definitions.
Steve Naroff248a7532008-04-15 22:42:06 +0000441 if (Old->isFileVarDecl() && New->isFileVarDecl()) {
Steve Naroffb7b032e2008-01-30 00:44:01 +0000442 // Handle C "tentative" external object definitions (C99 6.9.2).
443 bool OldIsTentative = false;
444 bool NewIsTentative = false;
445
Steve Naroff248a7532008-04-15 22:42:06 +0000446 if (!Old->getInit() &&
447 (Old->getStorageClass() == VarDecl::None ||
448 Old->getStorageClass() == VarDecl::Static))
Steve Naroffb7b032e2008-01-30 00:44:01 +0000449 OldIsTentative = true;
450
451 // FIXME: this check doesn't work (since the initializer hasn't been
452 // attached yet). This check should be moved to FinalizeDeclaratorGroup.
453 // Unfortunately, by the time we get to FinializeDeclaratorGroup, we've
454 // thrown out the old decl.
Steve Naroff248a7532008-04-15 22:42:06 +0000455 if (!New->getInit() &&
456 (New->getStorageClass() == VarDecl::None ||
457 New->getStorageClass() == VarDecl::Static))
Steve Naroffb7b032e2008-01-30 00:44:01 +0000458 ; // change to NewIsTentative = true; once the code is moved.
459
460 if (NewIsTentative || OldIsTentative)
461 return New;
462 }
Steve Naroff235549c2008-05-12 22:36:43 +0000463 // Handle __private_extern__ just like extern.
Steve Naroffb7b032e2008-01-30 00:44:01 +0000464 if (Old->getStorageClass() != VarDecl::Extern &&
Steve Naroff235549c2008-05-12 22:36:43 +0000465 Old->getStorageClass() != VarDecl::PrivateExtern &&
466 New->getStorageClass() != VarDecl::Extern &&
467 New->getStorageClass() != VarDecl::PrivateExtern) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000468 Diag(New->getLocation(), diag::err_redefinition, New->getName());
469 Diag(Old->getLocation(), diag::err_previous_definition);
470 }
471 return New;
472}
473
Chris Lattner04421082008-04-08 04:40:51 +0000474/// CheckParmsForFunctionDef - Check that the parameters of the given
475/// function are appropriate for the definition of a function. This
476/// takes care of any checks that cannot be performed on the
477/// declaration itself, e.g., that the types of each of the function
478/// parameters are complete.
479bool Sema::CheckParmsForFunctionDef(FunctionDecl *FD) {
480 bool HasInvalidParm = false;
481 for (unsigned p = 0, NumParams = FD->getNumParams(); p < NumParams; ++p) {
482 ParmVarDecl *Param = FD->getParamDecl(p);
483
484 // C99 6.7.5.3p4: the parameters in a parameter type list in a
485 // function declarator that is part of a function definition of
486 // that function shall not have incomplete type.
487 if (Param->getType()->isIncompleteType() &&
488 !Param->isInvalidDecl()) {
489 Diag(Param->getLocation(), diag::err_typecheck_decl_incomplete_type,
490 Param->getType().getAsString());
491 Param->setInvalidDecl();
492 HasInvalidParm = true;
493 }
494 }
495
496 return HasInvalidParm;
497}
498
499/// CreateImplicitParameter - Creates an implicit function parameter
500/// in the scope S and with the given type. This routine is used, for
501/// example, to create the implicit "self" parameter in an Objective-C
502/// method.
Chris Lattner41110242008-06-17 18:05:57 +0000503ImplicitParamDecl *
Chris Lattner04421082008-04-08 04:40:51 +0000504Sema::CreateImplicitParameter(Scope *S, IdentifierInfo *Id,
505 SourceLocation IdLoc, QualType Type) {
Chris Lattner41110242008-06-17 18:05:57 +0000506 ImplicitParamDecl *New = ImplicitParamDecl::Create(Context, CurContext,
507 IdLoc, Id, Type, 0);
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +0000508 if (Id)
509 PushOnScopeChains(New, S);
Chris Lattner04421082008-04-08 04:40:51 +0000510
511 return New;
512}
513
Reid Spencer5f016e22007-07-11 17:01:13 +0000514/// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with
515/// no declarator (e.g. "struct foo;") is parsed.
516Sema::DeclTy *Sema::ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS) {
517 // TODO: emit error on 'int;' or 'const enum foo;'.
518 // TODO: emit error on 'typedef int;'
519 // if (!DS.isMissingDeclaratorOk()) Diag(...);
520
Steve Naroff92199282007-11-17 21:37:36 +0000521 return dyn_cast_or_null<TagDecl>(static_cast<Decl *>(DS.getTypeRep()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000522}
523
Steve Naroffd0091aa2008-01-10 22:15:12 +0000524bool Sema::CheckSingleInitializer(Expr *&Init, QualType DeclType) {
Steve Narofff0090632007-09-02 02:04:30 +0000525 // Get the type before calling CheckSingleAssignmentConstraints(), since
526 // it can promote the expression.
Chris Lattner5cf216b2008-01-04 18:04:52 +0000527 QualType InitType = Init->getType();
Steve Narofff0090632007-09-02 02:04:30 +0000528
Chris Lattner5cf216b2008-01-04 18:04:52 +0000529 AssignConvertType ConvTy = CheckSingleAssignmentConstraints(DeclType, Init);
530 return DiagnoseAssignmentResult(ConvTy, Init->getLocStart(), DeclType,
531 InitType, Init, "initializing");
Steve Narofff0090632007-09-02 02:04:30 +0000532}
533
Steve Naroffa49e1fa2008-01-22 00:55:40 +0000534bool Sema::CheckStringLiteralInit(StringLiteral *strLiteral, QualType &DeclT) {
Eli Friedmanc5773c42008-02-15 18:16:39 +0000535 if (const IncompleteArrayType *IAT = DeclT->getAsIncompleteArrayType()) {
Steve Naroffa49e1fa2008-01-22 00:55:40 +0000536 // C99 6.7.8p14. We have an array of character type with unknown size
537 // being initialized to a string literal.
538 llvm::APSInt ConstVal(32);
539 ConstVal = strLiteral->getByteLength() + 1;
540 // Return a new array type (C99 6.7.8p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000541 DeclT = Context.getConstantArrayType(IAT->getElementType(), ConstVal,
Steve Naroffa49e1fa2008-01-22 00:55:40 +0000542 ArrayType::Normal, 0);
543 } else if (const ConstantArrayType *CAT = DeclT->getAsConstantArrayType()) {
544 // C99 6.7.8p14. We have an array of character type with known size.
545 if (strLiteral->getByteLength() > (unsigned)CAT->getMaximumElements())
546 Diag(strLiteral->getSourceRange().getBegin(),
547 diag::warn_initializer_string_for_char_array_too_long,
548 strLiteral->getSourceRange());
549 } else {
550 assert(0 && "HandleStringLiteralInit(): Invalid array type");
551 }
552 // Set type from "char *" to "constant array of char".
553 strLiteral->setType(DeclT);
554 // For now, we always return false (meaning success).
555 return false;
556}
557
558StringLiteral *Sema::IsStringLiteralInit(Expr *Init, QualType DeclType) {
Steve Naroffa49e1fa2008-01-22 00:55:40 +0000559 const ArrayType *AT = DeclType->getAsArrayType();
Steve Naroffa9960332008-01-25 00:51:06 +0000560 if (AT && AT->getElementType()->isCharType()) {
561 return dyn_cast<StringLiteral>(Init);
562 }
Steve Naroffa49e1fa2008-01-22 00:55:40 +0000563 return 0;
564}
565
Steve Naroffa9960332008-01-25 00:51:06 +0000566bool Sema::CheckInitializerTypes(Expr *&Init, QualType &DeclType) {
Steve Naroffca107302008-01-21 23:53:58 +0000567 // C99 6.7.8p3: The type of the entity to be initialized shall be an array
568 // of unknown size ("[]") or an object type that is not a variable array type.
Eli Friedmanc5773c42008-02-15 18:16:39 +0000569 if (const VariableArrayType *VAT = DeclType->getAsVariableArrayType())
Steve Naroffca107302008-01-21 23:53:58 +0000570 return Diag(VAT->getSizeExpr()->getLocStart(),
571 diag::err_variable_object_no_init,
572 VAT->getSizeExpr()->getSourceRange());
573
Steve Naroff2fdc3742007-12-10 22:44:33 +0000574 InitListExpr *InitList = dyn_cast<InitListExpr>(Init);
575 if (!InitList) {
Steve Naroffa49e1fa2008-01-22 00:55:40 +0000576 // FIXME: Handle wide strings
577 if (StringLiteral *strLiteral = IsStringLiteralInit(Init, DeclType))
578 return CheckStringLiteralInit(strLiteral, DeclType);
Eli Friedmana312ce22008-02-08 00:48:24 +0000579
580 if (DeclType->isArrayType())
581 return Diag(Init->getLocStart(),
582 diag::err_array_init_list_required,
583 Init->getSourceRange());
584
Steve Naroffd0091aa2008-01-10 22:15:12 +0000585 return CheckSingleInitializer(Init, DeclType);
Steve Naroff2fdc3742007-12-10 22:44:33 +0000586 }
Eli Friedmane6f058f2008-06-06 19:40:52 +0000587
Steve Naroff0cca7492008-05-01 22:18:59 +0000588 InitListChecker CheckInitList(this, InitList, DeclType);
589 return CheckInitList.HadError();
Steve Narofff0090632007-09-02 02:04:30 +0000590}
591
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000592Sema::DeclTy *
Steve Naroff08d92e42007-09-15 18:49:24 +0000593Sema::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *lastDecl) {
Steve Naroff94745042007-09-13 23:52:58 +0000594 ScopedDecl *LastDeclarator = dyn_cast_or_null<ScopedDecl>((Decl *)lastDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000595 IdentifierInfo *II = D.getIdentifier();
596
Chris Lattnere80a59c2007-07-25 00:24:17 +0000597 // All of these full declarators require an identifier. If it doesn't have
598 // one, the ParsedFreeStandingDeclSpec action should be used.
599 if (II == 0) {
Chris Lattner311ff022007-10-16 22:36:42 +0000600 Diag(D.getDeclSpec().getSourceRange().getBegin(),
Chris Lattner98e08632007-08-28 06:17:15 +0000601 diag::err_declarator_need_ident,
Chris Lattnere80a59c2007-07-25 00:24:17 +0000602 D.getDeclSpec().getSourceRange(), D.getSourceRange());
603 return 0;
604 }
605
Chris Lattner31e05722007-08-26 06:24:45 +0000606 // The scope passed in may not be a decl scope. Zip up the scope tree until
607 // we find one that is.
608 while ((S->getFlags() & Scope::DeclScope) == 0)
609 S = S->getParent();
610
Reid Spencer5f016e22007-07-11 17:01:13 +0000611 // See if this is a redefinition of a variable in the same scope.
Steve Naroffb327ce02008-04-02 14:35:35 +0000612 Decl *PrevDecl = LookupDecl(II, Decl::IDNS_Ordinary, S);
Steve Naroffc752d042007-09-13 18:10:37 +0000613 ScopedDecl *New;
Steve Naroff5912a352007-08-28 20:14:24 +0000614 bool InvalidDecl = false;
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000615
616 // In C++, the previous declaration we find might be a tag type
617 // (class or enum). In this case, the new declaration will hide the
618 // tag type.
619 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
620 PrevDecl = 0;
621
Chris Lattner41af0932007-11-14 06:34:38 +0000622 QualType R = GetTypeForDeclarator(D, S);
623 assert(!R.isNull() && "GetTypeForDeclarator() returned null type");
624
Reid Spencer5f016e22007-07-11 17:01:13 +0000625 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
Douglas Gregor6d6eb572008-05-07 04:49:29 +0000626 // Check that there are no default arguments (C++ only).
627 if (getLangOptions().CPlusPlus)
628 CheckExtraCXXDefaultArguments(D);
629
Chris Lattner41af0932007-11-14 06:34:38 +0000630 TypedefDecl *NewTD = ParseTypedefDecl(S, D, R, LastDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +0000631 if (!NewTD) return 0;
632
633 // Handle attributes prior to checking for duplicates in MergeVarDecl
Chris Lattner3ff30c82008-06-29 00:02:00 +0000634 ProcessDeclAttributes(NewTD, D);
Steve Naroffffce4d52008-01-09 23:34:55 +0000635 // Merge the decl with the existing one if appropriate. If the decl is
636 // in an outer scope, it isn't the same thing.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000637 if (PrevDecl && IdResolver.isDeclInScope(PrevDecl, CurContext, S)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000638 NewTD = MergeTypeDefDecl(NewTD, PrevDecl);
639 if (NewTD == 0) return 0;
640 }
641 New = NewTD;
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000642 if (S->getFnParent() == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000643 // C99 6.7.7p2: If a typedef name specifies a variably modified type
644 // then it shall have block scope.
Eli Friedman9db13972008-02-15 12:53:51 +0000645 if (NewTD->getUnderlyingType()->isVariablyModifiedType()) {
646 // FIXME: Diagnostic needs to be fixed.
647 Diag(D.getIdentifierLoc(), diag::err_typecheck_illegal_vla);
Steve Naroffd7444aa2007-08-31 17:20:07 +0000648 InvalidDecl = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000649 }
650 }
Chris Lattner41af0932007-11-14 06:34:38 +0000651 } else if (R.getTypePtr()->isFunctionType()) {
Chris Lattner271f1a62007-09-27 15:15:46 +0000652 FunctionDecl::StorageClass SC = FunctionDecl::None;
Reid Spencer5f016e22007-07-11 17:01:13 +0000653 switch (D.getDeclSpec().getStorageClassSpec()) {
654 default: assert(0 && "Unknown storage class!");
655 case DeclSpec::SCS_auto:
656 case DeclSpec::SCS_register:
657 Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_func,
658 R.getAsString());
Steve Naroff5912a352007-08-28 20:14:24 +0000659 InvalidDecl = true;
660 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000661 case DeclSpec::SCS_unspecified: SC = FunctionDecl::None; break;
662 case DeclSpec::SCS_extern: SC = FunctionDecl::Extern; break;
663 case DeclSpec::SCS_static: SC = FunctionDecl::Static; break;
Steve Naroff7dd0bd42008-01-28 21:57:15 +0000664 case DeclSpec::SCS_private_extern: SC = FunctionDecl::PrivateExtern;break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000665 }
666
Chris Lattnera98e58d2008-03-15 21:24:04 +0000667 bool isInline = D.getDeclSpec().isInlineSpecified();
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000668 FunctionDecl *NewFD;
669 if (D.getContext() == Declarator::MemberContext) {
670 // This is a C++ method declaration.
671 NewFD = CXXMethodDecl::Create(Context, cast<CXXRecordDecl>(CurContext),
672 D.getIdentifierLoc(), II, R,
673 (SC == FunctionDecl::Static), isInline,
674 LastDeclarator);
675 } else {
676 NewFD = FunctionDecl::Create(Context, CurContext,
677 D.getIdentifierLoc(),
678 II, R, SC, isInline,
679 LastDeclarator);
680 }
Ted Kremenekf5c93c12008-02-27 22:18:07 +0000681 // Handle attributes.
Chris Lattner3ff30c82008-06-29 00:02:00 +0000682 ProcessDeclAttributes(NewFD, D);
Chris Lattner04421082008-04-08 04:40:51 +0000683
684 // Copy the parameter declarations from the declarator D to
685 // the function declaration NewFD, if they are available.
686 if (D.getNumTypeObjects() > 0 &&
687 D.getTypeObject(0).Fun.hasPrototype) {
688 DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
689
690 // Create Decl objects for each parameter, adding them to the
691 // FunctionDecl.
692 llvm::SmallVector<ParmVarDecl*, 16> Params;
693
694 // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs
695 // function that takes no arguments, not a function that takes a
Chris Lattner8123a952008-04-10 02:22:51 +0000696 // single void argument.
Eli Friedman6d1e4b52008-05-22 08:54:03 +0000697 // We let through "const void" here because Sema::GetTypeForDeclarator
698 // already checks for that case.
Chris Lattner04421082008-04-08 04:40:51 +0000699 if (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 &&
700 FTI.ArgInfo[0].Param &&
Chris Lattner04421082008-04-08 04:40:51 +0000701 ((ParmVarDecl*)FTI.ArgInfo[0].Param)->getType()->isVoidType()) {
702 // empty arg list, don't push any params.
Chris Lattner8123a952008-04-10 02:22:51 +0000703 ParmVarDecl *Param = (ParmVarDecl*)FTI.ArgInfo[0].Param;
704
Chris Lattnerdef026a2008-04-10 02:26:16 +0000705 // In C++, the empty parameter-type-list must be spelled "void"; a
706 // typedef of void is not permitted.
707 if (getLangOptions().CPlusPlus &&
Eli Friedman6d1e4b52008-05-22 08:54:03 +0000708 Param->getType().getUnqualifiedType() != Context.VoidTy) {
Chris Lattner8123a952008-04-10 02:22:51 +0000709 Diag(Param->getLocation(), diag::ext_param_typedef_of_void);
710 }
711
Chris Lattner04421082008-04-08 04:40:51 +0000712 } else {
713 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i)
714 Params.push_back((ParmVarDecl *)FTI.ArgInfo[i].Param);
715 }
716
717 NewFD->setParams(&Params[0], Params.size());
718 }
719
Steve Naroffffce4d52008-01-09 23:34:55 +0000720 // Merge the decl with the existing one if appropriate. Since C functions
721 // are in a flat namespace, make sure we consider decls in outer scopes.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000722 if (PrevDecl &&
723 (!getLangOptions().CPlusPlus ||
724 IdResolver.isDeclInScope(PrevDecl, CurContext, S)) ) {
Douglas Gregorf0097952008-04-21 02:02:58 +0000725 bool Redeclaration = false;
726 NewFD = MergeFunctionDecl(NewFD, PrevDecl, Redeclaration);
Reid Spencer5f016e22007-07-11 17:01:13 +0000727 if (NewFD == 0) return 0;
Douglas Gregorf0097952008-04-21 02:02:58 +0000728 if (Redeclaration) {
Eli Friedman27424962008-05-27 05:07:37 +0000729 NewFD->setPreviousDeclaration(cast<FunctionDecl>(PrevDecl));
Douglas Gregorf0097952008-04-21 02:02:58 +0000730 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000731 }
732 New = NewFD;
Chris Lattner04421082008-04-08 04:40:51 +0000733
734 // In C++, check default arguments now that we have merged decls.
735 if (getLangOptions().CPlusPlus)
736 CheckCXXDefaultArguments(NewFD);
Reid Spencer5f016e22007-07-11 17:01:13 +0000737 } else {
Douglas Gregor6d6eb572008-05-07 04:49:29 +0000738 // Check that there are no default arguments (C++ only).
739 if (getLangOptions().CPlusPlus)
740 CheckExtraCXXDefaultArguments(D);
741
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000742 if (R.getTypePtr()->isObjCInterfaceType()) {
Fariborz Jahaniane7f64cc2007-10-12 22:10:42 +0000743 Diag(D.getIdentifierLoc(), diag::err_statically_allocated_object,
744 D.getIdentifier()->getName());
745 InvalidDecl = true;
746 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000747
748 VarDecl *NewVD;
749 VarDecl::StorageClass SC;
750 switch (D.getDeclSpec().getStorageClassSpec()) {
Chris Lattner9e151e12008-03-15 21:10:16 +0000751 default: assert(0 && "Unknown storage class!");
752 case DeclSpec::SCS_unspecified: SC = VarDecl::None; break;
753 case DeclSpec::SCS_extern: SC = VarDecl::Extern; break;
754 case DeclSpec::SCS_static: SC = VarDecl::Static; break;
755 case DeclSpec::SCS_auto: SC = VarDecl::Auto; break;
756 case DeclSpec::SCS_register: SC = VarDecl::Register; break;
757 case DeclSpec::SCS_private_extern: SC = VarDecl::PrivateExtern; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000758 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000759 if (D.getContext() == Declarator::MemberContext) {
760 assert(SC == VarDecl::Static && "Invalid storage class for member!");
761 // This is a static data member for a C++ class.
762 NewVD = CXXClassVarDecl::Create(Context, cast<CXXRecordDecl>(CurContext),
763 D.getIdentifierLoc(), II,
764 R, LastDeclarator);
Steve Narofff0090632007-09-02 02:04:30 +0000765 } else {
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000766 if (S->getFnParent() == 0) {
767 // C99 6.9p2: The storage-class specifiers auto and register shall not
768 // appear in the declaration specifiers in an external declaration.
769 if (SC == VarDecl::Auto || SC == VarDecl::Register) {
770 Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_fscope,
771 R.getAsString());
772 InvalidDecl = true;
773 }
774 NewVD = VarDecl::Create(Context, CurContext, D.getIdentifierLoc(),
775 II, R, SC, LastDeclarator);
776 } else {
777 NewVD = VarDecl::Create(Context, CurContext, D.getIdentifierLoc(),
778 II, R, SC, LastDeclarator);
779 }
Steve Naroff53a32342007-08-28 18:45:29 +0000780 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000781 // Handle attributes prior to checking for duplicates in MergeVarDecl
Chris Lattner3ff30c82008-06-29 00:02:00 +0000782 ProcessDeclAttributes(NewVD, D);
Nate Begemanc8e89a82008-03-14 18:07:10 +0000783
784 // Emit an error if an address space was applied to decl with local storage.
785 // This includes arrays of objects with address space qualifiers, but not
786 // automatic variables that point to other address spaces.
787 // ISO/IEC TR 18037 S5.1.2
Nate Begeman8e7dafe2008-03-25 18:36:32 +0000788 if (NewVD->hasLocalStorage() && (NewVD->getType().getAddressSpace() != 0)) {
789 Diag(D.getIdentifierLoc(), diag::err_as_qualified_auto_decl);
790 InvalidDecl = true;
Nate Begeman5af27e02008-03-14 00:22:18 +0000791 }
Steve Naroffffce4d52008-01-09 23:34:55 +0000792 // Merge the decl with the existing one if appropriate. If the decl is
793 // in an outer scope, it isn't the same thing.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000794 if (PrevDecl && IdResolver.isDeclInScope(PrevDecl, CurContext, S)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000795 NewVD = MergeVarDecl(NewVD, PrevDecl);
796 if (NewVD == 0) return 0;
797 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000798 New = NewVD;
799 }
800
801 // If this has an identifier, add it to the scope stack.
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +0000802 if (II)
803 PushOnScopeChains(New, S);
Steve Naroff5912a352007-08-28 20:14:24 +0000804 // If any semantic error occurred, mark the decl as invalid.
805 if (D.getInvalidType() || InvalidDecl)
806 New->setInvalidDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +0000807
808 return New;
809}
810
Eli Friedmanc594b322008-05-20 13:48:25 +0000811bool Sema::CheckAddressConstantExpressionLValue(const Expr* Init) {
812 switch (Init->getStmtClass()) {
813 default:
814 Diag(Init->getExprLoc(),
815 diag::err_init_element_not_constant, Init->getSourceRange());
816 return true;
817 case Expr::ParenExprClass: {
818 const ParenExpr* PE = cast<ParenExpr>(Init);
819 return CheckAddressConstantExpressionLValue(PE->getSubExpr());
820 }
821 case Expr::CompoundLiteralExprClass:
822 return cast<CompoundLiteralExpr>(Init)->isFileScope();
823 case Expr::DeclRefExprClass: {
824 const Decl *D = cast<DeclRefExpr>(Init)->getDecl();
Eli Friedman97c0a392008-05-21 03:39:11 +0000825 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
826 if (VD->hasGlobalStorage())
827 return false;
828 Diag(Init->getExprLoc(),
829 diag::err_init_element_not_constant, Init->getSourceRange());
830 return true;
831 }
Eli Friedmanc594b322008-05-20 13:48:25 +0000832 if (isa<FunctionDecl>(D))
833 return false;
834 Diag(Init->getExprLoc(),
835 diag::err_init_element_not_constant, Init->getSourceRange());
Steve Naroffd0091aa2008-01-10 22:15:12 +0000836 return true;
837 }
Eli Friedmanc594b322008-05-20 13:48:25 +0000838 case Expr::MemberExprClass: {
839 const MemberExpr *M = cast<MemberExpr>(Init);
840 if (M->isArrow())
841 return CheckAddressConstantExpression(M->getBase());
842 return CheckAddressConstantExpressionLValue(M->getBase());
843 }
844 case Expr::ArraySubscriptExprClass: {
845 // FIXME: Should we pedwarn for "x[0+0]" (where x is a pointer)?
846 const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(Init);
847 return CheckAddressConstantExpression(ASE->getBase()) ||
848 CheckArithmeticConstantExpression(ASE->getIdx());
849 }
850 case Expr::StringLiteralClass:
851 case Expr::PreDefinedExprClass:
852 return false;
853 case Expr::UnaryOperatorClass: {
854 const UnaryOperator *Exp = cast<UnaryOperator>(Init);
855
856 // C99 6.6p9
857 if (Exp->getOpcode() == UnaryOperator::Deref)
Eli Friedman97c0a392008-05-21 03:39:11 +0000858 return CheckAddressConstantExpression(Exp->getSubExpr());
Eli Friedmanc594b322008-05-20 13:48:25 +0000859
860 Diag(Init->getExprLoc(),
861 diag::err_init_element_not_constant, Init->getSourceRange());
862 return true;
863 }
864 }
865}
866
867bool Sema::CheckAddressConstantExpression(const Expr* Init) {
868 switch (Init->getStmtClass()) {
869 default:
870 Diag(Init->getExprLoc(),
871 diag::err_init_element_not_constant, Init->getSourceRange());
872 return true;
873 case Expr::ParenExprClass: {
874 const ParenExpr* PE = cast<ParenExpr>(Init);
875 return CheckAddressConstantExpression(PE->getSubExpr());
876 }
877 case Expr::StringLiteralClass:
878 case Expr::ObjCStringLiteralClass:
879 return false;
880 case Expr::CallExprClass: {
881 const CallExpr *CE = cast<CallExpr>(Init);
882 if (CE->isBuiltinConstantExpr())
883 return false;
884 Diag(Init->getExprLoc(),
885 diag::err_init_element_not_constant, Init->getSourceRange());
886 return true;
887 }
888 case Expr::UnaryOperatorClass: {
889 const UnaryOperator *Exp = cast<UnaryOperator>(Init);
890
891 // C99 6.6p9
892 if (Exp->getOpcode() == UnaryOperator::AddrOf)
893 return CheckAddressConstantExpressionLValue(Exp->getSubExpr());
894
895 if (Exp->getOpcode() == UnaryOperator::Extension)
896 return CheckAddressConstantExpression(Exp->getSubExpr());
897
898 Diag(Init->getExprLoc(),
899 diag::err_init_element_not_constant, Init->getSourceRange());
900 return true;
901 }
902 case Expr::BinaryOperatorClass: {
903 // FIXME: Should we pedwarn for expressions like "a + 1 + 2"?
904 const BinaryOperator *Exp = cast<BinaryOperator>(Init);
905
906 Expr *PExp = Exp->getLHS();
907 Expr *IExp = Exp->getRHS();
908 if (IExp->getType()->isPointerType())
909 std::swap(PExp, IExp);
910
911 // FIXME: Should we pedwarn if IExp isn't an integer constant expression?
912 return CheckAddressConstantExpression(PExp) ||
913 CheckArithmeticConstantExpression(IExp);
914 }
915 case Expr::ImplicitCastExprClass: {
916 const Expr* SubExpr = cast<ImplicitCastExpr>(Init)->getSubExpr();
917
918 // Check for implicit promotion
919 if (SubExpr->getType()->isFunctionType() ||
920 SubExpr->getType()->isArrayType())
921 return CheckAddressConstantExpressionLValue(SubExpr);
922
923 // Check for pointer->pointer cast
924 if (SubExpr->getType()->isPointerType())
925 return CheckAddressConstantExpression(SubExpr);
926
927 if (SubExpr->getType()->isArithmeticType())
928 return CheckArithmeticConstantExpression(SubExpr);
929
930 Diag(Init->getExprLoc(),
931 diag::err_init_element_not_constant, Init->getSourceRange());
932 return true;
933 }
934 case Expr::CastExprClass: {
935 const Expr* SubExpr = cast<CastExpr>(Init)->getSubExpr();
936
937 // Check for pointer->pointer cast
938 if (SubExpr->getType()->isPointerType())
939 return CheckAddressConstantExpression(SubExpr);
940
941 // FIXME: Should we pedwarn for (int*)(0+0)?
942 if (SubExpr->getType()->isArithmeticType())
943 return CheckArithmeticConstantExpression(SubExpr);
944
945 Diag(Init->getExprLoc(),
946 diag::err_init_element_not_constant, Init->getSourceRange());
947 return true;
948 }
949 case Expr::ConditionalOperatorClass: {
950 // FIXME: Should we pedwarn here?
951 const ConditionalOperator *Exp = cast<ConditionalOperator>(Init);
952 if (!Exp->getCond()->getType()->isArithmeticType()) {
953 Diag(Init->getExprLoc(),
954 diag::err_init_element_not_constant, Init->getSourceRange());
955 return true;
956 }
957 if (CheckArithmeticConstantExpression(Exp->getCond()))
958 return true;
959 if (Exp->getLHS() &&
960 CheckAddressConstantExpression(Exp->getLHS()))
961 return true;
962 return CheckAddressConstantExpression(Exp->getRHS());
963 }
964 case Expr::AddrLabelExprClass:
965 return false;
966 }
967}
968
Eli Friedman4caf0552008-06-09 05:05:07 +0000969static const Expr* FindExpressionBaseAddress(const Expr* E);
970
971static const Expr* FindExpressionBaseAddressLValue(const Expr* E) {
972 switch (E->getStmtClass()) {
973 default:
974 return E;
975 case Expr::ParenExprClass: {
976 const ParenExpr* PE = cast<ParenExpr>(E);
977 return FindExpressionBaseAddressLValue(PE->getSubExpr());
978 }
979 case Expr::MemberExprClass: {
980 const MemberExpr *M = cast<MemberExpr>(E);
981 if (M->isArrow())
982 return FindExpressionBaseAddress(M->getBase());
983 return FindExpressionBaseAddressLValue(M->getBase());
984 }
985 case Expr::ArraySubscriptExprClass: {
986 const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(E);
987 return FindExpressionBaseAddress(ASE->getBase());
988 }
989 case Expr::UnaryOperatorClass: {
990 const UnaryOperator *Exp = cast<UnaryOperator>(E);
991
992 if (Exp->getOpcode() == UnaryOperator::Deref)
993 return FindExpressionBaseAddress(Exp->getSubExpr());
994
995 return E;
996 }
997 }
998}
999
1000static const Expr* FindExpressionBaseAddress(const Expr* E) {
1001 switch (E->getStmtClass()) {
1002 default:
1003 return E;
1004 case Expr::ParenExprClass: {
1005 const ParenExpr* PE = cast<ParenExpr>(E);
1006 return FindExpressionBaseAddress(PE->getSubExpr());
1007 }
1008 case Expr::UnaryOperatorClass: {
1009 const UnaryOperator *Exp = cast<UnaryOperator>(E);
1010
1011 // C99 6.6p9
1012 if (Exp->getOpcode() == UnaryOperator::AddrOf)
1013 return FindExpressionBaseAddressLValue(Exp->getSubExpr());
1014
1015 if (Exp->getOpcode() == UnaryOperator::Extension)
1016 return FindExpressionBaseAddress(Exp->getSubExpr());
1017
1018 return E;
1019 }
1020 case Expr::BinaryOperatorClass: {
1021 const BinaryOperator *Exp = cast<BinaryOperator>(E);
1022
1023 Expr *PExp = Exp->getLHS();
1024 Expr *IExp = Exp->getRHS();
1025 if (IExp->getType()->isPointerType())
1026 std::swap(PExp, IExp);
1027
1028 return FindExpressionBaseAddress(PExp);
1029 }
1030 case Expr::ImplicitCastExprClass: {
1031 const Expr* SubExpr = cast<ImplicitCastExpr>(E)->getSubExpr();
1032
1033 // Check for implicit promotion
1034 if (SubExpr->getType()->isFunctionType() ||
1035 SubExpr->getType()->isArrayType())
1036 return FindExpressionBaseAddressLValue(SubExpr);
1037
1038 // Check for pointer->pointer cast
1039 if (SubExpr->getType()->isPointerType())
1040 return FindExpressionBaseAddress(SubExpr);
1041
1042 // We assume that we have an arithmetic expression here;
1043 // if we don't, we'll figure it out later
1044 return 0;
1045 }
1046 case Expr::CastExprClass: {
1047 const Expr* SubExpr = cast<CastExpr>(E)->getSubExpr();
1048
1049 // Check for pointer->pointer cast
1050 if (SubExpr->getType()->isPointerType())
1051 return FindExpressionBaseAddress(SubExpr);
1052
1053 // We assume that we have an arithmetic expression here;
1054 // if we don't, we'll figure it out later
1055 return 0;
1056 }
1057 }
1058}
1059
Eli Friedmanc594b322008-05-20 13:48:25 +00001060bool Sema::CheckArithmeticConstantExpression(const Expr* Init) {
1061 switch (Init->getStmtClass()) {
1062 default:
1063 Diag(Init->getExprLoc(),
1064 diag::err_init_element_not_constant, Init->getSourceRange());
1065 return true;
1066 case Expr::ParenExprClass: {
1067 const ParenExpr* PE = cast<ParenExpr>(Init);
1068 return CheckArithmeticConstantExpression(PE->getSubExpr());
1069 }
1070 case Expr::FloatingLiteralClass:
1071 case Expr::IntegerLiteralClass:
1072 case Expr::CharacterLiteralClass:
1073 case Expr::ImaginaryLiteralClass:
1074 case Expr::TypesCompatibleExprClass:
1075 case Expr::CXXBoolLiteralExprClass:
1076 return false;
1077 case Expr::CallExprClass: {
1078 const CallExpr *CE = cast<CallExpr>(Init);
1079 if (CE->isBuiltinConstantExpr())
1080 return false;
1081 Diag(Init->getExprLoc(),
1082 diag::err_init_element_not_constant, Init->getSourceRange());
1083 return true;
1084 }
1085 case Expr::DeclRefExprClass: {
1086 const Decl *D = cast<DeclRefExpr>(Init)->getDecl();
1087 if (isa<EnumConstantDecl>(D))
1088 return false;
1089 Diag(Init->getExprLoc(),
1090 diag::err_init_element_not_constant, Init->getSourceRange());
1091 return true;
1092 }
1093 case Expr::CompoundLiteralExprClass:
1094 // Allow "(vector type){2,4}"; normal C constraints don't allow this,
1095 // but vectors are allowed to be magic.
1096 if (Init->getType()->isVectorType())
1097 return false;
1098 Diag(Init->getExprLoc(),
1099 diag::err_init_element_not_constant, Init->getSourceRange());
1100 return true;
1101 case Expr::UnaryOperatorClass: {
1102 const UnaryOperator *Exp = cast<UnaryOperator>(Init);
1103
1104 switch (Exp->getOpcode()) {
1105 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1106 // See C99 6.6p3.
1107 default:
1108 Diag(Init->getExprLoc(),
1109 diag::err_init_element_not_constant, Init->getSourceRange());
1110 return true;
1111 case UnaryOperator::SizeOf:
1112 case UnaryOperator::AlignOf:
1113 case UnaryOperator::OffsetOf:
1114 // sizeof(E) is a constantexpr if and only if E is not evaluted.
1115 // See C99 6.5.3.4p2 and 6.6p3.
1116 if (Exp->getSubExpr()->getType()->isConstantSizeType())
1117 return false;
1118 Diag(Init->getExprLoc(),
1119 diag::err_init_element_not_constant, Init->getSourceRange());
1120 return true;
1121 case UnaryOperator::Extension:
1122 case UnaryOperator::LNot:
1123 case UnaryOperator::Plus:
1124 case UnaryOperator::Minus:
1125 case UnaryOperator::Not:
1126 return CheckArithmeticConstantExpression(Exp->getSubExpr());
1127 }
1128 }
1129 case Expr::SizeOfAlignOfTypeExprClass: {
1130 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(Init);
1131 // Special check for void types, which are allowed as an extension
1132 if (Exp->getArgumentType()->isVoidType())
1133 return false;
1134 // alignof always evaluates to a constant.
1135 // FIXME: is sizeof(int[3.0]) a constant expression?
1136 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType()) {
1137 Diag(Init->getExprLoc(),
1138 diag::err_init_element_not_constant, Init->getSourceRange());
1139 return true;
1140 }
1141 return false;
1142 }
1143 case Expr::BinaryOperatorClass: {
1144 const BinaryOperator *Exp = cast<BinaryOperator>(Init);
1145
1146 if (Exp->getLHS()->getType()->isArithmeticType() &&
1147 Exp->getRHS()->getType()->isArithmeticType()) {
1148 return CheckArithmeticConstantExpression(Exp->getLHS()) ||
1149 CheckArithmeticConstantExpression(Exp->getRHS());
1150 }
1151
Eli Friedman4caf0552008-06-09 05:05:07 +00001152 if (Exp->getLHS()->getType()->isPointerType() &&
1153 Exp->getRHS()->getType()->isPointerType()) {
1154 const Expr* LHSBase = FindExpressionBaseAddress(Exp->getLHS());
1155 const Expr* RHSBase = FindExpressionBaseAddress(Exp->getRHS());
1156
1157 // Only allow a null (constant integer) base; we could
1158 // allow some additional cases if necessary, but this
1159 // is sufficient to cover offsetof-like constructs.
1160 if (!LHSBase && !RHSBase) {
1161 return CheckAddressConstantExpression(Exp->getLHS()) ||
1162 CheckAddressConstantExpression(Exp->getRHS());
1163 }
1164 }
1165
Eli Friedmanc594b322008-05-20 13:48:25 +00001166 Diag(Init->getExprLoc(),
1167 diag::err_init_element_not_constant, Init->getSourceRange());
1168 return true;
1169 }
1170 case Expr::ImplicitCastExprClass:
1171 case Expr::CastExprClass: {
1172 const Expr *SubExpr;
1173 if (const CastExpr *C = dyn_cast<CastExpr>(Init)) {
1174 SubExpr = C->getSubExpr();
1175 } else {
1176 SubExpr = cast<ImplicitCastExpr>(Init)->getSubExpr();
1177 }
1178
1179 if (SubExpr->getType()->isArithmeticType())
1180 return CheckArithmeticConstantExpression(SubExpr);
1181
1182 Diag(Init->getExprLoc(),
1183 diag::err_init_element_not_constant, Init->getSourceRange());
1184 return true;
1185 }
1186 case Expr::ConditionalOperatorClass: {
1187 const ConditionalOperator *Exp = cast<ConditionalOperator>(Init);
1188 if (CheckArithmeticConstantExpression(Exp->getCond()))
1189 return true;
1190 if (Exp->getLHS() &&
1191 CheckArithmeticConstantExpression(Exp->getLHS()))
1192 return true;
1193 return CheckArithmeticConstantExpression(Exp->getRHS());
1194 }
1195 }
1196}
1197
1198bool Sema::CheckForConstantInitializer(Expr *Init, QualType DclT) {
1199 // Look through CXXDefaultArgExprs; they have no meaning in this context.
1200 if (CXXDefaultArgExpr* DAE = dyn_cast<CXXDefaultArgExpr>(Init))
1201 return CheckForConstantInitializer(DAE->getExpr(), DclT);
1202
1203 if (Init->getType()->isReferenceType()) {
1204 // FIXME: Work out how the heck reference types work
1205 return false;
1206#if 0
1207 // A reference is constant if the address of the expression
1208 // is constant
1209 // We look through initlists here to simplify
1210 // CheckAddressConstantExpressionLValue.
1211 if (InitListExpr *Exp = dyn_cast<InitListExpr>(Init)) {
1212 assert(Exp->getNumInits() > 0 &&
1213 "Refernce initializer cannot be empty");
1214 Init = Exp->getInit(0);
1215 }
1216 return CheckAddressConstantExpressionLValue(Init);
1217#endif
1218 }
1219
1220 if (InitListExpr *Exp = dyn_cast<InitListExpr>(Init)) {
1221 unsigned numInits = Exp->getNumInits();
1222 for (unsigned i = 0; i < numInits; i++) {
1223 // FIXME: Need to get the type of the declaration for C++,
1224 // because it could be a reference?
1225 if (CheckForConstantInitializer(Exp->getInit(i),
1226 Exp->getInit(i)->getType()))
1227 return true;
1228 }
1229 return false;
1230 }
1231
1232 if (Init->isNullPointerConstant(Context))
1233 return false;
1234 if (Init->getType()->isArithmeticType()) {
Eli Friedmanc1cc6dc2008-05-30 18:14:48 +00001235 QualType InitTy = Init->getType().getCanonicalType().getUnqualifiedType();
1236 if (InitTy == Context.BoolTy) {
1237 // Special handling for pointers implicitly cast to bool;
1238 // (e.g. "_Bool rr = &rr;"). This is only legal at the top level.
1239 if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Init)) {
1240 Expr* SubE = ICE->getSubExpr();
1241 if (SubE->getType()->isPointerType() ||
1242 SubE->getType()->isArrayType() ||
1243 SubE->getType()->isFunctionType()) {
1244 return CheckAddressConstantExpression(Init);
1245 }
1246 }
1247 } else if (InitTy->isIntegralType()) {
1248 Expr* SubE = 0;
1249 if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Init))
1250 SubE = ICE->getSubExpr();
1251 else if (CastExpr* CE = dyn_cast<CastExpr>(Init))
1252 SubE = CE->getSubExpr();
1253 // Special check for pointer cast to int; we allow as an extension
1254 // an address constant cast to an integer if the integer
1255 // is of an appropriate width (this sort of code is apparently used
1256 // in some places).
1257 // FIXME: Add pedwarn?
1258 // FIXME: Don't allow bitfields here! Need the FieldDecl for that.
1259 if (SubE && (SubE->getType()->isPointerType() ||
1260 SubE->getType()->isArrayType() ||
1261 SubE->getType()->isFunctionType())) {
1262 unsigned IntWidth = Context.getTypeSize(Init->getType());
1263 unsigned PointerWidth = Context.getTypeSize(Context.VoidPtrTy);
1264 if (IntWidth >= PointerWidth)
1265 return CheckAddressConstantExpression(Init);
1266 }
Eli Friedmanc594b322008-05-20 13:48:25 +00001267 }
1268
1269 return CheckArithmeticConstantExpression(Init);
1270 }
1271
1272 if (Init->getType()->isPointerType())
1273 return CheckAddressConstantExpression(Init);
1274
Eli Friedmanc1cc6dc2008-05-30 18:14:48 +00001275 // An array type at the top level that isn't an init-list must
1276 // be a string literal
Eli Friedmanc594b322008-05-20 13:48:25 +00001277 if (Init->getType()->isArrayType())
1278 return false;
1279
1280 Diag(Init->getExprLoc(), diag::err_init_element_not_constant,
1281 Init->getSourceRange());
1282 return true;
Steve Naroffd0091aa2008-01-10 22:15:12 +00001283}
1284
Steve Naroffbb204692007-09-12 14:07:44 +00001285void Sema::AddInitializerToDecl(DeclTy *dcl, ExprTy *init) {
Steve Naroff410e3e22007-09-12 20:13:48 +00001286 Decl *RealDecl = static_cast<Decl *>(dcl);
Steve Naroffbb204692007-09-12 14:07:44 +00001287 Expr *Init = static_cast<Expr *>(init);
Chris Lattner9a11b9a2007-10-19 20:10:30 +00001288 assert(Init && "missing initializer");
Steve Naroffbb204692007-09-12 14:07:44 +00001289
Chris Lattner9a11b9a2007-10-19 20:10:30 +00001290 // If there is no declaration, there was an error parsing it. Just ignore
1291 // the initializer.
1292 if (RealDecl == 0) {
1293 delete Init;
1294 return;
1295 }
Steve Naroffbb204692007-09-12 14:07:44 +00001296
Steve Naroff410e3e22007-09-12 20:13:48 +00001297 VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl);
1298 if (!VDecl) {
Steve Naroff8e74c932007-09-13 21:41:19 +00001299 Diag(dyn_cast<ScopedDecl>(RealDecl)->getLocation(),
1300 diag::err_illegal_initializer);
Steve Naroff410e3e22007-09-12 20:13:48 +00001301 RealDecl->setInvalidDecl();
1302 return;
1303 }
Steve Naroffbb204692007-09-12 14:07:44 +00001304 // Get the decls type and save a reference for later, since
Steve Naroffd0091aa2008-01-10 22:15:12 +00001305 // CheckInitializerTypes may change it.
Steve Naroff410e3e22007-09-12 20:13:48 +00001306 QualType DclT = VDecl->getType(), SavT = DclT;
Steve Naroff248a7532008-04-15 22:42:06 +00001307 if (VDecl->isBlockVarDecl()) {
1308 VarDecl::StorageClass SC = VDecl->getStorageClass();
Steve Naroffbb204692007-09-12 14:07:44 +00001309 if (SC == VarDecl::Extern) { // C99 6.7.8p5
Steve Naroff410e3e22007-09-12 20:13:48 +00001310 Diag(VDecl->getLocation(), diag::err_block_extern_cant_init);
Steve Naroff248a7532008-04-15 22:42:06 +00001311 VDecl->setInvalidDecl();
1312 } else if (!VDecl->isInvalidDecl()) {
Steve Naroffa9960332008-01-25 00:51:06 +00001313 if (CheckInitializerTypes(Init, DclT))
Steve Naroff248a7532008-04-15 22:42:06 +00001314 VDecl->setInvalidDecl();
Steve Naroffd0091aa2008-01-10 22:15:12 +00001315 if (SC == VarDecl::Static) // C99 6.7.8p4.
1316 CheckForConstantInitializer(Init, DclT);
Steve Naroffbb204692007-09-12 14:07:44 +00001317 }
Steve Naroff248a7532008-04-15 22:42:06 +00001318 } else if (VDecl->isFileVarDecl()) {
1319 if (VDecl->getStorageClass() == VarDecl::Extern)
Steve Naroff410e3e22007-09-12 20:13:48 +00001320 Diag(VDecl->getLocation(), diag::warn_extern_init);
Steve Naroff248a7532008-04-15 22:42:06 +00001321 if (!VDecl->isInvalidDecl())
Steve Naroffa9960332008-01-25 00:51:06 +00001322 if (CheckInitializerTypes(Init, DclT))
Steve Naroff248a7532008-04-15 22:42:06 +00001323 VDecl->setInvalidDecl();
Steve Naroffd0091aa2008-01-10 22:15:12 +00001324
1325 // C99 6.7.8p4. All file scoped initializers need to be constant.
1326 CheckForConstantInitializer(Init, DclT);
Steve Naroffbb204692007-09-12 14:07:44 +00001327 }
1328 // If the type changed, it means we had an incomplete type that was
1329 // completed by the initializer. For example:
1330 // int ary[] = { 1, 3, 5 };
1331 // "ary" transitions from a VariableArrayType to a ConstantArrayType.
Christopher Lamb48b12392007-11-29 19:09:19 +00001332 if (!VDecl->isInvalidDecl() && (DclT != SavT)) {
Steve Naroff410e3e22007-09-12 20:13:48 +00001333 VDecl->setType(DclT);
Christopher Lamb48b12392007-11-29 19:09:19 +00001334 Init->setType(DclT);
1335 }
Steve Naroffbb204692007-09-12 14:07:44 +00001336
1337 // Attach the initializer to the decl.
Steve Naroff410e3e22007-09-12 20:13:48 +00001338 VDecl->setInit(Init);
Steve Naroffbb204692007-09-12 14:07:44 +00001339 return;
1340}
1341
Reid Spencer5f016e22007-07-11 17:01:13 +00001342/// The declarators are chained together backwards, reverse the list.
1343Sema::DeclTy *Sema::FinalizeDeclaratorGroup(Scope *S, DeclTy *group) {
1344 // Often we have single declarators, handle them quickly.
Steve Naroff94745042007-09-13 23:52:58 +00001345 Decl *GroupDecl = static_cast<Decl*>(group);
1346 if (GroupDecl == 0)
Steve Naroffbb204692007-09-12 14:07:44 +00001347 return 0;
Steve Naroff94745042007-09-13 23:52:58 +00001348
1349 ScopedDecl *Group = dyn_cast<ScopedDecl>(GroupDecl);
1350 ScopedDecl *NewGroup = 0;
Steve Naroffbb204692007-09-12 14:07:44 +00001351 if (Group->getNextDeclarator() == 0)
Reid Spencer5f016e22007-07-11 17:01:13 +00001352 NewGroup = Group;
Steve Naroffbb204692007-09-12 14:07:44 +00001353 else { // reverse the list.
1354 while (Group) {
Steve Naroff94745042007-09-13 23:52:58 +00001355 ScopedDecl *Next = Group->getNextDeclarator();
Steve Naroffbb204692007-09-12 14:07:44 +00001356 Group->setNextDeclarator(NewGroup);
1357 NewGroup = Group;
1358 Group = Next;
1359 }
1360 }
1361 // Perform semantic analysis that depends on having fully processed both
1362 // the declarator and initializer.
Steve Naroff94745042007-09-13 23:52:58 +00001363 for (ScopedDecl *ID = NewGroup; ID; ID = ID->getNextDeclarator()) {
Steve Naroffbb204692007-09-12 14:07:44 +00001364 VarDecl *IDecl = dyn_cast<VarDecl>(ID);
1365 if (!IDecl)
1366 continue;
Steve Naroffbb204692007-09-12 14:07:44 +00001367 QualType T = IDecl->getType();
1368
1369 // C99 6.7.5.2p2: If an identifier is declared to be an object with
1370 // static storage duration, it shall not have a variable length array.
Steve Naroff248a7532008-04-15 22:42:06 +00001371 if ((IDecl->isFileVarDecl() || IDecl->isBlockVarDecl()) &&
1372 IDecl->getStorageClass() == VarDecl::Static) {
Eli Friedman3fe02932008-02-15 19:53:52 +00001373 if (T->getAsVariableArrayType()) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001374 Diag(IDecl->getLocation(), diag::err_typecheck_illegal_vla);
1375 IDecl->setInvalidDecl();
Steve Naroffbb204692007-09-12 14:07:44 +00001376 }
1377 }
1378 // Block scope. C99 6.7p7: If an identifier for an object is declared with
1379 // no linkage (C99 6.2.2p6), the type for the object shall be complete...
Steve Naroff248a7532008-04-15 22:42:06 +00001380 if (IDecl->isBlockVarDecl() &&
1381 IDecl->getStorageClass() != VarDecl::Extern) {
Chris Lattnerfd89bc82008-04-02 01:05:10 +00001382 if (T->isIncompleteType() && !IDecl->isInvalidDecl()) {
Chris Lattner8b1be772007-12-02 07:50:03 +00001383 Diag(IDecl->getLocation(), diag::err_typecheck_decl_incomplete_type,
1384 T.getAsString());
Steve Naroffbb204692007-09-12 14:07:44 +00001385 IDecl->setInvalidDecl();
1386 }
1387 }
1388 // File scope. C99 6.9.2p2: A declaration of an identifier for and
1389 // object that has file scope without an initializer, and without a
1390 // storage-class specifier or with the storage-class specifier "static",
1391 // constitutes a tentative definition. Note: A tentative definition with
1392 // external linkage is valid (C99 6.2.2p5).
Steve Naroff248a7532008-04-15 22:42:06 +00001393 if (IDecl && !IDecl->getInit() &&
1394 (IDecl->getStorageClass() == VarDecl::Static ||
1395 IDecl->getStorageClass() == VarDecl::None)) {
Eli Friedman9db13972008-02-15 12:53:51 +00001396 if (T->isIncompleteArrayType()) {
Steve Naroff9a75f8a2008-01-18 20:40:52 +00001397 // C99 6.9.2 (p2, p5): Implicit initialization causes an incomplete
1398 // array to be completed. Don't issue a diagnostic.
Chris Lattnerfd89bc82008-04-02 01:05:10 +00001399 } else if (T->isIncompleteType() && !IDecl->isInvalidDecl()) {
Steve Naroff9a75f8a2008-01-18 20:40:52 +00001400 // C99 6.9.2p3: If the declaration of an identifier for an object is
1401 // a tentative definition and has internal linkage (C99 6.2.2p3), the
1402 // declared type shall not be an incomplete type.
Chris Lattner8b1be772007-12-02 07:50:03 +00001403 Diag(IDecl->getLocation(), diag::err_typecheck_decl_incomplete_type,
1404 T.getAsString());
Steve Naroffbb204692007-09-12 14:07:44 +00001405 IDecl->setInvalidDecl();
1406 }
1407 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001408 }
1409 return NewGroup;
1410}
Steve Naroffe1223f72007-08-28 03:03:08 +00001411
Chris Lattner04421082008-04-08 04:40:51 +00001412/// ActOnParamDeclarator - Called from Parser::ParseFunctionDeclarator()
1413/// to introduce parameters into function prototype scope.
1414Sema::DeclTy *
1415Sema::ActOnParamDeclarator(Scope *S, Declarator &D) {
Chris Lattner985abd92008-06-26 06:49:43 +00001416 const DeclSpec &DS = D.getDeclSpec();
Chris Lattner04421082008-04-08 04:40:51 +00001417
1418 // Verify C99 6.7.5.3p2: The only SCS allowed is 'register'.
1419 if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
1420 DS.getStorageClassSpec() != DeclSpec::SCS_register) {
1421 Diag(DS.getStorageClassSpecLoc(),
1422 diag::err_invalid_storage_class_in_func_decl);
Chris Lattner985abd92008-06-26 06:49:43 +00001423 D.getMutableDeclSpec().ClearStorageClassSpecs();
Chris Lattner04421082008-04-08 04:40:51 +00001424 }
1425 if (DS.isThreadSpecified()) {
1426 Diag(DS.getThreadSpecLoc(),
1427 diag::err_invalid_storage_class_in_func_decl);
Chris Lattner985abd92008-06-26 06:49:43 +00001428 D.getMutableDeclSpec().ClearStorageClassSpecs();
Chris Lattner04421082008-04-08 04:40:51 +00001429 }
1430
Douglas Gregor6d6eb572008-05-07 04:49:29 +00001431 // Check that there are no default arguments inside the type of this
1432 // parameter (C++ only).
1433 if (getLangOptions().CPlusPlus)
1434 CheckExtraCXXDefaultArguments(D);
1435
Chris Lattner04421082008-04-08 04:40:51 +00001436 // In this context, we *do not* check D.getInvalidType(). If the declarator
1437 // type was invalid, GetTypeForDeclarator() still returns a "valid" type,
1438 // though it will not reflect the user specified type.
1439 QualType parmDeclType = GetTypeForDeclarator(D, S);
1440
1441 assert(!parmDeclType.isNull() && "GetTypeForDeclarator() returned null type");
1442
Reid Spencer5f016e22007-07-11 17:01:13 +00001443 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
1444 // Can this happen for params? We already checked that they don't conflict
1445 // among each other. Here they can only shadow globals, which is ok.
Chris Lattner04421082008-04-08 04:40:51 +00001446 IdentifierInfo *II = D.getIdentifier();
1447 if (Decl *PrevDecl = LookupDecl(II, Decl::IDNS_Ordinary, S)) {
1448 if (S->isDeclScope(PrevDecl)) {
1449 Diag(D.getIdentifierLoc(), diag::err_param_redefinition,
1450 dyn_cast<NamedDecl>(PrevDecl)->getName());
1451
1452 // Recover by removing the name
1453 II = 0;
1454 D.SetIdentifier(0, D.getIdentifierLoc());
1455 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001456 }
Steve Naroff6a9f3e32007-08-07 22:44:21 +00001457
1458 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
1459 // Doing the promotion here has a win and a loss. The win is the type for
1460 // both Decl's and DeclRefExpr's will match (a convenient invariant for the
1461 // code generator). The loss is the orginal type isn't preserved. For example:
1462 //
1463 // void func(int parmvardecl[5]) { // convert "int [5]" to "int *"
1464 // int blockvardecl[5];
1465 // sizeof(parmvardecl); // size == 4
1466 // sizeof(blockvardecl); // size == 20
1467 // }
1468 //
1469 // For expressions, all implicit conversions are captured using the
1470 // ImplicitCastExpr AST node (we have no such mechanism for Decl's).
1471 //
1472 // FIXME: If a source translation tool needs to see the original type, then
1473 // we need to consider storing both types (in ParmVarDecl)...
1474 //
Chris Lattnere6327742008-04-02 05:18:44 +00001475 if (parmDeclType->isArrayType()) {
Chris Lattner529bd022008-01-02 22:50:48 +00001476 // int x[restrict 4] -> int *restrict
Chris Lattnere6327742008-04-02 05:18:44 +00001477 parmDeclType = Context.getArrayDecayedType(parmDeclType);
Chris Lattner529bd022008-01-02 22:50:48 +00001478 } else if (parmDeclType->isFunctionType())
Steve Naroff6a9f3e32007-08-07 22:44:21 +00001479 parmDeclType = Context.getPointerType(parmDeclType);
1480
Chris Lattner04421082008-04-08 04:40:51 +00001481 ParmVarDecl *New = ParmVarDecl::Create(Context, CurContext,
1482 D.getIdentifierLoc(), II,
1483 parmDeclType, VarDecl::None,
1484 0, 0);
Anders Carlssonf78915f2008-02-15 07:04:12 +00001485
Chris Lattner04421082008-04-08 04:40:51 +00001486 if (D.getInvalidType())
Steve Naroff53a32342007-08-28 18:45:29 +00001487 New->setInvalidDecl();
1488
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +00001489 if (II)
1490 PushOnScopeChains(New, S);
Nate Begemanb7894b52008-02-17 21:20:31 +00001491
Chris Lattner3ff30c82008-06-29 00:02:00 +00001492 ProcessDeclAttributes(New, D);
Reid Spencer5f016e22007-07-11 17:01:13 +00001493 return New;
Chris Lattner04421082008-04-08 04:40:51 +00001494
Reid Spencer5f016e22007-07-11 17:01:13 +00001495}
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001496
Chris Lattnerb652cea2007-10-09 17:14:05 +00001497Sema::DeclTy *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Declarator &D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +00001498 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
Reid Spencer5f016e22007-07-11 17:01:13 +00001499 assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function &&
1500 "Not a function declarator!");
1501 DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
Chris Lattner04421082008-04-08 04:40:51 +00001502
Reid Spencer5f016e22007-07-11 17:01:13 +00001503 // Verify 6.9.1p6: 'every identifier in the identifier list shall be declared'
1504 // for a K&R function.
1505 if (!FTI.hasPrototype) {
1506 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Chris Lattner04421082008-04-08 04:40:51 +00001507 if (FTI.ArgInfo[i].Param == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001508 Diag(FTI.ArgInfo[i].IdentLoc, diag::ext_param_not_declared,
1509 FTI.ArgInfo[i].Ident->getName());
1510 // Implicitly declare the argument as type 'int' for lack of a better
1511 // type.
Chris Lattner04421082008-04-08 04:40:51 +00001512 DeclSpec DS;
1513 const char* PrevSpec; // unused
1514 DS.SetTypeSpecType(DeclSpec::TST_int, FTI.ArgInfo[i].IdentLoc,
1515 PrevSpec);
1516 Declarator ParamD(DS, Declarator::KNRTypeListContext);
1517 ParamD.SetIdentifier(FTI.ArgInfo[i].Ident, FTI.ArgInfo[i].IdentLoc);
1518 FTI.ArgInfo[i].Param = ActOnParamDeclarator(FnBodyScope, ParamD);
Reid Spencer5f016e22007-07-11 17:01:13 +00001519 }
1520 }
Chris Lattner52804082008-02-17 19:31:09 +00001521
Reid Spencer5f016e22007-07-11 17:01:13 +00001522 // Since this is a function definition, act as though we have information
1523 // about the arguments.
Chris Lattner52804082008-02-17 19:31:09 +00001524 if (FTI.NumArgs)
1525 FTI.hasPrototype = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001526 } else {
Chris Lattner04421082008-04-08 04:40:51 +00001527 // FIXME: Diagnose arguments without names in C.
Reid Spencer5f016e22007-07-11 17:01:13 +00001528 }
1529
1530 Scope *GlobalScope = FnBodyScope->getParent();
Steve Naroffadbbd0c2008-01-14 20:51:29 +00001531
1532 // See if this is a redefinition.
Steve Naroffe8043c32008-04-01 23:04:06 +00001533 Decl *PrevDcl = LookupDecl(D.getIdentifier(), Decl::IDNS_Ordinary,
Steve Naroffb327ce02008-04-02 14:35:35 +00001534 GlobalScope);
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +00001535 if (PrevDcl && IdResolver.isDeclInScope(PrevDcl, CurContext)) {
1536 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PrevDcl)) {
1537 const FunctionDecl *Definition;
1538 if (FD->getBody(Definition)) {
1539 Diag(D.getIdentifierLoc(), diag::err_redefinition,
1540 D.getIdentifier()->getName());
1541 Diag(Definition->getLocation(), diag::err_previous_definition);
1542 }
Steve Naroffadbbd0c2008-01-14 20:51:29 +00001543 }
1544 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001545
1546 return ActOnStartOfFunctionDef(FnBodyScope,
1547 ActOnDeclarator(GlobalScope, D, 0));
1548}
1549
1550Sema::DeclTy *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, DeclTy *D) {
1551 Decl *decl = static_cast<Decl*>(D);
Chris Lattnere9ba3232008-02-16 01:20:36 +00001552 FunctionDecl *FD = cast<FunctionDecl>(decl);
Chris Lattnerb048c982008-04-06 04:47:34 +00001553 PushDeclContext(FD);
Chris Lattner04421082008-04-08 04:40:51 +00001554
1555 // Check the validity of our function parameters
1556 CheckParmsForFunctionDef(FD);
1557
1558 // Introduce our parameters into the function scope
1559 for (unsigned p = 0, NumParams = FD->getNumParams(); p < NumParams; ++p) {
1560 ParmVarDecl *Param = FD->getParamDecl(p);
1561 // If this has an identifier, add it to the scope stack.
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +00001562 if (Param->getIdentifier())
1563 PushOnScopeChains(Param, FnBodyScope);
Reid Spencer5f016e22007-07-11 17:01:13 +00001564 }
Chris Lattner04421082008-04-08 04:40:51 +00001565
Reid Spencer5f016e22007-07-11 17:01:13 +00001566 return FD;
1567}
1568
Steve Naroffd6d054d2007-11-11 23:20:51 +00001569Sema::DeclTy *Sema::ActOnFinishFunctionBody(DeclTy *D, StmtTy *Body) {
1570 Decl *dcl = static_cast<Decl *>(D);
1571 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(dcl)) {
1572 FD->setBody((Stmt*)Body);
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +00001573 assert(FD == getCurFunctionDecl() && "Function parsing confused");
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001574 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(dcl)) {
Steve Naroffd6d054d2007-11-11 23:20:51 +00001575 MD->setBody((Stmt*)Body);
Steve Naroff4d832202007-12-13 18:18:56 +00001576 }
Chris Lattnerb048c982008-04-06 04:47:34 +00001577 PopDeclContext();
Reid Spencer5f016e22007-07-11 17:01:13 +00001578 // Verify and clean out per-function state.
1579
1580 // Check goto/label use.
1581 for (llvm::DenseMap<IdentifierInfo*, LabelStmt*>::iterator
1582 I = LabelMap.begin(), E = LabelMap.end(); I != E; ++I) {
1583 // Verify that we have no forward references left. If so, there was a goto
1584 // or address of a label taken, but no definition of it. Label fwd
1585 // definitions are indicated with a null substmt.
1586 if (I->second->getSubStmt() == 0) {
1587 LabelStmt *L = I->second;
1588 // Emit error.
1589 Diag(L->getIdentLoc(), diag::err_undeclared_label_use, L->getName());
1590
1591 // At this point, we have gotos that use the bogus label. Stitch it into
1592 // the function body so that they aren't leaked and that the AST is well
1593 // formed.
Chris Lattner0cbc2152008-01-25 00:01:10 +00001594 if (Body) {
1595 L->setSubStmt(new NullStmt(L->getIdentLoc()));
1596 cast<CompoundStmt>((Stmt*)Body)->push_back(L);
1597 } else {
1598 // The whole function wasn't parsed correctly, just delete this.
1599 delete L;
1600 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001601 }
1602 }
1603 LabelMap.clear();
1604
Steve Naroffd6d054d2007-11-11 23:20:51 +00001605 return D;
Fariborz Jahanian60fbca02007-11-10 16:31:34 +00001606}
1607
Reid Spencer5f016e22007-07-11 17:01:13 +00001608/// ImplicitlyDefineFunction - An undeclared identifier was used in a function
1609/// call, forming a call to an implicitly defined function (per C99 6.5.1p2).
Steve Naroff8c9f13e2007-09-16 16:16:00 +00001610ScopedDecl *Sema::ImplicitlyDefineFunction(SourceLocation Loc,
1611 IdentifierInfo &II, Scope *S) {
Chris Lattner37d10842008-05-05 21:18:06 +00001612 // Extension in C99. Legal in C90, but warn about it.
1613 if (getLangOptions().C99)
Reid Spencer5f016e22007-07-11 17:01:13 +00001614 Diag(Loc, diag::ext_implicit_function_decl, II.getName());
Chris Lattner37d10842008-05-05 21:18:06 +00001615 else
Reid Spencer5f016e22007-07-11 17:01:13 +00001616 Diag(Loc, diag::warn_implicit_function_decl, II.getName());
1617
1618 // FIXME: handle stuff like:
1619 // void foo() { extern float X(); }
1620 // void bar() { X(); } <-- implicit decl for X in another scope.
1621
1622 // Set a Declarator for the implicit definition: int foo();
1623 const char *Dummy;
1624 DeclSpec DS;
1625 bool Error = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, Dummy);
1626 Error = Error; // Silence warning.
1627 assert(!Error && "Error setting up implicit decl!");
1628 Declarator D(DS, Declarator::BlockContext);
1629 D.AddTypeInfo(DeclaratorChunk::getFunction(false, false, 0, 0, Loc));
1630 D.SetIdentifier(&II, Loc);
1631
Argyrios Kyrtzidis93213bb2008-05-01 21:04:16 +00001632 // Insert this function into translation-unit scope.
1633
1634 DeclContext *PrevDC = CurContext;
1635 CurContext = Context.getTranslationUnitDecl();
1636
Steve Naroffe2ef8152008-04-04 14:32:09 +00001637 FunctionDecl *FD =
Argyrios Kyrtzidis93213bb2008-05-01 21:04:16 +00001638 dyn_cast<FunctionDecl>(static_cast<Decl*>(ActOnDeclarator(TUScope, D, 0)));
Steve Naroffe2ef8152008-04-04 14:32:09 +00001639 FD->setImplicit();
Argyrios Kyrtzidis93213bb2008-05-01 21:04:16 +00001640
1641 CurContext = PrevDC;
1642
Steve Naroffe2ef8152008-04-04 14:32:09 +00001643 return FD;
Reid Spencer5f016e22007-07-11 17:01:13 +00001644}
1645
1646
Chris Lattner41af0932007-11-14 06:34:38 +00001647TypedefDecl *Sema::ParseTypedefDecl(Scope *S, Declarator &D, QualType T,
Steve Naroff94745042007-09-13 23:52:58 +00001648 ScopedDecl *LastDeclarator) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001649 assert(D.getIdentifier() && "Wrong callback for declspec without declarator");
Steve Naroff5912a352007-08-28 20:14:24 +00001650 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001651
1652 // Scope manipulation handled by caller.
Chris Lattner0ed844b2008-04-04 06:12:32 +00001653 TypedefDecl *NewTD = TypedefDecl::Create(Context, CurContext,
1654 D.getIdentifierLoc(),
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001655 D.getIdentifier(),
Chris Lattnerc63e6602008-03-15 21:32:50 +00001656 T, LastDeclarator);
Steve Naroff5912a352007-08-28 20:14:24 +00001657 if (D.getInvalidType())
1658 NewTD->setInvalidDecl();
1659 return NewTD;
Reid Spencer5f016e22007-07-11 17:01:13 +00001660}
1661
Steve Naroff08d92e42007-09-15 18:49:24 +00001662/// ActOnTag - This is invoked when we see 'struct foo' or 'struct {'. In the
Reid Spencer5f016e22007-07-11 17:01:13 +00001663/// former case, Name will be non-null. In the later case, Name will be null.
1664/// TagType indicates what kind of tag this is. TK indicates whether this is a
1665/// reference/declaration/definition of a tag.
Steve Naroff08d92e42007-09-15 18:49:24 +00001666Sema::DeclTy *Sema::ActOnTag(Scope *S, unsigned TagType, TagKind TK,
Reid Spencer5f016e22007-07-11 17:01:13 +00001667 SourceLocation KWLoc, IdentifierInfo *Name,
1668 SourceLocation NameLoc, AttributeList *Attr) {
1669 // If this is a use of an existing tag, it must have a name.
1670 assert((Name != 0 || TK == TK_Definition) &&
1671 "Nameless record must be a definition!");
1672
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001673 TagDecl::TagKind Kind;
Reid Spencer5f016e22007-07-11 17:01:13 +00001674 switch (TagType) {
1675 default: assert(0 && "Unknown tag type!");
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001676 case DeclSpec::TST_struct: Kind = TagDecl::TK_struct; break;
1677 case DeclSpec::TST_union: Kind = TagDecl::TK_union; break;
1678 case DeclSpec::TST_class: Kind = TagDecl::TK_class; break;
1679 case DeclSpec::TST_enum: Kind = TagDecl::TK_enum; break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001680 }
1681
1682 // If this is a named struct, check to see if there was a previous forward
1683 // declaration or definition.
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +00001684 // Use ScopedDecl instead of TagDecl, because a NamespaceDecl may come up.
1685 if (ScopedDecl *PrevDecl =
1686 dyn_cast_or_null<ScopedDecl>(LookupDecl(Name, Decl::IDNS_Tag, S))) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001687
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +00001688 assert((isa<TagDecl>(PrevDecl) || isa<NamespaceDecl>(PrevDecl)) &&
1689 "unexpected Decl type");
1690 if (TagDecl *PrevTagDecl = dyn_cast<TagDecl>(PrevDecl)) {
Chris Lattner14943b92008-07-03 03:30:58 +00001691 // If this is a use of a previous tag, or if the tag is already declared
1692 // in the same scope (so that the definition/declaration completes or
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +00001693 // rementions the tag), reuse the decl.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +00001694 if (TK == TK_Reference ||
1695 IdResolver.isDeclInScope(PrevDecl, CurContext, S)) {
Chris Lattner14943b92008-07-03 03:30:58 +00001696 // Make sure that this wasn't declared as an enum and now used as a
1697 // struct or something similar.
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001698 if (PrevTagDecl->getTagKind() != Kind) {
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +00001699 Diag(KWLoc, diag::err_use_with_wrong_tag, Name->getName());
1700 Diag(PrevDecl->getLocation(), diag::err_previous_use);
Chris Lattner14943b92008-07-03 03:30:58 +00001701 // Recover by making this an anonymous redefinition.
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +00001702 Name = 0;
Chris Lattner14943b92008-07-03 03:30:58 +00001703 PrevDecl = 0;
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +00001704 } else {
Chris Lattner14943b92008-07-03 03:30:58 +00001705 // If this is a use or a forward declaration, we're good.
1706 if (TK != TK_Definition)
1707 return PrevDecl;
1708
1709 // Diagnose attempts to redefine a tag.
1710 if (PrevTagDecl->isDefinition()) {
1711 Diag(NameLoc, diag::err_redefinition, Name->getName());
1712 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
1713 // If this is a redefinition, recover by making this struct be
1714 // anonymous, which will make any later references get the previous
1715 // definition.
1716 Name = 0;
1717 } else {
1718 // Okay, this is definition of a previously declared or referenced
1719 // tag. Move the location of the decl to be the definition site.
1720 PrevDecl->setLocation(NameLoc);
1721 return PrevDecl;
1722 }
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +00001723 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001724 }
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +00001725 // If we get here, this is a definition of a new struct type in a nested
1726 // scope, e.g. "struct foo; void bar() { struct foo; }", just create a new
1727 // type.
1728 } else {
1729 // The tag name clashes with a namespace name, issue an error and recover
1730 // by making this tag be anonymous.
1731 Diag(NameLoc, diag::err_redefinition_different_kind, Name->getName());
1732 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
1733 Name = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001734 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001735 }
1736
1737 // If there is an identifier, use the location of the identifier as the
1738 // location of the decl, otherwise use the location of the struct/union
1739 // keyword.
1740 SourceLocation Loc = NameLoc.isValid() ? NameLoc : KWLoc;
1741
1742 // Otherwise, if this is the first time we've seen this tag, create the decl.
1743 TagDecl *New;
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001744 if (Kind == TagDecl::TK_enum) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001745 // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.:
1746 // enum X { A, B, C } D; D should chain to X.
Chris Lattner0ed844b2008-04-04 06:12:32 +00001747 New = EnumDecl::Create(Context, CurContext, Loc, Name, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +00001748 // If this is an undefined enum, warn.
1749 if (TK != TK_Definition) Diag(Loc, diag::ext_forward_ref_enum);
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001750 } else {
1751 // struct/union/class
1752
Reid Spencer5f016e22007-07-11 17:01:13 +00001753 // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.:
1754 // struct X { int A; } D; D should chain to X.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001755 if (getLangOptions().CPlusPlus)
1756 // FIXME: Look for a way to use RecordDecl for simple structs.
1757 New = CXXRecordDecl::Create(Context, Kind, CurContext, Loc, Name, 0);
1758 else
1759 New = RecordDecl::Create(Context, Kind, CurContext, Loc, Name, 0);
1760 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001761
1762 // If this has an identifier, add it to the scope stack.
1763 if (Name) {
Chris Lattner31e05722007-08-26 06:24:45 +00001764 // The scope passed in may not be a decl scope. Zip up the scope tree until
1765 // we find one that is.
1766 while ((S->getFlags() & Scope::DeclScope) == 0)
1767 S = S->getParent();
1768
1769 // Add it to the decl chain.
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +00001770 PushOnScopeChains(New, S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001771 }
Chris Lattnere1e79852008-02-06 00:51:33 +00001772
Chris Lattnerf2e4bd52008-06-28 23:58:55 +00001773 if (Attr)
1774 ProcessDeclAttributeList(New, Attr);
Reid Spencer5f016e22007-07-11 17:01:13 +00001775 return New;
1776}
1777
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001778/// Collect the instance variables declared in an Objective-C object. Used in
1779/// the creation of structures from objects using the @defs directive.
1780static void CollectIvars(ObjCInterfaceDecl *Class,
1781 llvm::SmallVector<Sema::DeclTy*, 16> &ivars) {
1782 if (Class->getSuperClass())
1783 CollectIvars(Class->getSuperClass(), ivars);
1784 ivars.append(Class->ivar_begin(), Class->ivar_end());
1785}
1786
1787/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1788/// instance variables of ClassName into Decls.
1789void Sema::ActOnDefs(Scope *S, SourceLocation DeclStart,
1790 IdentifierInfo *ClassName,
1791 llvm::SmallVector<DeclTy*, 16> &Decls) {
1792 // Check that ClassName is a valid class
1793 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
1794 if (!Class) {
1795 Diag(DeclStart, diag::err_undef_interface, ClassName->getName());
1796 return;
1797 }
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001798 // Collect the instance variables
1799 CollectIvars(Class, Decls);
1800}
1801
1802
Eli Friedman1b76ada2008-06-03 21:01:11 +00001803static bool CalcFakeICEVal(const Expr* Expr,
1804 llvm::APSInt& Result,
1805 ASTContext& Context) {
1806 // Calculate the value of an expression that has a calculatable
1807 // value, but isn't an ICE. Currently, this only supports
1808 // a very narrow set of extensions, but it can be expanded if needed.
1809 if (const ParenExpr *PE = dyn_cast<ParenExpr>(Expr))
1810 return CalcFakeICEVal(PE->getSubExpr(), Result, Context);
1811
1812 if (const CastExpr *CE = dyn_cast<CastExpr>(Expr)) {
1813 QualType CETy = CE->getType();
1814 if ((CETy->isIntegralType() && !CETy->isBooleanType()) ||
1815 CETy->isPointerType()) {
1816 if (CalcFakeICEVal(CE->getSubExpr(), Result, Context)) {
1817 Result.extOrTrunc(Context.getTypeSize(CETy));
1818 // FIXME: This assumes pointers are signed.
1819 Result.setIsSigned(CETy->isSignedIntegerType() ||
1820 CETy->isPointerType());
1821 return true;
1822 }
1823 }
1824 }
1825
1826 if (Expr->getType()->isIntegralType())
1827 return Expr->isIntegerConstantExpr(Result, Context);
1828
1829 return false;
1830}
1831
1832QualType Sema::TryFixInvalidVariablyModifiedType(QualType T) {
1833 // This method tries to turn a variable array into a constant
1834 // array even when the size isn't an ICE. This is necessary
1835 // for compatibility with code that depends on gcc's buggy
1836 // constant expression folding, like struct {char x[(int)(char*)2];}
1837 if (const VariableArrayType* VLATy = dyn_cast<VariableArrayType>(T)) {
1838 llvm::APSInt Result(32);
1839 if (VLATy->getSizeExpr() &&
1840 CalcFakeICEVal(VLATy->getSizeExpr(), Result, Context) &&
1841 Result > llvm::APSInt(Result.getBitWidth(), Result.isUnsigned())) {
1842 return Context.getConstantArrayType(VLATy->getElementType(),
1843 Result, ArrayType::Normal, 0);
1844 }
1845 }
1846 return QualType();
1847}
1848
Steve Naroff08d92e42007-09-15 18:49:24 +00001849/// ActOnField - Each field of a struct/union/class is passed into this in order
Reid Spencer5f016e22007-07-11 17:01:13 +00001850/// to create a FieldDecl object for it.
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +00001851Sema::DeclTy *Sema::ActOnField(Scope *S,
Reid Spencer5f016e22007-07-11 17:01:13 +00001852 SourceLocation DeclStart,
1853 Declarator &D, ExprTy *BitfieldWidth) {
1854 IdentifierInfo *II = D.getIdentifier();
1855 Expr *BitWidth = (Expr*)BitfieldWidth;
Reid Spencer5f016e22007-07-11 17:01:13 +00001856 SourceLocation Loc = DeclStart;
1857 if (II) Loc = D.getIdentifierLoc();
1858
1859 // FIXME: Unnamed fields can be handled in various different ways, for
1860 // example, unnamed unions inject all members into the struct namespace!
1861
1862
1863 if (BitWidth) {
1864 // TODO: Validate.
1865 //printf("WARNING: BITFIELDS IGNORED!\n");
1866
1867 // 6.7.2.1p3
1868 // 6.7.2.1p4
1869
1870 } else {
1871 // Not a bitfield.
1872
1873 // validate II.
1874
1875 }
1876
1877 QualType T = GetTypeForDeclarator(D, S);
Steve Naroff5912a352007-08-28 20:14:24 +00001878 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
1879 bool InvalidDecl = false;
Steve Naroffd7444aa2007-08-31 17:20:07 +00001880
Reid Spencer5f016e22007-07-11 17:01:13 +00001881 // C99 6.7.2.1p8: A member of a structure or union may have any type other
1882 // than a variably modified type.
Eli Friedman9db13972008-02-15 12:53:51 +00001883 if (T->isVariablyModifiedType()) {
Eli Friedman1b76ada2008-06-03 21:01:11 +00001884 QualType FixedTy = TryFixInvalidVariablyModifiedType(T);
1885 if (!FixedTy.isNull()) {
1886 Diag(Loc, diag::warn_illegal_constant_array_size, Loc);
1887 T = FixedTy;
1888 } else {
1889 // FIXME: This diagnostic needs work
1890 Diag(Loc, diag::err_typecheck_illegal_vla, Loc);
1891 InvalidDecl = true;
1892 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001893 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001894 // FIXME: Chain fielddecls together.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001895 FieldDecl *NewFD;
1896
1897 if (getLangOptions().CPlusPlus) {
1898 // FIXME: Replace CXXFieldDecls with FieldDecls for simple structs.
1899 NewFD = CXXFieldDecl::Create(Context, cast<CXXRecordDecl>(CurContext),
1900 Loc, II, T, BitWidth);
1901 if (II)
1902 PushOnScopeChains(NewFD, S);
1903 }
1904 else
1905 NewFD = FieldDecl::Create(Context, Loc, II, T, BitWidth);
Steve Naroff44739212007-09-11 21:17:26 +00001906
Chris Lattner3ff30c82008-06-29 00:02:00 +00001907 ProcessDeclAttributes(NewFD, D);
Anders Carlssonad148062008-02-16 00:29:18 +00001908
Steve Naroff5912a352007-08-28 20:14:24 +00001909 if (D.getInvalidType() || InvalidDecl)
1910 NewFD->setInvalidDecl();
1911 return NewFD;
Reid Spencer5f016e22007-07-11 17:01:13 +00001912}
1913
Fariborz Jahanian89204a12007-10-01 16:53:59 +00001914/// TranslateIvarVisibility - Translate visibility from a token ID to an
1915/// AST enum value.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001916static ObjCIvarDecl::AccessControl
Fariborz Jahanian89204a12007-10-01 16:53:59 +00001917TranslateIvarVisibility(tok::ObjCKeywordKind ivarVisibility) {
Steve Narofff13271f2007-09-14 23:09:53 +00001918 switch (ivarVisibility) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001919 case tok::objc_private: return ObjCIvarDecl::Private;
1920 case tok::objc_public: return ObjCIvarDecl::Public;
1921 case tok::objc_protected: return ObjCIvarDecl::Protected;
1922 case tok::objc_package: return ObjCIvarDecl::Package;
Fariborz Jahanian89204a12007-10-01 16:53:59 +00001923 default: assert(false && "Unknown visitibility kind");
Steve Narofff13271f2007-09-14 23:09:53 +00001924 }
1925}
1926
Fariborz Jahanian45bc03f2008-04-11 16:55:42 +00001927/// ActOnIvar - Each ivar field of an objective-c class is passed into this
1928/// in order to create an IvarDecl object for it.
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +00001929Sema::DeclTy *Sema::ActOnIvar(Scope *S,
Fariborz Jahanian45bc03f2008-04-11 16:55:42 +00001930 SourceLocation DeclStart,
1931 Declarator &D, ExprTy *BitfieldWidth,
1932 tok::ObjCKeywordKind Visibility) {
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +00001933 IdentifierInfo *II = D.getIdentifier();
1934 Expr *BitWidth = (Expr*)BitfieldWidth;
1935 SourceLocation Loc = DeclStart;
1936 if (II) Loc = D.getIdentifierLoc();
1937
1938 // FIXME: Unnamed fields can be handled in various different ways, for
1939 // example, unnamed unions inject all members into the struct namespace!
1940
1941
1942 if (BitWidth) {
1943 // TODO: Validate.
1944 //printf("WARNING: BITFIELDS IGNORED!\n");
1945
1946 // 6.7.2.1p3
1947 // 6.7.2.1p4
1948
1949 } else {
1950 // Not a bitfield.
1951
1952 // validate II.
1953
1954 }
1955
1956 QualType T = GetTypeForDeclarator(D, S);
1957 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
1958 bool InvalidDecl = false;
1959
1960 // C99 6.7.2.1p8: A member of a structure or union may have any type other
1961 // than a variably modified type.
1962 if (T->isVariablyModifiedType()) {
1963 // FIXME: This diagnostic needs work
1964 Diag(Loc, diag::err_typecheck_illegal_vla, Loc);
1965 InvalidDecl = true;
1966 }
1967
Fariborz Jahanian45bc03f2008-04-11 16:55:42 +00001968 ObjCIvarDecl *NewID = ObjCIvarDecl::Create(Context, Loc, II, T);
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +00001969
Chris Lattner3ff30c82008-06-29 00:02:00 +00001970 ProcessDeclAttributes(NewID, D);
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +00001971
1972 if (D.getInvalidType() || InvalidDecl)
1973 NewID->setInvalidDecl();
1974 // If we have visibility info, make sure the AST is set accordingly.
Fariborz Jahanian45bc03f2008-04-11 16:55:42 +00001975 if (Visibility != tok::objc_not_keyword)
1976 NewID->setAccessControl(TranslateIvarVisibility(Visibility));
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +00001977 return NewID;
1978}
1979
Fariborz Jahanian9d048ff2007-09-29 00:54:24 +00001980void Sema::ActOnFields(Scope* S,
Fariborz Jahanian3f5faf72007-10-04 00:45:27 +00001981 SourceLocation RecLoc, DeclTy *RecDecl,
Steve Naroff08d92e42007-09-15 18:49:24 +00001982 DeclTy **Fields, unsigned NumFields,
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +00001983 SourceLocation LBrac, SourceLocation RBrac) {
Steve Naroff74216642007-09-14 22:20:54 +00001984 Decl *EnclosingDecl = static_cast<Decl*>(RecDecl);
1985 assert(EnclosingDecl && "missing record or interface decl");
1986 RecordDecl *Record = dyn_cast<RecordDecl>(EnclosingDecl);
1987
Fariborz Jahaniane267ab62007-09-14 16:27:55 +00001988 if (Record && Record->isDefinition()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001989 // Diagnose code like:
1990 // struct S { struct S {} X; };
1991 // We discover this when we complete the outer S. Reject and ignore the
1992 // outer S.
1993 Diag(Record->getLocation(), diag::err_nested_redefinition,
1994 Record->getKindName());
1995 Diag(RecLoc, diag::err_previous_definition);
Steve Naroff74216642007-09-14 22:20:54 +00001996 Record->setInvalidDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +00001997 return;
1998 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001999 // Verify that all the fields are okay.
2000 unsigned NumNamedMembers = 0;
2001 llvm::SmallVector<FieldDecl*, 32> RecFields;
2002 llvm::SmallSet<const IdentifierInfo*, 32> FieldIDs;
Steve Naroff74216642007-09-14 22:20:54 +00002003
Reid Spencer5f016e22007-07-11 17:01:13 +00002004 for (unsigned i = 0; i != NumFields; ++i) {
Fariborz Jahaniane267ab62007-09-14 16:27:55 +00002005
Steve Naroff74216642007-09-14 22:20:54 +00002006 FieldDecl *FD = cast_or_null<FieldDecl>(static_cast<Decl*>(Fields[i]));
2007 assert(FD && "missing field decl");
2008
2009 // Remember all fields.
2010 RecFields.push_back(FD);
Reid Spencer5f016e22007-07-11 17:01:13 +00002011
2012 // Get the type for the field.
Chris Lattner02c642e2007-07-31 21:33:24 +00002013 Type *FDTy = FD->getType().getTypePtr();
Steve Narofff13271f2007-09-14 23:09:53 +00002014
Reid Spencer5f016e22007-07-11 17:01:13 +00002015 // C99 6.7.2.1p2 - A field may not be a function type.
Chris Lattner02c642e2007-07-31 21:33:24 +00002016 if (FDTy->isFunctionType()) {
Steve Naroff74216642007-09-14 22:20:54 +00002017 Diag(FD->getLocation(), diag::err_field_declared_as_function,
Reid Spencer5f016e22007-07-11 17:01:13 +00002018 FD->getName());
Steve Naroff74216642007-09-14 22:20:54 +00002019 FD->setInvalidDecl();
2020 EnclosingDecl->setInvalidDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +00002021 continue;
2022 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002023 // C99 6.7.2.1p2 - A field may not be an incomplete type except...
2024 if (FDTy->isIncompleteType()) {
Fariborz Jahaniane267ab62007-09-14 16:27:55 +00002025 if (!Record) { // Incomplete ivar type is always an error.
Fariborz Jahanian3f5faf72007-10-04 00:45:27 +00002026 Diag(FD->getLocation(), diag::err_field_incomplete, FD->getName());
Steve Naroff74216642007-09-14 22:20:54 +00002027 FD->setInvalidDecl();
2028 EnclosingDecl->setInvalidDecl();
Fariborz Jahanian3f5faf72007-10-04 00:45:27 +00002029 continue;
Fariborz Jahaniane267ab62007-09-14 16:27:55 +00002030 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002031 if (i != NumFields-1 || // ... that the last member ...
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00002032 !Record->isStruct() || // ... of a structure ...
Chris Lattner02c642e2007-07-31 21:33:24 +00002033 !FDTy->isArrayType()) { //... may have incomplete array type.
Reid Spencer5f016e22007-07-11 17:01:13 +00002034 Diag(FD->getLocation(), diag::err_field_incomplete, FD->getName());
Steve Naroff74216642007-09-14 22:20:54 +00002035 FD->setInvalidDecl();
2036 EnclosingDecl->setInvalidDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +00002037 continue;
2038 }
Fariborz Jahaniane267ab62007-09-14 16:27:55 +00002039 if (NumNamedMembers < 1) { //... must have more than named member ...
Reid Spencer5f016e22007-07-11 17:01:13 +00002040 Diag(FD->getLocation(), diag::err_flexible_array_empty_struct,
2041 FD->getName());
Steve Naroff74216642007-09-14 22:20:54 +00002042 FD->setInvalidDecl();
2043 EnclosingDecl->setInvalidDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +00002044 continue;
2045 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002046 // Okay, we have a legal flexible array member at the end of the struct.
Fariborz Jahaniane267ab62007-09-14 16:27:55 +00002047 if (Record)
2048 Record->setHasFlexibleArrayMember(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002049 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002050 /// C99 6.7.2.1p2 - a struct ending in a flexible array member cannot be the
2051 /// field of another structure or the element of an array.
Chris Lattner02c642e2007-07-31 21:33:24 +00002052 if (const RecordType *FDTTy = FDTy->getAsRecordType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002053 if (FDTTy->getDecl()->hasFlexibleArrayMember()) {
2054 // If this is a member of a union, then entire union becomes "flexible".
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00002055 if (Record && Record->isUnion()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002056 Record->setHasFlexibleArrayMember(true);
2057 } else {
2058 // If this is a struct/class and this is not the last element, reject
2059 // it. Note that GCC supports variable sized arrays in the middle of
2060 // structures.
2061 if (i != NumFields-1) {
2062 Diag(FD->getLocation(), diag::err_variable_sized_type_in_struct,
2063 FD->getName());
Steve Naroff74216642007-09-14 22:20:54 +00002064 FD->setInvalidDecl();
2065 EnclosingDecl->setInvalidDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +00002066 continue;
2067 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002068 // We support flexible arrays at the end of structs in other structs
2069 // as an extension.
2070 Diag(FD->getLocation(), diag::ext_flexible_array_in_struct,
2071 FD->getName());
Fariborz Jahanian3f5faf72007-10-04 00:45:27 +00002072 if (Record)
Fariborz Jahaniane267ab62007-09-14 16:27:55 +00002073 Record->setHasFlexibleArrayMember(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002074 }
2075 }
2076 }
Fariborz Jahaniane7f64cc2007-10-12 22:10:42 +00002077 /// A field cannot be an Objective-c object
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002078 if (FDTy->isObjCInterfaceType()) {
Fariborz Jahaniane7f64cc2007-10-12 22:10:42 +00002079 Diag(FD->getLocation(), diag::err_statically_allocated_object,
2080 FD->getName());
2081 FD->setInvalidDecl();
2082 EnclosingDecl->setInvalidDecl();
2083 continue;
2084 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002085 // Keep track of the number of named members.
2086 if (IdentifierInfo *II = FD->getIdentifier()) {
2087 // Detect duplicate member names.
2088 if (!FieldIDs.insert(II)) {
2089 Diag(FD->getLocation(), diag::err_duplicate_member, II->getName());
2090 // Find the previous decl.
2091 SourceLocation PrevLoc;
2092 for (unsigned i = 0, e = RecFields.size(); ; ++i) {
2093 assert(i != e && "Didn't find previous def!");
2094 if (RecFields[i]->getIdentifier() == II) {
2095 PrevLoc = RecFields[i]->getLocation();
2096 break;
2097 }
2098 }
2099 Diag(PrevLoc, diag::err_previous_definition);
Steve Naroff74216642007-09-14 22:20:54 +00002100 FD->setInvalidDecl();
2101 EnclosingDecl->setInvalidDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +00002102 continue;
2103 }
2104 ++NumNamedMembers;
2105 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002106 }
2107
Reid Spencer5f016e22007-07-11 17:01:13 +00002108 // Okay, we successfully defined 'Record'.
Chris Lattnere1e79852008-02-06 00:51:33 +00002109 if (Record) {
Fariborz Jahaniane267ab62007-09-14 16:27:55 +00002110 Record->defineBody(&RecFields[0], RecFields.size());
Chris Lattnere1e79852008-02-06 00:51:33 +00002111 Consumer.HandleTagDeclDefinition(Record);
2112 } else {
Chris Lattnera91d3812008-02-05 22:40:55 +00002113 ObjCIvarDecl **ClsFields = reinterpret_cast<ObjCIvarDecl**>(&RecFields[0]);
2114 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(EnclosingDecl))
2115 ID->addInstanceVariablesToClass(ClsFields, RecFields.size(), RBrac);
2116 else if (ObjCImplementationDecl *IMPDecl =
2117 dyn_cast<ObjCImplementationDecl>(EnclosingDecl)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002118 assert(IMPDecl && "ActOnFields - missing ObjCImplementationDecl");
2119 IMPDecl->ObjCAddInstanceVariablesToClassImpl(ClsFields, RecFields.size());
Fariborz Jahanian3a3ca1b2007-10-31 18:48:14 +00002120 CheckImplementationIvars(IMPDecl, ClsFields, RecFields.size(), RBrac);
Fariborz Jahaniand0b90bf2007-09-26 18:27:25 +00002121 }
Fariborz Jahanianb04a0212007-09-14 21:08:27 +00002122 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002123}
2124
Steve Naroff08d92e42007-09-15 18:49:24 +00002125Sema::DeclTy *Sema::ActOnEnumConstant(Scope *S, DeclTy *theEnumDecl,
Reid Spencer5f016e22007-07-11 17:01:13 +00002126 DeclTy *lastEnumConst,
2127 SourceLocation IdLoc, IdentifierInfo *Id,
2128 SourceLocation EqualLoc, ExprTy *val) {
Chris Lattner0ed844b2008-04-04 06:12:32 +00002129 EnumDecl *TheEnumDecl = cast<EnumDecl>(static_cast<Decl*>(theEnumDecl));
Reid Spencer5f016e22007-07-11 17:01:13 +00002130 EnumConstantDecl *LastEnumConst =
2131 cast_or_null<EnumConstantDecl>(static_cast<Decl*>(lastEnumConst));
2132 Expr *Val = static_cast<Expr*>(val);
2133
Chris Lattner31e05722007-08-26 06:24:45 +00002134 // The scope passed in may not be a decl scope. Zip up the scope tree until
2135 // we find one that is.
2136 while ((S->getFlags() & Scope::DeclScope) == 0)
2137 S = S->getParent();
2138
Reid Spencer5f016e22007-07-11 17:01:13 +00002139 // Verify that there isn't already something declared with this name in this
2140 // scope.
Steve Naroffb327ce02008-04-02 14:35:35 +00002141 if (Decl *PrevDecl = LookupDecl(Id, Decl::IDNS_Ordinary, S)) {
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +00002142 if (IdResolver.isDeclInScope(PrevDecl, CurContext, S)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002143 if (isa<EnumConstantDecl>(PrevDecl))
2144 Diag(IdLoc, diag::err_redefinition_of_enumerator, Id->getName());
2145 else
2146 Diag(IdLoc, diag::err_redefinition, Id->getName());
2147 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
Chris Lattnera73349d2008-02-26 00:33:57 +00002148 delete Val;
Reid Spencer5f016e22007-07-11 17:01:13 +00002149 return 0;
2150 }
2151 }
2152
2153 llvm::APSInt EnumVal(32);
2154 QualType EltTy;
2155 if (Val) {
Chris Lattner421a23d2007-08-27 21:16:18 +00002156 // Make sure to promote the operand type to int.
2157 UsualUnaryConversions(Val);
2158
Reid Spencer5f016e22007-07-11 17:01:13 +00002159 // C99 6.7.2.2p2: Make sure we have an integer constant expression.
2160 SourceLocation ExpLoc;
Chris Lattner590b6642007-07-15 23:26:56 +00002161 if (!Val->isIntegerConstantExpr(EnumVal, Context, &ExpLoc)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002162 Diag(ExpLoc, diag::err_enum_value_not_integer_constant_expr,
2163 Id->getName());
Chris Lattnera73349d2008-02-26 00:33:57 +00002164 delete Val;
Chris Lattnerb7416f92007-08-27 17:37:24 +00002165 Val = 0; // Just forget about it.
Chris Lattnere9ca8512007-08-29 16:03:41 +00002166 } else {
2167 EltTy = Val->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +00002168 }
Chris Lattnerb7416f92007-08-27 17:37:24 +00002169 }
2170
2171 if (!Val) {
2172 if (LastEnumConst) {
2173 // Assign the last value + 1.
2174 EnumVal = LastEnumConst->getInitVal();
2175 ++EnumVal;
Chris Lattner421a23d2007-08-27 21:16:18 +00002176
2177 // Check for overflow on increment.
2178 if (EnumVal < LastEnumConst->getInitVal())
2179 Diag(IdLoc, diag::warn_enum_value_overflow);
2180
Chris Lattnerb7416f92007-08-27 17:37:24 +00002181 EltTy = LastEnumConst->getType();
2182 } else {
2183 // First value, set to zero.
2184 EltTy = Context.IntTy;
Chris Lattner98be4942008-03-05 18:54:05 +00002185 EnumVal.zextOrTrunc(static_cast<uint32_t>(Context.getTypeSize(EltTy)));
Chris Lattnerb7416f92007-08-27 17:37:24 +00002186 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002187 }
2188
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00002189 EnumConstantDecl *New =
Chris Lattner0ed844b2008-04-04 06:12:32 +00002190 EnumConstantDecl::Create(Context, TheEnumDecl, IdLoc, Id, EltTy,
2191 Val, EnumVal,
Chris Lattnerc63e6602008-03-15 21:32:50 +00002192 LastEnumConst);
Reid Spencer5f016e22007-07-11 17:01:13 +00002193
2194 // Register this decl in the current scope stack.
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +00002195 PushOnScopeChains(New, S);
Reid Spencer5f016e22007-07-11 17:01:13 +00002196 return New;
2197}
2198
Steve Naroff08d92e42007-09-15 18:49:24 +00002199void Sema::ActOnEnumBody(SourceLocation EnumLoc, DeclTy *EnumDeclX,
Reid Spencer5f016e22007-07-11 17:01:13 +00002200 DeclTy **Elements, unsigned NumElements) {
2201 EnumDecl *Enum = cast<EnumDecl>(static_cast<Decl*>(EnumDeclX));
2202 assert(!Enum->isDefinition() && "Enum redefinitions can't reach here");
2203
Chris Lattnere37f0be2007-08-28 05:10:31 +00002204 // TODO: If the result value doesn't fit in an int, it must be a long or long
2205 // long value. ISO C does not support this, but GCC does as an extension,
2206 // emit a warning.
Chris Lattner98be4942008-03-05 18:54:05 +00002207 unsigned IntWidth = Context.Target.getIntWidth();
Chris Lattnere37f0be2007-08-28 05:10:31 +00002208
Chris Lattnerac609682007-08-28 06:15:15 +00002209 // Verify that all the values are okay, compute the size of the values, and
2210 // reverse the list.
2211 unsigned NumNegativeBits = 0;
2212 unsigned NumPositiveBits = 0;
2213
2214 // Keep track of whether all elements have type int.
2215 bool AllElementsInt = true;
2216
Reid Spencer5f016e22007-07-11 17:01:13 +00002217 EnumConstantDecl *EltList = 0;
2218 for (unsigned i = 0; i != NumElements; ++i) {
2219 EnumConstantDecl *ECD =
2220 cast_or_null<EnumConstantDecl>(static_cast<Decl*>(Elements[i]));
2221 if (!ECD) continue; // Already issued a diagnostic.
Chris Lattner211a30e2007-08-28 05:27:00 +00002222
2223 // If the enum value doesn't fit in an int, emit an extension warning.
Chris Lattner211a30e2007-08-28 05:27:00 +00002224 const llvm::APSInt &InitVal = ECD->getInitVal();
Chris Lattnera73349d2008-02-26 00:33:57 +00002225 assert(InitVal.getBitWidth() >= IntWidth &&
2226 "Should have promoted value to int");
Chris Lattner211a30e2007-08-28 05:27:00 +00002227 if (InitVal.getBitWidth() > IntWidth) {
2228 llvm::APSInt V(InitVal);
2229 V.trunc(IntWidth);
2230 V.extend(InitVal.getBitWidth());
2231 if (V != InitVal)
2232 Diag(ECD->getLocation(), diag::ext_enum_value_not_int,
2233 InitVal.toString());
2234 }
Chris Lattnerac609682007-08-28 06:15:15 +00002235
2236 // Keep track of the size of positive and negative values.
Chris Lattnera73349d2008-02-26 00:33:57 +00002237 if (InitVal.isUnsigned() || InitVal.isNonNegative())
Chris Lattner21dd8212008-01-14 21:47:29 +00002238 NumPositiveBits = std::max(NumPositiveBits,
2239 (unsigned)InitVal.getActiveBits());
Chris Lattnerac609682007-08-28 06:15:15 +00002240 else
Chris Lattner21dd8212008-01-14 21:47:29 +00002241 NumNegativeBits = std::max(NumNegativeBits,
2242 (unsigned)InitVal.getMinSignedBits());
Reid Spencer5f016e22007-07-11 17:01:13 +00002243
Chris Lattnerac609682007-08-28 06:15:15 +00002244 // Keep track of whether every enum element has type int (very commmon).
2245 if (AllElementsInt)
2246 AllElementsInt = ECD->getType() == Context.IntTy;
2247
Reid Spencer5f016e22007-07-11 17:01:13 +00002248 ECD->setNextDeclarator(EltList);
2249 EltList = ECD;
2250 }
2251
Chris Lattnerac609682007-08-28 06:15:15 +00002252 // Figure out the type that should be used for this enum.
2253 // FIXME: Support attribute(packed) on enums and -fshort-enums.
2254 QualType BestType;
Chris Lattnerb7f6e082007-08-29 17:31:48 +00002255 unsigned BestWidth;
Chris Lattnerac609682007-08-28 06:15:15 +00002256
2257 if (NumNegativeBits) {
2258 // If there is a negative value, figure out the smallest integer type (of
2259 // int/long/longlong) that fits.
Chris Lattnerb7f6e082007-08-29 17:31:48 +00002260 if (NumNegativeBits <= IntWidth && NumPositiveBits < IntWidth) {
Chris Lattnerac609682007-08-28 06:15:15 +00002261 BestType = Context.IntTy;
Chris Lattnerb7f6e082007-08-29 17:31:48 +00002262 BestWidth = IntWidth;
2263 } else {
Chris Lattner98be4942008-03-05 18:54:05 +00002264 BestWidth = Context.Target.getLongWidth();
Ted Kremenek9c728dc2007-12-12 22:39:36 +00002265
Chris Lattnerb7f6e082007-08-29 17:31:48 +00002266 if (NumNegativeBits <= BestWidth && NumPositiveBits < BestWidth)
Chris Lattnerac609682007-08-28 06:15:15 +00002267 BestType = Context.LongTy;
2268 else {
Chris Lattner98be4942008-03-05 18:54:05 +00002269 BestWidth = Context.Target.getLongLongWidth();
Ted Kremenek9c728dc2007-12-12 22:39:36 +00002270
Chris Lattnerb7f6e082007-08-29 17:31:48 +00002271 if (NumNegativeBits > BestWidth || NumPositiveBits >= BestWidth)
Chris Lattnerac609682007-08-28 06:15:15 +00002272 Diag(Enum->getLocation(), diag::warn_enum_too_large);
2273 BestType = Context.LongLongTy;
2274 }
2275 }
2276 } else {
2277 // If there is no negative value, figure out which of uint, ulong, ulonglong
2278 // fits.
Chris Lattnerb7f6e082007-08-29 17:31:48 +00002279 if (NumPositiveBits <= IntWidth) {
Chris Lattnerac609682007-08-28 06:15:15 +00002280 BestType = Context.UnsignedIntTy;
Chris Lattnerb7f6e082007-08-29 17:31:48 +00002281 BestWidth = IntWidth;
2282 } else if (NumPositiveBits <=
Chris Lattner98be4942008-03-05 18:54:05 +00002283 (BestWidth = Context.Target.getLongWidth())) {
Chris Lattnerac609682007-08-28 06:15:15 +00002284 BestType = Context.UnsignedLongTy;
Chris Lattner98be4942008-03-05 18:54:05 +00002285 } else {
2286 BestWidth = Context.Target.getLongLongWidth();
Chris Lattnerb7f6e082007-08-29 17:31:48 +00002287 assert(NumPositiveBits <= BestWidth &&
Chris Lattnerac609682007-08-28 06:15:15 +00002288 "How could an initializer get larger than ULL?");
2289 BestType = Context.UnsignedLongLongTy;
2290 }
2291 }
2292
Chris Lattnerb7f6e082007-08-29 17:31:48 +00002293 // Loop over all of the enumerator constants, changing their types to match
2294 // the type of the enum if needed.
2295 for (unsigned i = 0; i != NumElements; ++i) {
2296 EnumConstantDecl *ECD =
2297 cast_or_null<EnumConstantDecl>(static_cast<Decl*>(Elements[i]));
2298 if (!ECD) continue; // Already issued a diagnostic.
2299
2300 // Standard C says the enumerators have int type, but we allow, as an
2301 // extension, the enumerators to be larger than int size. If each
2302 // enumerator value fits in an int, type it as an int, otherwise type it the
2303 // same as the enumerator decl itself. This means that in "enum { X = 1U }"
2304 // that X has type 'int', not 'unsigned'.
Chris Lattnera73349d2008-02-26 00:33:57 +00002305 if (ECD->getType() == Context.IntTy) {
2306 // Make sure the init value is signed.
2307 llvm::APSInt IV = ECD->getInitVal();
2308 IV.setIsSigned(true);
2309 ECD->setInitVal(IV);
Chris Lattnerb7f6e082007-08-29 17:31:48 +00002310 continue; // Already int type.
Chris Lattnera73349d2008-02-26 00:33:57 +00002311 }
Chris Lattnerb7f6e082007-08-29 17:31:48 +00002312
2313 // Determine whether the value fits into an int.
2314 llvm::APSInt InitVal = ECD->getInitVal();
2315 bool FitsInInt;
2316 if (InitVal.isUnsigned() || !InitVal.isNegative())
2317 FitsInInt = InitVal.getActiveBits() < IntWidth;
2318 else
2319 FitsInInt = InitVal.getMinSignedBits() <= IntWidth;
2320
2321 // If it fits into an integer type, force it. Otherwise force it to match
2322 // the enum decl type.
2323 QualType NewTy;
2324 unsigned NewWidth;
2325 bool NewSign;
2326 if (FitsInInt) {
2327 NewTy = Context.IntTy;
2328 NewWidth = IntWidth;
2329 NewSign = true;
2330 } else if (ECD->getType() == BestType) {
2331 // Already the right type!
2332 continue;
2333 } else {
2334 NewTy = BestType;
2335 NewWidth = BestWidth;
2336 NewSign = BestType->isSignedIntegerType();
2337 }
2338
2339 // Adjust the APSInt value.
2340 InitVal.extOrTrunc(NewWidth);
2341 InitVal.setIsSigned(NewSign);
2342 ECD->setInitVal(InitVal);
2343
2344 // Adjust the Expr initializer and type.
2345 ECD->setInitExpr(new ImplicitCastExpr(NewTy, ECD->getInitExpr()));
2346 ECD->setType(NewTy);
2347 }
Chris Lattnerac609682007-08-28 06:15:15 +00002348
Chris Lattnere00b18c2007-08-28 18:24:31 +00002349 Enum->defineElements(EltList, BestType);
Chris Lattnere1e79852008-02-06 00:51:33 +00002350 Consumer.HandleTagDeclDefinition(Enum);
Reid Spencer5f016e22007-07-11 17:01:13 +00002351}
2352
Anders Carlssondfab6cb2008-02-08 00:33:21 +00002353Sema::DeclTy *Sema::ActOnFileScopeAsmDecl(SourceLocation Loc,
2354 ExprTy *expr) {
2355 StringLiteral *AsmString = cast<StringLiteral>((Expr*)expr);
2356
Chris Lattner8e25d862008-03-16 00:16:02 +00002357 return FileScopeAsmDecl::Create(Context, Loc, AsmString);
Anders Carlssondfab6cb2008-02-08 00:33:21 +00002358}
2359
Chris Lattnerc6fdc342008-01-12 07:05:38 +00002360Sema::DeclTy* Sema::ActOnLinkageSpec(SourceLocation Loc,
Chris Lattnerc81c8142008-02-25 21:04:36 +00002361 SourceLocation LBrace,
2362 SourceLocation RBrace,
2363 const char *Lang,
2364 unsigned StrSize,
2365 DeclTy *D) {
Chris Lattnerc6fdc342008-01-12 07:05:38 +00002366 LinkageSpecDecl::LanguageIDs Language;
2367 Decl *dcl = static_cast<Decl *>(D);
2368 if (strncmp(Lang, "\"C\"", StrSize) == 0)
2369 Language = LinkageSpecDecl::lang_c;
2370 else if (strncmp(Lang, "\"C++\"", StrSize) == 0)
2371 Language = LinkageSpecDecl::lang_cxx;
2372 else {
2373 Diag(Loc, diag::err_bad_language);
2374 return 0;
2375 }
2376
2377 // FIXME: Add all the various semantics of linkage specifications
Chris Lattner8e25d862008-03-16 00:16:02 +00002378 return LinkageSpecDecl::Create(Context, Loc, Language, dcl);
Chris Lattnerc6fdc342008-01-12 07:05:38 +00002379}