blob: 2300065adc293d840411afd8ba93dc0334c14ae5 [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"
Chris Lattner697e5d62006-11-09 06:32:27 +000026using namespace llvm;
27using namespace clang;
28
Chris Lattnere168f762006-11-10 05:29:30 +000029
Chris Lattner2ebe4bb2006-11-20 01:29:42 +000030Sema::DeclTy *Sema::isTypeName(const IdentifierInfo &II, Scope *S) const {
Chris Lattner32d920b2007-01-26 02:01:53 +000031 return dyn_cast_or_null<TypedefDecl>(II.getFETokenInfo<Decl>());
Chris Lattnere168f762006-11-10 05:29:30 +000032}
33
Chris Lattner302b4be2006-11-19 02:31:38 +000034void Sema::PopScope(SourceLocation Loc, Scope *S) {
35 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
36 I != E; ++I) {
Chris Lattner99d31772007-01-21 22:37:37 +000037 Decl *D = static_cast<Decl*>(*I);
Chris Lattner302b4be2006-11-19 02:31:38 +000038 assert(D && "This decl didn't get pushed??");
Chris Lattnerff65b6b2007-01-23 01:33:16 +000039 IdentifierInfo *II = D->getIdentifier();
40 if (!II) continue;
Chris Lattner302b4be2006-11-19 02:31:38 +000041
Chris Lattnerff65b6b2007-01-23 01:33:16 +000042 // Unlink this decl from the identifier. Because the scope contains decls
43 // in an unordered collection, and because we have multiple identifier
44 // namespaces (e.g. tag, normal, label),the decl may not be the first entry.
45 if (II->getFETokenInfo<Decl>() == D) {
46 // Normal case, no multiple decls in different namespaces.
47 II->setFETokenInfo(D->getNext());
48 } else {
49 // Scan ahead. There are only three namespaces in C, so this loop can
50 // never execute more than 3 times.
51 Decl *SomeDecl = II->getFETokenInfo<Decl>();
52 while (SomeDecl->getNext() != D) {
53 SomeDecl = SomeDecl->getNext();
54 assert(SomeDecl && "Didn't find this decl on its identifier's chain!");
55 }
56 SomeDecl->setNext(D->getNext());
57 }
Chris Lattner302b4be2006-11-19 02:31:38 +000058
Chris Lattner740b2f32006-11-21 01:32:20 +000059 // This will have to be revisited for C++: there we want to nest stuff in
60 // namespace decls etc. Even for C, we might want a top-level translation
61 // unit decl or something.
62 if (!CurFunctionDecl)
63 continue;
64
65 // Chain this decl to the containing function, it now owns the memory for
66 // the decl.
67 D->setNext(CurFunctionDecl->getDeclChain());
68 CurFunctionDecl->setDeclChain(D);
Chris Lattner302b4be2006-11-19 02:31:38 +000069 }
70}
71
Chris Lattner18b19622007-01-22 07:39:13 +000072/// LookupScopedDecl - Look up the inner-most declaration in the specified
73/// namespace.
Chris Lattner9561a0b2007-01-28 08:20:04 +000074Decl *Sema::LookupScopedDecl(IdentifierInfo *II, unsigned NSI,
75 SourceLocation IdLoc, Scope *S) {
Chris Lattner18b19622007-01-22 07:39:13 +000076 if (II == 0) return 0;
Chris Lattnerb6738ec2007-01-28 00:38:24 +000077 Decl::IdentifierNamespace NS = (Decl::IdentifierNamespace)NSI;
Chris Lattner18b19622007-01-22 07:39:13 +000078
79 // Scan up the scope chain looking for a decl that matches this identifier
80 // that is in the appropriate namespace. This search should not take long, as
81 // shadowing of names is uncommon, and deep shadowing is extremely uncommon.
82 for (Decl *D = II->getFETokenInfo<Decl>(); D; D = D->getNext())
83 if (D->getIdentifierNamespace() == NS)
84 return D;
Chris Lattnerb6738ec2007-01-28 00:38:24 +000085
Chris Lattner9561a0b2007-01-28 08:20:04 +000086 // If we didn't find a use of this identifier, and if the identifier
87 // corresponds to a compiler builtin, create the decl object for the builtin
88 // now, injecting it into translation unit scope, and return it.
89 if (NS == Decl::IDNS_Ordinary) {
90 // If this is a builtin on some other target, or if this builtin varies
91 // across targets (e.g. in type), emit a diagnostic and mark the translation
92 // unit non-portable for using it.
93 if (II->isNonPortableBuiltin()) {
94 // Only emit this diagnostic once for this builtin.
95 II->setNonPortableBuiltin(false);
96 Context.Target.DiagnoseNonPortability(IdLoc,
97 diag::port_target_builtin_use);
98 }
Chris Lattner9561a0b2007-01-28 08:20:04 +000099 // If this is a builtin on this (or all) targets, create the decl.
100 if (unsigned BuiltinID = II->getBuiltinID())
101 return LazilyCreateBuiltin(II, BuiltinID, S);
102 }
Chris Lattner18b19622007-01-22 07:39:13 +0000103 return 0;
104}
105
Chris Lattner9561a0b2007-01-28 08:20:04 +0000106/// LazilyCreateBuiltin - The specified Builtin-ID was first used at file scope.
107/// lazily create a decl for it.
108Decl *Sema::LazilyCreateBuiltin(IdentifierInfo *II, unsigned bid, Scope *S) {
109 Builtin::ID BID = (Builtin::ID)bid;
110
Chris Lattner10a5b382007-01-29 05:24:35 +0000111 TypeRef R = Context.BuiltinInfo.GetBuiltinType(BID, Context);
Chris Lattner9561a0b2007-01-28 08:20:04 +0000112 FunctionDecl *New = new FunctionDecl(SourceLocation(), II, R);
113
114 // Find translation-unit scope to insert this function into.
115 while (S->getParent())
116 S = S->getParent();
117 S->AddDecl(New);
118
119 // Add this decl to the end of the identifier info.
120 if (Decl *LastDecl = II->getFETokenInfo<Decl>()) {
121 // Scan until we find the last (outermost) decl in the id chain.
122 while (LastDecl->getNext())
123 LastDecl = LastDecl->getNext();
124 // Insert before (outside) it.
125 LastDecl->setNext(New);
126 } else {
127 II->setFETokenInfo(New);
128 }
129 // Make sure clients iterating over decls see this.
130 LastInGroupList.push_back(New);
131
132 return New;
133}
134
Chris Lattner01564d92007-01-27 19:27:06 +0000135/// MergeTypeDefDecl - We just parsed a typedef 'New' which has the same name
136/// and scope as a previous declaration 'Old'. Figure out how to resolve this
137/// situation, merging decls or emitting diagnostics as appropriate.
138///
Chris Lattnerc511efb2007-01-27 19:32:14 +0000139TypedefDecl *Sema::MergeTypeDefDecl(TypedefDecl *New, Decl *OldD) {
140 // Verify the old decl was also a typedef.
141 TypedefDecl *Old = dyn_cast<TypedefDecl>(OldD);
142 if (!Old) {
143 Diag(New->getLocation(), diag::err_redefinition_different_kind,
144 New->getName());
145 Diag(OldD->getLocation(), diag::err_previous_definition);
146 return New;
147 }
148
Chris Lattner01564d92007-01-27 19:27:06 +0000149 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
150 // TODO: This is totally simplistic. It should handle merging functions
151 // together etc, merging extern int X; int X; ...
152 Diag(New->getLocation(), diag::err_redefinition, New->getName());
153 Diag(Old->getLocation(), diag::err_previous_definition);
154 return New;
155}
156
157/// MergeFunctionDecl - We just parsed a function '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 +0000161FunctionDecl *Sema::MergeFunctionDecl(FunctionDecl *New, Decl *OldD) {
162 // Verify the old decl was also a function.
163 FunctionDecl *Old = dyn_cast<FunctionDecl>(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 Lattnerefe4aea2007-01-27 19:35:39 +0000171 // This is not right, but it's a start. If 'Old' is a function prototype with
172 // the same type as 'New', silently allow this. FIXME: We should link up decl
173 // objects here.
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000174 if (Old->getBody() == 0 &&
175 Old->getCanonicalType() == New->getCanonicalType()) {
Chris Lattnerefe4aea2007-01-27 19:35:39 +0000176 return New;
177 }
Chris Lattnerc511efb2007-01-27 19:32:14 +0000178
Chris Lattner01564d92007-01-27 19:27:06 +0000179 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
180 // TODO: This is totally simplistic. It should handle merging functions
181 // together etc, merging extern int X; int X; ...
182 Diag(New->getLocation(), diag::err_redefinition, New->getName());
183 Diag(Old->getLocation(), diag::err_previous_definition);
184 return New;
185}
186
187/// MergeVarDecl - We just parsed a variable 'New' which has the same name
188/// and scope as a previous declaration 'Old'. Figure out how to resolve this
189/// situation, merging decls or emitting diagnostics as appropriate.
190///
Steve Narofffc49d672007-04-01 21:27:45 +0000191/// FIXME: Need to carefully consider tentative definition rules (C99 6.9.2p2).
192/// For example, we incorrectly complain about i1, i4 from C99 6.9.2p4.
193///
Chris Lattnerc511efb2007-01-27 19:32:14 +0000194VarDecl *Sema::MergeVarDecl(VarDecl *New, Decl *OldD) {
195 // Verify the old decl was also a variable.
196 VarDecl *Old = dyn_cast<VarDecl>(OldD);
197 if (!Old) {
198 Diag(New->getLocation(), diag::err_redefinition_different_kind,
199 New->getName());
200 Diag(OldD->getLocation(), diag::err_previous_definition);
201 return New;
202 }
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000203 // Verify the types match.
204 if (Old->getCanonicalType() != New->getCanonicalType()) {
205 Diag(New->getLocation(), diag::err_redefinition, New->getName());
206 Diag(Old->getLocation(), diag::err_previous_definition);
207 return New;
208 }
209 // We've verified the types match, now check if Old is "extern".
210 if (Old->getStorageClass() != ObjectDecl::Extern) {
211 Diag(New->getLocation(), diag::err_redefinition, New->getName());
212 Diag(Old->getLocation(), diag::err_previous_definition);
213 }
Chris Lattner01564d92007-01-27 19:27:06 +0000214 return New;
215}
216
Chris Lattnerb6738ec2007-01-28 00:38:24 +0000217/// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with
218/// no declarator (e.g. "struct foo;") is parsed.
219Sema::DeclTy *Sema::ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS) {
220 // TODO: emit error on 'int;' or 'const enum foo;'.
221 // TODO: emit error on 'typedef int;'
222 // if (!DS.isMissingDeclaratorOk()) Diag(...);
223
224 return 0;
225}
226
Chris Lattner18b19622007-01-22 07:39:13 +0000227
Chris Lattnere168f762006-11-10 05:29:30 +0000228Action::DeclTy *
229Sema::ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init,
230 DeclTy *LastInGroup) {
231 IdentifierInfo *II = D.getIdentifier();
Chris Lattner302b4be2006-11-19 02:31:38 +0000232
Chris Lattner01564d92007-01-27 19:27:06 +0000233 // See if this is a redefinition of a variable in the same scope.
Chris Lattner9561a0b2007-01-28 08:20:04 +0000234 Decl *PrevDecl = LookupScopedDecl(II, Decl::IDNS_Ordinary,
235 D.getIdentifierLoc(), S);
Chris Lattner01564d92007-01-27 19:27:06 +0000236 if (!S->isDeclScope(PrevDecl))
237 PrevDecl = 0; // If in outer scope, it isn't the same thing.
238
Chris Lattnere168f762006-11-10 05:29:30 +0000239 Decl *New;
Chris Lattner01a7c532007-01-25 23:09:03 +0000240 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
Chris Lattner01564d92007-01-27 19:27:06 +0000241 TypedefDecl *NewTD = ParseTypedefDecl(S, D);
242 if (!NewTD) return 0;
243
244 // Merge the decl with the existing one if appropriate.
245 if (PrevDecl) {
246 NewTD = MergeTypeDefDecl(NewTD, PrevDecl);
247 if (NewTD == 0) return 0;
248 }
249 New = NewTD;
Chris Lattner01a7c532007-01-25 23:09:03 +0000250 } else if (D.isFunctionDeclarator()) {
251 TypeRef R = GetTypeForDeclarator(D, S);
252 if (R.isNull()) return 0;
Chris Lattner01564d92007-01-27 19:27:06 +0000253
254 FunctionDecl *NewFD = new FunctionDecl(D.getIdentifierLoc(), II, R);
255
256 // Merge the decl with the existing one if appropriate.
257 if (PrevDecl) {
258 NewFD = MergeFunctionDecl(NewFD, PrevDecl);
259 if (NewFD == 0) return 0;
260 }
261 New = NewFD;
Chris Lattner01a7c532007-01-25 23:09:03 +0000262 } else {
263 TypeRef R = GetTypeForDeclarator(D, S);
264 if (R.isNull()) return 0;
Chris Lattner01564d92007-01-27 19:27:06 +0000265
Steve Naroffca8f7122007-04-01 01:41:35 +0000266 ObjectDecl::StorageClass SC;
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000267 switch (D.getDeclSpec().getStorageClassSpec()) {
268 default: assert(0 && "Unknown storage class!");
Steve Naroffca8f7122007-04-01 01:41:35 +0000269 case DeclSpec::SCS_unspecified: SC = ObjectDecl::None; break;
270 case DeclSpec::SCS_extern: SC = ObjectDecl::Extern; break;
271 case DeclSpec::SCS_static: SC = ObjectDecl::Static; break;
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000272 // The following 2 should never be seen in this context.
Steve Naroffca8f7122007-04-01 01:41:35 +0000273 case DeclSpec::SCS_auto: SC = ObjectDecl::Auto; break;
274 case DeclSpec::SCS_register: SC = ObjectDecl::Register; break;
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000275 }
Steve Narofffc49d672007-04-01 21:27:45 +0000276 if (S->getParent() == 0) {
277 // File scope. C99 6.9.2p2: A declaration of an identifier for and
278 // object that has file scope without an initializer, and without a
279 // storage-class specifier or with the storage-class specifier "static",
280 // constitutes a tentative definition. Note: A tentative definition with
281 // external linkage is valid (C99 6.2.2p5).
282 if (!Init && SC == ObjectDecl::Static) {
283 // C99 6.9.2p3: If the declaration of an identifier for an object is
284 // a tentative definition and has internal linkage (C99 6.2.2p3), the
285 // declared type shall not be an incomplete type.
286 if (R->isIncompleteType()) {
287 Diag(D.getIdentifierLoc(), diag::err_typecheck_decl_incomplete_type, R);
288 return 0;
289 }
Steve Naroffca8f7122007-04-01 01:41:35 +0000290 }
Steve Narofffc49d672007-04-01 21:27:45 +0000291 } else {
292 // Block scope. C99 6.7p7: If an identifier for an object is declared with
293 // no linkage (C99 6.2.2p6), the type for the object shall be complete...
294 if (SC != ObjectDecl::Extern) {
295 if (R->isIncompleteType()) {
296 Diag(D.getIdentifierLoc(), diag::err_typecheck_decl_incomplete_type, R);
297 return 0;
298 }
299 }
300 }
Steve Naroffca8f7122007-04-01 01:41:35 +0000301 VarDecl *NewVD = new VarDecl(D.getIdentifierLoc(), II, R, SC);
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000302
Chris Lattner01564d92007-01-27 19:27:06 +0000303 // Merge the decl with the existing one if appropriate.
304 if (PrevDecl) {
305 NewVD = MergeVarDecl(NewVD, PrevDecl);
306 if (NewVD == 0) return 0;
307 }
308 New = NewVD;
Chris Lattner01a7c532007-01-25 23:09:03 +0000309 }
Chris Lattner302b4be2006-11-19 02:31:38 +0000310
311
Chris Lattnere168f762006-11-10 05:29:30 +0000312 // If this has an identifier, add it to the scope stack.
313 if (II) {
Chris Lattner18b19622007-01-22 07:39:13 +0000314 New->setNext(II->getFETokenInfo<Decl>());
Chris Lattnere168f762006-11-10 05:29:30 +0000315 II->setFETokenInfo(New);
Chris Lattner99d31772007-01-21 22:37:37 +0000316 S->AddDecl(New);
Chris Lattnere168f762006-11-10 05:29:30 +0000317 }
318
Steve Naroff26c8ea52007-03-21 21:08:52 +0000319 if (S->getParent() == 0)
320 AddTopLevelDecl(New, (Decl *)LastInGroup);
Chris Lattnere168f762006-11-10 05:29:30 +0000321
322 return New;
323}
324
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000325VarDecl *
326Sema::ParseParamDeclarator(DeclaratorChunk &FTI, unsigned ArgNo,
327 Scope *FnScope) {
328 const DeclaratorChunk::ParamInfo &PI = FTI.Fun.ArgInfo[ArgNo];
Chris Lattner200bdc32006-11-19 02:43:37 +0000329
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000330 IdentifierInfo *II = PI.Ident;
Chris Lattnerc284e9b2007-01-23 05:14:32 +0000331 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
332 // Can this happen for params? We already checked that they don't conflict
333 // among each other. Here they can only shadow globals, which is ok.
Chris Lattner9561a0b2007-01-28 08:20:04 +0000334 if (Decl *PrevDecl = LookupScopedDecl(II, Decl::IDNS_Ordinary,
335 PI.IdentLoc, FnScope)) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000336
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000337 }
338
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000339 // FIXME: Handle storage class (auto, register). No declarator?
Chris Lattner38047f92007-01-27 06:24:01 +0000340 VarDecl *New = new VarDecl(PI.IdentLoc, II,
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000341 TypeRef::getFromOpaquePtr(PI.TypeInfo),
342 ObjectDecl::None);
343
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000344 // If this has an identifier, add it to the scope stack.
345 if (II) {
Chris Lattner18b19622007-01-22 07:39:13 +0000346 New->setNext(II->getFETokenInfo<Decl>());
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000347 II->setFETokenInfo(New);
Chris Lattner99d31772007-01-21 22:37:37 +0000348 FnScope->AddDecl(New);
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000349 }
Chris Lattner229ce602006-11-21 01:21:07 +0000350
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000351 return New;
352}
353
354
355Sema::DeclTy *Sema::ParseStartOfFunctionDef(Scope *FnBodyScope, Declarator &D) {
Chris Lattner229ce602006-11-21 01:21:07 +0000356 assert(CurFunctionDecl == 0 && "Function parsing confused");
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000357 assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function &&
358 "Not a function declarator!");
359 DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
360
361 // Verify 6.9.1p6: 'every identifier in the identifier list shall be declared'
362 // for a K&R function.
363 if (!FTI.hasPrototype) {
364 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
365 if (FTI.ArgInfo[i].TypeInfo == 0) {
366 Diag(FTI.ArgInfo[i].IdentLoc, diag::err_param_not_declared,
367 FTI.ArgInfo[i].Ident->getName());
368 // Implicitly declare the argument as type 'int' for lack of a better
369 // type.
370 FTI.ArgInfo[i].TypeInfo = Context.IntTy.getAsOpaquePtr();
371 }
372 }
373
374 // Since this is a function definition, act as though we have information
375 // about the arguments.
376 FTI.hasPrototype = true;
Chris Lattner2114d5e2006-12-04 07:40:24 +0000377 } else {
378 // FIXME: Diagnose arguments without names in C.
379
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000380 }
381
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000382 Scope *GlobalScope = FnBodyScope->getParent();
383
384 FunctionDecl *FD =
385 static_cast<FunctionDecl*>(ParseDeclarator(GlobalScope, D, 0, 0));
Chris Lattner229ce602006-11-21 01:21:07 +0000386 CurFunctionDecl = FD;
Chris Lattner2114d5e2006-12-04 07:40:24 +0000387
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000388 // Create Decl objects for each parameter, adding them to the FunctionDecl.
389 SmallVector<VarDecl*, 16> Params;
Chris Lattnerf61c8a82007-01-21 19:04:43 +0000390
391 // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs function that takes
392 // no arguments, not a function that takes a single void argument.
393 if (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 &&
394 FTI.ArgInfo[0].TypeInfo == Context.VoidTy.getAsOpaquePtr()) {
Chris Lattner99d31772007-01-21 22:37:37 +0000395 // empty arg list, don't push any params.
Chris Lattnerf61c8a82007-01-21 19:04:43 +0000396 } else {
397 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i)
398 Params.push_back(ParseParamDeclarator(D.getTypeObject(0), i,FnBodyScope));
399 }
Chris Lattner2114d5e2006-12-04 07:40:24 +0000400
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000401 FD->setParams(&Params[0], Params.size());
Chris Lattner2114d5e2006-12-04 07:40:24 +0000402
Chris Lattnere168f762006-11-10 05:29:30 +0000403 return FD;
404}
405
Chris Lattner229ce602006-11-21 01:21:07 +0000406Sema::DeclTy *Sema::ParseFunctionDefBody(DeclTy *D, StmtTy *Body) {
407 FunctionDecl *FD = static_cast<FunctionDecl*>(D);
408 FD->setBody((Stmt*)Body);
409
410 assert(FD == CurFunctionDecl && "Function parsing confused");
411 CurFunctionDecl = 0;
412 return FD;
413}
414
415
Chris Lattnerac18be92006-11-20 06:49:47 +0000416/// ImplicitlyDefineFunction - An undeclared identifier was used in a function
417/// call, forming a call to an implicitly defined function (per C99 6.5.1p2).
418Decl *Sema::ImplicitlyDefineFunction(SourceLocation Loc, IdentifierInfo &II,
419 Scope *S) {
420 if (getLangOptions().C99) // Extension in C99.
421 Diag(Loc, diag::ext_implicit_function_decl, II.getName());
422 else // Legal in C90, but warn about it.
423 Diag(Loc, diag::warn_implicit_function_decl, II.getName());
424
425 // FIXME: handle stuff like:
426 // void foo() { extern float X(); }
427 // void bar() { X(); } <-- implicit decl for X in another scope.
428
429 // Set a Declarator for the implicit definition: int foo();
Chris Lattner353f5742006-11-28 04:50:12 +0000430 const char *Dummy;
Chris Lattnerac18be92006-11-20 06:49:47 +0000431 DeclSpec DS;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000432 bool Error = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, Dummy);
Chris Lattnerb055f2d2007-02-11 08:19:57 +0000433 Error = Error; // Silence warning.
Chris Lattner353f5742006-11-28 04:50:12 +0000434 assert(!Error && "Error setting up implicit decl!");
Chris Lattnerac18be92006-11-20 06:49:47 +0000435 Declarator D(DS, Declarator::BlockContext);
Chris Lattnercbc426d2006-12-02 06:43:02 +0000436 D.AddTypeInfo(DeclaratorChunk::getFunction(false, false, 0, 0, Loc));
Chris Lattnerac18be92006-11-20 06:49:47 +0000437 D.SetIdentifier(&II, Loc);
438
Chris Lattner62d2e662007-01-28 00:21:37 +0000439 // Find translation-unit scope to insert this function into.
440 while (S->getParent())
441 S = S->getParent();
Chris Lattnerac18be92006-11-20 06:49:47 +0000442
Chris Lattner62d2e662007-01-28 00:21:37 +0000443 return static_cast<Decl*>(ParseDeclarator(S, D, 0, 0));
Chris Lattnerac18be92006-11-20 06:49:47 +0000444}
445
Chris Lattner302b4be2006-11-19 02:31:38 +0000446
Chris Lattner01564d92007-01-27 19:27:06 +0000447TypedefDecl *Sema::ParseTypedefDecl(Scope *S, Declarator &D) {
Chris Lattnerda8aa7b2006-11-19 23:12:30 +0000448 assert(D.getIdentifier() && "Wrong callback for declspec withotu declarator");
Chris Lattner302b4be2006-11-19 02:31:38 +0000449
Chris Lattner5ca17df2006-11-19 23:32:49 +0000450 TypeRef T = GetTypeForDeclarator(D, S);
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000451 if (T.isNull()) return 0;
452
Chris Lattner18b19622007-01-22 07:39:13 +0000453 // Scope manipulation handled by caller.
454 return new TypedefDecl(D.getIdentifierLoc(), D.getIdentifier(), T);
Chris Lattnere168f762006-11-10 05:29:30 +0000455}
456
Chris Lattner18b19622007-01-22 07:39:13 +0000457
Chris Lattner1300fb92007-01-23 23:42:53 +0000458/// ParseTag - This is invoked when we see 'struct foo' or 'struct {'. In the
459/// former case, Name will be non-null. In the later case, Name will be null.
460/// TagType indicates what kind of tag this is. TK indicates whether this is a
461/// reference/declaration/definition of a tag.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000462Sema::DeclTy *Sema::ParseTag(Scope *S, unsigned TagType, TagKind TK,
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000463 SourceLocation KWLoc, IdentifierInfo *Name,
464 SourceLocation NameLoc) {
Chris Lattner8799cf22007-01-23 01:57:16 +0000465 // If this is a use of an existing tag, it must have a name.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000466 assert((Name != 0 || TK == TK_Definition) &&
467 "Nameless record must be a definition!");
Chris Lattner8799cf22007-01-23 01:57:16 +0000468
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000469 Decl::Kind Kind;
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000470 switch (TagType) {
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000471 default: assert(0 && "Unknown tag type!");
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000472 case DeclSpec::TST_struct: Kind = Decl::Struct; break;
473 case DeclSpec::TST_union: Kind = Decl::Union; break;
474//case DeclSpec::TST_class: Kind = Decl::Class; break;
475 case DeclSpec::TST_enum: Kind = Decl::Enum; break;
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000476 }
Chris Lattner7e783a12007-01-23 02:05:42 +0000477
Chris Lattner18b19622007-01-22 07:39:13 +0000478 // If this is a named struct, check to see if there was a previous forward
479 // declaration or definition.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000480 if (TagDecl *PrevDecl =
Chris Lattner9561a0b2007-01-28 08:20:04 +0000481 dyn_cast_or_null<TagDecl>(LookupScopedDecl(Name, Decl::IDNS_Tag,
482 NameLoc, S))) {
Chris Lattner8799cf22007-01-23 01:57:16 +0000483
484 // If this is a use of a previous tag, or if the tag is already declared in
485 // the same scope (so that the definition/declaration completes or
486 // rementions the tag), reuse the decl.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000487 if (TK == TK_Reference || S->isDeclScope(PrevDecl)) {
Chris Lattner7e783a12007-01-23 02:05:42 +0000488 // Make sure that this wasn't declared as an enum and now used as a struct
489 // or something similar.
490 if (PrevDecl->getKind() != Kind) {
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000491 Diag(KWLoc, diag::err_use_with_wrong_tag, Name->getName());
Chris Lattner7e783a12007-01-23 02:05:42 +0000492 Diag(PrevDecl->getLocation(), diag::err_previous_use);
493 }
Chris Lattner7b9ace62007-01-23 20:11:08 +0000494
495 // If this is a use or a forward declaration, we're good.
496 if (TK != TK_Definition)
497 return PrevDecl;
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000498
Chris Lattner7b9ace62007-01-23 20:11:08 +0000499 // Diagnose attempts to redefine a tag.
500 if (PrevDecl->isDefinition()) {
501 Diag(NameLoc, diag::err_redefinition, Name->getName());
502 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
503 // If this is a redefinition, recover by making this struct be
504 // anonymous, which will make any later references get the previous
505 // definition.
506 Name = 0;
507 } else {
508 // Okay, this is definition of a previously declared or referenced tag.
509 // Move the location of the decl to be the definition site.
510 PrevDecl->setLocation(NameLoc);
Chris Lattner7b9ace62007-01-23 20:11:08 +0000511 return PrevDecl;
512 }
Chris Lattner8799cf22007-01-23 01:57:16 +0000513 }
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000514 // If we get here, this is a definition of a new struct type in a nested
515 // scope, e.g. "struct foo; void bar() { struct foo; }", just create a new
516 // type.
Chris Lattner18b19622007-01-22 07:39:13 +0000517 }
518
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000519 // If there is an identifier, use the location of the identifier as the
520 // location of the decl, otherwise use the location of the struct/union
521 // keyword.
522 SourceLocation Loc = NameLoc.isValid() ? NameLoc : KWLoc;
523
Chris Lattner18b19622007-01-22 07:39:13 +0000524 // Otherwise, if this is the first time we've seen this tag, create the decl.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000525 TagDecl *New;
Chris Lattner720a0542007-01-25 00:44:24 +0000526 switch (Kind) {
527 default: assert(0 && "Unknown tag kind!");
Chris Lattner5f521502007-01-25 06:27:24 +0000528 case Decl::Enum:
529 New = new EnumDecl(Loc, Name);
530 // If this is an undefined enum, warn.
Chris Lattnerc1915e22007-01-25 07:29:02 +0000531 if (TK != TK_Definition) Diag(Loc, diag::ext_forward_ref_enum);
Chris Lattner5f521502007-01-25 06:27:24 +0000532 break;
Chris Lattner720a0542007-01-25 00:44:24 +0000533 case Decl::Union:
534 case Decl::Struct:
535 case Decl::Class:
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000536 New = new RecordDecl(Kind, Loc, Name);
Chris Lattner720a0542007-01-25 00:44:24 +0000537 break;
538 }
Chris Lattner18b19622007-01-22 07:39:13 +0000539
540 // If this has an identifier, add it to the scope stack.
541 if (Name) {
542 New->setNext(Name->getFETokenInfo<Decl>());
543 Name->setFETokenInfo(New);
544 S->AddDecl(New);
545 }
546
547 return New;
548}
Chris Lattner1300fb92007-01-23 23:42:53 +0000549
550/// ParseField - Each field of a struct/union/class is passed into this in order
551/// to create a FieldDecl object for it.
552Sema::DeclTy *Sema::ParseField(Scope *S, DeclTy *TagDecl,
553 SourceLocation DeclStart,
554 Declarator &D, ExprTy *BitfieldWidth) {
555 IdentifierInfo *II = D.getIdentifier();
556 Expr *BitWidth = (Expr*)BitfieldWidth;
557
558 SourceLocation Loc = DeclStart;
559 if (II) Loc = D.getIdentifierLoc();
560
Chris Lattner62d2e662007-01-28 00:21:37 +0000561 // FIXME: Unnamed fields can be handled in various different ways, for
562 // example, unnamed unions inject all members into the struct namespace!
563
564
Chris Lattner1300fb92007-01-23 23:42:53 +0000565 if (BitWidth) {
566 // TODO: Validate.
Chris Lattner01a7c532007-01-25 23:09:03 +0000567 printf("WARNING: BITFIELDS IGNORED!\n");
Chris Lattner1300fb92007-01-23 23:42:53 +0000568
569 // 6.7.2.1p3
570 // 6.7.2.1p4
571
572 } else {
573 // Not a bitfield.
574
575 // validate II.
576
577 }
578
Chris Lattner01a7c532007-01-25 23:09:03 +0000579 TypeRef T = GetTypeForDeclarator(D, S);
580 if (T.isNull()) return 0;
581
582 return new FieldDecl(Loc, II, T);
Chris Lattner1300fb92007-01-23 23:42:53 +0000583}
584
585void Sema::ParseRecordBody(SourceLocation RecLoc, DeclTy *RecDecl,
586 DeclTy **Fields, unsigned NumFields) {
Chris Lattnerc1915e22007-01-25 07:29:02 +0000587 RecordDecl *Record = cast<RecordDecl>(static_cast<Decl*>(RecDecl));
Chris Lattner1300fb92007-01-23 23:42:53 +0000588 if (Record->isDefinition()) {
589 // Diagnose code like:
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000590 // struct S { struct S {} X; };
Chris Lattner1300fb92007-01-23 23:42:53 +0000591 // We discover this when we complete the outer S. Reject and ignore the
592 // outer S.
593 Diag(Record->getLocation(), diag::err_nested_redefinition,
594 Record->getKindName());
595 Diag(RecLoc, diag::err_previous_definition);
596 return;
597 }
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000598
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000599 // Verify that all the fields are okay.
Chris Lattner82625602007-01-24 02:26:21 +0000600 unsigned NumNamedMembers = 0;
Steve Naroffcc321422007-03-26 23:09:51 +0000601 SmallVector<FieldDecl*, 32> RecFields;
Chris Lattnere5a66562007-01-25 22:48:42 +0000602 SmallSet<const IdentifierInfo*, 32> FieldIDs;
603
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000604 for (unsigned i = 0; i != NumFields; ++i) {
605 FieldDecl *FD = cast_or_null<FieldDecl>(static_cast<Decl*>(Fields[i]));
606 if (!FD) continue; // Already issued a diagnostic.
Chris Lattner720a0542007-01-25 00:44:24 +0000607
608 // Get the type for the field.
609 Type *FDTy = FD->getType()->getCanonicalType();
610
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000611 // C99 6.7.2.1p2 - A field may not be a function type.
Chris Lattner720a0542007-01-25 00:44:24 +0000612 if (isa<FunctionType>(FDTy)) {
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000613 Diag(FD->getLocation(), diag::err_field_declared_as_function,
614 FD->getName());
Chris Lattner82625602007-01-24 02:26:21 +0000615 delete FD;
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000616 continue;
617 }
618
Chris Lattner82625602007-01-24 02:26:21 +0000619 // C99 6.7.2.1p2 - A field may not be an incomplete type except...
Chris Lattner720a0542007-01-25 00:44:24 +0000620 if (FDTy->isIncompleteType()) {
Chris Lattner82625602007-01-24 02:26:21 +0000621 if (i != NumFields-1 || // ... that the last member ...
622 Record->getKind() != Decl::Struct || // ... of a structure ...
Chris Lattner720a0542007-01-25 00:44:24 +0000623 !isa<ArrayType>(FDTy)) { //... may have incomplete array type.
Chris Lattner82625602007-01-24 02:26:21 +0000624 Diag(FD->getLocation(), diag::err_field_incomplete, FD->getName());
625 delete FD;
626 continue;
627 }
Chris Lattner720a0542007-01-25 00:44:24 +0000628 if (NumNamedMembers < 1) { //... must have more than named member ...
Chris Lattner82625602007-01-24 02:26:21 +0000629 Diag(FD->getLocation(), diag::err_flexible_array_empty_struct,
630 FD->getName());
631 delete FD;
632 continue;
633 }
Chris Lattner720a0542007-01-25 00:44:24 +0000634
635 // Okay, we have a legal flexible array member at the end of the struct.
Chris Lattner41943152007-01-25 04:52:46 +0000636 Record->setHasFlexibleArrayMember(true);
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000637 }
Chris Lattner720a0542007-01-25 00:44:24 +0000638
639
640 /// C99 6.7.2.1p2 - a struct ending in a flexible array member cannot be the
641 /// field of another structure or the element of an array.
642 if (RecordType *FDTTy = dyn_cast<RecordType>(FDTy)) {
643 if (FDTTy->getDecl()->hasFlexibleArrayMember()) {
644 // If this is a member of a union, then entire union becomes "flexible".
645 if (Record->getKind() == Decl::Union) {
Chris Lattner41943152007-01-25 04:52:46 +0000646 Record->setHasFlexibleArrayMember(true);
Chris Lattner720a0542007-01-25 00:44:24 +0000647 } else {
648 // If this is a struct/class and this is not the last element, reject
649 // it. Note that GCC supports variable sized arrays in the middle of
650 // structures.
651 if (i != NumFields-1) {
652 Diag(FD->getLocation(), diag::err_variable_sized_type_in_struct,
653 FD->getName());
654 delete FD;
655 continue;
656 }
657
658 // We support flexible arrays at the end of structs in other structs
659 // as an extension.
660 Diag(FD->getLocation(), diag::ext_flexible_array_in_struct,
661 FD->getName());
Chris Lattner41943152007-01-25 04:52:46 +0000662 Record->setHasFlexibleArrayMember(true);
Chris Lattner720a0542007-01-25 00:44:24 +0000663 }
664 }
665 }
666
Chris Lattner82625602007-01-24 02:26:21 +0000667 // Keep track of the number of named members.
Chris Lattnere5a66562007-01-25 22:48:42 +0000668 if (IdentifierInfo *II = FD->getIdentifier()) {
669 // Detect duplicate member names.
Chris Lattnerbaf33662007-01-27 02:14:08 +0000670 if (!FieldIDs.insert(II)) {
Chris Lattnere5a66562007-01-25 22:48:42 +0000671 Diag(FD->getLocation(), diag::err_duplicate_member, II->getName());
672 // Find the previous decl.
673 SourceLocation PrevLoc;
674 for (unsigned i = 0, e = RecFields.size(); ; ++i) {
675 assert(i != e && "Didn't find previous def!");
676 if (RecFields[i]->getIdentifier() == II) {
677 PrevLoc = RecFields[i]->getLocation();
678 break;
679 }
680 }
681 Diag(PrevLoc, diag::err_previous_definition);
682 delete FD;
683 continue;
684 }
Chris Lattner82625602007-01-24 02:26:21 +0000685 ++NumNamedMembers;
Chris Lattnere5a66562007-01-25 22:48:42 +0000686 }
Chris Lattner41943152007-01-25 04:52:46 +0000687
688 // Remember good fields.
689 RecFields.push_back(FD);
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000690 }
Chris Lattner82625602007-01-24 02:26:21 +0000691
692
693 // Okay, we successfully defined 'Record'.
Chris Lattner41943152007-01-25 04:52:46 +0000694 Record->defineBody(&RecFields[0], RecFields.size());
Chris Lattner1300fb92007-01-23 23:42:53 +0000695}
696
Chris Lattnerc1915e22007-01-25 07:29:02 +0000697Sema::DeclTy *Sema::ParseEnumConstant(Scope *S, DeclTy *EnumDeclX,
698 SourceLocation IdLoc, IdentifierInfo *Id,
699 SourceLocation EqualLoc, ExprTy *Val) {
700 EnumDecl *TheEnumDecl = cast<EnumDecl>(static_cast<Decl*>(EnumDeclX));
Chris Lattner8116d1b2007-01-25 22:38:29 +0000701
702 // Verify that there isn't already something declared with this name in this
703 // scope.
Chris Lattner9561a0b2007-01-28 08:20:04 +0000704 if (Decl *PrevDecl = LookupScopedDecl(Id, Decl::IDNS_Ordinary, IdLoc, S)) {
Chris Lattner8116d1b2007-01-25 22:38:29 +0000705 if (S->isDeclScope(PrevDecl)) {
706 if (isa<EnumConstantDecl>(PrevDecl))
707 Diag(IdLoc, diag::err_redefinition_of_enumerator, Id->getName());
708 else
709 Diag(IdLoc, diag::err_redefinition, Id->getName());
710 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
711 return 0;
712 }
713 }
714
Chris Lattnerc1915e22007-01-25 07:29:02 +0000715 TypeRef Ty = Context.getTagDeclType(TheEnumDecl);
Chris Lattner8116d1b2007-01-25 22:38:29 +0000716 EnumConstantDecl *New = new EnumConstantDecl(IdLoc, Id, Ty);
717
718 // Register this decl in the current scope stack.
719 New->setNext(Id->getFETokenInfo<Decl>());
720 Id->setFETokenInfo(New);
721 S->AddDecl(New);
722 return New;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000723}
724
725void Sema::ParseEnumBody(SourceLocation EnumLoc, DeclTy *EnumDeclX,
726 DeclTy **Elements, unsigned NumElements) {
727 EnumDecl *Enum = cast<EnumDecl>(static_cast<Decl*>(EnumDeclX));
728 assert(!Enum->isDefinition() && "Enum redefinitions can't reach here");
729
730 // Verify that all the values are okay.
731 SmallVector<EnumConstantDecl*, 32> Values;
732 for (unsigned i = 0; i != NumElements; ++i) {
733 EnumConstantDecl *ECD =
734 cast_or_null<EnumConstantDecl>(static_cast<Decl*>(Elements[i]));
735 if (!ECD) continue; // Already issued a diagnostic.
736
737 Values.push_back(ECD);
738 }
739
740 Enum->defineElements(&Values[0], Values.size());
741}
Chris Lattner1300fb92007-01-23 23:42:53 +0000742
Steve Naroff26c8ea52007-03-21 21:08:52 +0000743void Sema::AddTopLevelDecl(Decl *current, Decl *last) {
744 if (!current) return;
745
746 // If this is a top-level decl that is chained to some other (e.g. int A,B,C;)
747 // remember this in the LastInGroupList list.
748 if (last) {
749 LastInGroupList.push_back((Decl*)last);
750 }
751}