blob: e1848c40c8e3c5c7b6450a133ff4dfd9c8888f1b [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 Lattnerb6738ec2007-01-28 00:38:24 +000099
Chris Lattner9561a0b2007-01-28 08:20:04 +0000100 // If this is a builtin on this (or all) targets, create the decl.
101 if (unsigned BuiltinID = II->getBuiltinID())
102 return LazilyCreateBuiltin(II, BuiltinID, S);
103 }
Chris Lattner18b19622007-01-22 07:39:13 +0000104 return 0;
105}
106
Chris Lattner9561a0b2007-01-28 08:20:04 +0000107/// LazilyCreateBuiltin - The specified Builtin-ID was first used at file scope.
108/// lazily create a decl for it.
109Decl *Sema::LazilyCreateBuiltin(IdentifierInfo *II, unsigned bid, Scope *S) {
110 Builtin::ID BID = (Builtin::ID)bid;
111
Chris Lattner10a5b382007-01-29 05:24:35 +0000112 TypeRef R = Context.BuiltinInfo.GetBuiltinType(BID, Context);
Chris Lattner9561a0b2007-01-28 08:20:04 +0000113 FunctionDecl *New = new FunctionDecl(SourceLocation(), II, R);
114
115 // Find translation-unit scope to insert this function into.
116 while (S->getParent())
117 S = S->getParent();
118 S->AddDecl(New);
119
120 // Add this decl to the end of the identifier info.
121 if (Decl *LastDecl = II->getFETokenInfo<Decl>()) {
122 // Scan until we find the last (outermost) decl in the id chain.
123 while (LastDecl->getNext())
124 LastDecl = LastDecl->getNext();
125 // Insert before (outside) it.
126 LastDecl->setNext(New);
127 } else {
128 II->setFETokenInfo(New);
129 }
130 // Make sure clients iterating over decls see this.
131 LastInGroupList.push_back(New);
132
133 return New;
134}
135
Chris Lattner01564d92007-01-27 19:27:06 +0000136/// MergeTypeDefDecl - We just parsed a typedef 'New' which has the same name
137/// and scope as a previous declaration 'Old'. Figure out how to resolve this
138/// situation, merging decls or emitting diagnostics as appropriate.
139///
Chris Lattnerc511efb2007-01-27 19:32:14 +0000140TypedefDecl *Sema::MergeTypeDefDecl(TypedefDecl *New, Decl *OldD) {
141 // Verify the old decl was also a typedef.
142 TypedefDecl *Old = dyn_cast<TypedefDecl>(OldD);
143 if (!Old) {
144 Diag(New->getLocation(), diag::err_redefinition_different_kind,
145 New->getName());
146 Diag(OldD->getLocation(), diag::err_previous_definition);
147 return New;
148 }
149
Chris Lattner01564d92007-01-27 19:27:06 +0000150 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
151 // TODO: This is totally simplistic. It should handle merging functions
152 // together etc, merging extern int X; int X; ...
153 Diag(New->getLocation(), diag::err_redefinition, New->getName());
154 Diag(Old->getLocation(), diag::err_previous_definition);
155 return New;
156}
157
158/// MergeFunctionDecl - We just parsed a function 'New' which has the same name
159/// and scope as a previous declaration 'Old'. Figure out how to resolve this
160/// situation, merging decls or emitting diagnostics as appropriate.
161///
Chris Lattnerc511efb2007-01-27 19:32:14 +0000162FunctionDecl *Sema::MergeFunctionDecl(FunctionDecl *New, Decl *OldD) {
163 // Verify the old decl was also a function.
164 FunctionDecl *Old = dyn_cast<FunctionDecl>(OldD);
165 if (!Old) {
166 Diag(New->getLocation(), diag::err_redefinition_different_kind,
167 New->getName());
168 Diag(OldD->getLocation(), diag::err_previous_definition);
169 return New;
170 }
171
Chris Lattnerefe4aea2007-01-27 19:35:39 +0000172 // This is not right, but it's a start. If 'Old' is a function prototype with
173 // the same type as 'New', silently allow this. FIXME: We should link up decl
174 // objects here.
175 if (Old->getBody() == 0 && Old->getType() == New->getType()) {
176 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///
Chris Lattnerc511efb2007-01-27 19:32:14 +0000191VarDecl *Sema::MergeVarDecl(VarDecl *New, Decl *OldD) {
192 // Verify the old decl was also a variable.
193 VarDecl *Old = dyn_cast<VarDecl>(OldD);
194 if (!Old) {
195 Diag(New->getLocation(), diag::err_redefinition_different_kind,
196 New->getName());
197 Diag(OldD->getLocation(), diag::err_previous_definition);
198 return New;
199 }
200
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
Chris Lattnerb6738ec2007-01-28 00:38:24 +0000209/// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with
210/// no declarator (e.g. "struct foo;") is parsed.
211Sema::DeclTy *Sema::ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS) {
212 // TODO: emit error on 'int;' or 'const enum foo;'.
213 // TODO: emit error on 'typedef int;'
214 // if (!DS.isMissingDeclaratorOk()) Diag(...);
215
216 return 0;
217}
218
Chris Lattner18b19622007-01-22 07:39:13 +0000219
Chris Lattnere168f762006-11-10 05:29:30 +0000220Action::DeclTy *
221Sema::ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init,
222 DeclTy *LastInGroup) {
223 IdentifierInfo *II = D.getIdentifier();
Chris Lattner302b4be2006-11-19 02:31:38 +0000224
Chris Lattner01564d92007-01-27 19:27:06 +0000225 // See if this is a redefinition of a variable in the same scope.
Chris Lattner9561a0b2007-01-28 08:20:04 +0000226 Decl *PrevDecl = LookupScopedDecl(II, Decl::IDNS_Ordinary,
227 D.getIdentifierLoc(), S);
Chris Lattner01564d92007-01-27 19:27:06 +0000228 if (!S->isDeclScope(PrevDecl))
229 PrevDecl = 0; // If in outer scope, it isn't the same thing.
230
Chris Lattnere168f762006-11-10 05:29:30 +0000231 Decl *New;
Chris Lattner01a7c532007-01-25 23:09:03 +0000232 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
Chris Lattner01564d92007-01-27 19:27:06 +0000233 TypedefDecl *NewTD = ParseTypedefDecl(S, D);
234 if (!NewTD) return 0;
235
236 // Merge the decl with the existing one if appropriate.
237 if (PrevDecl) {
238 NewTD = MergeTypeDefDecl(NewTD, PrevDecl);
239 if (NewTD == 0) return 0;
240 }
241 New = NewTD;
Chris Lattner01a7c532007-01-25 23:09:03 +0000242 } else if (D.isFunctionDeclarator()) {
243 TypeRef R = GetTypeForDeclarator(D, S);
244 if (R.isNull()) return 0;
Chris Lattner01564d92007-01-27 19:27:06 +0000245
246 FunctionDecl *NewFD = new FunctionDecl(D.getIdentifierLoc(), II, R);
247
248 // Merge the decl with the existing one if appropriate.
249 if (PrevDecl) {
250 NewFD = MergeFunctionDecl(NewFD, PrevDecl);
251 if (NewFD == 0) return 0;
252 }
253 New = NewFD;
Chris Lattner01a7c532007-01-25 23:09:03 +0000254 } else {
255 TypeRef R = GetTypeForDeclarator(D, S);
256 if (R.isNull()) return 0;
Chris Lattner01564d92007-01-27 19:27:06 +0000257 VarDecl *NewVD = new VarDecl(D.getIdentifierLoc(), II, R);
258
259 // Merge the decl with the existing one if appropriate.
260 if (PrevDecl) {
261 NewVD = MergeVarDecl(NewVD, PrevDecl);
262 if (NewVD == 0) return 0;
263 }
264 New = NewVD;
Chris Lattner01a7c532007-01-25 23:09:03 +0000265 }
Chris Lattner302b4be2006-11-19 02:31:38 +0000266
267
Chris Lattnere168f762006-11-10 05:29:30 +0000268 // If this has an identifier, add it to the scope stack.
269 if (II) {
Chris Lattner18b19622007-01-22 07:39:13 +0000270 New->setNext(II->getFETokenInfo<Decl>());
Chris Lattnere168f762006-11-10 05:29:30 +0000271 II->setFETokenInfo(New);
Chris Lattner99d31772007-01-21 22:37:37 +0000272 S->AddDecl(New);
Chris Lattnere168f762006-11-10 05:29:30 +0000273 }
274
275 // If this is a top-level decl that is chained to some other (e.g. int A,B,C;)
276 // remember this in the LastInGroupList list.
277 if (LastInGroup && S->getParent() == 0)
278 LastInGroupList.push_back((Decl*)LastInGroup);
279
280 return New;
281}
282
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000283VarDecl *
284Sema::ParseParamDeclarator(DeclaratorChunk &FTI, unsigned ArgNo,
285 Scope *FnScope) {
286 const DeclaratorChunk::ParamInfo &PI = FTI.Fun.ArgInfo[ArgNo];
Chris Lattner200bdc32006-11-19 02:43:37 +0000287
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000288 IdentifierInfo *II = PI.Ident;
Chris Lattnerc284e9b2007-01-23 05:14:32 +0000289 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
290 // Can this happen for params? We already checked that they don't conflict
291 // among each other. Here they can only shadow globals, which is ok.
Chris Lattner9561a0b2007-01-28 08:20:04 +0000292 if (Decl *PrevDecl = LookupScopedDecl(II, Decl::IDNS_Ordinary,
293 PI.IdentLoc, FnScope)) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000294
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000295 }
296
Chris Lattner38047f92007-01-27 06:24:01 +0000297 VarDecl *New = new VarDecl(PI.IdentLoc, II,
298 TypeRef::getFromOpaquePtr(PI.TypeInfo));
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000299
300 // If this has an identifier, add it to the scope stack.
301 if (II) {
Chris Lattner18b19622007-01-22 07:39:13 +0000302 New->setNext(II->getFETokenInfo<Decl>());
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000303 II->setFETokenInfo(New);
Chris Lattner99d31772007-01-21 22:37:37 +0000304 FnScope->AddDecl(New);
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000305 }
Chris Lattner229ce602006-11-21 01:21:07 +0000306
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000307 return New;
308}
309
310
311Sema::DeclTy *Sema::ParseStartOfFunctionDef(Scope *FnBodyScope, Declarator &D) {
Chris Lattner229ce602006-11-21 01:21:07 +0000312 assert(CurFunctionDecl == 0 && "Function parsing confused");
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000313 assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function &&
314 "Not a function declarator!");
315 DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
316
317 // Verify 6.9.1p6: 'every identifier in the identifier list shall be declared'
318 // for a K&R function.
319 if (!FTI.hasPrototype) {
320 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
321 if (FTI.ArgInfo[i].TypeInfo == 0) {
322 Diag(FTI.ArgInfo[i].IdentLoc, diag::err_param_not_declared,
323 FTI.ArgInfo[i].Ident->getName());
324 // Implicitly declare the argument as type 'int' for lack of a better
325 // type.
326 FTI.ArgInfo[i].TypeInfo = Context.IntTy.getAsOpaquePtr();
327 }
328 }
329
330 // Since this is a function definition, act as though we have information
331 // about the arguments.
332 FTI.hasPrototype = true;
Chris Lattner2114d5e2006-12-04 07:40:24 +0000333 } else {
334 // FIXME: Diagnose arguments without names in C.
335
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000336 }
337
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000338 Scope *GlobalScope = FnBodyScope->getParent();
339
340 FunctionDecl *FD =
341 static_cast<FunctionDecl*>(ParseDeclarator(GlobalScope, D, 0, 0));
Chris Lattner229ce602006-11-21 01:21:07 +0000342 CurFunctionDecl = FD;
Chris Lattner2114d5e2006-12-04 07:40:24 +0000343
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000344 // Create Decl objects for each parameter, adding them to the FunctionDecl.
345 SmallVector<VarDecl*, 16> Params;
Chris Lattnerf61c8a82007-01-21 19:04:43 +0000346
347 // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs function that takes
348 // no arguments, not a function that takes a single void argument.
349 if (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 &&
350 FTI.ArgInfo[0].TypeInfo == Context.VoidTy.getAsOpaquePtr()) {
Chris Lattner99d31772007-01-21 22:37:37 +0000351 // empty arg list, don't push any params.
Chris Lattnerf61c8a82007-01-21 19:04:43 +0000352 } else {
353 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i)
354 Params.push_back(ParseParamDeclarator(D.getTypeObject(0), i,FnBodyScope));
355 }
Chris Lattner2114d5e2006-12-04 07:40:24 +0000356
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000357 FD->setParams(&Params[0], Params.size());
Chris Lattner2114d5e2006-12-04 07:40:24 +0000358
Chris Lattnere168f762006-11-10 05:29:30 +0000359 return FD;
360}
361
Chris Lattner229ce602006-11-21 01:21:07 +0000362Sema::DeclTy *Sema::ParseFunctionDefBody(DeclTy *D, StmtTy *Body) {
363 FunctionDecl *FD = static_cast<FunctionDecl*>(D);
364 FD->setBody((Stmt*)Body);
365
366 assert(FD == CurFunctionDecl && "Function parsing confused");
367 CurFunctionDecl = 0;
368 return FD;
369}
370
371
Chris Lattnerac18be92006-11-20 06:49:47 +0000372/// ImplicitlyDefineFunction - An undeclared identifier was used in a function
373/// call, forming a call to an implicitly defined function (per C99 6.5.1p2).
374Decl *Sema::ImplicitlyDefineFunction(SourceLocation Loc, IdentifierInfo &II,
375 Scope *S) {
376 if (getLangOptions().C99) // Extension in C99.
377 Diag(Loc, diag::ext_implicit_function_decl, II.getName());
378 else // Legal in C90, but warn about it.
379 Diag(Loc, diag::warn_implicit_function_decl, II.getName());
380
381 // FIXME: handle stuff like:
382 // void foo() { extern float X(); }
383 // void bar() { X(); } <-- implicit decl for X in another scope.
384
385 // Set a Declarator for the implicit definition: int foo();
Chris Lattner353f5742006-11-28 04:50:12 +0000386 const char *Dummy;
Chris Lattnerac18be92006-11-20 06:49:47 +0000387 DeclSpec DS;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000388 bool Error = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, Dummy);
Chris Lattnerb055f2d2007-02-11 08:19:57 +0000389 Error = Error; // Silence warning.
Chris Lattner353f5742006-11-28 04:50:12 +0000390 assert(!Error && "Error setting up implicit decl!");
Chris Lattnerac18be92006-11-20 06:49:47 +0000391 Declarator D(DS, Declarator::BlockContext);
Chris Lattnercbc426d2006-12-02 06:43:02 +0000392 D.AddTypeInfo(DeclaratorChunk::getFunction(false, false, 0, 0, Loc));
Chris Lattnerac18be92006-11-20 06:49:47 +0000393 D.SetIdentifier(&II, Loc);
394
Chris Lattner62d2e662007-01-28 00:21:37 +0000395 // Find translation-unit scope to insert this function into.
396 while (S->getParent())
397 S = S->getParent();
Chris Lattnerac18be92006-11-20 06:49:47 +0000398
Chris Lattner62d2e662007-01-28 00:21:37 +0000399 return static_cast<Decl*>(ParseDeclarator(S, D, 0, 0));
Chris Lattnerac18be92006-11-20 06:49:47 +0000400}
401
Chris Lattner302b4be2006-11-19 02:31:38 +0000402
Chris Lattner01564d92007-01-27 19:27:06 +0000403TypedefDecl *Sema::ParseTypedefDecl(Scope *S, Declarator &D) {
Chris Lattnerda8aa7b2006-11-19 23:12:30 +0000404 assert(D.getIdentifier() && "Wrong callback for declspec withotu declarator");
Chris Lattner302b4be2006-11-19 02:31:38 +0000405
Chris Lattner5ca17df2006-11-19 23:32:49 +0000406 TypeRef T = GetTypeForDeclarator(D, S);
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000407 if (T.isNull()) return 0;
408
Chris Lattner18b19622007-01-22 07:39:13 +0000409 // Scope manipulation handled by caller.
410 return new TypedefDecl(D.getIdentifierLoc(), D.getIdentifier(), T);
Chris Lattnere168f762006-11-10 05:29:30 +0000411}
412
Chris Lattner18b19622007-01-22 07:39:13 +0000413
Chris Lattner1300fb92007-01-23 23:42:53 +0000414/// ParseTag - This is invoked when we see 'struct foo' or 'struct {'. In the
415/// former case, Name will be non-null. In the later case, Name will be null.
416/// TagType indicates what kind of tag this is. TK indicates whether this is a
417/// reference/declaration/definition of a tag.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000418Sema::DeclTy *Sema::ParseTag(Scope *S, unsigned TagType, TagKind TK,
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000419 SourceLocation KWLoc, IdentifierInfo *Name,
420 SourceLocation NameLoc) {
Chris Lattner8799cf22007-01-23 01:57:16 +0000421 // If this is a use of an existing tag, it must have a name.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000422 assert((Name != 0 || TK == TK_Definition) &&
423 "Nameless record must be a definition!");
Chris Lattner8799cf22007-01-23 01:57:16 +0000424
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000425 Decl::Kind Kind;
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000426 switch (TagType) {
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000427 default: assert(0 && "Unknown tag type!");
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000428 case DeclSpec::TST_struct: Kind = Decl::Struct; break;
429 case DeclSpec::TST_union: Kind = Decl::Union; break;
430//case DeclSpec::TST_class: Kind = Decl::Class; break;
431 case DeclSpec::TST_enum: Kind = Decl::Enum; break;
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000432 }
Chris Lattner7e783a12007-01-23 02:05:42 +0000433
Chris Lattner18b19622007-01-22 07:39:13 +0000434 // If this is a named struct, check to see if there was a previous forward
435 // declaration or definition.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000436 if (TagDecl *PrevDecl =
Chris Lattner9561a0b2007-01-28 08:20:04 +0000437 dyn_cast_or_null<TagDecl>(LookupScopedDecl(Name, Decl::IDNS_Tag,
438 NameLoc, S))) {
Chris Lattner8799cf22007-01-23 01:57:16 +0000439
440 // If this is a use of a previous tag, or if the tag is already declared in
441 // the same scope (so that the definition/declaration completes or
442 // rementions the tag), reuse the decl.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000443 if (TK == TK_Reference || S->isDeclScope(PrevDecl)) {
Chris Lattner7e783a12007-01-23 02:05:42 +0000444 // Make sure that this wasn't declared as an enum and now used as a struct
445 // or something similar.
446 if (PrevDecl->getKind() != Kind) {
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000447 Diag(KWLoc, diag::err_use_with_wrong_tag, Name->getName());
Chris Lattner7e783a12007-01-23 02:05:42 +0000448 Diag(PrevDecl->getLocation(), diag::err_previous_use);
449 }
Chris Lattner7b9ace62007-01-23 20:11:08 +0000450
451 // If this is a use or a forward declaration, we're good.
452 if (TK != TK_Definition)
453 return PrevDecl;
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000454
Chris Lattner7b9ace62007-01-23 20:11:08 +0000455 // Diagnose attempts to redefine a tag.
456 if (PrevDecl->isDefinition()) {
457 Diag(NameLoc, diag::err_redefinition, Name->getName());
458 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
459 // If this is a redefinition, recover by making this struct be
460 // anonymous, which will make any later references get the previous
461 // definition.
462 Name = 0;
463 } else {
464 // Okay, this is definition of a previously declared or referenced tag.
465 // Move the location of the decl to be the definition site.
466 PrevDecl->setLocation(NameLoc);
Chris Lattner7b9ace62007-01-23 20:11:08 +0000467 return PrevDecl;
468 }
Chris Lattner8799cf22007-01-23 01:57:16 +0000469 }
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000470 // If we get here, this is a definition of a new struct type in a nested
471 // scope, e.g. "struct foo; void bar() { struct foo; }", just create a new
472 // type.
Chris Lattner18b19622007-01-22 07:39:13 +0000473 }
474
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000475 // If there is an identifier, use the location of the identifier as the
476 // location of the decl, otherwise use the location of the struct/union
477 // keyword.
478 SourceLocation Loc = NameLoc.isValid() ? NameLoc : KWLoc;
479
Chris Lattner18b19622007-01-22 07:39:13 +0000480 // Otherwise, if this is the first time we've seen this tag, create the decl.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000481 TagDecl *New;
Chris Lattner720a0542007-01-25 00:44:24 +0000482 switch (Kind) {
483 default: assert(0 && "Unknown tag kind!");
Chris Lattner5f521502007-01-25 06:27:24 +0000484 case Decl::Enum:
485 New = new EnumDecl(Loc, Name);
486 // If this is an undefined enum, warn.
Chris Lattnerc1915e22007-01-25 07:29:02 +0000487 if (TK != TK_Definition) Diag(Loc, diag::ext_forward_ref_enum);
Chris Lattner5f521502007-01-25 06:27:24 +0000488 break;
Chris Lattner720a0542007-01-25 00:44:24 +0000489 case Decl::Union:
490 case Decl::Struct:
491 case Decl::Class:
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000492 New = new RecordDecl(Kind, Loc, Name);
Chris Lattner720a0542007-01-25 00:44:24 +0000493 break;
494 }
Chris Lattner18b19622007-01-22 07:39:13 +0000495
496 // If this has an identifier, add it to the scope stack.
497 if (Name) {
498 New->setNext(Name->getFETokenInfo<Decl>());
499 Name->setFETokenInfo(New);
500 S->AddDecl(New);
501 }
502
503 return New;
504}
Chris Lattner1300fb92007-01-23 23:42:53 +0000505
506/// ParseField - Each field of a struct/union/class is passed into this in order
507/// to create a FieldDecl object for it.
508Sema::DeclTy *Sema::ParseField(Scope *S, DeclTy *TagDecl,
509 SourceLocation DeclStart,
510 Declarator &D, ExprTy *BitfieldWidth) {
511 IdentifierInfo *II = D.getIdentifier();
512 Expr *BitWidth = (Expr*)BitfieldWidth;
513
514 SourceLocation Loc = DeclStart;
515 if (II) Loc = D.getIdentifierLoc();
516
Chris Lattner62d2e662007-01-28 00:21:37 +0000517 // FIXME: Unnamed fields can be handled in various different ways, for
518 // example, unnamed unions inject all members into the struct namespace!
519
520
Chris Lattner1300fb92007-01-23 23:42:53 +0000521 if (BitWidth) {
522 // TODO: Validate.
Chris Lattner01a7c532007-01-25 23:09:03 +0000523 printf("WARNING: BITFIELDS IGNORED!\n");
Chris Lattner1300fb92007-01-23 23:42:53 +0000524
525 // 6.7.2.1p3
526 // 6.7.2.1p4
527
528 } else {
529 // Not a bitfield.
530
531 // validate II.
532
533 }
534
Chris Lattner01a7c532007-01-25 23:09:03 +0000535 TypeRef T = GetTypeForDeclarator(D, S);
536 if (T.isNull()) return 0;
537
538 return new FieldDecl(Loc, II, T);
Chris Lattner1300fb92007-01-23 23:42:53 +0000539}
540
541void Sema::ParseRecordBody(SourceLocation RecLoc, DeclTy *RecDecl,
542 DeclTy **Fields, unsigned NumFields) {
Chris Lattnerc1915e22007-01-25 07:29:02 +0000543 RecordDecl *Record = cast<RecordDecl>(static_cast<Decl*>(RecDecl));
Chris Lattner1300fb92007-01-23 23:42:53 +0000544 if (Record->isDefinition()) {
545 // Diagnose code like:
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000546 // struct S { struct S {} X; };
Chris Lattner1300fb92007-01-23 23:42:53 +0000547 // We discover this when we complete the outer S. Reject and ignore the
548 // outer S.
549 Diag(Record->getLocation(), diag::err_nested_redefinition,
550 Record->getKindName());
551 Diag(RecLoc, diag::err_previous_definition);
552 return;
553 }
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000554
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000555 // Verify that all the fields are okay.
Chris Lattner82625602007-01-24 02:26:21 +0000556 unsigned NumNamedMembers = 0;
Chris Lattner41943152007-01-25 04:52:46 +0000557 SmallVector<Decl*, 32> RecFields;
Chris Lattnere5a66562007-01-25 22:48:42 +0000558 SmallSet<const IdentifierInfo*, 32> FieldIDs;
559
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000560 for (unsigned i = 0; i != NumFields; ++i) {
561 FieldDecl *FD = cast_or_null<FieldDecl>(static_cast<Decl*>(Fields[i]));
562 if (!FD) continue; // Already issued a diagnostic.
Chris Lattner720a0542007-01-25 00:44:24 +0000563
564 // Get the type for the field.
565 Type *FDTy = FD->getType()->getCanonicalType();
566
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000567 // C99 6.7.2.1p2 - A field may not be a function type.
Chris Lattner720a0542007-01-25 00:44:24 +0000568 if (isa<FunctionType>(FDTy)) {
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000569 Diag(FD->getLocation(), diag::err_field_declared_as_function,
570 FD->getName());
Chris Lattner82625602007-01-24 02:26:21 +0000571 delete FD;
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000572 continue;
573 }
574
Chris Lattner82625602007-01-24 02:26:21 +0000575 // C99 6.7.2.1p2 - A field may not be an incomplete type except...
Chris Lattner720a0542007-01-25 00:44:24 +0000576 if (FDTy->isIncompleteType()) {
Chris Lattner82625602007-01-24 02:26:21 +0000577 if (i != NumFields-1 || // ... that the last member ...
578 Record->getKind() != Decl::Struct || // ... of a structure ...
Chris Lattner720a0542007-01-25 00:44:24 +0000579 !isa<ArrayType>(FDTy)) { //... may have incomplete array type.
Chris Lattner82625602007-01-24 02:26:21 +0000580 Diag(FD->getLocation(), diag::err_field_incomplete, FD->getName());
581 delete FD;
582 continue;
583 }
Chris Lattner720a0542007-01-25 00:44:24 +0000584 if (NumNamedMembers < 1) { //... must have more than named member ...
Chris Lattner82625602007-01-24 02:26:21 +0000585 Diag(FD->getLocation(), diag::err_flexible_array_empty_struct,
586 FD->getName());
587 delete FD;
588 continue;
589 }
Chris Lattner720a0542007-01-25 00:44:24 +0000590
591 // Okay, we have a legal flexible array member at the end of the struct.
Chris Lattner41943152007-01-25 04:52:46 +0000592 Record->setHasFlexibleArrayMember(true);
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000593 }
Chris Lattner720a0542007-01-25 00:44:24 +0000594
595
596 /// C99 6.7.2.1p2 - a struct ending in a flexible array member cannot be the
597 /// field of another structure or the element of an array.
598 if (RecordType *FDTTy = dyn_cast<RecordType>(FDTy)) {
599 if (FDTTy->getDecl()->hasFlexibleArrayMember()) {
600 // If this is a member of a union, then entire union becomes "flexible".
601 if (Record->getKind() == Decl::Union) {
Chris Lattner41943152007-01-25 04:52:46 +0000602 Record->setHasFlexibleArrayMember(true);
Chris Lattner720a0542007-01-25 00:44:24 +0000603 } else {
604 // If this is a struct/class and this is not the last element, reject
605 // it. Note that GCC supports variable sized arrays in the middle of
606 // structures.
607 if (i != NumFields-1) {
608 Diag(FD->getLocation(), diag::err_variable_sized_type_in_struct,
609 FD->getName());
610 delete FD;
611 continue;
612 }
613
614 // We support flexible arrays at the end of structs in other structs
615 // as an extension.
616 Diag(FD->getLocation(), diag::ext_flexible_array_in_struct,
617 FD->getName());
Chris Lattner41943152007-01-25 04:52:46 +0000618 Record->setHasFlexibleArrayMember(true);
Chris Lattner720a0542007-01-25 00:44:24 +0000619 }
620 }
621 }
622
Chris Lattner82625602007-01-24 02:26:21 +0000623 // Keep track of the number of named members.
Chris Lattnere5a66562007-01-25 22:48:42 +0000624 if (IdentifierInfo *II = FD->getIdentifier()) {
625 // Detect duplicate member names.
Chris Lattnerbaf33662007-01-27 02:14:08 +0000626 if (!FieldIDs.insert(II)) {
Chris Lattnere5a66562007-01-25 22:48:42 +0000627 Diag(FD->getLocation(), diag::err_duplicate_member, II->getName());
628 // Find the previous decl.
629 SourceLocation PrevLoc;
630 for (unsigned i = 0, e = RecFields.size(); ; ++i) {
631 assert(i != e && "Didn't find previous def!");
632 if (RecFields[i]->getIdentifier() == II) {
633 PrevLoc = RecFields[i]->getLocation();
634 break;
635 }
636 }
637 Diag(PrevLoc, diag::err_previous_definition);
638 delete FD;
639 continue;
640 }
Chris Lattner82625602007-01-24 02:26:21 +0000641 ++NumNamedMembers;
Chris Lattnere5a66562007-01-25 22:48:42 +0000642 }
Chris Lattner41943152007-01-25 04:52:46 +0000643
644 // Remember good fields.
645 RecFields.push_back(FD);
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000646 }
Chris Lattner82625602007-01-24 02:26:21 +0000647
648
649 // Okay, we successfully defined 'Record'.
Chris Lattner41943152007-01-25 04:52:46 +0000650 Record->defineBody(&RecFields[0], RecFields.size());
Chris Lattner1300fb92007-01-23 23:42:53 +0000651}
652
Chris Lattnerc1915e22007-01-25 07:29:02 +0000653Sema::DeclTy *Sema::ParseEnumConstant(Scope *S, DeclTy *EnumDeclX,
654 SourceLocation IdLoc, IdentifierInfo *Id,
655 SourceLocation EqualLoc, ExprTy *Val) {
656 EnumDecl *TheEnumDecl = cast<EnumDecl>(static_cast<Decl*>(EnumDeclX));
Chris Lattner8116d1b2007-01-25 22:38:29 +0000657
658 // Verify that there isn't already something declared with this name in this
659 // scope.
Chris Lattner9561a0b2007-01-28 08:20:04 +0000660 if (Decl *PrevDecl = LookupScopedDecl(Id, Decl::IDNS_Ordinary, IdLoc, S)) {
Chris Lattner8116d1b2007-01-25 22:38:29 +0000661 if (S->isDeclScope(PrevDecl)) {
662 if (isa<EnumConstantDecl>(PrevDecl))
663 Diag(IdLoc, diag::err_redefinition_of_enumerator, Id->getName());
664 else
665 Diag(IdLoc, diag::err_redefinition, Id->getName());
666 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
667 return 0;
668 }
669 }
670
Chris Lattnerc1915e22007-01-25 07:29:02 +0000671 TypeRef Ty = Context.getTagDeclType(TheEnumDecl);
Chris Lattner8116d1b2007-01-25 22:38:29 +0000672 EnumConstantDecl *New = new EnumConstantDecl(IdLoc, Id, Ty);
673
674 // Register this decl in the current scope stack.
675 New->setNext(Id->getFETokenInfo<Decl>());
676 Id->setFETokenInfo(New);
677 S->AddDecl(New);
678 return New;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000679}
680
681void Sema::ParseEnumBody(SourceLocation EnumLoc, DeclTy *EnumDeclX,
682 DeclTy **Elements, unsigned NumElements) {
683 EnumDecl *Enum = cast<EnumDecl>(static_cast<Decl*>(EnumDeclX));
684 assert(!Enum->isDefinition() && "Enum redefinitions can't reach here");
685
686 // Verify that all the values are okay.
687 SmallVector<EnumConstantDecl*, 32> Values;
688 for (unsigned i = 0; i != NumElements; ++i) {
689 EnumConstantDecl *ECD =
690 cast_or_null<EnumConstantDecl>(static_cast<Decl*>(Elements[i]));
691 if (!ECD) continue; // Already issued a diagnostic.
692
693 Values.push_back(ECD);
694 }
695
696 Enum->defineElements(&Values[0], Values.size());
697}
Chris Lattner1300fb92007-01-23 23:42:53 +0000698