blob: 99b1872ae2b2066c83b935b7410610a3ae08ec2f [file] [log] [blame]
Chris Lattner697e5d62006-11-09 06:32:27 +00001//===--- SemaDecl.cpp - Semantic Analysis for Declarations ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for declarations.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattnere168f762006-11-10 05:29:30 +000014#include "Sema.h"
Chris Lattner5c5fbcc2006-12-03 08:41:30 +000015#include "clang/AST/ASTContext.h"
Chris Lattner9561a0b2007-01-28 08:20:04 +000016#include "clang/AST/Builtins.h"
Chris Lattnere168f762006-11-10 05:29:30 +000017#include "clang/AST/Decl.h"
Chris Lattner1300fb92007-01-23 23:42:53 +000018#include "clang/AST/Expr.h"
Chris Lattnerf84a79c2006-11-11 22:59:23 +000019#include "clang/AST/Type.h"
Steve Naroff45552922007-06-01 21:56:17 +000020#include "clang/AST/Attr.h"
Chris Lattner591a6752006-11-19 23:16:18 +000021#include "clang/Parse/DeclSpec.h"
Chris Lattnere168f762006-11-10 05:29:30 +000022#include "clang/Parse/Scope.h"
23#include "clang/Lex/IdentifierTable.h"
Chris Lattnerac18be92006-11-20 06:49:47 +000024#include "clang/Basic/LangOptions.h"
Chris Lattner9561a0b2007-01-28 08:20:04 +000025#include "clang/Basic/TargetInfo.h"
Chris Lattner38047f92007-01-27 06:24:01 +000026#include "llvm/ADT/SmallSet.h"
Steve Naroffd50c88e2007-04-05 21:15:20 +000027
Chris Lattner697e5d62006-11-09 06:32:27 +000028using namespace llvm;
29using namespace clang;
30
Steve Naroff8eeeb132007-05-08 21:09:37 +000031// C99: 6.7.5p3: Used by ParseDeclarator/ParseField to make sure we have
32// a constant expression of type int with a value greater than zero.
Chris Lattner6046e002007-06-05 06:39:23 +000033bool Sema::VerifyConstantArrayType(const ArrayType *Array,
34 SourceLocation DeclLoc) {
35 const Expr *Size = Array->getSize();
36 if (Size == 0) return false; // incomplete type.
37
38 if (!Size->getType()->isIntegerType()) {
39 Diag(Size->getLocStart(), diag::err_array_size_non_int,
40 Size->getType().getAsString(), Size->getSourceRange());
41 return true;
Steve Naroff8eeeb132007-05-08 21:09:37 +000042 }
Chris Lattner6046e002007-06-05 06:39:23 +000043
44 // Verify that the size of the array is an integer constant expr.
45 SourceLocation Loc;
46 APSInt SizeVal(32);
47 if (!Size->isIntegerConstantExpr(SizeVal, &Loc)) {
48 // FIXME: This emits the diagnostic to enforce 6.7.2.1p8, but the message
49 // is wrong. It is also wrong for static variables.
50 Diag(DeclLoc, diag::err_typecheck_illegal_vla, Size->getSourceRange());
51 return true;
52 }
53
54 // We have a constant expression with an integer type, now make sure
55 // value greater than zero (C99 6.7.5.2p1).
56
57 // FIXME: This check isn't specific to static VLAs, this should be moved
58 // elsewhere or replicated. 'int X[-1];' inside a function should emit an
59 // error.
60 if (SizeVal.isSigned()) {
61 APSInt Zero(SizeVal.getBitWidth());
62 Zero.setIsUnsigned(false);
63 if (SizeVal < Zero) {
64 Diag(DeclLoc, diag::err_typecheck_negative_array_size,
65 Size->getSourceRange());
66 return true;
67 } else if (SizeVal == 0) {
68 // GCC accepts zero sized static arrays.
69 Diag(DeclLoc, diag::err_typecheck_zero_array_size,
70 Size->getSourceRange());
71 }
72 }
73 return false;
Steve Naroff8eeeb132007-05-08 21:09:37 +000074}
Chris Lattnere168f762006-11-10 05:29:30 +000075
Chris Lattner2ebe4bb2006-11-20 01:29:42 +000076Sema::DeclTy *Sema::isTypeName(const IdentifierInfo &II, Scope *S) const {
Chris Lattner32d920b2007-01-26 02:01:53 +000077 return dyn_cast_or_null<TypedefDecl>(II.getFETokenInfo<Decl>());
Chris Lattnere168f762006-11-10 05:29:30 +000078}
79
Chris Lattner302b4be2006-11-19 02:31:38 +000080void Sema::PopScope(SourceLocation Loc, Scope *S) {
81 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
82 I != E; ++I) {
Chris Lattner99d31772007-01-21 22:37:37 +000083 Decl *D = static_cast<Decl*>(*I);
Chris Lattner302b4be2006-11-19 02:31:38 +000084 assert(D && "This decl didn't get pushed??");
Chris Lattnerff65b6b2007-01-23 01:33:16 +000085 IdentifierInfo *II = D->getIdentifier();
86 if (!II) continue;
Chris Lattner302b4be2006-11-19 02:31:38 +000087
Chris Lattnerff65b6b2007-01-23 01:33:16 +000088 // Unlink this decl from the identifier. Because the scope contains decls
89 // in an unordered collection, and because we have multiple identifier
90 // namespaces (e.g. tag, normal, label),the decl may not be the first entry.
91 if (II->getFETokenInfo<Decl>() == D) {
92 // Normal case, no multiple decls in different namespaces.
93 II->setFETokenInfo(D->getNext());
94 } else {
95 // Scan ahead. There are only three namespaces in C, so this loop can
96 // never execute more than 3 times.
97 Decl *SomeDecl = II->getFETokenInfo<Decl>();
98 while (SomeDecl->getNext() != D) {
99 SomeDecl = SomeDecl->getNext();
100 assert(SomeDecl && "Didn't find this decl on its identifier's chain!");
101 }
102 SomeDecl->setNext(D->getNext());
103 }
Chris Lattner302b4be2006-11-19 02:31:38 +0000104
Chris Lattner740b2f32006-11-21 01:32:20 +0000105 // This will have to be revisited for C++: there we want to nest stuff in
106 // namespace decls etc. Even for C, we might want a top-level translation
107 // unit decl or something.
108 if (!CurFunctionDecl)
109 continue;
110
111 // Chain this decl to the containing function, it now owns the memory for
112 // the decl.
113 D->setNext(CurFunctionDecl->getDeclChain());
114 CurFunctionDecl->setDeclChain(D);
Chris Lattner302b4be2006-11-19 02:31:38 +0000115 }
116}
117
Chris Lattner18b19622007-01-22 07:39:13 +0000118/// LookupScopedDecl - Look up the inner-most declaration in the specified
119/// namespace.
Chris Lattner9561a0b2007-01-28 08:20:04 +0000120Decl *Sema::LookupScopedDecl(IdentifierInfo *II, unsigned NSI,
121 SourceLocation IdLoc, Scope *S) {
Chris Lattner18b19622007-01-22 07:39:13 +0000122 if (II == 0) return 0;
Chris Lattnerb6738ec2007-01-28 00:38:24 +0000123 Decl::IdentifierNamespace NS = (Decl::IdentifierNamespace)NSI;
Chris Lattner18b19622007-01-22 07:39:13 +0000124
125 // Scan up the scope chain looking for a decl that matches this identifier
126 // that is in the appropriate namespace. This search should not take long, as
127 // shadowing of names is uncommon, and deep shadowing is extremely uncommon.
128 for (Decl *D = II->getFETokenInfo<Decl>(); D; D = D->getNext())
129 if (D->getIdentifierNamespace() == NS)
130 return D;
Chris Lattnerb6738ec2007-01-28 00:38:24 +0000131
Chris Lattner9561a0b2007-01-28 08:20:04 +0000132 // If we didn't find a use of this identifier, and if the identifier
133 // corresponds to a compiler builtin, create the decl object for the builtin
134 // now, injecting it into translation unit scope, and return it.
135 if (NS == Decl::IDNS_Ordinary) {
136 // If this is a builtin on some other target, or if this builtin varies
137 // across targets (e.g. in type), emit a diagnostic and mark the translation
138 // unit non-portable for using it.
139 if (II->isNonPortableBuiltin()) {
140 // Only emit this diagnostic once for this builtin.
141 II->setNonPortableBuiltin(false);
142 Context.Target.DiagnoseNonPortability(IdLoc,
143 diag::port_target_builtin_use);
144 }
Chris Lattner9561a0b2007-01-28 08:20:04 +0000145 // If this is a builtin on this (or all) targets, create the decl.
146 if (unsigned BuiltinID = II->getBuiltinID())
147 return LazilyCreateBuiltin(II, BuiltinID, S);
148 }
Chris Lattner18b19622007-01-22 07:39:13 +0000149 return 0;
150}
151
Chris Lattner9561a0b2007-01-28 08:20:04 +0000152/// LazilyCreateBuiltin - The specified Builtin-ID was first used at file scope.
153/// lazily create a decl for it.
154Decl *Sema::LazilyCreateBuiltin(IdentifierInfo *II, unsigned bid, Scope *S) {
155 Builtin::ID BID = (Builtin::ID)bid;
156
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000157 QualType R = Context.BuiltinInfo.GetBuiltinType(BID, Context);
Chris Lattner776fac82007-06-09 00:53:06 +0000158 FunctionDecl *New = new FunctionDecl(SourceLocation(), II, R,
159 FunctionDecl::Extern, 0);
Chris Lattner9561a0b2007-01-28 08:20:04 +0000160
161 // Find translation-unit scope to insert this function into.
162 while (S->getParent())
163 S = S->getParent();
164 S->AddDecl(New);
165
166 // Add this decl to the end of the identifier info.
167 if (Decl *LastDecl = II->getFETokenInfo<Decl>()) {
168 // Scan until we find the last (outermost) decl in the id chain.
169 while (LastDecl->getNext())
170 LastDecl = LastDecl->getNext();
171 // Insert before (outside) it.
172 LastDecl->setNext(New);
173 } else {
174 II->setFETokenInfo(New);
175 }
176 // Make sure clients iterating over decls see this.
177 LastInGroupList.push_back(New);
178
179 return New;
180}
181
Chris Lattner01564d92007-01-27 19:27:06 +0000182/// MergeTypeDefDecl - We just parsed a typedef 'New' which has the same name
183/// and scope as a previous declaration 'Old'. Figure out how to resolve this
184/// situation, merging decls or emitting diagnostics as appropriate.
185///
Chris Lattnerc511efb2007-01-27 19:32:14 +0000186TypedefDecl *Sema::MergeTypeDefDecl(TypedefDecl *New, Decl *OldD) {
187 // Verify the old decl was also a typedef.
188 TypedefDecl *Old = dyn_cast<TypedefDecl>(OldD);
189 if (!Old) {
190 Diag(New->getLocation(), diag::err_redefinition_different_kind,
191 New->getName());
192 Diag(OldD->getLocation(), diag::err_previous_definition);
193 return New;
194 }
195
Chris Lattner01564d92007-01-27 19:27:06 +0000196 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
197 // TODO: This is totally simplistic. It should handle merging functions
198 // together etc, merging extern int X; int X; ...
199 Diag(New->getLocation(), diag::err_redefinition, New->getName());
200 Diag(Old->getLocation(), diag::err_previous_definition);
201 return New;
202}
203
204/// MergeFunctionDecl - We just parsed a function 'New' which has the same name
205/// and scope as a previous declaration 'Old'. Figure out how to resolve this
206/// situation, merging decls or emitting diagnostics as appropriate.
207///
Chris Lattnerc511efb2007-01-27 19:32:14 +0000208FunctionDecl *Sema::MergeFunctionDecl(FunctionDecl *New, Decl *OldD) {
209 // Verify the old decl was also a function.
210 FunctionDecl *Old = dyn_cast<FunctionDecl>(OldD);
211 if (!Old) {
212 Diag(New->getLocation(), diag::err_redefinition_different_kind,
213 New->getName());
214 Diag(OldD->getLocation(), diag::err_previous_definition);
215 return New;
216 }
217
Chris Lattnerefe4aea2007-01-27 19:35:39 +0000218 // This is not right, but it's a start. If 'Old' is a function prototype with
219 // the same type as 'New', silently allow this. FIXME: We should link up decl
220 // objects here.
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000221 if (Old->getBody() == 0 &&
222 Old->getCanonicalType() == New->getCanonicalType()) {
Chris Lattnerefe4aea2007-01-27 19:35:39 +0000223 return New;
224 }
Chris Lattnerc511efb2007-01-27 19:32:14 +0000225
Chris Lattner01564d92007-01-27 19:27:06 +0000226 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
227 // TODO: This is totally simplistic. It should handle merging functions
228 // together etc, merging extern int X; int X; ...
229 Diag(New->getLocation(), diag::err_redefinition, New->getName());
230 Diag(Old->getLocation(), diag::err_previous_definition);
231 return New;
232}
233
234/// MergeVarDecl - We just parsed a variable '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 Narofffc49d672007-04-01 21:27:45 +0000238/// FIXME: Need to carefully consider tentative definition rules (C99 6.9.2p2).
239/// For example, we incorrectly complain about i1, i4 from C99 6.9.2p4.
240///
Chris Lattnerc511efb2007-01-27 19:32:14 +0000241VarDecl *Sema::MergeVarDecl(VarDecl *New, Decl *OldD) {
242 // Verify the old decl was also a variable.
243 VarDecl *Old = dyn_cast<VarDecl>(OldD);
244 if (!Old) {
245 Diag(New->getLocation(), diag::err_redefinition_different_kind,
246 New->getName());
247 Diag(OldD->getLocation(), diag::err_previous_definition);
248 return New;
249 }
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000250 // Verify the types match.
251 if (Old->getCanonicalType() != New->getCanonicalType()) {
252 Diag(New->getLocation(), diag::err_redefinition, New->getName());
253 Diag(Old->getLocation(), diag::err_previous_definition);
254 return New;
255 }
256 // We've verified the types match, now check if Old is "extern".
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000257 if (Old->getStorageClass() != VarDecl::Extern) {
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000258 Diag(New->getLocation(), diag::err_redefinition, New->getName());
259 Diag(Old->getLocation(), diag::err_previous_definition);
260 }
Chris Lattner01564d92007-01-27 19:27:06 +0000261 return New;
262}
263
Chris Lattnerb6738ec2007-01-28 00:38:24 +0000264/// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with
265/// no declarator (e.g. "struct foo;") is parsed.
266Sema::DeclTy *Sema::ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS) {
267 // TODO: emit error on 'int;' or 'const enum foo;'.
268 // TODO: emit error on 'typedef int;'
269 // if (!DS.isMissingDeclaratorOk()) Diag(...);
270
271 return 0;
272}
273
Chris Lattner776fac82007-06-09 00:53:06 +0000274Sema::DeclTy *
Chris Lattnere168f762006-11-10 05:29:30 +0000275Sema::ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init,
Chris Lattner776fac82007-06-09 00:53:06 +0000276 DeclTy *lastDeclarator) {
277 Decl *LastDeclarator = (Decl*)lastDeclarator;
Chris Lattnere168f762006-11-10 05:29:30 +0000278 IdentifierInfo *II = D.getIdentifier();
Chris Lattner302b4be2006-11-19 02:31:38 +0000279
Chris Lattner01564d92007-01-27 19:27:06 +0000280 // See if this is a redefinition of a variable in the same scope.
Chris Lattner9561a0b2007-01-28 08:20:04 +0000281 Decl *PrevDecl = LookupScopedDecl(II, Decl::IDNS_Ordinary,
282 D.getIdentifierLoc(), S);
Chris Lattner776fac82007-06-09 00:53:06 +0000283 if (PrevDecl && !S->isDeclScope(PrevDecl))
Chris Lattner01564d92007-01-27 19:27:06 +0000284 PrevDecl = 0; // If in outer scope, it isn't the same thing.
285
Chris Lattnere168f762006-11-10 05:29:30 +0000286 Decl *New;
Chris Lattner01a7c532007-01-25 23:09:03 +0000287 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
Chris Lattner776fac82007-06-09 00:53:06 +0000288 TypedefDecl *NewTD = ParseTypedefDecl(S, D, LastDeclarator);
Chris Lattner01564d92007-01-27 19:27:06 +0000289 if (!NewTD) return 0;
290
291 // Merge the decl with the existing one if appropriate.
292 if (PrevDecl) {
293 NewTD = MergeTypeDefDecl(NewTD, PrevDecl);
294 if (NewTD == 0) return 0;
295 }
296 New = NewTD;
Steve Naroff8eeeb132007-05-08 21:09:37 +0000297 if (S->getParent() == 0) {
298 // C99 6.7.7p2: If a typedef name specifies a variably modified type
299 // then it shall have block scope.
300 if (ArrayType *ary = dyn_cast<ArrayType>(NewTD->getUnderlyingType())) {
Chris Lattner6046e002007-06-05 06:39:23 +0000301 if (VerifyConstantArrayType(ary, D.getIdentifierLoc()))
Steve Naroff8eeeb132007-05-08 21:09:37 +0000302 return 0;
303 }
304 }
Chris Lattner01a7c532007-01-25 23:09:03 +0000305 } else if (D.isFunctionDeclarator()) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000306 QualType R = GetTypeForDeclarator(D, S);
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000307 if (R.isNull()) return 0; // FIXME: "auto func();" passes through...
Chris Lattner01564d92007-01-27 19:27:06 +0000308
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000309 FunctionDecl::StorageClass SC;
310 switch (D.getDeclSpec().getStorageClassSpec()) {
311 default: assert(0 && "Unknown storage class!");
312 case DeclSpec::SCS_auto:
313 case DeclSpec::SCS_register:
Chris Lattnerc04bd6a2007-05-16 18:09:54 +0000314 Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_func,
315 R.getAsString());
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000316 return 0;
317 case DeclSpec::SCS_unspecified: SC = FunctionDecl::None; break;
318 case DeclSpec::SCS_extern: SC = FunctionDecl::Extern; break;
319 case DeclSpec::SCS_static: SC = FunctionDecl::Static; break;
320 }
321
Chris Lattner776fac82007-06-09 00:53:06 +0000322 FunctionDecl *NewFD = new FunctionDecl(D.getIdentifierLoc(), II, R, SC,
323 LastDeclarator);
Chris Lattner01564d92007-01-27 19:27:06 +0000324
325 // Merge the decl with the existing one if appropriate.
326 if (PrevDecl) {
327 NewFD = MergeFunctionDecl(NewFD, PrevDecl);
328 if (NewFD == 0) return 0;
329 }
330 New = NewFD;
Chris Lattner01a7c532007-01-25 23:09:03 +0000331 } else {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000332 QualType R = GetTypeForDeclarator(D, S);
Chris Lattner01a7c532007-01-25 23:09:03 +0000333 if (R.isNull()) return 0;
Chris Lattner01564d92007-01-27 19:27:06 +0000334
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000335 VarDecl *NewVD;
336 VarDecl::StorageClass SC;
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000337 switch (D.getDeclSpec().getStorageClassSpec()) {
338 default: assert(0 && "Unknown storage class!");
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000339 case DeclSpec::SCS_unspecified: SC = VarDecl::None; break;
340 case DeclSpec::SCS_extern: SC = VarDecl::Extern; break;
341 case DeclSpec::SCS_static: SC = VarDecl::Static; break;
342 case DeclSpec::SCS_auto: SC = VarDecl::Auto; break;
343 case DeclSpec::SCS_register: SC = VarDecl::Register; break;
344 }
Steve Narofffc49d672007-04-01 21:27:45 +0000345 if (S->getParent() == 0) {
346 // File scope. C99 6.9.2p2: A declaration of an identifier for and
347 // object that has file scope without an initializer, and without a
348 // storage-class specifier or with the storage-class specifier "static",
349 // constitutes a tentative definition. Note: A tentative definition with
350 // external linkage is valid (C99 6.2.2p5).
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000351 if (!Init && SC == VarDecl::Static) {
Steve Narofffc49d672007-04-01 21:27:45 +0000352 // C99 6.9.2p3: If the declaration of an identifier for an object is
353 // a tentative definition and has internal linkage (C99 6.2.2p3), the
354 // declared type shall not be an incomplete type.
355 if (R->isIncompleteType()) {
Chris Lattnerc04bd6a2007-05-16 18:09:54 +0000356 Diag(D.getIdentifierLoc(), diag::err_typecheck_decl_incomplete_type,
357 R.getAsString());
Steve Narofffc49d672007-04-01 21:27:45 +0000358 return 0;
359 }
Steve Naroffca8f7122007-04-01 01:41:35 +0000360 }
Bill Wendlingd6de6572007-06-02 09:40:07 +0000361 // C99 6.9p2: The storage-class specifiers auto and register shall not
362 // appear in the declaration specifiers in an external declaration.
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000363 if (SC == VarDecl::Auto || SC == VarDecl::Register) {
Chris Lattnerc04bd6a2007-05-16 18:09:54 +0000364 Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_fscope,
365 R.getAsString());
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000366 return 0;
367 }
Steve Naroff8eeeb132007-05-08 21:09:37 +0000368 // C99 6.7.5.2p2: If an identifier is declared to be an object with
369 // static storage duration, it shall not have a variable length array.
Chris Lattner6046e002007-06-05 06:39:23 +0000370 if (ArrayType *ary = dyn_cast<ArrayType>(R.getCanonicalType())) {
371 if (VerifyConstantArrayType(ary, D.getIdentifierLoc()))
Steve Naroff8eeeb132007-05-08 21:09:37 +0000372 return 0;
373 }
Chris Lattner776fac82007-06-09 00:53:06 +0000374 NewVD = new FileVarDecl(D.getIdentifierLoc(), II, R, SC, LastDeclarator);
Steve Narofffc49d672007-04-01 21:27:45 +0000375 } else {
376 // Block scope. C99 6.7p7: If an identifier for an object is declared with
377 // no linkage (C99 6.2.2p6), the type for the object shall be complete...
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000378 if (SC != VarDecl::Extern) {
Steve Narofffc49d672007-04-01 21:27:45 +0000379 if (R->isIncompleteType()) {
Chris Lattnerc04bd6a2007-05-16 18:09:54 +0000380 Diag(D.getIdentifierLoc(), diag::err_typecheck_decl_incomplete_type,
381 R.getAsString());
Steve Narofffc49d672007-04-01 21:27:45 +0000382 return 0;
383 }
384 }
Steve Naroff8eeeb132007-05-08 21:09:37 +0000385 if (SC == VarDecl::Static) {
386 // C99 6.7.5.2p2: If an identifier is declared to be an object with
387 // static storage duration, it shall not have a variable length array.
Chris Lattner6046e002007-06-05 06:39:23 +0000388 if (ArrayType *ary = dyn_cast<ArrayType>(R.getCanonicalType())) {
389 if (VerifyConstantArrayType(ary, D.getIdentifierLoc()))
Steve Naroff8eeeb132007-05-08 21:09:37 +0000390 return 0;
391 }
392 }
Chris Lattner776fac82007-06-09 00:53:06 +0000393 NewVD = new BlockVarDecl(D.getIdentifierLoc(), II, R, SC, LastDeclarator);
Steve Naroffef2ab6a2007-04-02 20:50:54 +0000394 }
Chris Lattner01564d92007-01-27 19:27:06 +0000395 // Merge the decl with the existing one if appropriate.
396 if (PrevDecl) {
397 NewVD = MergeVarDecl(NewVD, PrevDecl);
398 if (NewVD == 0) return 0;
399 }
400 New = NewVD;
Chris Lattner01a7c532007-01-25 23:09:03 +0000401 }
Chris Lattner302b4be2006-11-19 02:31:38 +0000402
403
Chris Lattnere168f762006-11-10 05:29:30 +0000404 // If this has an identifier, add it to the scope stack.
405 if (II) {
Chris Lattner18b19622007-01-22 07:39:13 +0000406 New->setNext(II->getFETokenInfo<Decl>());
Chris Lattnere168f762006-11-10 05:29:30 +0000407 II->setFETokenInfo(New);
Chris Lattner99d31772007-01-21 22:37:37 +0000408 S->AddDecl(New);
Chris Lattnere168f762006-11-10 05:29:30 +0000409 }
410
Steve Naroff26c8ea52007-03-21 21:08:52 +0000411 if (S->getParent() == 0)
Chris Lattner776fac82007-06-09 00:53:06 +0000412 AddTopLevelDecl(New, LastDeclarator);
Chris Lattnere168f762006-11-10 05:29:30 +0000413
414 return New;
415}
416
Chris Lattner776fac82007-06-09 00:53:06 +0000417/// The declarators are chained together backwards, reverse the list.
418Sema::DeclTy *Sema::FinalizeDeclaratorGroup(Scope *S, DeclTy *group) {
419 // Often we have single declarators, handle them quickly.
420 Decl *Group = static_cast<Decl*>(group);
421 if (Group->getNextDeclarator() == 0) return Group;
422
423 Decl *NewGroup = 0;
424 while (Group) {
425 Decl *Next = Group->getNextDeclarator();
426 Group->setNextDeclarator(NewGroup);
427 NewGroup = Group;
428 Group = Next;
429 }
430 return NewGroup;
431}
432
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000433VarDecl *
434Sema::ParseParamDeclarator(DeclaratorChunk &FTI, unsigned ArgNo,
435 Scope *FnScope) {
436 const DeclaratorChunk::ParamInfo &PI = FTI.Fun.ArgInfo[ArgNo];
Chris Lattner200bdc32006-11-19 02:43:37 +0000437
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000438 IdentifierInfo *II = PI.Ident;
Chris Lattnerc284e9b2007-01-23 05:14:32 +0000439 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
440 // Can this happen for params? We already checked that they don't conflict
441 // among each other. Here they can only shadow globals, which is ok.
Chris Lattner9561a0b2007-01-28 08:20:04 +0000442 if (Decl *PrevDecl = LookupScopedDecl(II, Decl::IDNS_Ordinary,
443 PI.IdentLoc, FnScope)) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000444
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000445 }
446
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000447 // FIXME: Handle storage class (auto, register). No declarator?
Chris Lattner776fac82007-06-09 00:53:06 +0000448 // TODO: Chain to previous parameter with the prevdeclarator chain?
Steve Naroffef2ab6a2007-04-02 20:50:54 +0000449 VarDecl *New = new ParmVarDecl(PI.IdentLoc, II,
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000450 QualType::getFromOpaquePtr(PI.TypeInfo),
Chris Lattner776fac82007-06-09 00:53:06 +0000451 VarDecl::None, 0);
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000452
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000453 // If this has an identifier, add it to the scope stack.
454 if (II) {
Chris Lattner18b19622007-01-22 07:39:13 +0000455 New->setNext(II->getFETokenInfo<Decl>());
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000456 II->setFETokenInfo(New);
Chris Lattner99d31772007-01-21 22:37:37 +0000457 FnScope->AddDecl(New);
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000458 }
Chris Lattner229ce602006-11-21 01:21:07 +0000459
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000460 return New;
461}
462
463
464Sema::DeclTy *Sema::ParseStartOfFunctionDef(Scope *FnBodyScope, Declarator &D) {
Chris Lattner229ce602006-11-21 01:21:07 +0000465 assert(CurFunctionDecl == 0 && "Function parsing confused");
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000466 assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function &&
467 "Not a function declarator!");
468 DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
469
470 // Verify 6.9.1p6: 'every identifier in the identifier list shall be declared'
471 // for a K&R function.
472 if (!FTI.hasPrototype) {
473 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
474 if (FTI.ArgInfo[i].TypeInfo == 0) {
475 Diag(FTI.ArgInfo[i].IdentLoc, diag::err_param_not_declared,
476 FTI.ArgInfo[i].Ident->getName());
477 // Implicitly declare the argument as type 'int' for lack of a better
478 // type.
479 FTI.ArgInfo[i].TypeInfo = Context.IntTy.getAsOpaquePtr();
480 }
481 }
482
483 // Since this is a function definition, act as though we have information
484 // about the arguments.
485 FTI.hasPrototype = true;
Chris Lattner2114d5e2006-12-04 07:40:24 +0000486 } else {
487 // FIXME: Diagnose arguments without names in C.
488
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000489 }
490
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000491 Scope *GlobalScope = FnBodyScope->getParent();
492
493 FunctionDecl *FD =
494 static_cast<FunctionDecl*>(ParseDeclarator(GlobalScope, D, 0, 0));
Chris Lattner229ce602006-11-21 01:21:07 +0000495 CurFunctionDecl = FD;
Chris Lattner2114d5e2006-12-04 07:40:24 +0000496
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000497 // Create Decl objects for each parameter, adding them to the FunctionDecl.
498 SmallVector<VarDecl*, 16> Params;
Chris Lattnerf61c8a82007-01-21 19:04:43 +0000499
500 // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs function that takes
501 // no arguments, not a function that takes a single void argument.
502 if (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 &&
503 FTI.ArgInfo[0].TypeInfo == Context.VoidTy.getAsOpaquePtr()) {
Chris Lattner99d31772007-01-21 22:37:37 +0000504 // empty arg list, don't push any params.
Chris Lattnerf61c8a82007-01-21 19:04:43 +0000505 } else {
506 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i)
507 Params.push_back(ParseParamDeclarator(D.getTypeObject(0), i,FnBodyScope));
508 }
Chris Lattner2114d5e2006-12-04 07:40:24 +0000509
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000510 FD->setParams(&Params[0], Params.size());
Chris Lattner2114d5e2006-12-04 07:40:24 +0000511
Chris Lattnere168f762006-11-10 05:29:30 +0000512 return FD;
513}
514
Chris Lattner229ce602006-11-21 01:21:07 +0000515Sema::DeclTy *Sema::ParseFunctionDefBody(DeclTy *D, StmtTy *Body) {
516 FunctionDecl *FD = static_cast<FunctionDecl*>(D);
517 FD->setBody((Stmt*)Body);
518
519 assert(FD == CurFunctionDecl && "Function parsing confused");
520 CurFunctionDecl = 0;
Chris Lattnere2473062007-05-28 06:28:18 +0000521
522 // Verify and clean out per-function state.
523
524 // Check goto/label use.
525 for (DenseMap<IdentifierInfo*, LabelStmt*>::iterator I = LabelMap.begin(),
526 E = LabelMap.end(); I != E; ++I) {
527 // Verify that we have no forward references left. If so, there was a goto
528 // or address of a label taken, but no definition of it. Label fwd
529 // definitions are indicated with a null substmt.
530 if (I->second->getSubStmt() == 0) {
531 LabelStmt *L = I->second;
532 // Emit error.
Chris Lattnereefa10e2007-05-28 06:56:27 +0000533 Diag(L->getIdentLoc(), diag::err_undeclared_label_use, L->getName());
Chris Lattnere2473062007-05-28 06:28:18 +0000534
535 // At this point, we have gotos that use the bogus label. Stitch it into
536 // the function body so that they aren't leaked and that the AST is well
537 // formed.
538 L->setSubStmt(new NullStmt(L->getIdentLoc()));
539 cast<CompoundStmt>((Stmt*)Body)->push_back(L);
540 }
541 }
542 LabelMap.clear();
543
Chris Lattner229ce602006-11-21 01:21:07 +0000544 return FD;
545}
546
547
Chris Lattnerac18be92006-11-20 06:49:47 +0000548/// ImplicitlyDefineFunction - An undeclared identifier was used in a function
549/// call, forming a call to an implicitly defined function (per C99 6.5.1p2).
550Decl *Sema::ImplicitlyDefineFunction(SourceLocation Loc, IdentifierInfo &II,
551 Scope *S) {
552 if (getLangOptions().C99) // Extension in C99.
553 Diag(Loc, diag::ext_implicit_function_decl, II.getName());
554 else // Legal in C90, but warn about it.
555 Diag(Loc, diag::warn_implicit_function_decl, II.getName());
556
557 // FIXME: handle stuff like:
558 // void foo() { extern float X(); }
559 // void bar() { X(); } <-- implicit decl for X in another scope.
560
561 // Set a Declarator for the implicit definition: int foo();
Chris Lattner353f5742006-11-28 04:50:12 +0000562 const char *Dummy;
Chris Lattnerac18be92006-11-20 06:49:47 +0000563 DeclSpec DS;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000564 bool Error = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, Dummy);
Chris Lattnerb055f2d2007-02-11 08:19:57 +0000565 Error = Error; // Silence warning.
Chris Lattner353f5742006-11-28 04:50:12 +0000566 assert(!Error && "Error setting up implicit decl!");
Chris Lattnerac18be92006-11-20 06:49:47 +0000567 Declarator D(DS, Declarator::BlockContext);
Chris Lattnercbc426d2006-12-02 06:43:02 +0000568 D.AddTypeInfo(DeclaratorChunk::getFunction(false, false, 0, 0, Loc));
Chris Lattnerac18be92006-11-20 06:49:47 +0000569 D.SetIdentifier(&II, Loc);
570
Chris Lattner62d2e662007-01-28 00:21:37 +0000571 // Find translation-unit scope to insert this function into.
572 while (S->getParent())
573 S = S->getParent();
Chris Lattnerac18be92006-11-20 06:49:47 +0000574
Chris Lattner62d2e662007-01-28 00:21:37 +0000575 return static_cast<Decl*>(ParseDeclarator(S, D, 0, 0));
Chris Lattnerac18be92006-11-20 06:49:47 +0000576}
577
Chris Lattner302b4be2006-11-19 02:31:38 +0000578
Chris Lattner776fac82007-06-09 00:53:06 +0000579TypedefDecl *Sema::ParseTypedefDecl(Scope *S, Declarator &D,
580 Decl *LastDeclarator) {
581 assert(D.getIdentifier() && "Wrong callback for declspec without declarator");
Chris Lattner302b4be2006-11-19 02:31:38 +0000582
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000583 QualType T = GetTypeForDeclarator(D, S);
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000584 if (T.isNull()) return 0;
585
Chris Lattner18b19622007-01-22 07:39:13 +0000586 // Scope manipulation handled by caller.
Chris Lattner776fac82007-06-09 00:53:06 +0000587 return new TypedefDecl(D.getIdentifierLoc(), D.getIdentifier(), T,
588 LastDeclarator);
Chris Lattnere168f762006-11-10 05:29:30 +0000589}
590
Chris Lattner18b19622007-01-22 07:39:13 +0000591
Chris Lattner1300fb92007-01-23 23:42:53 +0000592/// ParseTag - This is invoked when we see 'struct foo' or 'struct {'. In the
593/// former case, Name will be non-null. In the later case, Name will be null.
594/// TagType indicates what kind of tag this is. TK indicates whether this is a
595/// reference/declaration/definition of a tag.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000596Sema::DeclTy *Sema::ParseTag(Scope *S, unsigned TagType, TagKind TK,
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000597 SourceLocation KWLoc, IdentifierInfo *Name,
598 SourceLocation NameLoc) {
Chris Lattner8799cf22007-01-23 01:57:16 +0000599 // If this is a use of an existing tag, it must have a name.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000600 assert((Name != 0 || TK == TK_Definition) &&
601 "Nameless record must be a definition!");
Chris Lattner8799cf22007-01-23 01:57:16 +0000602
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000603 Decl::Kind Kind;
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000604 switch (TagType) {
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000605 default: assert(0 && "Unknown tag type!");
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000606 case DeclSpec::TST_struct: Kind = Decl::Struct; break;
607 case DeclSpec::TST_union: Kind = Decl::Union; break;
608//case DeclSpec::TST_class: Kind = Decl::Class; break;
609 case DeclSpec::TST_enum: Kind = Decl::Enum; break;
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000610 }
Chris Lattner7e783a12007-01-23 02:05:42 +0000611
Chris Lattner18b19622007-01-22 07:39:13 +0000612 // If this is a named struct, check to see if there was a previous forward
613 // declaration or definition.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000614 if (TagDecl *PrevDecl =
Chris Lattner9561a0b2007-01-28 08:20:04 +0000615 dyn_cast_or_null<TagDecl>(LookupScopedDecl(Name, Decl::IDNS_Tag,
616 NameLoc, S))) {
Chris Lattner8799cf22007-01-23 01:57:16 +0000617
618 // If this is a use of a previous tag, or if the tag is already declared in
619 // the same scope (so that the definition/declaration completes or
620 // rementions the tag), reuse the decl.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000621 if (TK == TK_Reference || S->isDeclScope(PrevDecl)) {
Chris Lattner7e783a12007-01-23 02:05:42 +0000622 // Make sure that this wasn't declared as an enum and now used as a struct
623 // or something similar.
624 if (PrevDecl->getKind() != Kind) {
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000625 Diag(KWLoc, diag::err_use_with_wrong_tag, Name->getName());
Chris Lattner7e783a12007-01-23 02:05:42 +0000626 Diag(PrevDecl->getLocation(), diag::err_previous_use);
627 }
Chris Lattner7b9ace62007-01-23 20:11:08 +0000628
629 // If this is a use or a forward declaration, we're good.
630 if (TK != TK_Definition)
631 return PrevDecl;
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000632
Chris Lattner7b9ace62007-01-23 20:11:08 +0000633 // Diagnose attempts to redefine a tag.
634 if (PrevDecl->isDefinition()) {
635 Diag(NameLoc, diag::err_redefinition, Name->getName());
636 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
637 // If this is a redefinition, recover by making this struct be
638 // anonymous, which will make any later references get the previous
639 // definition.
640 Name = 0;
641 } else {
642 // Okay, this is definition of a previously declared or referenced tag.
643 // Move the location of the decl to be the definition site.
644 PrevDecl->setLocation(NameLoc);
Chris Lattner7b9ace62007-01-23 20:11:08 +0000645 return PrevDecl;
646 }
Chris Lattner8799cf22007-01-23 01:57:16 +0000647 }
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000648 // If we get here, this is a definition of a new struct type in a nested
649 // scope, e.g. "struct foo; void bar() { struct foo; }", just create a new
650 // type.
Chris Lattner18b19622007-01-22 07:39:13 +0000651 }
652
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000653 // If there is an identifier, use the location of the identifier as the
654 // location of the decl, otherwise use the location of the struct/union
655 // keyword.
656 SourceLocation Loc = NameLoc.isValid() ? NameLoc : KWLoc;
657
Chris Lattner18b19622007-01-22 07:39:13 +0000658 // Otherwise, if this is the first time we've seen this tag, create the decl.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000659 TagDecl *New;
Chris Lattner720a0542007-01-25 00:44:24 +0000660 switch (Kind) {
661 default: assert(0 && "Unknown tag kind!");
Chris Lattner5f521502007-01-25 06:27:24 +0000662 case Decl::Enum:
Chris Lattner776fac82007-06-09 00:53:06 +0000663 // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.:
664 // enum X { A, B, C } D; D should chain to X.
665 New = new EnumDecl(Loc, Name, 0);
Chris Lattner5f521502007-01-25 06:27:24 +0000666 // If this is an undefined enum, warn.
Chris Lattnerc1915e22007-01-25 07:29:02 +0000667 if (TK != TK_Definition) Diag(Loc, diag::ext_forward_ref_enum);
Chris Lattner5f521502007-01-25 06:27:24 +0000668 break;
Chris Lattner720a0542007-01-25 00:44:24 +0000669 case Decl::Union:
670 case Decl::Struct:
671 case Decl::Class:
Chris Lattner776fac82007-06-09 00:53:06 +0000672 // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.:
673 // struct X { int A; } D; D should chain to X.
674 New = new RecordDecl(Kind, Loc, Name, 0);
Chris Lattner720a0542007-01-25 00:44:24 +0000675 break;
676 }
Chris Lattner18b19622007-01-22 07:39:13 +0000677
678 // If this has an identifier, add it to the scope stack.
679 if (Name) {
680 New->setNext(Name->getFETokenInfo<Decl>());
681 Name->setFETokenInfo(New);
682 S->AddDecl(New);
683 }
684
685 return New;
686}
Chris Lattner1300fb92007-01-23 23:42:53 +0000687
688/// ParseField - Each field of a struct/union/class is passed into this in order
689/// to create a FieldDecl object for it.
690Sema::DeclTy *Sema::ParseField(Scope *S, DeclTy *TagDecl,
691 SourceLocation DeclStart,
692 Declarator &D, ExprTy *BitfieldWidth) {
693 IdentifierInfo *II = D.getIdentifier();
694 Expr *BitWidth = (Expr*)BitfieldWidth;
695
696 SourceLocation Loc = DeclStart;
697 if (II) Loc = D.getIdentifierLoc();
698
Chris Lattner62d2e662007-01-28 00:21:37 +0000699 // FIXME: Unnamed fields can be handled in various different ways, for
700 // example, unnamed unions inject all members into the struct namespace!
701
702
Chris Lattner1300fb92007-01-23 23:42:53 +0000703 if (BitWidth) {
704 // TODO: Validate.
Steve Narofff84d11f2007-05-23 21:48:04 +0000705 //printf("WARNING: BITFIELDS IGNORED!\n");
Chris Lattner1300fb92007-01-23 23:42:53 +0000706
707 // 6.7.2.1p3
708 // 6.7.2.1p4
709
710 } else {
711 // Not a bitfield.
712
713 // validate II.
714
715 }
716
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000717 QualType T = GetTypeForDeclarator(D, S);
Chris Lattner01a7c532007-01-25 23:09:03 +0000718 if (T.isNull()) return 0;
Steve Naroff8eeeb132007-05-08 21:09:37 +0000719
720 // C99 6.7.2.1p8: A member of a structure or union may have any type other
721 // than a variably modified type.
Chris Lattner6046e002007-06-05 06:39:23 +0000722 if (ArrayType *ary = dyn_cast<ArrayType>(T.getCanonicalType())) {
723 if (VerifyConstantArrayType(ary, Loc))
Steve Naroff8eeeb132007-05-08 21:09:37 +0000724 return 0;
725 }
Chris Lattner776fac82007-06-09 00:53:06 +0000726
727 // FIXME: Chain fielddecls together.
728 return new FieldDecl(Loc, II, T, 0);
Chris Lattner1300fb92007-01-23 23:42:53 +0000729}
730
731void Sema::ParseRecordBody(SourceLocation RecLoc, DeclTy *RecDecl,
732 DeclTy **Fields, unsigned NumFields) {
Chris Lattnerc1915e22007-01-25 07:29:02 +0000733 RecordDecl *Record = cast<RecordDecl>(static_cast<Decl*>(RecDecl));
Chris Lattner1300fb92007-01-23 23:42:53 +0000734 if (Record->isDefinition()) {
735 // Diagnose code like:
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000736 // struct S { struct S {} X; };
Chris Lattner1300fb92007-01-23 23:42:53 +0000737 // We discover this when we complete the outer S. Reject and ignore the
738 // outer S.
739 Diag(Record->getLocation(), diag::err_nested_redefinition,
740 Record->getKindName());
741 Diag(RecLoc, diag::err_previous_definition);
742 return;
743 }
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000744
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000745 // Verify that all the fields are okay.
Chris Lattner82625602007-01-24 02:26:21 +0000746 unsigned NumNamedMembers = 0;
Steve Naroffcc321422007-03-26 23:09:51 +0000747 SmallVector<FieldDecl*, 32> RecFields;
Chris Lattnere5a66562007-01-25 22:48:42 +0000748 SmallSet<const IdentifierInfo*, 32> FieldIDs;
749
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000750 for (unsigned i = 0; i != NumFields; ++i) {
751 FieldDecl *FD = cast_or_null<FieldDecl>(static_cast<Decl*>(Fields[i]));
752 if (!FD) continue; // Already issued a diagnostic.
Chris Lattner720a0542007-01-25 00:44:24 +0000753
754 // Get the type for the field.
Steve Naroffd50c88e2007-04-05 21:15:20 +0000755 Type *FDTy = FD->getType().getCanonicalType().getTypePtr();
Chris Lattner720a0542007-01-25 00:44:24 +0000756
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000757 // C99 6.7.2.1p2 - A field may not be a function type.
Chris Lattner720a0542007-01-25 00:44:24 +0000758 if (isa<FunctionType>(FDTy)) {
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000759 Diag(FD->getLocation(), diag::err_field_declared_as_function,
760 FD->getName());
Chris Lattner82625602007-01-24 02:26:21 +0000761 delete FD;
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000762 continue;
763 }
764
Chris Lattner82625602007-01-24 02:26:21 +0000765 // C99 6.7.2.1p2 - A field may not be an incomplete type except...
Chris Lattner720a0542007-01-25 00:44:24 +0000766 if (FDTy->isIncompleteType()) {
Chris Lattner82625602007-01-24 02:26:21 +0000767 if (i != NumFields-1 || // ... that the last member ...
768 Record->getKind() != Decl::Struct || // ... of a structure ...
Chris Lattner720a0542007-01-25 00:44:24 +0000769 !isa<ArrayType>(FDTy)) { //... may have incomplete array type.
Chris Lattner82625602007-01-24 02:26:21 +0000770 Diag(FD->getLocation(), diag::err_field_incomplete, FD->getName());
771 delete FD;
772 continue;
773 }
Chris Lattner720a0542007-01-25 00:44:24 +0000774 if (NumNamedMembers < 1) { //... must have more than named member ...
Chris Lattner82625602007-01-24 02:26:21 +0000775 Diag(FD->getLocation(), diag::err_flexible_array_empty_struct,
776 FD->getName());
777 delete FD;
778 continue;
779 }
Chris Lattner720a0542007-01-25 00:44:24 +0000780
781 // Okay, we have a legal flexible array member at the end of the struct.
Chris Lattner41943152007-01-25 04:52:46 +0000782 Record->setHasFlexibleArrayMember(true);
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000783 }
Chris Lattner720a0542007-01-25 00:44:24 +0000784
785
786 /// C99 6.7.2.1p2 - a struct ending in a flexible array member cannot be the
787 /// field of another structure or the element of an array.
788 if (RecordType *FDTTy = dyn_cast<RecordType>(FDTy)) {
789 if (FDTTy->getDecl()->hasFlexibleArrayMember()) {
790 // If this is a member of a union, then entire union becomes "flexible".
791 if (Record->getKind() == Decl::Union) {
Chris Lattner41943152007-01-25 04:52:46 +0000792 Record->setHasFlexibleArrayMember(true);
Chris Lattner720a0542007-01-25 00:44:24 +0000793 } else {
794 // If this is a struct/class and this is not the last element, reject
795 // it. Note that GCC supports variable sized arrays in the middle of
796 // structures.
797 if (i != NumFields-1) {
798 Diag(FD->getLocation(), diag::err_variable_sized_type_in_struct,
799 FD->getName());
800 delete FD;
801 continue;
802 }
803
804 // We support flexible arrays at the end of structs in other structs
805 // as an extension.
806 Diag(FD->getLocation(), diag::ext_flexible_array_in_struct,
807 FD->getName());
Chris Lattner41943152007-01-25 04:52:46 +0000808 Record->setHasFlexibleArrayMember(true);
Chris Lattner720a0542007-01-25 00:44:24 +0000809 }
810 }
811 }
812
Chris Lattner82625602007-01-24 02:26:21 +0000813 // Keep track of the number of named members.
Chris Lattnere5a66562007-01-25 22:48:42 +0000814 if (IdentifierInfo *II = FD->getIdentifier()) {
815 // Detect duplicate member names.
Chris Lattnerbaf33662007-01-27 02:14:08 +0000816 if (!FieldIDs.insert(II)) {
Chris Lattnere5a66562007-01-25 22:48:42 +0000817 Diag(FD->getLocation(), diag::err_duplicate_member, II->getName());
818 // Find the previous decl.
819 SourceLocation PrevLoc;
820 for (unsigned i = 0, e = RecFields.size(); ; ++i) {
821 assert(i != e && "Didn't find previous def!");
822 if (RecFields[i]->getIdentifier() == II) {
823 PrevLoc = RecFields[i]->getLocation();
824 break;
825 }
826 }
827 Diag(PrevLoc, diag::err_previous_definition);
828 delete FD;
829 continue;
830 }
Chris Lattner82625602007-01-24 02:26:21 +0000831 ++NumNamedMembers;
Chris Lattnere5a66562007-01-25 22:48:42 +0000832 }
Chris Lattner41943152007-01-25 04:52:46 +0000833
834 // Remember good fields.
835 RecFields.push_back(FD);
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000836 }
Chris Lattner82625602007-01-24 02:26:21 +0000837
838
839 // Okay, we successfully defined 'Record'.
Chris Lattner41943152007-01-25 04:52:46 +0000840 Record->defineBody(&RecFields[0], RecFields.size());
Chris Lattner1300fb92007-01-23 23:42:53 +0000841}
842
Chris Lattnerc1915e22007-01-25 07:29:02 +0000843Sema::DeclTy *Sema::ParseEnumConstant(Scope *S, DeclTy *EnumDeclX,
844 SourceLocation IdLoc, IdentifierInfo *Id,
845 SourceLocation EqualLoc, ExprTy *Val) {
846 EnumDecl *TheEnumDecl = cast<EnumDecl>(static_cast<Decl*>(EnumDeclX));
Chris Lattner8116d1b2007-01-25 22:38:29 +0000847
848 // Verify that there isn't already something declared with this name in this
849 // scope.
Chris Lattner9561a0b2007-01-28 08:20:04 +0000850 if (Decl *PrevDecl = LookupScopedDecl(Id, Decl::IDNS_Ordinary, IdLoc, S)) {
Chris Lattner8116d1b2007-01-25 22:38:29 +0000851 if (S->isDeclScope(PrevDecl)) {
852 if (isa<EnumConstantDecl>(PrevDecl))
853 Diag(IdLoc, diag::err_redefinition_of_enumerator, Id->getName());
854 else
855 Diag(IdLoc, diag::err_redefinition, Id->getName());
856 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
857 return 0;
858 }
859 }
Steve Naroff72cada02007-05-18 00:18:52 +0000860 SourceLocation expLoc;
Steve Naroff63969212007-05-07 21:22:42 +0000861 // C99 6.7.2.2p2: Make sure we have an integer constant expression.
Chris Lattner6046e002007-06-05 06:39:23 +0000862 // FIXME: Capture this value in the enumconstantdecl.
Chris Lattner238cbc52007-06-02 04:48:48 +0000863 if (Val && !((Expr *)Val)->isIntegerConstantExpr(&expLoc)) {
Steve Naroff72cada02007-05-18 00:18:52 +0000864 Diag(expLoc, diag::err_enum_value_not_integer_constant_expr, Id->getName());
Steve Naroff63969212007-05-07 21:22:42 +0000865 return 0;
866 }
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000867 QualType Ty = Context.getTagDeclType(TheEnumDecl);
Chris Lattner776fac82007-06-09 00:53:06 +0000868 // FIXME: Chain EnumConstantDecl's together.
869 EnumConstantDecl *New = new EnumConstantDecl(IdLoc, Id, Ty, (Expr *)Val, 0);
Chris Lattner8116d1b2007-01-25 22:38:29 +0000870
871 // Register this decl in the current scope stack.
872 New->setNext(Id->getFETokenInfo<Decl>());
873 Id->setFETokenInfo(New);
874 S->AddDecl(New);
875 return New;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000876}
877
878void Sema::ParseEnumBody(SourceLocation EnumLoc, DeclTy *EnumDeclX,
879 DeclTy **Elements, unsigned NumElements) {
880 EnumDecl *Enum = cast<EnumDecl>(static_cast<Decl*>(EnumDeclX));
881 assert(!Enum->isDefinition() && "Enum redefinitions can't reach here");
882
883 // Verify that all the values are okay.
884 SmallVector<EnumConstantDecl*, 32> Values;
885 for (unsigned i = 0; i != NumElements; ++i) {
886 EnumConstantDecl *ECD =
887 cast_or_null<EnumConstantDecl>(static_cast<Decl*>(Elements[i]));
888 if (!ECD) continue; // Already issued a diagnostic.
889
890 Values.push_back(ECD);
891 }
892
893 Enum->defineElements(&Values[0], Values.size());
894}
Chris Lattner1300fb92007-01-23 23:42:53 +0000895
Steve Naroff26c8ea52007-03-21 21:08:52 +0000896void Sema::AddTopLevelDecl(Decl *current, Decl *last) {
897 if (!current) return;
898
899 // If this is a top-level decl that is chained to some other (e.g. int A,B,C;)
900 // remember this in the LastInGroupList list.
Chris Lattner776fac82007-06-09 00:53:06 +0000901 if (last)
Steve Naroff26c8ea52007-03-21 21:08:52 +0000902 LastInGroupList.push_back((Decl*)last);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000903}
Steve Naroff0f2fe172007-06-01 17:11:19 +0000904
905/// ParseAttribute GCC __attribute__
Steve Naroff45552922007-06-01 21:56:17 +0000906Sema::AttrTy *Sema::ParseAttribute(
907 IdentifierInfo *AttrName, SourceLocation AttrNameLoc, AttrTy *PrevAttr,
Steve Naroff0f2fe172007-06-01 17:11:19 +0000908 IdentifierInfo *ParmName, SourceLocation ParmNameLoc,
909 ExprTy **Args, unsigned NumArgs,
910 SourceLocation LParenLoc, SourceLocation RParenLoc) {
Steve Naroff45552922007-06-01 21:56:17 +0000911 Attr *attrib = new Attr(AttrNameLoc, AttrName, ParmName, (Expr **)Args,
912 NumArgs);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000913 if (PrevAttr)
914 // reuse Decl's "Next" pointer for chaining the attribute list
Steve Naroff45552922007-06-01 21:56:17 +0000915 attrib->setNext(static_cast<Attr *>(PrevAttr));
Steve Naroff0f2fe172007-06-01 17:11:19 +0000916 return attrib;
917}