blob: a30944ba92c255ddd3cd10acb1a21e94e03fb38c [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.
33bool Sema::isConstantArrayType(ArrayType *ary, SourceLocation loc) {
34 if (Expr *size = ary->getSize()) {
Steve Naroff72cada02007-05-18 00:18:52 +000035 SourceLocation expLoc;
Chris Lattner238cbc52007-06-02 04:48:48 +000036 if (!size->isConstantExpr(&expLoc)) {
Steve Naroff72cada02007-05-18 00:18:52 +000037 Diag(expLoc, diag::err_typecheck_illegal_vla);
Steve Naroff8eeeb132007-05-08 21:09:37 +000038 return false;
39 }
40 if (!size->getType()->isIntegerType()) {
Steve Naroff72cada02007-05-18 00:18:52 +000041 Diag(size->getLocStart(), diag::err_array_size_non_int,
42 size->getType().getAsString());
Steve Naroff8eeeb132007-05-08 21:09:37 +000043 return false;
44 }
45 // We have a constant expression with an integer type, now make sure
46 // value greater than zero (C99 6.7.5.2p1).
47 // FIXME: evaluate constant expression.
48 }
49 return true;
50}
Chris Lattnere168f762006-11-10 05:29:30 +000051
Chris Lattner2ebe4bb2006-11-20 01:29:42 +000052Sema::DeclTy *Sema::isTypeName(const IdentifierInfo &II, Scope *S) const {
Chris Lattner32d920b2007-01-26 02:01:53 +000053 return dyn_cast_or_null<TypedefDecl>(II.getFETokenInfo<Decl>());
Chris Lattnere168f762006-11-10 05:29:30 +000054}
55
Chris Lattner302b4be2006-11-19 02:31:38 +000056void Sema::PopScope(SourceLocation Loc, Scope *S) {
57 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
58 I != E; ++I) {
Chris Lattner99d31772007-01-21 22:37:37 +000059 Decl *D = static_cast<Decl*>(*I);
Chris Lattner302b4be2006-11-19 02:31:38 +000060 assert(D && "This decl didn't get pushed??");
Chris Lattnerff65b6b2007-01-23 01:33:16 +000061 IdentifierInfo *II = D->getIdentifier();
62 if (!II) continue;
Chris Lattner302b4be2006-11-19 02:31:38 +000063
Chris Lattnerff65b6b2007-01-23 01:33:16 +000064 // Unlink this decl from the identifier. Because the scope contains decls
65 // in an unordered collection, and because we have multiple identifier
66 // namespaces (e.g. tag, normal, label),the decl may not be the first entry.
67 if (II->getFETokenInfo<Decl>() == D) {
68 // Normal case, no multiple decls in different namespaces.
69 II->setFETokenInfo(D->getNext());
70 } else {
71 // Scan ahead. There are only three namespaces in C, so this loop can
72 // never execute more than 3 times.
73 Decl *SomeDecl = II->getFETokenInfo<Decl>();
74 while (SomeDecl->getNext() != D) {
75 SomeDecl = SomeDecl->getNext();
76 assert(SomeDecl && "Didn't find this decl on its identifier's chain!");
77 }
78 SomeDecl->setNext(D->getNext());
79 }
Chris Lattner302b4be2006-11-19 02:31:38 +000080
Chris Lattner740b2f32006-11-21 01:32:20 +000081 // This will have to be revisited for C++: there we want to nest stuff in
82 // namespace decls etc. Even for C, we might want a top-level translation
83 // unit decl or something.
84 if (!CurFunctionDecl)
85 continue;
86
87 // Chain this decl to the containing function, it now owns the memory for
88 // the decl.
89 D->setNext(CurFunctionDecl->getDeclChain());
90 CurFunctionDecl->setDeclChain(D);
Chris Lattner302b4be2006-11-19 02:31:38 +000091 }
92}
93
Chris Lattner18b19622007-01-22 07:39:13 +000094/// LookupScopedDecl - Look up the inner-most declaration in the specified
95/// namespace.
Chris Lattner9561a0b2007-01-28 08:20:04 +000096Decl *Sema::LookupScopedDecl(IdentifierInfo *II, unsigned NSI,
97 SourceLocation IdLoc, Scope *S) {
Chris Lattner18b19622007-01-22 07:39:13 +000098 if (II == 0) return 0;
Chris Lattnerb6738ec2007-01-28 00:38:24 +000099 Decl::IdentifierNamespace NS = (Decl::IdentifierNamespace)NSI;
Chris Lattner18b19622007-01-22 07:39:13 +0000100
101 // Scan up the scope chain looking for a decl that matches this identifier
102 // that is in the appropriate namespace. This search should not take long, as
103 // shadowing of names is uncommon, and deep shadowing is extremely uncommon.
104 for (Decl *D = II->getFETokenInfo<Decl>(); D; D = D->getNext())
105 if (D->getIdentifierNamespace() == NS)
106 return D;
Chris Lattnerb6738ec2007-01-28 00:38:24 +0000107
Chris Lattner9561a0b2007-01-28 08:20:04 +0000108 // If we didn't find a use of this identifier, and if the identifier
109 // corresponds to a compiler builtin, create the decl object for the builtin
110 // now, injecting it into translation unit scope, and return it.
111 if (NS == Decl::IDNS_Ordinary) {
112 // If this is a builtin on some other target, or if this builtin varies
113 // across targets (e.g. in type), emit a diagnostic and mark the translation
114 // unit non-portable for using it.
115 if (II->isNonPortableBuiltin()) {
116 // Only emit this diagnostic once for this builtin.
117 II->setNonPortableBuiltin(false);
118 Context.Target.DiagnoseNonPortability(IdLoc,
119 diag::port_target_builtin_use);
120 }
Chris Lattner9561a0b2007-01-28 08:20:04 +0000121 // If this is a builtin on this (or all) targets, create the decl.
122 if (unsigned BuiltinID = II->getBuiltinID())
123 return LazilyCreateBuiltin(II, BuiltinID, S);
124 }
Chris Lattner18b19622007-01-22 07:39:13 +0000125 return 0;
126}
127
Chris Lattner9561a0b2007-01-28 08:20:04 +0000128/// LazilyCreateBuiltin - The specified Builtin-ID was first used at file scope.
129/// lazily create a decl for it.
130Decl *Sema::LazilyCreateBuiltin(IdentifierInfo *II, unsigned bid, Scope *S) {
131 Builtin::ID BID = (Builtin::ID)bid;
132
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000133 QualType R = Context.BuiltinInfo.GetBuiltinType(BID, Context);
Chris Lattner9561a0b2007-01-28 08:20:04 +0000134 FunctionDecl *New = new FunctionDecl(SourceLocation(), II, R);
135
136 // Find translation-unit scope to insert this function into.
137 while (S->getParent())
138 S = S->getParent();
139 S->AddDecl(New);
140
141 // Add this decl to the end of the identifier info.
142 if (Decl *LastDecl = II->getFETokenInfo<Decl>()) {
143 // Scan until we find the last (outermost) decl in the id chain.
144 while (LastDecl->getNext())
145 LastDecl = LastDecl->getNext();
146 // Insert before (outside) it.
147 LastDecl->setNext(New);
148 } else {
149 II->setFETokenInfo(New);
150 }
151 // Make sure clients iterating over decls see this.
152 LastInGroupList.push_back(New);
153
154 return New;
155}
156
Chris Lattner01564d92007-01-27 19:27:06 +0000157/// MergeTypeDefDecl - We just parsed a typedef 'New' which has the same name
158/// and scope as a previous declaration 'Old'. Figure out how to resolve this
159/// situation, merging decls or emitting diagnostics as appropriate.
160///
Chris Lattnerc511efb2007-01-27 19:32:14 +0000161TypedefDecl *Sema::MergeTypeDefDecl(TypedefDecl *New, Decl *OldD) {
162 // Verify the old decl was also a typedef.
163 TypedefDecl *Old = dyn_cast<TypedefDecl>(OldD);
164 if (!Old) {
165 Diag(New->getLocation(), diag::err_redefinition_different_kind,
166 New->getName());
167 Diag(OldD->getLocation(), diag::err_previous_definition);
168 return New;
169 }
170
Chris Lattner01564d92007-01-27 19:27:06 +0000171 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
172 // TODO: This is totally simplistic. It should handle merging functions
173 // together etc, merging extern int X; int X; ...
174 Diag(New->getLocation(), diag::err_redefinition, New->getName());
175 Diag(Old->getLocation(), diag::err_previous_definition);
176 return New;
177}
178
179/// MergeFunctionDecl - We just parsed a function 'New' which has the same name
180/// and scope as a previous declaration 'Old'. Figure out how to resolve this
181/// situation, merging decls or emitting diagnostics as appropriate.
182///
Chris Lattnerc511efb2007-01-27 19:32:14 +0000183FunctionDecl *Sema::MergeFunctionDecl(FunctionDecl *New, Decl *OldD) {
184 // Verify the old decl was also a function.
185 FunctionDecl *Old = dyn_cast<FunctionDecl>(OldD);
186 if (!Old) {
187 Diag(New->getLocation(), diag::err_redefinition_different_kind,
188 New->getName());
189 Diag(OldD->getLocation(), diag::err_previous_definition);
190 return New;
191 }
192
Chris Lattnerefe4aea2007-01-27 19:35:39 +0000193 // This is not right, but it's a start. If 'Old' is a function prototype with
194 // the same type as 'New', silently allow this. FIXME: We should link up decl
195 // objects here.
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000196 if (Old->getBody() == 0 &&
197 Old->getCanonicalType() == New->getCanonicalType()) {
Chris Lattnerefe4aea2007-01-27 19:35:39 +0000198 return New;
199 }
Chris Lattnerc511efb2007-01-27 19:32:14 +0000200
Chris Lattner01564d92007-01-27 19:27:06 +0000201 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
202 // TODO: This is totally simplistic. It should handle merging functions
203 // together etc, merging extern int X; int X; ...
204 Diag(New->getLocation(), diag::err_redefinition, New->getName());
205 Diag(Old->getLocation(), diag::err_previous_definition);
206 return New;
207}
208
209/// MergeVarDecl - We just parsed a variable 'New' which has the same name
210/// and scope as a previous declaration 'Old'. Figure out how to resolve this
211/// situation, merging decls or emitting diagnostics as appropriate.
212///
Steve Narofffc49d672007-04-01 21:27:45 +0000213/// FIXME: Need to carefully consider tentative definition rules (C99 6.9.2p2).
214/// For example, we incorrectly complain about i1, i4 from C99 6.9.2p4.
215///
Chris Lattnerc511efb2007-01-27 19:32:14 +0000216VarDecl *Sema::MergeVarDecl(VarDecl *New, Decl *OldD) {
217 // Verify the old decl was also a variable.
218 VarDecl *Old = dyn_cast<VarDecl>(OldD);
219 if (!Old) {
220 Diag(New->getLocation(), diag::err_redefinition_different_kind,
221 New->getName());
222 Diag(OldD->getLocation(), diag::err_previous_definition);
223 return New;
224 }
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000225 // Verify the types match.
226 if (Old->getCanonicalType() != New->getCanonicalType()) {
227 Diag(New->getLocation(), diag::err_redefinition, New->getName());
228 Diag(Old->getLocation(), diag::err_previous_definition);
229 return New;
230 }
231 // We've verified the types match, now check if Old is "extern".
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000232 if (Old->getStorageClass() != VarDecl::Extern) {
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000233 Diag(New->getLocation(), diag::err_redefinition, New->getName());
234 Diag(Old->getLocation(), diag::err_previous_definition);
235 }
Chris Lattner01564d92007-01-27 19:27:06 +0000236 return New;
237}
238
Chris Lattnerb6738ec2007-01-28 00:38:24 +0000239/// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with
240/// no declarator (e.g. "struct foo;") is parsed.
241Sema::DeclTy *Sema::ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS) {
242 // TODO: emit error on 'int;' or 'const enum foo;'.
243 // TODO: emit error on 'typedef int;'
244 // if (!DS.isMissingDeclaratorOk()) Diag(...);
245
246 return 0;
247}
248
Chris Lattnere168f762006-11-10 05:29:30 +0000249Action::DeclTy *
250Sema::ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init,
251 DeclTy *LastInGroup) {
252 IdentifierInfo *II = D.getIdentifier();
Chris Lattner302b4be2006-11-19 02:31:38 +0000253
Chris Lattner01564d92007-01-27 19:27:06 +0000254 // See if this is a redefinition of a variable in the same scope.
Chris Lattner9561a0b2007-01-28 08:20:04 +0000255 Decl *PrevDecl = LookupScopedDecl(II, Decl::IDNS_Ordinary,
256 D.getIdentifierLoc(), S);
Chris Lattner01564d92007-01-27 19:27:06 +0000257 if (!S->isDeclScope(PrevDecl))
258 PrevDecl = 0; // If in outer scope, it isn't the same thing.
259
Chris Lattnere168f762006-11-10 05:29:30 +0000260 Decl *New;
Chris Lattner01a7c532007-01-25 23:09:03 +0000261 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
Chris Lattner01564d92007-01-27 19:27:06 +0000262 TypedefDecl *NewTD = ParseTypedefDecl(S, D);
263 if (!NewTD) return 0;
264
265 // Merge the decl with the existing one if appropriate.
266 if (PrevDecl) {
267 NewTD = MergeTypeDefDecl(NewTD, PrevDecl);
268 if (NewTD == 0) return 0;
269 }
270 New = NewTD;
Steve Naroff8eeeb132007-05-08 21:09:37 +0000271 if (S->getParent() == 0) {
272 // C99 6.7.7p2: If a typedef name specifies a variably modified type
273 // then it shall have block scope.
274 if (ArrayType *ary = dyn_cast<ArrayType>(NewTD->getUnderlyingType())) {
275 if (!isConstantArrayType(ary, D.getIdentifierLoc()))
276 return 0;
277 }
278 }
Chris Lattner01a7c532007-01-25 23:09:03 +0000279 } else if (D.isFunctionDeclarator()) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000280 QualType R = GetTypeForDeclarator(D, S);
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000281 if (R.isNull()) return 0; // FIXME: "auto func();" passes through...
Chris Lattner01564d92007-01-27 19:27:06 +0000282
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000283 FunctionDecl::StorageClass SC;
284 switch (D.getDeclSpec().getStorageClassSpec()) {
285 default: assert(0 && "Unknown storage class!");
286 case DeclSpec::SCS_auto:
287 case DeclSpec::SCS_register:
Chris Lattnerc04bd6a2007-05-16 18:09:54 +0000288 Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_func,
289 R.getAsString());
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000290 return 0;
291 case DeclSpec::SCS_unspecified: SC = FunctionDecl::None; break;
292 case DeclSpec::SCS_extern: SC = FunctionDecl::Extern; break;
293 case DeclSpec::SCS_static: SC = FunctionDecl::Static; break;
294 }
295
296 FunctionDecl *NewFD = new FunctionDecl(D.getIdentifierLoc(), II, R, SC);
Chris Lattner01564d92007-01-27 19:27:06 +0000297
298 // Merge the decl with the existing one if appropriate.
299 if (PrevDecl) {
300 NewFD = MergeFunctionDecl(NewFD, PrevDecl);
301 if (NewFD == 0) return 0;
302 }
303 New = NewFD;
Chris Lattner01a7c532007-01-25 23:09:03 +0000304 } else {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000305 QualType R = GetTypeForDeclarator(D, S);
Chris Lattner01a7c532007-01-25 23:09:03 +0000306 if (R.isNull()) return 0;
Chris Lattner01564d92007-01-27 19:27:06 +0000307
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000308 VarDecl *NewVD;
309 VarDecl::StorageClass SC;
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000310 switch (D.getDeclSpec().getStorageClassSpec()) {
311 default: assert(0 && "Unknown storage class!");
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000312 case DeclSpec::SCS_unspecified: SC = VarDecl::None; break;
313 case DeclSpec::SCS_extern: SC = VarDecl::Extern; break;
314 case DeclSpec::SCS_static: SC = VarDecl::Static; break;
315 case DeclSpec::SCS_auto: SC = VarDecl::Auto; break;
316 case DeclSpec::SCS_register: SC = VarDecl::Register; break;
317 }
Steve Narofffc49d672007-04-01 21:27:45 +0000318 if (S->getParent() == 0) {
319 // File scope. C99 6.9.2p2: A declaration of an identifier for and
320 // object that has file scope without an initializer, and without a
321 // storage-class specifier or with the storage-class specifier "static",
322 // constitutes a tentative definition. Note: A tentative definition with
323 // external linkage is valid (C99 6.2.2p5).
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000324 if (!Init && SC == VarDecl::Static) {
Steve Narofffc49d672007-04-01 21:27:45 +0000325 // C99 6.9.2p3: If the declaration of an identifier for an object is
326 // a tentative definition and has internal linkage (C99 6.2.2p3), the
327 // declared type shall not be an incomplete type.
328 if (R->isIncompleteType()) {
Chris Lattnerc04bd6a2007-05-16 18:09:54 +0000329 Diag(D.getIdentifierLoc(), diag::err_typecheck_decl_incomplete_type,
330 R.getAsString());
Steve Narofffc49d672007-04-01 21:27:45 +0000331 return 0;
332 }
Steve Naroffca8f7122007-04-01 01:41:35 +0000333 }
Bill Wendlingd6de6572007-06-02 09:40:07 +0000334 // C99 6.9p2: The storage-class specifiers auto and register shall not
335 // appear in the declaration specifiers in an external declaration.
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000336 if (SC == VarDecl::Auto || SC == VarDecl::Register) {
Chris Lattnerc04bd6a2007-05-16 18:09:54 +0000337 Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_fscope,
338 R.getAsString());
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000339 return 0;
340 }
Steve Naroff8eeeb132007-05-08 21:09:37 +0000341 // C99 6.7.5.2p2: If an identifier is declared to be an object with
342 // static storage duration, it shall not have a variable length array.
343 if (ArrayType *ary = dyn_cast<ArrayType>(R)) {
344 if (!isConstantArrayType(ary, D.getIdentifierLoc()))
345 return 0;
346 }
Steve Naroffef2ab6a2007-04-02 20:50:54 +0000347 NewVD = new FileVarDecl(D.getIdentifierLoc(), II, R, SC);
Steve Narofffc49d672007-04-01 21:27:45 +0000348 } else {
349 // Block scope. C99 6.7p7: If an identifier for an object is declared with
350 // no linkage (C99 6.2.2p6), the type for the object shall be complete...
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000351 if (SC != VarDecl::Extern) {
Steve Narofffc49d672007-04-01 21:27:45 +0000352 if (R->isIncompleteType()) {
Chris Lattnerc04bd6a2007-05-16 18:09:54 +0000353 Diag(D.getIdentifierLoc(), diag::err_typecheck_decl_incomplete_type,
354 R.getAsString());
Steve Narofffc49d672007-04-01 21:27:45 +0000355 return 0;
356 }
357 }
Steve Naroff8eeeb132007-05-08 21:09:37 +0000358 if (SC == VarDecl::Static) {
359 // C99 6.7.5.2p2: If an identifier is declared to be an object with
360 // static storage duration, it shall not have a variable length array.
361 if (ArrayType *ary = dyn_cast<ArrayType>(R)) {
362 if (!isConstantArrayType(ary, D.getIdentifierLoc()))
363 return 0;
364 }
365 }
Steve Naroffef2ab6a2007-04-02 20:50:54 +0000366 NewVD = new BlockVarDecl(D.getIdentifierLoc(), II, R, SC);
367 }
Chris Lattner01564d92007-01-27 19:27:06 +0000368 // Merge the decl with the existing one if appropriate.
369 if (PrevDecl) {
370 NewVD = MergeVarDecl(NewVD, PrevDecl);
371 if (NewVD == 0) return 0;
372 }
373 New = NewVD;
Chris Lattner01a7c532007-01-25 23:09:03 +0000374 }
Chris Lattner302b4be2006-11-19 02:31:38 +0000375
376
Chris Lattnere168f762006-11-10 05:29:30 +0000377 // If this has an identifier, add it to the scope stack.
378 if (II) {
Chris Lattner18b19622007-01-22 07:39:13 +0000379 New->setNext(II->getFETokenInfo<Decl>());
Chris Lattnere168f762006-11-10 05:29:30 +0000380 II->setFETokenInfo(New);
Chris Lattner99d31772007-01-21 22:37:37 +0000381 S->AddDecl(New);
Chris Lattnere168f762006-11-10 05:29:30 +0000382 }
383
Steve Naroff26c8ea52007-03-21 21:08:52 +0000384 if (S->getParent() == 0)
385 AddTopLevelDecl(New, (Decl *)LastInGroup);
Chris Lattnere168f762006-11-10 05:29:30 +0000386
387 return New;
388}
389
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000390VarDecl *
391Sema::ParseParamDeclarator(DeclaratorChunk &FTI, unsigned ArgNo,
392 Scope *FnScope) {
393 const DeclaratorChunk::ParamInfo &PI = FTI.Fun.ArgInfo[ArgNo];
Chris Lattner200bdc32006-11-19 02:43:37 +0000394
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000395 IdentifierInfo *II = PI.Ident;
Chris Lattnerc284e9b2007-01-23 05:14:32 +0000396 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
397 // Can this happen for params? We already checked that they don't conflict
398 // among each other. Here they can only shadow globals, which is ok.
Chris Lattner9561a0b2007-01-28 08:20:04 +0000399 if (Decl *PrevDecl = LookupScopedDecl(II, Decl::IDNS_Ordinary,
400 PI.IdentLoc, FnScope)) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000401
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000402 }
403
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000404 // FIXME: Handle storage class (auto, register). No declarator?
Steve Naroffef2ab6a2007-04-02 20:50:54 +0000405 VarDecl *New = new ParmVarDecl(PI.IdentLoc, II,
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000406 QualType::getFromOpaquePtr(PI.TypeInfo),
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000407 VarDecl::None);
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000408
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000409 // If this has an identifier, add it to the scope stack.
410 if (II) {
Chris Lattner18b19622007-01-22 07:39:13 +0000411 New->setNext(II->getFETokenInfo<Decl>());
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000412 II->setFETokenInfo(New);
Chris Lattner99d31772007-01-21 22:37:37 +0000413 FnScope->AddDecl(New);
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000414 }
Chris Lattner229ce602006-11-21 01:21:07 +0000415
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000416 return New;
417}
418
419
420Sema::DeclTy *Sema::ParseStartOfFunctionDef(Scope *FnBodyScope, Declarator &D) {
Chris Lattner229ce602006-11-21 01:21:07 +0000421 assert(CurFunctionDecl == 0 && "Function parsing confused");
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000422 assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function &&
423 "Not a function declarator!");
424 DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
425
426 // Verify 6.9.1p6: 'every identifier in the identifier list shall be declared'
427 // for a K&R function.
428 if (!FTI.hasPrototype) {
429 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
430 if (FTI.ArgInfo[i].TypeInfo == 0) {
431 Diag(FTI.ArgInfo[i].IdentLoc, diag::err_param_not_declared,
432 FTI.ArgInfo[i].Ident->getName());
433 // Implicitly declare the argument as type 'int' for lack of a better
434 // type.
435 FTI.ArgInfo[i].TypeInfo = Context.IntTy.getAsOpaquePtr();
436 }
437 }
438
439 // Since this is a function definition, act as though we have information
440 // about the arguments.
441 FTI.hasPrototype = true;
Chris Lattner2114d5e2006-12-04 07:40:24 +0000442 } else {
443 // FIXME: Diagnose arguments without names in C.
444
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000445 }
446
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000447 Scope *GlobalScope = FnBodyScope->getParent();
448
449 FunctionDecl *FD =
450 static_cast<FunctionDecl*>(ParseDeclarator(GlobalScope, D, 0, 0));
Chris Lattner229ce602006-11-21 01:21:07 +0000451 CurFunctionDecl = FD;
Chris Lattner2114d5e2006-12-04 07:40:24 +0000452
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000453 // Create Decl objects for each parameter, adding them to the FunctionDecl.
454 SmallVector<VarDecl*, 16> Params;
Chris Lattnerf61c8a82007-01-21 19:04:43 +0000455
456 // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs function that takes
457 // no arguments, not a function that takes a single void argument.
458 if (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 &&
459 FTI.ArgInfo[0].TypeInfo == Context.VoidTy.getAsOpaquePtr()) {
Chris Lattner99d31772007-01-21 22:37:37 +0000460 // empty arg list, don't push any params.
Chris Lattnerf61c8a82007-01-21 19:04:43 +0000461 } else {
462 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i)
463 Params.push_back(ParseParamDeclarator(D.getTypeObject(0), i,FnBodyScope));
464 }
Chris Lattner2114d5e2006-12-04 07:40:24 +0000465
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000466 FD->setParams(&Params[0], Params.size());
Chris Lattner2114d5e2006-12-04 07:40:24 +0000467
Chris Lattnere168f762006-11-10 05:29:30 +0000468 return FD;
469}
470
Chris Lattner229ce602006-11-21 01:21:07 +0000471Sema::DeclTy *Sema::ParseFunctionDefBody(DeclTy *D, StmtTy *Body) {
472 FunctionDecl *FD = static_cast<FunctionDecl*>(D);
473 FD->setBody((Stmt*)Body);
474
475 assert(FD == CurFunctionDecl && "Function parsing confused");
476 CurFunctionDecl = 0;
Chris Lattnere2473062007-05-28 06:28:18 +0000477
478 // Verify and clean out per-function state.
479
480 // Check goto/label use.
481 for (DenseMap<IdentifierInfo*, LabelStmt*>::iterator I = LabelMap.begin(),
482 E = LabelMap.end(); I != E; ++I) {
483 // Verify that we have no forward references left. If so, there was a goto
484 // or address of a label taken, but no definition of it. Label fwd
485 // definitions are indicated with a null substmt.
486 if (I->second->getSubStmt() == 0) {
487 LabelStmt *L = I->second;
488 // Emit error.
Chris Lattnereefa10e2007-05-28 06:56:27 +0000489 Diag(L->getIdentLoc(), diag::err_undeclared_label_use, L->getName());
Chris Lattnere2473062007-05-28 06:28:18 +0000490
491 // At this point, we have gotos that use the bogus label. Stitch it into
492 // the function body so that they aren't leaked and that the AST is well
493 // formed.
494 L->setSubStmt(new NullStmt(L->getIdentLoc()));
495 cast<CompoundStmt>((Stmt*)Body)->push_back(L);
496 }
497 }
498 LabelMap.clear();
499
Chris Lattner229ce602006-11-21 01:21:07 +0000500 return FD;
501}
502
503
Chris Lattnerac18be92006-11-20 06:49:47 +0000504/// ImplicitlyDefineFunction - An undeclared identifier was used in a function
505/// call, forming a call to an implicitly defined function (per C99 6.5.1p2).
506Decl *Sema::ImplicitlyDefineFunction(SourceLocation Loc, IdentifierInfo &II,
507 Scope *S) {
508 if (getLangOptions().C99) // Extension in C99.
509 Diag(Loc, diag::ext_implicit_function_decl, II.getName());
510 else // Legal in C90, but warn about it.
511 Diag(Loc, diag::warn_implicit_function_decl, II.getName());
512
513 // FIXME: handle stuff like:
514 // void foo() { extern float X(); }
515 // void bar() { X(); } <-- implicit decl for X in another scope.
516
517 // Set a Declarator for the implicit definition: int foo();
Chris Lattner353f5742006-11-28 04:50:12 +0000518 const char *Dummy;
Chris Lattnerac18be92006-11-20 06:49:47 +0000519 DeclSpec DS;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000520 bool Error = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, Dummy);
Chris Lattnerb055f2d2007-02-11 08:19:57 +0000521 Error = Error; // Silence warning.
Chris Lattner353f5742006-11-28 04:50:12 +0000522 assert(!Error && "Error setting up implicit decl!");
Chris Lattnerac18be92006-11-20 06:49:47 +0000523 Declarator D(DS, Declarator::BlockContext);
Chris Lattnercbc426d2006-12-02 06:43:02 +0000524 D.AddTypeInfo(DeclaratorChunk::getFunction(false, false, 0, 0, Loc));
Chris Lattnerac18be92006-11-20 06:49:47 +0000525 D.SetIdentifier(&II, Loc);
526
Chris Lattner62d2e662007-01-28 00:21:37 +0000527 // Find translation-unit scope to insert this function into.
528 while (S->getParent())
529 S = S->getParent();
Chris Lattnerac18be92006-11-20 06:49:47 +0000530
Chris Lattner62d2e662007-01-28 00:21:37 +0000531 return static_cast<Decl*>(ParseDeclarator(S, D, 0, 0));
Chris Lattnerac18be92006-11-20 06:49:47 +0000532}
533
Chris Lattner302b4be2006-11-19 02:31:38 +0000534
Chris Lattner01564d92007-01-27 19:27:06 +0000535TypedefDecl *Sema::ParseTypedefDecl(Scope *S, Declarator &D) {
Chris Lattnerda8aa7b2006-11-19 23:12:30 +0000536 assert(D.getIdentifier() && "Wrong callback for declspec withotu declarator");
Chris Lattner302b4be2006-11-19 02:31:38 +0000537
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000538 QualType T = GetTypeForDeclarator(D, S);
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000539 if (T.isNull()) return 0;
540
Chris Lattner18b19622007-01-22 07:39:13 +0000541 // Scope manipulation handled by caller.
542 return new TypedefDecl(D.getIdentifierLoc(), D.getIdentifier(), T);
Chris Lattnere168f762006-11-10 05:29:30 +0000543}
544
Chris Lattner18b19622007-01-22 07:39:13 +0000545
Chris Lattner1300fb92007-01-23 23:42:53 +0000546/// ParseTag - This is invoked when we see 'struct foo' or 'struct {'. In the
547/// former case, Name will be non-null. In the later case, Name will be null.
548/// TagType indicates what kind of tag this is. TK indicates whether this is a
549/// reference/declaration/definition of a tag.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000550Sema::DeclTy *Sema::ParseTag(Scope *S, unsigned TagType, TagKind TK,
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000551 SourceLocation KWLoc, IdentifierInfo *Name,
552 SourceLocation NameLoc) {
Chris Lattner8799cf22007-01-23 01:57:16 +0000553 // If this is a use of an existing tag, it must have a name.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000554 assert((Name != 0 || TK == TK_Definition) &&
555 "Nameless record must be a definition!");
Chris Lattner8799cf22007-01-23 01:57:16 +0000556
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000557 Decl::Kind Kind;
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000558 switch (TagType) {
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000559 default: assert(0 && "Unknown tag type!");
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000560 case DeclSpec::TST_struct: Kind = Decl::Struct; break;
561 case DeclSpec::TST_union: Kind = Decl::Union; break;
562//case DeclSpec::TST_class: Kind = Decl::Class; break;
563 case DeclSpec::TST_enum: Kind = Decl::Enum; break;
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000564 }
Chris Lattner7e783a12007-01-23 02:05:42 +0000565
Chris Lattner18b19622007-01-22 07:39:13 +0000566 // If this is a named struct, check to see if there was a previous forward
567 // declaration or definition.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000568 if (TagDecl *PrevDecl =
Chris Lattner9561a0b2007-01-28 08:20:04 +0000569 dyn_cast_or_null<TagDecl>(LookupScopedDecl(Name, Decl::IDNS_Tag,
570 NameLoc, S))) {
Chris Lattner8799cf22007-01-23 01:57:16 +0000571
572 // If this is a use of a previous tag, or if the tag is already declared in
573 // the same scope (so that the definition/declaration completes or
574 // rementions the tag), reuse the decl.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000575 if (TK == TK_Reference || S->isDeclScope(PrevDecl)) {
Chris Lattner7e783a12007-01-23 02:05:42 +0000576 // Make sure that this wasn't declared as an enum and now used as a struct
577 // or something similar.
578 if (PrevDecl->getKind() != Kind) {
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000579 Diag(KWLoc, diag::err_use_with_wrong_tag, Name->getName());
Chris Lattner7e783a12007-01-23 02:05:42 +0000580 Diag(PrevDecl->getLocation(), diag::err_previous_use);
581 }
Chris Lattner7b9ace62007-01-23 20:11:08 +0000582
583 // If this is a use or a forward declaration, we're good.
584 if (TK != TK_Definition)
585 return PrevDecl;
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000586
Chris Lattner7b9ace62007-01-23 20:11:08 +0000587 // Diagnose attempts to redefine a tag.
588 if (PrevDecl->isDefinition()) {
589 Diag(NameLoc, diag::err_redefinition, Name->getName());
590 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
591 // If this is a redefinition, recover by making this struct be
592 // anonymous, which will make any later references get the previous
593 // definition.
594 Name = 0;
595 } else {
596 // Okay, this is definition of a previously declared or referenced tag.
597 // Move the location of the decl to be the definition site.
598 PrevDecl->setLocation(NameLoc);
Chris Lattner7b9ace62007-01-23 20:11:08 +0000599 return PrevDecl;
600 }
Chris Lattner8799cf22007-01-23 01:57:16 +0000601 }
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000602 // If we get here, this is a definition of a new struct type in a nested
603 // scope, e.g. "struct foo; void bar() { struct foo; }", just create a new
604 // type.
Chris Lattner18b19622007-01-22 07:39:13 +0000605 }
606
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000607 // If there is an identifier, use the location of the identifier as the
608 // location of the decl, otherwise use the location of the struct/union
609 // keyword.
610 SourceLocation Loc = NameLoc.isValid() ? NameLoc : KWLoc;
611
Chris Lattner18b19622007-01-22 07:39:13 +0000612 // Otherwise, if this is the first time we've seen this tag, create the decl.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000613 TagDecl *New;
Chris Lattner720a0542007-01-25 00:44:24 +0000614 switch (Kind) {
615 default: assert(0 && "Unknown tag kind!");
Chris Lattner5f521502007-01-25 06:27:24 +0000616 case Decl::Enum:
617 New = new EnumDecl(Loc, Name);
618 // If this is an undefined enum, warn.
Chris Lattnerc1915e22007-01-25 07:29:02 +0000619 if (TK != TK_Definition) Diag(Loc, diag::ext_forward_ref_enum);
Chris Lattner5f521502007-01-25 06:27:24 +0000620 break;
Chris Lattner720a0542007-01-25 00:44:24 +0000621 case Decl::Union:
622 case Decl::Struct:
623 case Decl::Class:
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000624 New = new RecordDecl(Kind, Loc, Name);
Chris Lattner720a0542007-01-25 00:44:24 +0000625 break;
626 }
Chris Lattner18b19622007-01-22 07:39:13 +0000627
628 // If this has an identifier, add it to the scope stack.
629 if (Name) {
630 New->setNext(Name->getFETokenInfo<Decl>());
631 Name->setFETokenInfo(New);
632 S->AddDecl(New);
633 }
634
635 return New;
636}
Chris Lattner1300fb92007-01-23 23:42:53 +0000637
638/// ParseField - Each field of a struct/union/class is passed into this in order
639/// to create a FieldDecl object for it.
640Sema::DeclTy *Sema::ParseField(Scope *S, DeclTy *TagDecl,
641 SourceLocation DeclStart,
642 Declarator &D, ExprTy *BitfieldWidth) {
643 IdentifierInfo *II = D.getIdentifier();
644 Expr *BitWidth = (Expr*)BitfieldWidth;
645
646 SourceLocation Loc = DeclStart;
647 if (II) Loc = D.getIdentifierLoc();
648
Chris Lattner62d2e662007-01-28 00:21:37 +0000649 // FIXME: Unnamed fields can be handled in various different ways, for
650 // example, unnamed unions inject all members into the struct namespace!
651
652
Chris Lattner1300fb92007-01-23 23:42:53 +0000653 if (BitWidth) {
654 // TODO: Validate.
Steve Narofff84d11f2007-05-23 21:48:04 +0000655 //printf("WARNING: BITFIELDS IGNORED!\n");
Chris Lattner1300fb92007-01-23 23:42:53 +0000656
657 // 6.7.2.1p3
658 // 6.7.2.1p4
659
660 } else {
661 // Not a bitfield.
662
663 // validate II.
664
665 }
666
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000667 QualType T = GetTypeForDeclarator(D, S);
Chris Lattner01a7c532007-01-25 23:09:03 +0000668 if (T.isNull()) return 0;
Steve Naroff8eeeb132007-05-08 21:09:37 +0000669
670 // C99 6.7.2.1p8: A member of a structure or union may have any type other
671 // than a variably modified type.
672 if (ArrayType *ary = dyn_cast<ArrayType>(T)) {
673 if (!isConstantArrayType(ary, Loc))
674 return 0;
675 }
Chris Lattner01a7c532007-01-25 23:09:03 +0000676 return new FieldDecl(Loc, II, T);
Chris Lattner1300fb92007-01-23 23:42:53 +0000677}
678
679void Sema::ParseRecordBody(SourceLocation RecLoc, DeclTy *RecDecl,
680 DeclTy **Fields, unsigned NumFields) {
Chris Lattnerc1915e22007-01-25 07:29:02 +0000681 RecordDecl *Record = cast<RecordDecl>(static_cast<Decl*>(RecDecl));
Chris Lattner1300fb92007-01-23 23:42:53 +0000682 if (Record->isDefinition()) {
683 // Diagnose code like:
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000684 // struct S { struct S {} X; };
Chris Lattner1300fb92007-01-23 23:42:53 +0000685 // We discover this when we complete the outer S. Reject and ignore the
686 // outer S.
687 Diag(Record->getLocation(), diag::err_nested_redefinition,
688 Record->getKindName());
689 Diag(RecLoc, diag::err_previous_definition);
690 return;
691 }
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000692
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000693 // Verify that all the fields are okay.
Chris Lattner82625602007-01-24 02:26:21 +0000694 unsigned NumNamedMembers = 0;
Steve Naroffcc321422007-03-26 23:09:51 +0000695 SmallVector<FieldDecl*, 32> RecFields;
Chris Lattnere5a66562007-01-25 22:48:42 +0000696 SmallSet<const IdentifierInfo*, 32> FieldIDs;
697
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000698 for (unsigned i = 0; i != NumFields; ++i) {
699 FieldDecl *FD = cast_or_null<FieldDecl>(static_cast<Decl*>(Fields[i]));
700 if (!FD) continue; // Already issued a diagnostic.
Chris Lattner720a0542007-01-25 00:44:24 +0000701
702 // Get the type for the field.
Steve Naroffd50c88e2007-04-05 21:15:20 +0000703 Type *FDTy = FD->getType().getCanonicalType().getTypePtr();
Chris Lattner720a0542007-01-25 00:44:24 +0000704
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000705 // C99 6.7.2.1p2 - A field may not be a function type.
Chris Lattner720a0542007-01-25 00:44:24 +0000706 if (isa<FunctionType>(FDTy)) {
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000707 Diag(FD->getLocation(), diag::err_field_declared_as_function,
708 FD->getName());
Chris Lattner82625602007-01-24 02:26:21 +0000709 delete FD;
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000710 continue;
711 }
712
Chris Lattner82625602007-01-24 02:26:21 +0000713 // C99 6.7.2.1p2 - A field may not be an incomplete type except...
Chris Lattner720a0542007-01-25 00:44:24 +0000714 if (FDTy->isIncompleteType()) {
Chris Lattner82625602007-01-24 02:26:21 +0000715 if (i != NumFields-1 || // ... that the last member ...
716 Record->getKind() != Decl::Struct || // ... of a structure ...
Chris Lattner720a0542007-01-25 00:44:24 +0000717 !isa<ArrayType>(FDTy)) { //... may have incomplete array type.
Chris Lattner82625602007-01-24 02:26:21 +0000718 Diag(FD->getLocation(), diag::err_field_incomplete, FD->getName());
719 delete FD;
720 continue;
721 }
Chris Lattner720a0542007-01-25 00:44:24 +0000722 if (NumNamedMembers < 1) { //... must have more than named member ...
Chris Lattner82625602007-01-24 02:26:21 +0000723 Diag(FD->getLocation(), diag::err_flexible_array_empty_struct,
724 FD->getName());
725 delete FD;
726 continue;
727 }
Chris Lattner720a0542007-01-25 00:44:24 +0000728
729 // Okay, we have a legal flexible array member at the end of the struct.
Chris Lattner41943152007-01-25 04:52:46 +0000730 Record->setHasFlexibleArrayMember(true);
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000731 }
Chris Lattner720a0542007-01-25 00:44:24 +0000732
733
734 /// C99 6.7.2.1p2 - a struct ending in a flexible array member cannot be the
735 /// field of another structure or the element of an array.
736 if (RecordType *FDTTy = dyn_cast<RecordType>(FDTy)) {
737 if (FDTTy->getDecl()->hasFlexibleArrayMember()) {
738 // If this is a member of a union, then entire union becomes "flexible".
739 if (Record->getKind() == Decl::Union) {
Chris Lattner41943152007-01-25 04:52:46 +0000740 Record->setHasFlexibleArrayMember(true);
Chris Lattner720a0542007-01-25 00:44:24 +0000741 } else {
742 // If this is a struct/class and this is not the last element, reject
743 // it. Note that GCC supports variable sized arrays in the middle of
744 // structures.
745 if (i != NumFields-1) {
746 Diag(FD->getLocation(), diag::err_variable_sized_type_in_struct,
747 FD->getName());
748 delete FD;
749 continue;
750 }
751
752 // We support flexible arrays at the end of structs in other structs
753 // as an extension.
754 Diag(FD->getLocation(), diag::ext_flexible_array_in_struct,
755 FD->getName());
Chris Lattner41943152007-01-25 04:52:46 +0000756 Record->setHasFlexibleArrayMember(true);
Chris Lattner720a0542007-01-25 00:44:24 +0000757 }
758 }
759 }
760
Chris Lattner82625602007-01-24 02:26:21 +0000761 // Keep track of the number of named members.
Chris Lattnere5a66562007-01-25 22:48:42 +0000762 if (IdentifierInfo *II = FD->getIdentifier()) {
763 // Detect duplicate member names.
Chris Lattnerbaf33662007-01-27 02:14:08 +0000764 if (!FieldIDs.insert(II)) {
Chris Lattnere5a66562007-01-25 22:48:42 +0000765 Diag(FD->getLocation(), diag::err_duplicate_member, II->getName());
766 // Find the previous decl.
767 SourceLocation PrevLoc;
768 for (unsigned i = 0, e = RecFields.size(); ; ++i) {
769 assert(i != e && "Didn't find previous def!");
770 if (RecFields[i]->getIdentifier() == II) {
771 PrevLoc = RecFields[i]->getLocation();
772 break;
773 }
774 }
775 Diag(PrevLoc, diag::err_previous_definition);
776 delete FD;
777 continue;
778 }
Chris Lattner82625602007-01-24 02:26:21 +0000779 ++NumNamedMembers;
Chris Lattnere5a66562007-01-25 22:48:42 +0000780 }
Chris Lattner41943152007-01-25 04:52:46 +0000781
782 // Remember good fields.
783 RecFields.push_back(FD);
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000784 }
Chris Lattner82625602007-01-24 02:26:21 +0000785
786
787 // Okay, we successfully defined 'Record'.
Chris Lattner41943152007-01-25 04:52:46 +0000788 Record->defineBody(&RecFields[0], RecFields.size());
Chris Lattner1300fb92007-01-23 23:42:53 +0000789}
790
Chris Lattnerc1915e22007-01-25 07:29:02 +0000791Sema::DeclTy *Sema::ParseEnumConstant(Scope *S, DeclTy *EnumDeclX,
792 SourceLocation IdLoc, IdentifierInfo *Id,
793 SourceLocation EqualLoc, ExprTy *Val) {
794 EnumDecl *TheEnumDecl = cast<EnumDecl>(static_cast<Decl*>(EnumDeclX));
Chris Lattner8116d1b2007-01-25 22:38:29 +0000795
796 // Verify that there isn't already something declared with this name in this
797 // scope.
Chris Lattner9561a0b2007-01-28 08:20:04 +0000798 if (Decl *PrevDecl = LookupScopedDecl(Id, Decl::IDNS_Ordinary, IdLoc, S)) {
Chris Lattner8116d1b2007-01-25 22:38:29 +0000799 if (S->isDeclScope(PrevDecl)) {
800 if (isa<EnumConstantDecl>(PrevDecl))
801 Diag(IdLoc, diag::err_redefinition_of_enumerator, Id->getName());
802 else
803 Diag(IdLoc, diag::err_redefinition, Id->getName());
804 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
805 return 0;
806 }
807 }
Steve Naroff72cada02007-05-18 00:18:52 +0000808 SourceLocation expLoc;
Steve Naroff63969212007-05-07 21:22:42 +0000809 // C99 6.7.2.2p2: Make sure we have an integer constant expression.
Chris Lattner238cbc52007-06-02 04:48:48 +0000810 if (Val && !((Expr *)Val)->isIntegerConstantExpr(&expLoc)) {
Steve Naroff72cada02007-05-18 00:18:52 +0000811 Diag(expLoc, diag::err_enum_value_not_integer_constant_expr, Id->getName());
Steve Naroff63969212007-05-07 21:22:42 +0000812 return 0;
813 }
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000814 QualType Ty = Context.getTagDeclType(TheEnumDecl);
Steve Naroff63969212007-05-07 21:22:42 +0000815 EnumConstantDecl *New = new EnumConstantDecl(IdLoc, Id, Ty, (Expr *)Val);
Chris Lattner8116d1b2007-01-25 22:38:29 +0000816
817 // Register this decl in the current scope stack.
818 New->setNext(Id->getFETokenInfo<Decl>());
819 Id->setFETokenInfo(New);
820 S->AddDecl(New);
821 return New;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000822}
823
824void Sema::ParseEnumBody(SourceLocation EnumLoc, DeclTy *EnumDeclX,
825 DeclTy **Elements, unsigned NumElements) {
826 EnumDecl *Enum = cast<EnumDecl>(static_cast<Decl*>(EnumDeclX));
827 assert(!Enum->isDefinition() && "Enum redefinitions can't reach here");
828
829 // Verify that all the values are okay.
830 SmallVector<EnumConstantDecl*, 32> Values;
831 for (unsigned i = 0; i != NumElements; ++i) {
832 EnumConstantDecl *ECD =
833 cast_or_null<EnumConstantDecl>(static_cast<Decl*>(Elements[i]));
834 if (!ECD) continue; // Already issued a diagnostic.
835
836 Values.push_back(ECD);
837 }
838
839 Enum->defineElements(&Values[0], Values.size());
840}
Chris Lattner1300fb92007-01-23 23:42:53 +0000841
Steve Naroff26c8ea52007-03-21 21:08:52 +0000842void Sema::AddTopLevelDecl(Decl *current, Decl *last) {
843 if (!current) return;
844
845 // If this is a top-level decl that is chained to some other (e.g. int A,B,C;)
846 // remember this in the LastInGroupList list.
847 if (last) {
848 LastInGroupList.push_back((Decl*)last);
849 }
850}
Steve Naroff0f2fe172007-06-01 17:11:19 +0000851
852/// ParseAttribute GCC __attribute__
Steve Naroff45552922007-06-01 21:56:17 +0000853Sema::AttrTy *Sema::ParseAttribute(
854 IdentifierInfo *AttrName, SourceLocation AttrNameLoc, AttrTy *PrevAttr,
Steve Naroff0f2fe172007-06-01 17:11:19 +0000855 IdentifierInfo *ParmName, SourceLocation ParmNameLoc,
856 ExprTy **Args, unsigned NumArgs,
857 SourceLocation LParenLoc, SourceLocation RParenLoc) {
Steve Naroff45552922007-06-01 21:56:17 +0000858 Attr *attrib = new Attr(AttrNameLoc, AttrName, ParmName, (Expr **)Args,
859 NumArgs);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000860 if (PrevAttr)
861 // reuse Decl's "Next" pointer for chaining the attribute list
Steve Naroff45552922007-06-01 21:56:17 +0000862 attrib->setNext(static_cast<Attr *>(PrevAttr));
Steve Naroff0f2fe172007-06-01 17:11:19 +0000863 return attrib;
864}