blob: 93e97be0b1b801c406a1ada57b6a8df849f0a131 [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"
Chris Lattner591a6752006-11-19 23:16:18 +000020#include "clang/Parse/DeclSpec.h"
Chris Lattnere168f762006-11-10 05:29:30 +000021#include "clang/Parse/Scope.h"
22#include "clang/Lex/IdentifierTable.h"
Chris Lattnerac18be92006-11-20 06:49:47 +000023#include "clang/Basic/LangOptions.h"
Chris Lattner9561a0b2007-01-28 08:20:04 +000024#include "clang/Basic/TargetInfo.h"
Chris Lattner38047f92007-01-27 06:24:01 +000025#include "llvm/ADT/SmallSet.h"
Steve Naroffd50c88e2007-04-05 21:15:20 +000026
Chris Lattner697e5d62006-11-09 06:32:27 +000027using namespace llvm;
28using namespace clang;
29
Steve Naroff8eeeb132007-05-08 21:09:37 +000030// C99: 6.7.5p3: Used by ParseDeclarator/ParseField to make sure we have
31// a constant expression of type int with a value greater than zero.
32bool Sema::isConstantArrayType(ArrayType *ary, SourceLocation loc) {
33 if (Expr *size = ary->getSize()) {
Steve Naroff72cada02007-05-18 00:18:52 +000034 SourceLocation expLoc;
35 if (!size->isConstantExpr(expLoc)) {
36 Diag(expLoc, diag::err_typecheck_illegal_vla);
Steve Naroff8eeeb132007-05-08 21:09:37 +000037 return false;
38 }
39 if (!size->getType()->isIntegerType()) {
Steve Naroff72cada02007-05-18 00:18:52 +000040 Diag(size->getLocStart(), diag::err_array_size_non_int,
41 size->getType().getAsString());
Steve Naroff8eeeb132007-05-08 21:09:37 +000042 return false;
43 }
44 // We have a constant expression with an integer type, now make sure
45 // value greater than zero (C99 6.7.5.2p1).
46 // FIXME: evaluate constant expression.
47 }
48 return true;
49}
Chris Lattnere168f762006-11-10 05:29:30 +000050
Chris Lattner2ebe4bb2006-11-20 01:29:42 +000051Sema::DeclTy *Sema::isTypeName(const IdentifierInfo &II, Scope *S) const {
Chris Lattner32d920b2007-01-26 02:01:53 +000052 return dyn_cast_or_null<TypedefDecl>(II.getFETokenInfo<Decl>());
Chris Lattnere168f762006-11-10 05:29:30 +000053}
54
Chris Lattner302b4be2006-11-19 02:31:38 +000055void Sema::PopScope(SourceLocation Loc, Scope *S) {
56 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
57 I != E; ++I) {
Chris Lattner99d31772007-01-21 22:37:37 +000058 Decl *D = static_cast<Decl*>(*I);
Chris Lattner302b4be2006-11-19 02:31:38 +000059 assert(D && "This decl didn't get pushed??");
Chris Lattnerff65b6b2007-01-23 01:33:16 +000060 IdentifierInfo *II = D->getIdentifier();
61 if (!II) continue;
Chris Lattner302b4be2006-11-19 02:31:38 +000062
Chris Lattnerff65b6b2007-01-23 01:33:16 +000063 // Unlink this decl from the identifier. Because the scope contains decls
64 // in an unordered collection, and because we have multiple identifier
65 // namespaces (e.g. tag, normal, label),the decl may not be the first entry.
66 if (II->getFETokenInfo<Decl>() == D) {
67 // Normal case, no multiple decls in different namespaces.
68 II->setFETokenInfo(D->getNext());
69 } else {
70 // Scan ahead. There are only three namespaces in C, so this loop can
71 // never execute more than 3 times.
72 Decl *SomeDecl = II->getFETokenInfo<Decl>();
73 while (SomeDecl->getNext() != D) {
74 SomeDecl = SomeDecl->getNext();
75 assert(SomeDecl && "Didn't find this decl on its identifier's chain!");
76 }
77 SomeDecl->setNext(D->getNext());
78 }
Chris Lattner302b4be2006-11-19 02:31:38 +000079
Chris Lattner740b2f32006-11-21 01:32:20 +000080 // This will have to be revisited for C++: there we want to nest stuff in
81 // namespace decls etc. Even for C, we might want a top-level translation
82 // unit decl or something.
83 if (!CurFunctionDecl)
84 continue;
85
86 // Chain this decl to the containing function, it now owns the memory for
87 // the decl.
88 D->setNext(CurFunctionDecl->getDeclChain());
89 CurFunctionDecl->setDeclChain(D);
Chris Lattner302b4be2006-11-19 02:31:38 +000090 }
91}
92
Chris Lattner18b19622007-01-22 07:39:13 +000093/// LookupScopedDecl - Look up the inner-most declaration in the specified
94/// namespace.
Chris Lattner9561a0b2007-01-28 08:20:04 +000095Decl *Sema::LookupScopedDecl(IdentifierInfo *II, unsigned NSI,
96 SourceLocation IdLoc, Scope *S) {
Chris Lattner18b19622007-01-22 07:39:13 +000097 if (II == 0) return 0;
Chris Lattnerb6738ec2007-01-28 00:38:24 +000098 Decl::IdentifierNamespace NS = (Decl::IdentifierNamespace)NSI;
Chris Lattner18b19622007-01-22 07:39:13 +000099
100 // Scan up the scope chain looking for a decl that matches this identifier
101 // that is in the appropriate namespace. This search should not take long, as
102 // shadowing of names is uncommon, and deep shadowing is extremely uncommon.
103 for (Decl *D = II->getFETokenInfo<Decl>(); D; D = D->getNext())
104 if (D->getIdentifierNamespace() == NS)
105 return D;
Chris Lattnerb6738ec2007-01-28 00:38:24 +0000106
Chris Lattner9561a0b2007-01-28 08:20:04 +0000107 // If we didn't find a use of this identifier, and if the identifier
108 // corresponds to a compiler builtin, create the decl object for the builtin
109 // now, injecting it into translation unit scope, and return it.
110 if (NS == Decl::IDNS_Ordinary) {
111 // If this is a builtin on some other target, or if this builtin varies
112 // across targets (e.g. in type), emit a diagnostic and mark the translation
113 // unit non-portable for using it.
114 if (II->isNonPortableBuiltin()) {
115 // Only emit this diagnostic once for this builtin.
116 II->setNonPortableBuiltin(false);
117 Context.Target.DiagnoseNonPortability(IdLoc,
118 diag::port_target_builtin_use);
119 }
Chris Lattner9561a0b2007-01-28 08:20:04 +0000120 // If this is a builtin on this (or all) targets, create the decl.
121 if (unsigned BuiltinID = II->getBuiltinID())
122 return LazilyCreateBuiltin(II, BuiltinID, S);
123 }
Chris Lattner18b19622007-01-22 07:39:13 +0000124 return 0;
125}
126
Chris Lattner9561a0b2007-01-28 08:20:04 +0000127/// LazilyCreateBuiltin - The specified Builtin-ID was first used at file scope.
128/// lazily create a decl for it.
129Decl *Sema::LazilyCreateBuiltin(IdentifierInfo *II, unsigned bid, Scope *S) {
130 Builtin::ID BID = (Builtin::ID)bid;
131
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000132 QualType R = Context.BuiltinInfo.GetBuiltinType(BID, Context);
Chris Lattner9561a0b2007-01-28 08:20:04 +0000133 FunctionDecl *New = new FunctionDecl(SourceLocation(), II, R);
134
135 // Find translation-unit scope to insert this function into.
136 while (S->getParent())
137 S = S->getParent();
138 S->AddDecl(New);
139
140 // Add this decl to the end of the identifier info.
141 if (Decl *LastDecl = II->getFETokenInfo<Decl>()) {
142 // Scan until we find the last (outermost) decl in the id chain.
143 while (LastDecl->getNext())
144 LastDecl = LastDecl->getNext();
145 // Insert before (outside) it.
146 LastDecl->setNext(New);
147 } else {
148 II->setFETokenInfo(New);
149 }
150 // Make sure clients iterating over decls see this.
151 LastInGroupList.push_back(New);
152
153 return New;
154}
155
Chris Lattner01564d92007-01-27 19:27:06 +0000156/// MergeTypeDefDecl - We just parsed a typedef 'New' which has the same name
157/// and scope as a previous declaration 'Old'. Figure out how to resolve this
158/// situation, merging decls or emitting diagnostics as appropriate.
159///
Chris Lattnerc511efb2007-01-27 19:32:14 +0000160TypedefDecl *Sema::MergeTypeDefDecl(TypedefDecl *New, Decl *OldD) {
161 // Verify the old decl was also a typedef.
162 TypedefDecl *Old = dyn_cast<TypedefDecl>(OldD);
163 if (!Old) {
164 Diag(New->getLocation(), diag::err_redefinition_different_kind,
165 New->getName());
166 Diag(OldD->getLocation(), diag::err_previous_definition);
167 return New;
168 }
169
Chris Lattner01564d92007-01-27 19:27:06 +0000170 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
171 // TODO: This is totally simplistic. It should handle merging functions
172 // together etc, merging extern int X; int X; ...
173 Diag(New->getLocation(), diag::err_redefinition, New->getName());
174 Diag(Old->getLocation(), diag::err_previous_definition);
175 return New;
176}
177
178/// MergeFunctionDecl - We just parsed a function 'New' which has the same name
179/// and scope as a previous declaration 'Old'. Figure out how to resolve this
180/// situation, merging decls or emitting diagnostics as appropriate.
181///
Chris Lattnerc511efb2007-01-27 19:32:14 +0000182FunctionDecl *Sema::MergeFunctionDecl(FunctionDecl *New, Decl *OldD) {
183 // Verify the old decl was also a function.
184 FunctionDecl *Old = dyn_cast<FunctionDecl>(OldD);
185 if (!Old) {
186 Diag(New->getLocation(), diag::err_redefinition_different_kind,
187 New->getName());
188 Diag(OldD->getLocation(), diag::err_previous_definition);
189 return New;
190 }
191
Chris Lattnerefe4aea2007-01-27 19:35:39 +0000192 // This is not right, but it's a start. If 'Old' is a function prototype with
193 // the same type as 'New', silently allow this. FIXME: We should link up decl
194 // objects here.
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000195 if (Old->getBody() == 0 &&
196 Old->getCanonicalType() == New->getCanonicalType()) {
Chris Lattnerefe4aea2007-01-27 19:35:39 +0000197 return New;
198 }
Chris Lattnerc511efb2007-01-27 19:32:14 +0000199
Chris Lattner01564d92007-01-27 19:27:06 +0000200 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
201 // TODO: This is totally simplistic. It should handle merging functions
202 // together etc, merging extern int X; int X; ...
203 Diag(New->getLocation(), diag::err_redefinition, New->getName());
204 Diag(Old->getLocation(), diag::err_previous_definition);
205 return New;
206}
207
208/// MergeVarDecl - We just parsed a variable 'New' which has the same name
209/// and scope as a previous declaration 'Old'. Figure out how to resolve this
210/// situation, merging decls or emitting diagnostics as appropriate.
211///
Steve Narofffc49d672007-04-01 21:27:45 +0000212/// FIXME: Need to carefully consider tentative definition rules (C99 6.9.2p2).
213/// For example, we incorrectly complain about i1, i4 from C99 6.9.2p4.
214///
Chris Lattnerc511efb2007-01-27 19:32:14 +0000215VarDecl *Sema::MergeVarDecl(VarDecl *New, Decl *OldD) {
216 // Verify the old decl was also a variable.
217 VarDecl *Old = dyn_cast<VarDecl>(OldD);
218 if (!Old) {
219 Diag(New->getLocation(), diag::err_redefinition_different_kind,
220 New->getName());
221 Diag(OldD->getLocation(), diag::err_previous_definition);
222 return New;
223 }
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000224 // Verify the types match.
225 if (Old->getCanonicalType() != New->getCanonicalType()) {
226 Diag(New->getLocation(), diag::err_redefinition, New->getName());
227 Diag(Old->getLocation(), diag::err_previous_definition);
228 return New;
229 }
230 // We've verified the types match, now check if Old is "extern".
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000231 if (Old->getStorageClass() != VarDecl::Extern) {
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000232 Diag(New->getLocation(), diag::err_redefinition, New->getName());
233 Diag(Old->getLocation(), diag::err_previous_definition);
234 }
Chris Lattner01564d92007-01-27 19:27:06 +0000235 return New;
236}
237
Chris Lattnerb6738ec2007-01-28 00:38:24 +0000238/// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with
239/// no declarator (e.g. "struct foo;") is parsed.
240Sema::DeclTy *Sema::ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS) {
241 // TODO: emit error on 'int;' or 'const enum foo;'.
242 // TODO: emit error on 'typedef int;'
243 // if (!DS.isMissingDeclaratorOk()) Diag(...);
244
245 return 0;
246}
247
Chris Lattnere168f762006-11-10 05:29:30 +0000248Action::DeclTy *
249Sema::ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init,
250 DeclTy *LastInGroup) {
251 IdentifierInfo *II = D.getIdentifier();
Chris Lattner302b4be2006-11-19 02:31:38 +0000252
Chris Lattner01564d92007-01-27 19:27:06 +0000253 // See if this is a redefinition of a variable in the same scope.
Chris Lattner9561a0b2007-01-28 08:20:04 +0000254 Decl *PrevDecl = LookupScopedDecl(II, Decl::IDNS_Ordinary,
255 D.getIdentifierLoc(), S);
Chris Lattner01564d92007-01-27 19:27:06 +0000256 if (!S->isDeclScope(PrevDecl))
257 PrevDecl = 0; // If in outer scope, it isn't the same thing.
258
Chris Lattnere168f762006-11-10 05:29:30 +0000259 Decl *New;
Chris Lattner01a7c532007-01-25 23:09:03 +0000260 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
Chris Lattner01564d92007-01-27 19:27:06 +0000261 TypedefDecl *NewTD = ParseTypedefDecl(S, D);
262 if (!NewTD) return 0;
263
264 // Merge the decl with the existing one if appropriate.
265 if (PrevDecl) {
266 NewTD = MergeTypeDefDecl(NewTD, PrevDecl);
267 if (NewTD == 0) return 0;
268 }
269 New = NewTD;
Steve Naroff8eeeb132007-05-08 21:09:37 +0000270 if (S->getParent() == 0) {
271 // C99 6.7.7p2: If a typedef name specifies a variably modified type
272 // then it shall have block scope.
273 if (ArrayType *ary = dyn_cast<ArrayType>(NewTD->getUnderlyingType())) {
274 if (!isConstantArrayType(ary, D.getIdentifierLoc()))
275 return 0;
276 }
277 }
Chris Lattner01a7c532007-01-25 23:09:03 +0000278 } else if (D.isFunctionDeclarator()) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000279 QualType R = GetTypeForDeclarator(D, S);
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000280 if (R.isNull()) return 0; // FIXME: "auto func();" passes through...
Chris Lattner01564d92007-01-27 19:27:06 +0000281
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000282 FunctionDecl::StorageClass SC;
283 switch (D.getDeclSpec().getStorageClassSpec()) {
284 default: assert(0 && "Unknown storage class!");
285 case DeclSpec::SCS_auto:
286 case DeclSpec::SCS_register:
Chris Lattnerc04bd6a2007-05-16 18:09:54 +0000287 Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_func,
288 R.getAsString());
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000289 return 0;
290 case DeclSpec::SCS_unspecified: SC = FunctionDecl::None; break;
291 case DeclSpec::SCS_extern: SC = FunctionDecl::Extern; break;
292 case DeclSpec::SCS_static: SC = FunctionDecl::Static; break;
293 }
294
295 FunctionDecl *NewFD = new FunctionDecl(D.getIdentifierLoc(), II, R, SC);
Chris Lattner01564d92007-01-27 19:27:06 +0000296
297 // Merge the decl with the existing one if appropriate.
298 if (PrevDecl) {
299 NewFD = MergeFunctionDecl(NewFD, PrevDecl);
300 if (NewFD == 0) return 0;
301 }
302 New = NewFD;
Chris Lattner01a7c532007-01-25 23:09:03 +0000303 } else {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000304 QualType R = GetTypeForDeclarator(D, S);
Chris Lattner01a7c532007-01-25 23:09:03 +0000305 if (R.isNull()) return 0;
Chris Lattner01564d92007-01-27 19:27:06 +0000306
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000307 VarDecl *NewVD;
308 VarDecl::StorageClass SC;
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000309 switch (D.getDeclSpec().getStorageClassSpec()) {
310 default: assert(0 && "Unknown storage class!");
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000311 case DeclSpec::SCS_unspecified: SC = VarDecl::None; break;
312 case DeclSpec::SCS_extern: SC = VarDecl::Extern; break;
313 case DeclSpec::SCS_static: SC = VarDecl::Static; break;
314 case DeclSpec::SCS_auto: SC = VarDecl::Auto; break;
315 case DeclSpec::SCS_register: SC = VarDecl::Register; break;
316 }
Steve Narofffc49d672007-04-01 21:27:45 +0000317 if (S->getParent() == 0) {
318 // File scope. C99 6.9.2p2: A declaration of an identifier for and
319 // object that has file scope without an initializer, and without a
320 // storage-class specifier or with the storage-class specifier "static",
321 // constitutes a tentative definition. Note: A tentative definition with
322 // external linkage is valid (C99 6.2.2p5).
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000323 if (!Init && SC == VarDecl::Static) {
Steve Narofffc49d672007-04-01 21:27:45 +0000324 // C99 6.9.2p3: If the declaration of an identifier for an object is
325 // a tentative definition and has internal linkage (C99 6.2.2p3), the
326 // declared type shall not be an incomplete type.
327 if (R->isIncompleteType()) {
Chris Lattnerc04bd6a2007-05-16 18:09:54 +0000328 Diag(D.getIdentifierLoc(), diag::err_typecheck_decl_incomplete_type,
329 R.getAsString());
Steve Narofffc49d672007-04-01 21:27:45 +0000330 return 0;
331 }
Steve Naroffca8f7122007-04-01 01:41:35 +0000332 }
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000333 // FIXME: Find C99 spec reference
334 if (SC == VarDecl::Auto || SC == VarDecl::Register) {
Chris Lattnerc04bd6a2007-05-16 18:09:54 +0000335 Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_fscope,
336 R.getAsString());
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000337 return 0;
338 }
Steve Naroff8eeeb132007-05-08 21:09:37 +0000339 // C99 6.7.5.2p2: If an identifier is declared to be an object with
340 // static storage duration, it shall not have a variable length array.
341 if (ArrayType *ary = dyn_cast<ArrayType>(R)) {
342 if (!isConstantArrayType(ary, D.getIdentifierLoc()))
343 return 0;
344 }
Steve Naroffef2ab6a2007-04-02 20:50:54 +0000345 NewVD = new FileVarDecl(D.getIdentifierLoc(), II, R, SC);
Steve Narofffc49d672007-04-01 21:27:45 +0000346 } else {
347 // Block scope. C99 6.7p7: If an identifier for an object is declared with
348 // no linkage (C99 6.2.2p6), the type for the object shall be complete...
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000349 if (SC != VarDecl::Extern) {
Steve Narofffc49d672007-04-01 21:27:45 +0000350 if (R->isIncompleteType()) {
Chris Lattnerc04bd6a2007-05-16 18:09:54 +0000351 Diag(D.getIdentifierLoc(), diag::err_typecheck_decl_incomplete_type,
352 R.getAsString());
Steve Narofffc49d672007-04-01 21:27:45 +0000353 return 0;
354 }
355 }
Steve Naroff8eeeb132007-05-08 21:09:37 +0000356 if (SC == VarDecl::Static) {
357 // C99 6.7.5.2p2: If an identifier is declared to be an object with
358 // static storage duration, it shall not have a variable length array.
359 if (ArrayType *ary = dyn_cast<ArrayType>(R)) {
360 if (!isConstantArrayType(ary, D.getIdentifierLoc()))
361 return 0;
362 }
363 }
Steve Naroffef2ab6a2007-04-02 20:50:54 +0000364 NewVD = new BlockVarDecl(D.getIdentifierLoc(), II, R, SC);
365 }
Chris Lattner01564d92007-01-27 19:27:06 +0000366 // Merge the decl with the existing one if appropriate.
367 if (PrevDecl) {
368 NewVD = MergeVarDecl(NewVD, PrevDecl);
369 if (NewVD == 0) return 0;
370 }
371 New = NewVD;
Chris Lattner01a7c532007-01-25 23:09:03 +0000372 }
Chris Lattner302b4be2006-11-19 02:31:38 +0000373
374
Chris Lattnere168f762006-11-10 05:29:30 +0000375 // If this has an identifier, add it to the scope stack.
376 if (II) {
Chris Lattner18b19622007-01-22 07:39:13 +0000377 New->setNext(II->getFETokenInfo<Decl>());
Chris Lattnere168f762006-11-10 05:29:30 +0000378 II->setFETokenInfo(New);
Chris Lattner99d31772007-01-21 22:37:37 +0000379 S->AddDecl(New);
Chris Lattnere168f762006-11-10 05:29:30 +0000380 }
381
Steve Naroff26c8ea52007-03-21 21:08:52 +0000382 if (S->getParent() == 0)
383 AddTopLevelDecl(New, (Decl *)LastInGroup);
Chris Lattnere168f762006-11-10 05:29:30 +0000384
385 return New;
386}
387
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000388VarDecl *
389Sema::ParseParamDeclarator(DeclaratorChunk &FTI, unsigned ArgNo,
390 Scope *FnScope) {
391 const DeclaratorChunk::ParamInfo &PI = FTI.Fun.ArgInfo[ArgNo];
Chris Lattner200bdc32006-11-19 02:43:37 +0000392
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000393 IdentifierInfo *II = PI.Ident;
Chris Lattnerc284e9b2007-01-23 05:14:32 +0000394 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
395 // Can this happen for params? We already checked that they don't conflict
396 // among each other. Here they can only shadow globals, which is ok.
Chris Lattner9561a0b2007-01-28 08:20:04 +0000397 if (Decl *PrevDecl = LookupScopedDecl(II, Decl::IDNS_Ordinary,
398 PI.IdentLoc, FnScope)) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000399
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000400 }
401
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000402 // FIXME: Handle storage class (auto, register). No declarator?
Steve Naroffef2ab6a2007-04-02 20:50:54 +0000403 VarDecl *New = new ParmVarDecl(PI.IdentLoc, II,
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000404 QualType::getFromOpaquePtr(PI.TypeInfo),
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000405 VarDecl::None);
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000406
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000407 // If this has an identifier, add it to the scope stack.
408 if (II) {
Chris Lattner18b19622007-01-22 07:39:13 +0000409 New->setNext(II->getFETokenInfo<Decl>());
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000410 II->setFETokenInfo(New);
Chris Lattner99d31772007-01-21 22:37:37 +0000411 FnScope->AddDecl(New);
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000412 }
Chris Lattner229ce602006-11-21 01:21:07 +0000413
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000414 return New;
415}
416
417
418Sema::DeclTy *Sema::ParseStartOfFunctionDef(Scope *FnBodyScope, Declarator &D) {
Chris Lattner229ce602006-11-21 01:21:07 +0000419 assert(CurFunctionDecl == 0 && "Function parsing confused");
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000420 assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function &&
421 "Not a function declarator!");
422 DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
423
424 // Verify 6.9.1p6: 'every identifier in the identifier list shall be declared'
425 // for a K&R function.
426 if (!FTI.hasPrototype) {
427 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
428 if (FTI.ArgInfo[i].TypeInfo == 0) {
429 Diag(FTI.ArgInfo[i].IdentLoc, diag::err_param_not_declared,
430 FTI.ArgInfo[i].Ident->getName());
431 // Implicitly declare the argument as type 'int' for lack of a better
432 // type.
433 FTI.ArgInfo[i].TypeInfo = Context.IntTy.getAsOpaquePtr();
434 }
435 }
436
437 // Since this is a function definition, act as though we have information
438 // about the arguments.
439 FTI.hasPrototype = true;
Chris Lattner2114d5e2006-12-04 07:40:24 +0000440 } else {
441 // FIXME: Diagnose arguments without names in C.
442
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000443 }
444
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000445 Scope *GlobalScope = FnBodyScope->getParent();
446
447 FunctionDecl *FD =
448 static_cast<FunctionDecl*>(ParseDeclarator(GlobalScope, D, 0, 0));
Chris Lattner229ce602006-11-21 01:21:07 +0000449 CurFunctionDecl = FD;
Chris Lattner2114d5e2006-12-04 07:40:24 +0000450
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000451 // Create Decl objects for each parameter, adding them to the FunctionDecl.
452 SmallVector<VarDecl*, 16> Params;
Chris Lattnerf61c8a82007-01-21 19:04:43 +0000453
454 // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs function that takes
455 // no arguments, not a function that takes a single void argument.
456 if (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 &&
457 FTI.ArgInfo[0].TypeInfo == Context.VoidTy.getAsOpaquePtr()) {
Chris Lattner99d31772007-01-21 22:37:37 +0000458 // empty arg list, don't push any params.
Chris Lattnerf61c8a82007-01-21 19:04:43 +0000459 } else {
460 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i)
461 Params.push_back(ParseParamDeclarator(D.getTypeObject(0), i,FnBodyScope));
462 }
Chris Lattner2114d5e2006-12-04 07:40:24 +0000463
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000464 FD->setParams(&Params[0], Params.size());
Chris Lattner2114d5e2006-12-04 07:40:24 +0000465
Chris Lattnere168f762006-11-10 05:29:30 +0000466 return FD;
467}
468
Chris Lattner229ce602006-11-21 01:21:07 +0000469Sema::DeclTy *Sema::ParseFunctionDefBody(DeclTy *D, StmtTy *Body) {
470 FunctionDecl *FD = static_cast<FunctionDecl*>(D);
471 FD->setBody((Stmt*)Body);
472
473 assert(FD == CurFunctionDecl && "Function parsing confused");
474 CurFunctionDecl = 0;
Chris Lattnere2473062007-05-28 06:28:18 +0000475
476 // Verify and clean out per-function state.
477
478 // Check goto/label use.
479 for (DenseMap<IdentifierInfo*, LabelStmt*>::iterator I = LabelMap.begin(),
480 E = LabelMap.end(); I != E; ++I) {
481 // Verify that we have no forward references left. If so, there was a goto
482 // or address of a label taken, but no definition of it. Label fwd
483 // definitions are indicated with a null substmt.
484 if (I->second->getSubStmt() == 0) {
485 LabelStmt *L = I->second;
486 // Emit error.
Chris Lattnereefa10e2007-05-28 06:56:27 +0000487 Diag(L->getIdentLoc(), diag::err_undeclared_label_use, L->getName());
Chris Lattnere2473062007-05-28 06:28:18 +0000488
489 // At this point, we have gotos that use the bogus label. Stitch it into
490 // the function body so that they aren't leaked and that the AST is well
491 // formed.
492 L->setSubStmt(new NullStmt(L->getIdentLoc()));
493 cast<CompoundStmt>((Stmt*)Body)->push_back(L);
494 }
495 }
496 LabelMap.clear();
497
Chris Lattner229ce602006-11-21 01:21:07 +0000498 return FD;
499}
500
501
Chris Lattnerac18be92006-11-20 06:49:47 +0000502/// ImplicitlyDefineFunction - An undeclared identifier was used in a function
503/// call, forming a call to an implicitly defined function (per C99 6.5.1p2).
504Decl *Sema::ImplicitlyDefineFunction(SourceLocation Loc, IdentifierInfo &II,
505 Scope *S) {
506 if (getLangOptions().C99) // Extension in C99.
507 Diag(Loc, diag::ext_implicit_function_decl, II.getName());
508 else // Legal in C90, but warn about it.
509 Diag(Loc, diag::warn_implicit_function_decl, II.getName());
510
511 // FIXME: handle stuff like:
512 // void foo() { extern float X(); }
513 // void bar() { X(); } <-- implicit decl for X in another scope.
514
515 // Set a Declarator for the implicit definition: int foo();
Chris Lattner353f5742006-11-28 04:50:12 +0000516 const char *Dummy;
Chris Lattnerac18be92006-11-20 06:49:47 +0000517 DeclSpec DS;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000518 bool Error = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, Dummy);
Chris Lattnerb055f2d2007-02-11 08:19:57 +0000519 Error = Error; // Silence warning.
Chris Lattner353f5742006-11-28 04:50:12 +0000520 assert(!Error && "Error setting up implicit decl!");
Chris Lattnerac18be92006-11-20 06:49:47 +0000521 Declarator D(DS, Declarator::BlockContext);
Chris Lattnercbc426d2006-12-02 06:43:02 +0000522 D.AddTypeInfo(DeclaratorChunk::getFunction(false, false, 0, 0, Loc));
Chris Lattnerac18be92006-11-20 06:49:47 +0000523 D.SetIdentifier(&II, Loc);
524
Chris Lattner62d2e662007-01-28 00:21:37 +0000525 // Find translation-unit scope to insert this function into.
526 while (S->getParent())
527 S = S->getParent();
Chris Lattnerac18be92006-11-20 06:49:47 +0000528
Chris Lattner62d2e662007-01-28 00:21:37 +0000529 return static_cast<Decl*>(ParseDeclarator(S, D, 0, 0));
Chris Lattnerac18be92006-11-20 06:49:47 +0000530}
531
Chris Lattner302b4be2006-11-19 02:31:38 +0000532
Chris Lattner01564d92007-01-27 19:27:06 +0000533TypedefDecl *Sema::ParseTypedefDecl(Scope *S, Declarator &D) {
Chris Lattnerda8aa7b2006-11-19 23:12:30 +0000534 assert(D.getIdentifier() && "Wrong callback for declspec withotu declarator");
Chris Lattner302b4be2006-11-19 02:31:38 +0000535
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000536 QualType T = GetTypeForDeclarator(D, S);
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000537 if (T.isNull()) return 0;
538
Chris Lattner18b19622007-01-22 07:39:13 +0000539 // Scope manipulation handled by caller.
540 return new TypedefDecl(D.getIdentifierLoc(), D.getIdentifier(), T);
Chris Lattnere168f762006-11-10 05:29:30 +0000541}
542
Chris Lattner18b19622007-01-22 07:39:13 +0000543
Chris Lattner1300fb92007-01-23 23:42:53 +0000544/// ParseTag - This is invoked when we see 'struct foo' or 'struct {'. In the
545/// former case, Name will be non-null. In the later case, Name will be null.
546/// TagType indicates what kind of tag this is. TK indicates whether this is a
547/// reference/declaration/definition of a tag.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000548Sema::DeclTy *Sema::ParseTag(Scope *S, unsigned TagType, TagKind TK,
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000549 SourceLocation KWLoc, IdentifierInfo *Name,
550 SourceLocation NameLoc) {
Chris Lattner8799cf22007-01-23 01:57:16 +0000551 // If this is a use of an existing tag, it must have a name.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000552 assert((Name != 0 || TK == TK_Definition) &&
553 "Nameless record must be a definition!");
Chris Lattner8799cf22007-01-23 01:57:16 +0000554
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000555 Decl::Kind Kind;
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000556 switch (TagType) {
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000557 default: assert(0 && "Unknown tag type!");
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000558 case DeclSpec::TST_struct: Kind = Decl::Struct; break;
559 case DeclSpec::TST_union: Kind = Decl::Union; break;
560//case DeclSpec::TST_class: Kind = Decl::Class; break;
561 case DeclSpec::TST_enum: Kind = Decl::Enum; break;
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000562 }
Chris Lattner7e783a12007-01-23 02:05:42 +0000563
Chris Lattner18b19622007-01-22 07:39:13 +0000564 // If this is a named struct, check to see if there was a previous forward
565 // declaration or definition.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000566 if (TagDecl *PrevDecl =
Chris Lattner9561a0b2007-01-28 08:20:04 +0000567 dyn_cast_or_null<TagDecl>(LookupScopedDecl(Name, Decl::IDNS_Tag,
568 NameLoc, S))) {
Chris Lattner8799cf22007-01-23 01:57:16 +0000569
570 // If this is a use of a previous tag, or if the tag is already declared in
571 // the same scope (so that the definition/declaration completes or
572 // rementions the tag), reuse the decl.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000573 if (TK == TK_Reference || S->isDeclScope(PrevDecl)) {
Chris Lattner7e783a12007-01-23 02:05:42 +0000574 // Make sure that this wasn't declared as an enum and now used as a struct
575 // or something similar.
576 if (PrevDecl->getKind() != Kind) {
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000577 Diag(KWLoc, diag::err_use_with_wrong_tag, Name->getName());
Chris Lattner7e783a12007-01-23 02:05:42 +0000578 Diag(PrevDecl->getLocation(), diag::err_previous_use);
579 }
Chris Lattner7b9ace62007-01-23 20:11:08 +0000580
581 // If this is a use or a forward declaration, we're good.
582 if (TK != TK_Definition)
583 return PrevDecl;
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000584
Chris Lattner7b9ace62007-01-23 20:11:08 +0000585 // Diagnose attempts to redefine a tag.
586 if (PrevDecl->isDefinition()) {
587 Diag(NameLoc, diag::err_redefinition, Name->getName());
588 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
589 // If this is a redefinition, recover by making this struct be
590 // anonymous, which will make any later references get the previous
591 // definition.
592 Name = 0;
593 } else {
594 // Okay, this is definition of a previously declared or referenced tag.
595 // Move the location of the decl to be the definition site.
596 PrevDecl->setLocation(NameLoc);
Chris Lattner7b9ace62007-01-23 20:11:08 +0000597 return PrevDecl;
598 }
Chris Lattner8799cf22007-01-23 01:57:16 +0000599 }
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000600 // If we get here, this is a definition of a new struct type in a nested
601 // scope, e.g. "struct foo; void bar() { struct foo; }", just create a new
602 // type.
Chris Lattner18b19622007-01-22 07:39:13 +0000603 }
604
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000605 // If there is an identifier, use the location of the identifier as the
606 // location of the decl, otherwise use the location of the struct/union
607 // keyword.
608 SourceLocation Loc = NameLoc.isValid() ? NameLoc : KWLoc;
609
Chris Lattner18b19622007-01-22 07:39:13 +0000610 // Otherwise, if this is the first time we've seen this tag, create the decl.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000611 TagDecl *New;
Chris Lattner720a0542007-01-25 00:44:24 +0000612 switch (Kind) {
613 default: assert(0 && "Unknown tag kind!");
Chris Lattner5f521502007-01-25 06:27:24 +0000614 case Decl::Enum:
615 New = new EnumDecl(Loc, Name);
616 // If this is an undefined enum, warn.
Chris Lattnerc1915e22007-01-25 07:29:02 +0000617 if (TK != TK_Definition) Diag(Loc, diag::ext_forward_ref_enum);
Chris Lattner5f521502007-01-25 06:27:24 +0000618 break;
Chris Lattner720a0542007-01-25 00:44:24 +0000619 case Decl::Union:
620 case Decl::Struct:
621 case Decl::Class:
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000622 New = new RecordDecl(Kind, Loc, Name);
Chris Lattner720a0542007-01-25 00:44:24 +0000623 break;
624 }
Chris Lattner18b19622007-01-22 07:39:13 +0000625
626 // If this has an identifier, add it to the scope stack.
627 if (Name) {
628 New->setNext(Name->getFETokenInfo<Decl>());
629 Name->setFETokenInfo(New);
630 S->AddDecl(New);
631 }
632
633 return New;
634}
Chris Lattner1300fb92007-01-23 23:42:53 +0000635
636/// ParseField - Each field of a struct/union/class is passed into this in order
637/// to create a FieldDecl object for it.
638Sema::DeclTy *Sema::ParseField(Scope *S, DeclTy *TagDecl,
639 SourceLocation DeclStart,
640 Declarator &D, ExprTy *BitfieldWidth) {
641 IdentifierInfo *II = D.getIdentifier();
642 Expr *BitWidth = (Expr*)BitfieldWidth;
643
644 SourceLocation Loc = DeclStart;
645 if (II) Loc = D.getIdentifierLoc();
646
Chris Lattner62d2e662007-01-28 00:21:37 +0000647 // FIXME: Unnamed fields can be handled in various different ways, for
648 // example, unnamed unions inject all members into the struct namespace!
649
650
Chris Lattner1300fb92007-01-23 23:42:53 +0000651 if (BitWidth) {
652 // TODO: Validate.
Steve Narofff84d11f2007-05-23 21:48:04 +0000653 //printf("WARNING: BITFIELDS IGNORED!\n");
Chris Lattner1300fb92007-01-23 23:42:53 +0000654
655 // 6.7.2.1p3
656 // 6.7.2.1p4
657
658 } else {
659 // Not a bitfield.
660
661 // validate II.
662
663 }
664
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000665 QualType T = GetTypeForDeclarator(D, S);
Chris Lattner01a7c532007-01-25 23:09:03 +0000666 if (T.isNull()) return 0;
Steve Naroff8eeeb132007-05-08 21:09:37 +0000667
668 // C99 6.7.2.1p8: A member of a structure or union may have any type other
669 // than a variably modified type.
670 if (ArrayType *ary = dyn_cast<ArrayType>(T)) {
671 if (!isConstantArrayType(ary, Loc))
672 return 0;
673 }
Chris Lattner01a7c532007-01-25 23:09:03 +0000674 return new FieldDecl(Loc, II, T);
Chris Lattner1300fb92007-01-23 23:42:53 +0000675}
676
677void Sema::ParseRecordBody(SourceLocation RecLoc, DeclTy *RecDecl,
678 DeclTy **Fields, unsigned NumFields) {
Chris Lattnerc1915e22007-01-25 07:29:02 +0000679 RecordDecl *Record = cast<RecordDecl>(static_cast<Decl*>(RecDecl));
Chris Lattner1300fb92007-01-23 23:42:53 +0000680 if (Record->isDefinition()) {
681 // Diagnose code like:
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000682 // struct S { struct S {} X; };
Chris Lattner1300fb92007-01-23 23:42:53 +0000683 // We discover this when we complete the outer S. Reject and ignore the
684 // outer S.
685 Diag(Record->getLocation(), diag::err_nested_redefinition,
686 Record->getKindName());
687 Diag(RecLoc, diag::err_previous_definition);
688 return;
689 }
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000690
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000691 // Verify that all the fields are okay.
Chris Lattner82625602007-01-24 02:26:21 +0000692 unsigned NumNamedMembers = 0;
Steve Naroffcc321422007-03-26 23:09:51 +0000693 SmallVector<FieldDecl*, 32> RecFields;
Chris Lattnere5a66562007-01-25 22:48:42 +0000694 SmallSet<const IdentifierInfo*, 32> FieldIDs;
695
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000696 for (unsigned i = 0; i != NumFields; ++i) {
697 FieldDecl *FD = cast_or_null<FieldDecl>(static_cast<Decl*>(Fields[i]));
698 if (!FD) continue; // Already issued a diagnostic.
Chris Lattner720a0542007-01-25 00:44:24 +0000699
700 // Get the type for the field.
Steve Naroffd50c88e2007-04-05 21:15:20 +0000701 Type *FDTy = FD->getType().getCanonicalType().getTypePtr();
Chris Lattner720a0542007-01-25 00:44:24 +0000702
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000703 // C99 6.7.2.1p2 - A field may not be a function type.
Chris Lattner720a0542007-01-25 00:44:24 +0000704 if (isa<FunctionType>(FDTy)) {
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000705 Diag(FD->getLocation(), diag::err_field_declared_as_function,
706 FD->getName());
Chris Lattner82625602007-01-24 02:26:21 +0000707 delete FD;
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000708 continue;
709 }
710
Chris Lattner82625602007-01-24 02:26:21 +0000711 // C99 6.7.2.1p2 - A field may not be an incomplete type except...
Chris Lattner720a0542007-01-25 00:44:24 +0000712 if (FDTy->isIncompleteType()) {
Chris Lattner82625602007-01-24 02:26:21 +0000713 if (i != NumFields-1 || // ... that the last member ...
714 Record->getKind() != Decl::Struct || // ... of a structure ...
Chris Lattner720a0542007-01-25 00:44:24 +0000715 !isa<ArrayType>(FDTy)) { //... may have incomplete array type.
Chris Lattner82625602007-01-24 02:26:21 +0000716 Diag(FD->getLocation(), diag::err_field_incomplete, FD->getName());
717 delete FD;
718 continue;
719 }
Chris Lattner720a0542007-01-25 00:44:24 +0000720 if (NumNamedMembers < 1) { //... must have more than named member ...
Chris Lattner82625602007-01-24 02:26:21 +0000721 Diag(FD->getLocation(), diag::err_flexible_array_empty_struct,
722 FD->getName());
723 delete FD;
724 continue;
725 }
Chris Lattner720a0542007-01-25 00:44:24 +0000726
727 // Okay, we have a legal flexible array member at the end of the struct.
Chris Lattner41943152007-01-25 04:52:46 +0000728 Record->setHasFlexibleArrayMember(true);
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000729 }
Chris Lattner720a0542007-01-25 00:44:24 +0000730
731
732 /// C99 6.7.2.1p2 - a struct ending in a flexible array member cannot be the
733 /// field of another structure or the element of an array.
734 if (RecordType *FDTTy = dyn_cast<RecordType>(FDTy)) {
735 if (FDTTy->getDecl()->hasFlexibleArrayMember()) {
736 // If this is a member of a union, then entire union becomes "flexible".
737 if (Record->getKind() == Decl::Union) {
Chris Lattner41943152007-01-25 04:52:46 +0000738 Record->setHasFlexibleArrayMember(true);
Chris Lattner720a0542007-01-25 00:44:24 +0000739 } else {
740 // If this is a struct/class and this is not the last element, reject
741 // it. Note that GCC supports variable sized arrays in the middle of
742 // structures.
743 if (i != NumFields-1) {
744 Diag(FD->getLocation(), diag::err_variable_sized_type_in_struct,
745 FD->getName());
746 delete FD;
747 continue;
748 }
749
750 // We support flexible arrays at the end of structs in other structs
751 // as an extension.
752 Diag(FD->getLocation(), diag::ext_flexible_array_in_struct,
753 FD->getName());
Chris Lattner41943152007-01-25 04:52:46 +0000754 Record->setHasFlexibleArrayMember(true);
Chris Lattner720a0542007-01-25 00:44:24 +0000755 }
756 }
757 }
758
Chris Lattner82625602007-01-24 02:26:21 +0000759 // Keep track of the number of named members.
Chris Lattnere5a66562007-01-25 22:48:42 +0000760 if (IdentifierInfo *II = FD->getIdentifier()) {
761 // Detect duplicate member names.
Chris Lattnerbaf33662007-01-27 02:14:08 +0000762 if (!FieldIDs.insert(II)) {
Chris Lattnere5a66562007-01-25 22:48:42 +0000763 Diag(FD->getLocation(), diag::err_duplicate_member, II->getName());
764 // Find the previous decl.
765 SourceLocation PrevLoc;
766 for (unsigned i = 0, e = RecFields.size(); ; ++i) {
767 assert(i != e && "Didn't find previous def!");
768 if (RecFields[i]->getIdentifier() == II) {
769 PrevLoc = RecFields[i]->getLocation();
770 break;
771 }
772 }
773 Diag(PrevLoc, diag::err_previous_definition);
774 delete FD;
775 continue;
776 }
Chris Lattner82625602007-01-24 02:26:21 +0000777 ++NumNamedMembers;
Chris Lattnere5a66562007-01-25 22:48:42 +0000778 }
Chris Lattner41943152007-01-25 04:52:46 +0000779
780 // Remember good fields.
781 RecFields.push_back(FD);
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000782 }
Chris Lattner82625602007-01-24 02:26:21 +0000783
784
785 // Okay, we successfully defined 'Record'.
Chris Lattner41943152007-01-25 04:52:46 +0000786 Record->defineBody(&RecFields[0], RecFields.size());
Chris Lattner1300fb92007-01-23 23:42:53 +0000787}
788
Chris Lattnerc1915e22007-01-25 07:29:02 +0000789Sema::DeclTy *Sema::ParseEnumConstant(Scope *S, DeclTy *EnumDeclX,
790 SourceLocation IdLoc, IdentifierInfo *Id,
791 SourceLocation EqualLoc, ExprTy *Val) {
792 EnumDecl *TheEnumDecl = cast<EnumDecl>(static_cast<Decl*>(EnumDeclX));
Chris Lattner8116d1b2007-01-25 22:38:29 +0000793
794 // Verify that there isn't already something declared with this name in this
795 // scope.
Chris Lattner9561a0b2007-01-28 08:20:04 +0000796 if (Decl *PrevDecl = LookupScopedDecl(Id, Decl::IDNS_Ordinary, IdLoc, S)) {
Chris Lattner8116d1b2007-01-25 22:38:29 +0000797 if (S->isDeclScope(PrevDecl)) {
798 if (isa<EnumConstantDecl>(PrevDecl))
799 Diag(IdLoc, diag::err_redefinition_of_enumerator, Id->getName());
800 else
801 Diag(IdLoc, diag::err_redefinition, Id->getName());
802 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
803 return 0;
804 }
805 }
Steve Naroff72cada02007-05-18 00:18:52 +0000806 SourceLocation expLoc;
Steve Naroff63969212007-05-07 21:22:42 +0000807 // C99 6.7.2.2p2: Make sure we have an integer constant expression.
Steve Naroff72cada02007-05-18 00:18:52 +0000808 if (Val && !((Expr *)Val)->isIntegerConstantExpr(expLoc)) {
809 Diag(expLoc, diag::err_enum_value_not_integer_constant_expr, Id->getName());
Steve Naroff63969212007-05-07 21:22:42 +0000810 return 0;
811 }
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000812 QualType Ty = Context.getTagDeclType(TheEnumDecl);
Steve Naroff63969212007-05-07 21:22:42 +0000813 EnumConstantDecl *New = new EnumConstantDecl(IdLoc, Id, Ty, (Expr *)Val);
Chris Lattner8116d1b2007-01-25 22:38:29 +0000814
815 // Register this decl in the current scope stack.
816 New->setNext(Id->getFETokenInfo<Decl>());
817 Id->setFETokenInfo(New);
818 S->AddDecl(New);
819 return New;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000820}
821
822void Sema::ParseEnumBody(SourceLocation EnumLoc, DeclTy *EnumDeclX,
823 DeclTy **Elements, unsigned NumElements) {
824 EnumDecl *Enum = cast<EnumDecl>(static_cast<Decl*>(EnumDeclX));
825 assert(!Enum->isDefinition() && "Enum redefinitions can't reach here");
826
827 // Verify that all the values are okay.
828 SmallVector<EnumConstantDecl*, 32> Values;
829 for (unsigned i = 0; i != NumElements; ++i) {
830 EnumConstantDecl *ECD =
831 cast_or_null<EnumConstantDecl>(static_cast<Decl*>(Elements[i]));
832 if (!ECD) continue; // Already issued a diagnostic.
833
834 Values.push_back(ECD);
835 }
836
837 Enum->defineElements(&Values[0], Values.size());
838}
Chris Lattner1300fb92007-01-23 23:42:53 +0000839
Steve Naroff26c8ea52007-03-21 21:08:52 +0000840void Sema::AddTopLevelDecl(Decl *current, Decl *last) {
841 if (!current) return;
842
843 // If this is a top-level decl that is chained to some other (e.g. int A,B,C;)
844 // remember this in the LastInGroupList list.
845 if (last) {
846 LastInGroupList.push_back((Decl*)last);
847 }
848}