Chris Lattner | 697e5d6 | 2006-11-09 06:32:27 +0000 | [diff] [blame] | 1 | //===--- 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 Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 14 | #include "Sema.h" |
Chris Lattner | 5c5fbcc | 2006-12-03 08:41:30 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 16 | #include "clang/AST/Builtins.h" |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 17 | #include "clang/AST/Decl.h" |
Chris Lattner | 1300fb9 | 2007-01-23 23:42:53 +0000 | [diff] [blame] | 18 | #include "clang/AST/Expr.h" |
Chris Lattner | f84a79c | 2006-11-11 22:59:23 +0000 | [diff] [blame] | 19 | #include "clang/AST/Type.h" |
Steve Naroff | 4555292 | 2007-06-01 21:56:17 +0000 | [diff] [blame] | 20 | #include "clang/AST/Attr.h" |
Chris Lattner | 591a675 | 2006-11-19 23:16:18 +0000 | [diff] [blame] | 21 | #include "clang/Parse/DeclSpec.h" |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 22 | #include "clang/Parse/Scope.h" |
| 23 | #include "clang/Lex/IdentifierTable.h" |
Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 24 | #include "clang/Basic/LangOptions.h" |
Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 25 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | 38047f9 | 2007-01-27 06:24:01 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/SmallSet.h" |
Steve Naroff | d50c88e | 2007-04-05 21:15:20 +0000 | [diff] [blame] | 27 | |
Chris Lattner | 697e5d6 | 2006-11-09 06:32:27 +0000 | [diff] [blame] | 28 | using namespace llvm; |
| 29 | using namespace clang; |
| 30 | |
Steve Naroff | 8eeeb13 | 2007-05-08 21:09:37 +0000 | [diff] [blame] | 31 | // C99: 6.7.5p3: Used by ParseDeclarator/ParseField to make sure we have |
| 32 | // a constant expression of type int with a value greater than zero. |
Chris Lattner | 6046e00 | 2007-06-05 06:39:23 +0000 | [diff] [blame] | 33 | bool Sema::VerifyConstantArrayType(const ArrayType *Array, |
| 34 | SourceLocation DeclLoc) { |
| 35 | const Expr *Size = Array->getSize(); |
| 36 | if (Size == 0) return false; // incomplete type. |
| 37 | |
| 38 | if (!Size->getType()->isIntegerType()) { |
| 39 | Diag(Size->getLocStart(), diag::err_array_size_non_int, |
| 40 | Size->getType().getAsString(), Size->getSourceRange()); |
| 41 | return true; |
Steve Naroff | 8eeeb13 | 2007-05-08 21:09:37 +0000 | [diff] [blame] | 42 | } |
Chris Lattner | 6046e00 | 2007-06-05 06:39:23 +0000 | [diff] [blame] | 43 | |
| 44 | // Verify that the size of the array is an integer constant expr. |
| 45 | SourceLocation Loc; |
| 46 | APSInt SizeVal(32); |
| 47 | if (!Size->isIntegerConstantExpr(SizeVal, &Loc)) { |
| 48 | // FIXME: This emits the diagnostic to enforce 6.7.2.1p8, but the message |
| 49 | // is wrong. It is also wrong for static variables. |
Chris Lattner | 6d9b49a | 2007-06-09 05:56:19 +0000 | [diff] [blame] | 50 | // FIXME: This is also wrong for: |
| 51 | // int sub1(int i, char *pi) { typedef int foo[i]; |
| 52 | // struct bar {foo f1; int f2:3; int f3:4} *p; } |
Chris Lattner | 6046e00 | 2007-06-05 06:39:23 +0000 | [diff] [blame] | 53 | Diag(DeclLoc, diag::err_typecheck_illegal_vla, Size->getSourceRange()); |
| 54 | return true; |
| 55 | } |
| 56 | |
| 57 | // We have a constant expression with an integer type, now make sure |
| 58 | // value greater than zero (C99 6.7.5.2p1). |
| 59 | |
| 60 | // FIXME: This check isn't specific to static VLAs, this should be moved |
| 61 | // elsewhere or replicated. 'int X[-1];' inside a function should emit an |
| 62 | // error. |
| 63 | if (SizeVal.isSigned()) { |
| 64 | APSInt Zero(SizeVal.getBitWidth()); |
| 65 | Zero.setIsUnsigned(false); |
| 66 | if (SizeVal < Zero) { |
| 67 | Diag(DeclLoc, diag::err_typecheck_negative_array_size, |
| 68 | Size->getSourceRange()); |
| 69 | return true; |
| 70 | } else if (SizeVal == 0) { |
| 71 | // GCC accepts zero sized static arrays. |
| 72 | Diag(DeclLoc, diag::err_typecheck_zero_array_size, |
| 73 | Size->getSourceRange()); |
| 74 | } |
| 75 | } |
| 76 | return false; |
Steve Naroff | 8eeeb13 | 2007-05-08 21:09:37 +0000 | [diff] [blame] | 77 | } |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 78 | |
Chris Lattner | 2ebe4bb | 2006-11-20 01:29:42 +0000 | [diff] [blame] | 79 | Sema::DeclTy *Sema::isTypeName(const IdentifierInfo &II, Scope *S) const { |
Chris Lattner | 32d920b | 2007-01-26 02:01:53 +0000 | [diff] [blame] | 80 | return dyn_cast_or_null<TypedefDecl>(II.getFETokenInfo<Decl>()); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Chris Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 83 | void Sema::PopScope(SourceLocation Loc, Scope *S) { |
| 84 | for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end(); |
| 85 | I != E; ++I) { |
Chris Lattner | 99d3177 | 2007-01-21 22:37:37 +0000 | [diff] [blame] | 86 | Decl *D = static_cast<Decl*>(*I); |
Chris Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 87 | assert(D && "This decl didn't get pushed??"); |
Chris Lattner | ff65b6b | 2007-01-23 01:33:16 +0000 | [diff] [blame] | 88 | IdentifierInfo *II = D->getIdentifier(); |
| 89 | if (!II) continue; |
Chris Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 90 | |
Chris Lattner | ff65b6b | 2007-01-23 01:33:16 +0000 | [diff] [blame] | 91 | // Unlink this decl from the identifier. Because the scope contains decls |
| 92 | // in an unordered collection, and because we have multiple identifier |
| 93 | // namespaces (e.g. tag, normal, label),the decl may not be the first entry. |
| 94 | if (II->getFETokenInfo<Decl>() == D) { |
| 95 | // Normal case, no multiple decls in different namespaces. |
| 96 | II->setFETokenInfo(D->getNext()); |
| 97 | } else { |
| 98 | // Scan ahead. There are only three namespaces in C, so this loop can |
| 99 | // never execute more than 3 times. |
| 100 | Decl *SomeDecl = II->getFETokenInfo<Decl>(); |
| 101 | while (SomeDecl->getNext() != D) { |
| 102 | SomeDecl = SomeDecl->getNext(); |
| 103 | assert(SomeDecl && "Didn't find this decl on its identifier's chain!"); |
| 104 | } |
| 105 | SomeDecl->setNext(D->getNext()); |
| 106 | } |
Chris Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 107 | |
Chris Lattner | 740b2f3 | 2006-11-21 01:32:20 +0000 | [diff] [blame] | 108 | // This will have to be revisited for C++: there we want to nest stuff in |
| 109 | // namespace decls etc. Even for C, we might want a top-level translation |
| 110 | // unit decl or something. |
| 111 | if (!CurFunctionDecl) |
| 112 | continue; |
| 113 | |
| 114 | // Chain this decl to the containing function, it now owns the memory for |
| 115 | // the decl. |
| 116 | D->setNext(CurFunctionDecl->getDeclChain()); |
| 117 | CurFunctionDecl->setDeclChain(D); |
Chris Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
Chris Lattner | 18b1962 | 2007-01-22 07:39:13 +0000 | [diff] [blame] | 121 | /// LookupScopedDecl - Look up the inner-most declaration in the specified |
| 122 | /// namespace. |
Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 123 | Decl *Sema::LookupScopedDecl(IdentifierInfo *II, unsigned NSI, |
| 124 | SourceLocation IdLoc, Scope *S) { |
Chris Lattner | 18b1962 | 2007-01-22 07:39:13 +0000 | [diff] [blame] | 125 | if (II == 0) return 0; |
Chris Lattner | b6738ec | 2007-01-28 00:38:24 +0000 | [diff] [blame] | 126 | Decl::IdentifierNamespace NS = (Decl::IdentifierNamespace)NSI; |
Chris Lattner | 18b1962 | 2007-01-22 07:39:13 +0000 | [diff] [blame] | 127 | |
| 128 | // Scan up the scope chain looking for a decl that matches this identifier |
| 129 | // that is in the appropriate namespace. This search should not take long, as |
| 130 | // shadowing of names is uncommon, and deep shadowing is extremely uncommon. |
| 131 | for (Decl *D = II->getFETokenInfo<Decl>(); D; D = D->getNext()) |
| 132 | if (D->getIdentifierNamespace() == NS) |
| 133 | return D; |
Chris Lattner | b6738ec | 2007-01-28 00:38:24 +0000 | [diff] [blame] | 134 | |
Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 135 | // If we didn't find a use of this identifier, and if the identifier |
| 136 | // corresponds to a compiler builtin, create the decl object for the builtin |
| 137 | // now, injecting it into translation unit scope, and return it. |
| 138 | if (NS == Decl::IDNS_Ordinary) { |
| 139 | // If this is a builtin on some other target, or if this builtin varies |
| 140 | // across targets (e.g. in type), emit a diagnostic and mark the translation |
| 141 | // unit non-portable for using it. |
| 142 | if (II->isNonPortableBuiltin()) { |
| 143 | // Only emit this diagnostic once for this builtin. |
| 144 | II->setNonPortableBuiltin(false); |
| 145 | Context.Target.DiagnoseNonPortability(IdLoc, |
| 146 | diag::port_target_builtin_use); |
| 147 | } |
Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 148 | // If this is a builtin on this (or all) targets, create the decl. |
| 149 | if (unsigned BuiltinID = II->getBuiltinID()) |
| 150 | return LazilyCreateBuiltin(II, BuiltinID, S); |
| 151 | } |
Chris Lattner | 18b1962 | 2007-01-22 07:39:13 +0000 | [diff] [blame] | 152 | return 0; |
| 153 | } |
| 154 | |
Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 155 | /// LazilyCreateBuiltin - The specified Builtin-ID was first used at file scope. |
| 156 | /// lazily create a decl for it. |
| 157 | Decl *Sema::LazilyCreateBuiltin(IdentifierInfo *II, unsigned bid, Scope *S) { |
| 158 | Builtin::ID BID = (Builtin::ID)bid; |
| 159 | |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 160 | QualType R = Context.BuiltinInfo.GetBuiltinType(BID, Context); |
Chris Lattner | 776fac8 | 2007-06-09 00:53:06 +0000 | [diff] [blame] | 161 | FunctionDecl *New = new FunctionDecl(SourceLocation(), II, R, |
| 162 | FunctionDecl::Extern, 0); |
Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 163 | |
| 164 | // Find translation-unit scope to insert this function into. |
| 165 | while (S->getParent()) |
| 166 | S = S->getParent(); |
| 167 | S->AddDecl(New); |
| 168 | |
| 169 | // Add this decl to the end of the identifier info. |
| 170 | if (Decl *LastDecl = II->getFETokenInfo<Decl>()) { |
| 171 | // Scan until we find the last (outermost) decl in the id chain. |
| 172 | while (LastDecl->getNext()) |
| 173 | LastDecl = LastDecl->getNext(); |
| 174 | // Insert before (outside) it. |
| 175 | LastDecl->setNext(New); |
| 176 | } else { |
| 177 | II->setFETokenInfo(New); |
| 178 | } |
| 179 | // Make sure clients iterating over decls see this. |
| 180 | LastInGroupList.push_back(New); |
| 181 | |
| 182 | return New; |
| 183 | } |
| 184 | |
Chris Lattner | 01564d9 | 2007-01-27 19:27:06 +0000 | [diff] [blame] | 185 | /// MergeTypeDefDecl - We just parsed a typedef 'New' which has the same name |
| 186 | /// and scope as a previous declaration 'Old'. Figure out how to resolve this |
| 187 | /// situation, merging decls or emitting diagnostics as appropriate. |
| 188 | /// |
Chris Lattner | c511efb | 2007-01-27 19:32:14 +0000 | [diff] [blame] | 189 | TypedefDecl *Sema::MergeTypeDefDecl(TypedefDecl *New, Decl *OldD) { |
| 190 | // Verify the old decl was also a typedef. |
| 191 | TypedefDecl *Old = dyn_cast<TypedefDecl>(OldD); |
| 192 | if (!Old) { |
| 193 | Diag(New->getLocation(), diag::err_redefinition_different_kind, |
| 194 | New->getName()); |
| 195 | Diag(OldD->getLocation(), diag::err_previous_definition); |
| 196 | return New; |
| 197 | } |
| 198 | |
Chris Lattner | 01564d9 | 2007-01-27 19:27:06 +0000 | [diff] [blame] | 199 | // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope. |
| 200 | // TODO: This is totally simplistic. It should handle merging functions |
| 201 | // together etc, merging extern int X; int X; ... |
| 202 | Diag(New->getLocation(), diag::err_redefinition, New->getName()); |
| 203 | Diag(Old->getLocation(), diag::err_previous_definition); |
| 204 | return New; |
| 205 | } |
| 206 | |
| 207 | /// MergeFunctionDecl - We just parsed a function 'New' which has the same name |
| 208 | /// and scope as a previous declaration 'Old'. Figure out how to resolve this |
| 209 | /// situation, merging decls or emitting diagnostics as appropriate. |
| 210 | /// |
Chris Lattner | c511efb | 2007-01-27 19:32:14 +0000 | [diff] [blame] | 211 | FunctionDecl *Sema::MergeFunctionDecl(FunctionDecl *New, Decl *OldD) { |
| 212 | // Verify the old decl was also a function. |
| 213 | FunctionDecl *Old = dyn_cast<FunctionDecl>(OldD); |
| 214 | if (!Old) { |
| 215 | Diag(New->getLocation(), diag::err_redefinition_different_kind, |
| 216 | New->getName()); |
| 217 | Diag(OldD->getLocation(), diag::err_previous_definition); |
| 218 | return New; |
| 219 | } |
| 220 | |
Chris Lattner | efe4aea | 2007-01-27 19:35:39 +0000 | [diff] [blame] | 221 | // This is not right, but it's a start. If 'Old' is a function prototype with |
| 222 | // the same type as 'New', silently allow this. FIXME: We should link up decl |
| 223 | // objects here. |
Steve Naroff | 6fbf0dc | 2007-03-16 00:33:25 +0000 | [diff] [blame] | 224 | if (Old->getBody() == 0 && |
| 225 | Old->getCanonicalType() == New->getCanonicalType()) { |
Chris Lattner | efe4aea | 2007-01-27 19:35:39 +0000 | [diff] [blame] | 226 | return New; |
| 227 | } |
Chris Lattner | c511efb | 2007-01-27 19:32:14 +0000 | [diff] [blame] | 228 | |
Chris Lattner | 01564d9 | 2007-01-27 19:27:06 +0000 | [diff] [blame] | 229 | // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope. |
| 230 | // TODO: This is totally simplistic. It should handle merging functions |
| 231 | // together etc, merging extern int X; int X; ... |
| 232 | Diag(New->getLocation(), diag::err_redefinition, New->getName()); |
| 233 | Diag(Old->getLocation(), diag::err_previous_definition); |
| 234 | return New; |
| 235 | } |
| 236 | |
| 237 | /// MergeVarDecl - We just parsed a variable 'New' which has the same name |
| 238 | /// and scope as a previous declaration 'Old'. Figure out how to resolve this |
| 239 | /// situation, merging decls or emitting diagnostics as appropriate. |
| 240 | /// |
Steve Naroff | fc49d67 | 2007-04-01 21:27:45 +0000 | [diff] [blame] | 241 | /// FIXME: Need to carefully consider tentative definition rules (C99 6.9.2p2). |
| 242 | /// For example, we incorrectly complain about i1, i4 from C99 6.9.2p4. |
| 243 | /// |
Chris Lattner | c511efb | 2007-01-27 19:32:14 +0000 | [diff] [blame] | 244 | VarDecl *Sema::MergeVarDecl(VarDecl *New, Decl *OldD) { |
| 245 | // Verify the old decl was also a variable. |
| 246 | VarDecl *Old = dyn_cast<VarDecl>(OldD); |
| 247 | if (!Old) { |
| 248 | Diag(New->getLocation(), diag::err_redefinition_different_kind, |
| 249 | New->getName()); |
| 250 | Diag(OldD->getLocation(), diag::err_previous_definition); |
| 251 | return New; |
| 252 | } |
Steve Naroff | 6fbf0dc | 2007-03-16 00:33:25 +0000 | [diff] [blame] | 253 | // Verify the types match. |
| 254 | if (Old->getCanonicalType() != New->getCanonicalType()) { |
| 255 | Diag(New->getLocation(), diag::err_redefinition, New->getName()); |
| 256 | Diag(Old->getLocation(), diag::err_previous_definition); |
| 257 | return New; |
| 258 | } |
| 259 | // We've verified the types match, now check if Old is "extern". |
Steve Naroff | 46ba1eb | 2007-04-03 23:13:13 +0000 | [diff] [blame] | 260 | if (Old->getStorageClass() != VarDecl::Extern) { |
Steve Naroff | 6fbf0dc | 2007-03-16 00:33:25 +0000 | [diff] [blame] | 261 | Diag(New->getLocation(), diag::err_redefinition, New->getName()); |
| 262 | Diag(Old->getLocation(), diag::err_previous_definition); |
| 263 | } |
Chris Lattner | 01564d9 | 2007-01-27 19:27:06 +0000 | [diff] [blame] | 264 | return New; |
| 265 | } |
| 266 | |
Chris Lattner | b6738ec | 2007-01-28 00:38:24 +0000 | [diff] [blame] | 267 | /// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with |
| 268 | /// no declarator (e.g. "struct foo;") is parsed. |
| 269 | Sema::DeclTy *Sema::ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS) { |
| 270 | // TODO: emit error on 'int;' or 'const enum foo;'. |
| 271 | // TODO: emit error on 'typedef int;' |
| 272 | // if (!DS.isMissingDeclaratorOk()) Diag(...); |
| 273 | |
| 274 | return 0; |
| 275 | } |
| 276 | |
Chris Lattner | 776fac8 | 2007-06-09 00:53:06 +0000 | [diff] [blame] | 277 | Sema::DeclTy * |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 278 | Sema::ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init, |
Chris Lattner | 776fac8 | 2007-06-09 00:53:06 +0000 | [diff] [blame] | 279 | DeclTy *lastDeclarator) { |
| 280 | Decl *LastDeclarator = (Decl*)lastDeclarator; |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 281 | IdentifierInfo *II = D.getIdentifier(); |
Chris Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 282 | |
Chris Lattner | 01564d9 | 2007-01-27 19:27:06 +0000 | [diff] [blame] | 283 | // See if this is a redefinition of a variable in the same scope. |
Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 284 | Decl *PrevDecl = LookupScopedDecl(II, Decl::IDNS_Ordinary, |
| 285 | D.getIdentifierLoc(), S); |
Chris Lattner | 776fac8 | 2007-06-09 00:53:06 +0000 | [diff] [blame] | 286 | if (PrevDecl && !S->isDeclScope(PrevDecl)) |
Chris Lattner | 01564d9 | 2007-01-27 19:27:06 +0000 | [diff] [blame] | 287 | PrevDecl = 0; // If in outer scope, it isn't the same thing. |
| 288 | |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 289 | Decl *New; |
Chris Lattner | 01a7c53 | 2007-01-25 23:09:03 +0000 | [diff] [blame] | 290 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Chris Lattner | 776fac8 | 2007-06-09 00:53:06 +0000 | [diff] [blame] | 291 | TypedefDecl *NewTD = ParseTypedefDecl(S, D, LastDeclarator); |
Chris Lattner | 01564d9 | 2007-01-27 19:27:06 +0000 | [diff] [blame] | 292 | if (!NewTD) return 0; |
| 293 | |
| 294 | // Merge the decl with the existing one if appropriate. |
| 295 | if (PrevDecl) { |
| 296 | NewTD = MergeTypeDefDecl(NewTD, PrevDecl); |
| 297 | if (NewTD == 0) return 0; |
| 298 | } |
| 299 | New = NewTD; |
Steve Naroff | 8eeeb13 | 2007-05-08 21:09:37 +0000 | [diff] [blame] | 300 | if (S->getParent() == 0) { |
| 301 | // C99 6.7.7p2: If a typedef name specifies a variably modified type |
| 302 | // then it shall have block scope. |
| 303 | if (ArrayType *ary = dyn_cast<ArrayType>(NewTD->getUnderlyingType())) { |
Chris Lattner | 6046e00 | 2007-06-05 06:39:23 +0000 | [diff] [blame] | 304 | if (VerifyConstantArrayType(ary, D.getIdentifierLoc())) |
Steve Naroff | 8eeeb13 | 2007-05-08 21:09:37 +0000 | [diff] [blame] | 305 | return 0; |
| 306 | } |
| 307 | } |
Chris Lattner | 01a7c53 | 2007-01-25 23:09:03 +0000 | [diff] [blame] | 308 | } else if (D.isFunctionDeclarator()) { |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 309 | QualType R = GetTypeForDeclarator(D, S); |
Steve Naroff | 46ba1eb | 2007-04-03 23:13:13 +0000 | [diff] [blame] | 310 | if (R.isNull()) return 0; // FIXME: "auto func();" passes through... |
Chris Lattner | 01564d9 | 2007-01-27 19:27:06 +0000 | [diff] [blame] | 311 | |
Steve Naroff | 46ba1eb | 2007-04-03 23:13:13 +0000 | [diff] [blame] | 312 | FunctionDecl::StorageClass SC; |
| 313 | switch (D.getDeclSpec().getStorageClassSpec()) { |
| 314 | default: assert(0 && "Unknown storage class!"); |
| 315 | case DeclSpec::SCS_auto: |
| 316 | case DeclSpec::SCS_register: |
Chris Lattner | c04bd6a | 2007-05-16 18:09:54 +0000 | [diff] [blame] | 317 | Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_func, |
| 318 | R.getAsString()); |
Steve Naroff | 46ba1eb | 2007-04-03 23:13:13 +0000 | [diff] [blame] | 319 | return 0; |
| 320 | case DeclSpec::SCS_unspecified: SC = FunctionDecl::None; break; |
| 321 | case DeclSpec::SCS_extern: SC = FunctionDecl::Extern; break; |
| 322 | case DeclSpec::SCS_static: SC = FunctionDecl::Static; break; |
| 323 | } |
| 324 | |
Chris Lattner | 776fac8 | 2007-06-09 00:53:06 +0000 | [diff] [blame] | 325 | FunctionDecl *NewFD = new FunctionDecl(D.getIdentifierLoc(), II, R, SC, |
| 326 | LastDeclarator); |
Chris Lattner | 01564d9 | 2007-01-27 19:27:06 +0000 | [diff] [blame] | 327 | |
| 328 | // Merge the decl with the existing one if appropriate. |
| 329 | if (PrevDecl) { |
| 330 | NewFD = MergeFunctionDecl(NewFD, PrevDecl); |
| 331 | if (NewFD == 0) return 0; |
| 332 | } |
| 333 | New = NewFD; |
Chris Lattner | 01a7c53 | 2007-01-25 23:09:03 +0000 | [diff] [blame] | 334 | } else { |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 335 | QualType R = GetTypeForDeclarator(D, S); |
Chris Lattner | 01a7c53 | 2007-01-25 23:09:03 +0000 | [diff] [blame] | 336 | if (R.isNull()) return 0; |
Chris Lattner | 01564d9 | 2007-01-27 19:27:06 +0000 | [diff] [blame] | 337 | |
Steve Naroff | 46ba1eb | 2007-04-03 23:13:13 +0000 | [diff] [blame] | 338 | VarDecl *NewVD; |
| 339 | VarDecl::StorageClass SC; |
Steve Naroff | 6fbf0dc | 2007-03-16 00:33:25 +0000 | [diff] [blame] | 340 | switch (D.getDeclSpec().getStorageClassSpec()) { |
| 341 | default: assert(0 && "Unknown storage class!"); |
Steve Naroff | 46ba1eb | 2007-04-03 23:13:13 +0000 | [diff] [blame] | 342 | case DeclSpec::SCS_unspecified: SC = VarDecl::None; break; |
| 343 | case DeclSpec::SCS_extern: SC = VarDecl::Extern; break; |
| 344 | case DeclSpec::SCS_static: SC = VarDecl::Static; break; |
| 345 | case DeclSpec::SCS_auto: SC = VarDecl::Auto; break; |
| 346 | case DeclSpec::SCS_register: SC = VarDecl::Register; break; |
| 347 | } |
Steve Naroff | fc49d67 | 2007-04-01 21:27:45 +0000 | [diff] [blame] | 348 | if (S->getParent() == 0) { |
| 349 | // File scope. C99 6.9.2p2: A declaration of an identifier for and |
| 350 | // object that has file scope without an initializer, and without a |
| 351 | // storage-class specifier or with the storage-class specifier "static", |
| 352 | // constitutes a tentative definition. Note: A tentative definition with |
| 353 | // external linkage is valid (C99 6.2.2p5). |
Steve Naroff | 46ba1eb | 2007-04-03 23:13:13 +0000 | [diff] [blame] | 354 | if (!Init && SC == VarDecl::Static) { |
Steve Naroff | fc49d67 | 2007-04-01 21:27:45 +0000 | [diff] [blame] | 355 | // C99 6.9.2p3: If the declaration of an identifier for an object is |
| 356 | // a tentative definition and has internal linkage (C99 6.2.2p3), the |
| 357 | // declared type shall not be an incomplete type. |
| 358 | if (R->isIncompleteType()) { |
Chris Lattner | c04bd6a | 2007-05-16 18:09:54 +0000 | [diff] [blame] | 359 | Diag(D.getIdentifierLoc(), diag::err_typecheck_decl_incomplete_type, |
| 360 | R.getAsString()); |
Steve Naroff | fc49d67 | 2007-04-01 21:27:45 +0000 | [diff] [blame] | 361 | return 0; |
| 362 | } |
Steve Naroff | ca8f712 | 2007-04-01 01:41:35 +0000 | [diff] [blame] | 363 | } |
Bill Wendling | d6de657 | 2007-06-02 09:40:07 +0000 | [diff] [blame] | 364 | // C99 6.9p2: The storage-class specifiers auto and register shall not |
| 365 | // appear in the declaration specifiers in an external declaration. |
Steve Naroff | 46ba1eb | 2007-04-03 23:13:13 +0000 | [diff] [blame] | 366 | if (SC == VarDecl::Auto || SC == VarDecl::Register) { |
Chris Lattner | c04bd6a | 2007-05-16 18:09:54 +0000 | [diff] [blame] | 367 | Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_fscope, |
| 368 | R.getAsString()); |
Steve Naroff | 46ba1eb | 2007-04-03 23:13:13 +0000 | [diff] [blame] | 369 | return 0; |
| 370 | } |
Steve Naroff | 8eeeb13 | 2007-05-08 21:09:37 +0000 | [diff] [blame] | 371 | // C99 6.7.5.2p2: If an identifier is declared to be an object with |
| 372 | // static storage duration, it shall not have a variable length array. |
Chris Lattner | 6046e00 | 2007-06-05 06:39:23 +0000 | [diff] [blame] | 373 | if (ArrayType *ary = dyn_cast<ArrayType>(R.getCanonicalType())) { |
| 374 | if (VerifyConstantArrayType(ary, D.getIdentifierLoc())) |
Steve Naroff | 8eeeb13 | 2007-05-08 21:09:37 +0000 | [diff] [blame] | 375 | return 0; |
| 376 | } |
Chris Lattner | 776fac8 | 2007-06-09 00:53:06 +0000 | [diff] [blame] | 377 | NewVD = new FileVarDecl(D.getIdentifierLoc(), II, R, SC, LastDeclarator); |
Steve Naroff | fc49d67 | 2007-04-01 21:27:45 +0000 | [diff] [blame] | 378 | } else { |
| 379 | // Block scope. C99 6.7p7: If an identifier for an object is declared with |
| 380 | // no linkage (C99 6.2.2p6), the type for the object shall be complete... |
Steve Naroff | 46ba1eb | 2007-04-03 23:13:13 +0000 | [diff] [blame] | 381 | if (SC != VarDecl::Extern) { |
Steve Naroff | fc49d67 | 2007-04-01 21:27:45 +0000 | [diff] [blame] | 382 | if (R->isIncompleteType()) { |
Chris Lattner | c04bd6a | 2007-05-16 18:09:54 +0000 | [diff] [blame] | 383 | Diag(D.getIdentifierLoc(), diag::err_typecheck_decl_incomplete_type, |
| 384 | R.getAsString()); |
Steve Naroff | fc49d67 | 2007-04-01 21:27:45 +0000 | [diff] [blame] | 385 | return 0; |
| 386 | } |
| 387 | } |
Steve Naroff | 8eeeb13 | 2007-05-08 21:09:37 +0000 | [diff] [blame] | 388 | if (SC == VarDecl::Static) { |
| 389 | // C99 6.7.5.2p2: If an identifier is declared to be an object with |
| 390 | // static storage duration, it shall not have a variable length array. |
Chris Lattner | 6046e00 | 2007-06-05 06:39:23 +0000 | [diff] [blame] | 391 | if (ArrayType *ary = dyn_cast<ArrayType>(R.getCanonicalType())) { |
| 392 | if (VerifyConstantArrayType(ary, D.getIdentifierLoc())) |
Steve Naroff | 8eeeb13 | 2007-05-08 21:09:37 +0000 | [diff] [blame] | 393 | return 0; |
| 394 | } |
| 395 | } |
Chris Lattner | 776fac8 | 2007-06-09 00:53:06 +0000 | [diff] [blame] | 396 | NewVD = new BlockVarDecl(D.getIdentifierLoc(), II, R, SC, LastDeclarator); |
Steve Naroff | ef2ab6a | 2007-04-02 20:50:54 +0000 | [diff] [blame] | 397 | } |
Chris Lattner | 01564d9 | 2007-01-27 19:27:06 +0000 | [diff] [blame] | 398 | // Merge the decl with the existing one if appropriate. |
| 399 | if (PrevDecl) { |
| 400 | NewVD = MergeVarDecl(NewVD, PrevDecl); |
| 401 | if (NewVD == 0) return 0; |
| 402 | } |
| 403 | New = NewVD; |
Chris Lattner | 01a7c53 | 2007-01-25 23:09:03 +0000 | [diff] [blame] | 404 | } |
Chris Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 405 | |
| 406 | |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 407 | // If this has an identifier, add it to the scope stack. |
| 408 | if (II) { |
Chris Lattner | 18b1962 | 2007-01-22 07:39:13 +0000 | [diff] [blame] | 409 | New->setNext(II->getFETokenInfo<Decl>()); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 410 | II->setFETokenInfo(New); |
Chris Lattner | 99d3177 | 2007-01-21 22:37:37 +0000 | [diff] [blame] | 411 | S->AddDecl(New); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 414 | if (S->getParent() == 0) |
Chris Lattner | 776fac8 | 2007-06-09 00:53:06 +0000 | [diff] [blame] | 415 | AddTopLevelDecl(New, LastDeclarator); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 416 | |
| 417 | return New; |
| 418 | } |
| 419 | |
Chris Lattner | 776fac8 | 2007-06-09 00:53:06 +0000 | [diff] [blame] | 420 | /// The declarators are chained together backwards, reverse the list. |
| 421 | Sema::DeclTy *Sema::FinalizeDeclaratorGroup(Scope *S, DeclTy *group) { |
| 422 | // Often we have single declarators, handle them quickly. |
| 423 | Decl *Group = static_cast<Decl*>(group); |
Chris Lattner | b2dd241 | 2007-06-09 06:16:32 +0000 | [diff] [blame^] | 424 | if (Group == 0 || Group->getNextDeclarator() == 0) return Group; |
Chris Lattner | 776fac8 | 2007-06-09 00:53:06 +0000 | [diff] [blame] | 425 | |
| 426 | Decl *NewGroup = 0; |
| 427 | while (Group) { |
| 428 | Decl *Next = Group->getNextDeclarator(); |
| 429 | Group->setNextDeclarator(NewGroup); |
| 430 | NewGroup = Group; |
| 431 | Group = Next; |
| 432 | } |
| 433 | return NewGroup; |
| 434 | } |
| 435 | |
Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 436 | VarDecl * |
| 437 | Sema::ParseParamDeclarator(DeclaratorChunk &FTI, unsigned ArgNo, |
| 438 | Scope *FnScope) { |
| 439 | const DeclaratorChunk::ParamInfo &PI = FTI.Fun.ArgInfo[ArgNo]; |
Chris Lattner | 200bdc3 | 2006-11-19 02:43:37 +0000 | [diff] [blame] | 440 | |
Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 441 | IdentifierInfo *II = PI.Ident; |
Chris Lattner | c284e9b | 2007-01-23 05:14:32 +0000 | [diff] [blame] | 442 | // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope. |
| 443 | // Can this happen for params? We already checked that they don't conflict |
| 444 | // among each other. Here they can only shadow globals, which is ok. |
Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 445 | if (Decl *PrevDecl = LookupScopedDecl(II, Decl::IDNS_Ordinary, |
| 446 | PI.IdentLoc, FnScope)) { |
Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 447 | |
Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Steve Naroff | 6fbf0dc | 2007-03-16 00:33:25 +0000 | [diff] [blame] | 450 | // FIXME: Handle storage class (auto, register). No declarator? |
Chris Lattner | 776fac8 | 2007-06-09 00:53:06 +0000 | [diff] [blame] | 451 | // TODO: Chain to previous parameter with the prevdeclarator chain? |
Steve Naroff | ef2ab6a | 2007-04-02 20:50:54 +0000 | [diff] [blame] | 452 | VarDecl *New = new ParmVarDecl(PI.IdentLoc, II, |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 453 | QualType::getFromOpaquePtr(PI.TypeInfo), |
Chris Lattner | 776fac8 | 2007-06-09 00:53:06 +0000 | [diff] [blame] | 454 | VarDecl::None, 0); |
Steve Naroff | 6fbf0dc | 2007-03-16 00:33:25 +0000 | [diff] [blame] | 455 | |
Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 456 | // If this has an identifier, add it to the scope stack. |
| 457 | if (II) { |
Chris Lattner | 18b1962 | 2007-01-22 07:39:13 +0000 | [diff] [blame] | 458 | New->setNext(II->getFETokenInfo<Decl>()); |
Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 459 | II->setFETokenInfo(New); |
Chris Lattner | 99d3177 | 2007-01-21 22:37:37 +0000 | [diff] [blame] | 460 | FnScope->AddDecl(New); |
Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 461 | } |
Chris Lattner | 229ce60 | 2006-11-21 01:21:07 +0000 | [diff] [blame] | 462 | |
Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 463 | return New; |
| 464 | } |
| 465 | |
| 466 | |
| 467 | Sema::DeclTy *Sema::ParseStartOfFunctionDef(Scope *FnBodyScope, Declarator &D) { |
Chris Lattner | 229ce60 | 2006-11-21 01:21:07 +0000 | [diff] [blame] | 468 | assert(CurFunctionDecl == 0 && "Function parsing confused"); |
Chris Lattner | 5c5fbcc | 2006-12-03 08:41:30 +0000 | [diff] [blame] | 469 | assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function && |
| 470 | "Not a function declarator!"); |
| 471 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 472 | |
| 473 | // Verify 6.9.1p6: 'every identifier in the identifier list shall be declared' |
| 474 | // for a K&R function. |
| 475 | if (!FTI.hasPrototype) { |
| 476 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) { |
| 477 | if (FTI.ArgInfo[i].TypeInfo == 0) { |
| 478 | Diag(FTI.ArgInfo[i].IdentLoc, diag::err_param_not_declared, |
| 479 | FTI.ArgInfo[i].Ident->getName()); |
| 480 | // Implicitly declare the argument as type 'int' for lack of a better |
| 481 | // type. |
| 482 | FTI.ArgInfo[i].TypeInfo = Context.IntTy.getAsOpaquePtr(); |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | // Since this is a function definition, act as though we have information |
| 487 | // about the arguments. |
| 488 | FTI.hasPrototype = true; |
Chris Lattner | 2114d5e | 2006-12-04 07:40:24 +0000 | [diff] [blame] | 489 | } else { |
| 490 | // FIXME: Diagnose arguments without names in C. |
| 491 | |
Chris Lattner | 5c5fbcc | 2006-12-03 08:41:30 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 494 | Scope *GlobalScope = FnBodyScope->getParent(); |
| 495 | |
| 496 | FunctionDecl *FD = |
| 497 | static_cast<FunctionDecl*>(ParseDeclarator(GlobalScope, D, 0, 0)); |
Chris Lattner | 229ce60 | 2006-11-21 01:21:07 +0000 | [diff] [blame] | 498 | CurFunctionDecl = FD; |
Chris Lattner | 2114d5e | 2006-12-04 07:40:24 +0000 | [diff] [blame] | 499 | |
Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 500 | // Create Decl objects for each parameter, adding them to the FunctionDecl. |
| 501 | SmallVector<VarDecl*, 16> Params; |
Chris Lattner | f61c8a8 | 2007-01-21 19:04:43 +0000 | [diff] [blame] | 502 | |
| 503 | // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs function that takes |
| 504 | // no arguments, not a function that takes a single void argument. |
| 505 | if (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
| 506 | FTI.ArgInfo[0].TypeInfo == Context.VoidTy.getAsOpaquePtr()) { |
Chris Lattner | 99d3177 | 2007-01-21 22:37:37 +0000 | [diff] [blame] | 507 | // empty arg list, don't push any params. |
Chris Lattner | f61c8a8 | 2007-01-21 19:04:43 +0000 | [diff] [blame] | 508 | } else { |
| 509 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) |
| 510 | Params.push_back(ParseParamDeclarator(D.getTypeObject(0), i,FnBodyScope)); |
| 511 | } |
Chris Lattner | 2114d5e | 2006-12-04 07:40:24 +0000 | [diff] [blame] | 512 | |
Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 513 | FD->setParams(&Params[0], Params.size()); |
Chris Lattner | 2114d5e | 2006-12-04 07:40:24 +0000 | [diff] [blame] | 514 | |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 515 | return FD; |
| 516 | } |
| 517 | |
Chris Lattner | 229ce60 | 2006-11-21 01:21:07 +0000 | [diff] [blame] | 518 | Sema::DeclTy *Sema::ParseFunctionDefBody(DeclTy *D, StmtTy *Body) { |
| 519 | FunctionDecl *FD = static_cast<FunctionDecl*>(D); |
| 520 | FD->setBody((Stmt*)Body); |
| 521 | |
| 522 | assert(FD == CurFunctionDecl && "Function parsing confused"); |
| 523 | CurFunctionDecl = 0; |
Chris Lattner | e247306 | 2007-05-28 06:28:18 +0000 | [diff] [blame] | 524 | |
| 525 | // Verify and clean out per-function state. |
| 526 | |
| 527 | // Check goto/label use. |
| 528 | for (DenseMap<IdentifierInfo*, LabelStmt*>::iterator I = LabelMap.begin(), |
| 529 | E = LabelMap.end(); I != E; ++I) { |
| 530 | // Verify that we have no forward references left. If so, there was a goto |
| 531 | // or address of a label taken, but no definition of it. Label fwd |
| 532 | // definitions are indicated with a null substmt. |
| 533 | if (I->second->getSubStmt() == 0) { |
| 534 | LabelStmt *L = I->second; |
| 535 | // Emit error. |
Chris Lattner | eefa10e | 2007-05-28 06:56:27 +0000 | [diff] [blame] | 536 | Diag(L->getIdentLoc(), diag::err_undeclared_label_use, L->getName()); |
Chris Lattner | e247306 | 2007-05-28 06:28:18 +0000 | [diff] [blame] | 537 | |
| 538 | // At this point, we have gotos that use the bogus label. Stitch it into |
| 539 | // the function body so that they aren't leaked and that the AST is well |
| 540 | // formed. |
| 541 | L->setSubStmt(new NullStmt(L->getIdentLoc())); |
| 542 | cast<CompoundStmt>((Stmt*)Body)->push_back(L); |
| 543 | } |
| 544 | } |
| 545 | LabelMap.clear(); |
| 546 | |
Chris Lattner | 229ce60 | 2006-11-21 01:21:07 +0000 | [diff] [blame] | 547 | return FD; |
| 548 | } |
| 549 | |
| 550 | |
Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 551 | /// ImplicitlyDefineFunction - An undeclared identifier was used in a function |
| 552 | /// call, forming a call to an implicitly defined function (per C99 6.5.1p2). |
| 553 | Decl *Sema::ImplicitlyDefineFunction(SourceLocation Loc, IdentifierInfo &II, |
| 554 | Scope *S) { |
| 555 | if (getLangOptions().C99) // Extension in C99. |
| 556 | Diag(Loc, diag::ext_implicit_function_decl, II.getName()); |
| 557 | else // Legal in C90, but warn about it. |
| 558 | Diag(Loc, diag::warn_implicit_function_decl, II.getName()); |
| 559 | |
| 560 | // FIXME: handle stuff like: |
| 561 | // void foo() { extern float X(); } |
| 562 | // void bar() { X(); } <-- implicit decl for X in another scope. |
| 563 | |
| 564 | // Set a Declarator for the implicit definition: int foo(); |
Chris Lattner | 353f574 | 2006-11-28 04:50:12 +0000 | [diff] [blame] | 565 | const char *Dummy; |
Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 566 | DeclSpec DS; |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 567 | bool Error = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, Dummy); |
Chris Lattner | b055f2d | 2007-02-11 08:19:57 +0000 | [diff] [blame] | 568 | Error = Error; // Silence warning. |
Chris Lattner | 353f574 | 2006-11-28 04:50:12 +0000 | [diff] [blame] | 569 | assert(!Error && "Error setting up implicit decl!"); |
Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 570 | Declarator D(DS, Declarator::BlockContext); |
Chris Lattner | cbc426d | 2006-12-02 06:43:02 +0000 | [diff] [blame] | 571 | D.AddTypeInfo(DeclaratorChunk::getFunction(false, false, 0, 0, Loc)); |
Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 572 | D.SetIdentifier(&II, Loc); |
| 573 | |
Chris Lattner | 62d2e66 | 2007-01-28 00:21:37 +0000 | [diff] [blame] | 574 | // Find translation-unit scope to insert this function into. |
| 575 | while (S->getParent()) |
| 576 | S = S->getParent(); |
Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 577 | |
Chris Lattner | 62d2e66 | 2007-01-28 00:21:37 +0000 | [diff] [blame] | 578 | return static_cast<Decl*>(ParseDeclarator(S, D, 0, 0)); |
Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Chris Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 581 | |
Chris Lattner | 776fac8 | 2007-06-09 00:53:06 +0000 | [diff] [blame] | 582 | TypedefDecl *Sema::ParseTypedefDecl(Scope *S, Declarator &D, |
| 583 | Decl *LastDeclarator) { |
| 584 | assert(D.getIdentifier() && "Wrong callback for declspec without declarator"); |
Chris Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 585 | |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 586 | QualType T = GetTypeForDeclarator(D, S); |
Chris Lattner | 0d8b1a1 | 2006-11-20 04:34:45 +0000 | [diff] [blame] | 587 | if (T.isNull()) return 0; |
| 588 | |
Chris Lattner | 18b1962 | 2007-01-22 07:39:13 +0000 | [diff] [blame] | 589 | // Scope manipulation handled by caller. |
Chris Lattner | 776fac8 | 2007-06-09 00:53:06 +0000 | [diff] [blame] | 590 | return new TypedefDecl(D.getIdentifierLoc(), D.getIdentifier(), T, |
| 591 | LastDeclarator); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Chris Lattner | 18b1962 | 2007-01-22 07:39:13 +0000 | [diff] [blame] | 594 | |
Chris Lattner | 1300fb9 | 2007-01-23 23:42:53 +0000 | [diff] [blame] | 595 | /// ParseTag - This is invoked when we see 'struct foo' or 'struct {'. In the |
| 596 | /// former case, Name will be non-null. In the later case, Name will be null. |
| 597 | /// TagType indicates what kind of tag this is. TK indicates whether this is a |
| 598 | /// reference/declaration/definition of a tag. |
Chris Lattner | 7b9ace6 | 2007-01-23 20:11:08 +0000 | [diff] [blame] | 599 | Sema::DeclTy *Sema::ParseTag(Scope *S, unsigned TagType, TagKind TK, |
Chris Lattner | f34c4da | 2007-01-23 04:08:05 +0000 | [diff] [blame] | 600 | SourceLocation KWLoc, IdentifierInfo *Name, |
Steve Naroff | b309644 | 2007-06-09 03:47:53 +0000 | [diff] [blame] | 601 | SourceLocation NameLoc, AttributeList *Attr) { |
Chris Lattner | 8799cf2 | 2007-01-23 01:57:16 +0000 | [diff] [blame] | 602 | // If this is a use of an existing tag, it must have a name. |
Chris Lattner | 7b9ace6 | 2007-01-23 20:11:08 +0000 | [diff] [blame] | 603 | assert((Name != 0 || TK == TK_Definition) && |
| 604 | "Nameless record must be a definition!"); |
Chris Lattner | 8799cf2 | 2007-01-23 01:57:16 +0000 | [diff] [blame] | 605 | |
Chris Lattner | f34c4da | 2007-01-23 04:08:05 +0000 | [diff] [blame] | 606 | Decl::Kind Kind; |
Chris Lattner | bf0b798 | 2007-01-23 04:27:41 +0000 | [diff] [blame] | 607 | switch (TagType) { |
Chris Lattner | f34c4da | 2007-01-23 04:08:05 +0000 | [diff] [blame] | 608 | default: assert(0 && "Unknown tag type!"); |
Chris Lattner | bf0b798 | 2007-01-23 04:27:41 +0000 | [diff] [blame] | 609 | case DeclSpec::TST_struct: Kind = Decl::Struct; break; |
| 610 | case DeclSpec::TST_union: Kind = Decl::Union; break; |
| 611 | //case DeclSpec::TST_class: Kind = Decl::Class; break; |
| 612 | case DeclSpec::TST_enum: Kind = Decl::Enum; break; |
Chris Lattner | f34c4da | 2007-01-23 04:08:05 +0000 | [diff] [blame] | 613 | } |
Chris Lattner | 7e783a1 | 2007-01-23 02:05:42 +0000 | [diff] [blame] | 614 | |
Chris Lattner | 18b1962 | 2007-01-22 07:39:13 +0000 | [diff] [blame] | 615 | // If this is a named struct, check to see if there was a previous forward |
| 616 | // declaration or definition. |
Chris Lattner | 7b9ace6 | 2007-01-23 20:11:08 +0000 | [diff] [blame] | 617 | if (TagDecl *PrevDecl = |
Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 618 | dyn_cast_or_null<TagDecl>(LookupScopedDecl(Name, Decl::IDNS_Tag, |
| 619 | NameLoc, S))) { |
Chris Lattner | 8799cf2 | 2007-01-23 01:57:16 +0000 | [diff] [blame] | 620 | |
| 621 | // If this is a use of a previous tag, or if the tag is already declared in |
| 622 | // the same scope (so that the definition/declaration completes or |
| 623 | // rementions the tag), reuse the decl. |
Chris Lattner | 7b9ace6 | 2007-01-23 20:11:08 +0000 | [diff] [blame] | 624 | if (TK == TK_Reference || S->isDeclScope(PrevDecl)) { |
Chris Lattner | 7e783a1 | 2007-01-23 02:05:42 +0000 | [diff] [blame] | 625 | // Make sure that this wasn't declared as an enum and now used as a struct |
| 626 | // or something similar. |
| 627 | if (PrevDecl->getKind() != Kind) { |
Chris Lattner | f34c4da | 2007-01-23 04:08:05 +0000 | [diff] [blame] | 628 | Diag(KWLoc, diag::err_use_with_wrong_tag, Name->getName()); |
Chris Lattner | 7e783a1 | 2007-01-23 02:05:42 +0000 | [diff] [blame] | 629 | Diag(PrevDecl->getLocation(), diag::err_previous_use); |
| 630 | } |
Chris Lattner | 7b9ace6 | 2007-01-23 20:11:08 +0000 | [diff] [blame] | 631 | |
| 632 | // If this is a use or a forward declaration, we're good. |
| 633 | if (TK != TK_Definition) |
| 634 | return PrevDecl; |
Chris Lattner | f34c4da | 2007-01-23 04:08:05 +0000 | [diff] [blame] | 635 | |
Chris Lattner | 7b9ace6 | 2007-01-23 20:11:08 +0000 | [diff] [blame] | 636 | // Diagnose attempts to redefine a tag. |
| 637 | if (PrevDecl->isDefinition()) { |
| 638 | Diag(NameLoc, diag::err_redefinition, Name->getName()); |
| 639 | Diag(PrevDecl->getLocation(), diag::err_previous_definition); |
| 640 | // If this is a redefinition, recover by making this struct be |
| 641 | // anonymous, which will make any later references get the previous |
| 642 | // definition. |
| 643 | Name = 0; |
| 644 | } else { |
| 645 | // Okay, this is definition of a previously declared or referenced tag. |
| 646 | // Move the location of the decl to be the definition site. |
| 647 | PrevDecl->setLocation(NameLoc); |
Chris Lattner | 7b9ace6 | 2007-01-23 20:11:08 +0000 | [diff] [blame] | 648 | return PrevDecl; |
| 649 | } |
Chris Lattner | 8799cf2 | 2007-01-23 01:57:16 +0000 | [diff] [blame] | 650 | } |
Chris Lattner | f34c4da | 2007-01-23 04:08:05 +0000 | [diff] [blame] | 651 | // If we get here, this is a definition of a new struct type in a nested |
| 652 | // scope, e.g. "struct foo; void bar() { struct foo; }", just create a new |
| 653 | // type. |
Chris Lattner | 18b1962 | 2007-01-22 07:39:13 +0000 | [diff] [blame] | 654 | } |
| 655 | |
Chris Lattner | bf0b798 | 2007-01-23 04:27:41 +0000 | [diff] [blame] | 656 | // If there is an identifier, use the location of the identifier as the |
| 657 | // location of the decl, otherwise use the location of the struct/union |
| 658 | // keyword. |
| 659 | SourceLocation Loc = NameLoc.isValid() ? NameLoc : KWLoc; |
| 660 | |
Chris Lattner | 18b1962 | 2007-01-22 07:39:13 +0000 | [diff] [blame] | 661 | // Otherwise, if this is the first time we've seen this tag, create the decl. |
Chris Lattner | 7b9ace6 | 2007-01-23 20:11:08 +0000 | [diff] [blame] | 662 | TagDecl *New; |
Chris Lattner | 720a054 | 2007-01-25 00:44:24 +0000 | [diff] [blame] | 663 | switch (Kind) { |
| 664 | default: assert(0 && "Unknown tag kind!"); |
Chris Lattner | 5f52150 | 2007-01-25 06:27:24 +0000 | [diff] [blame] | 665 | case Decl::Enum: |
Chris Lattner | 776fac8 | 2007-06-09 00:53:06 +0000 | [diff] [blame] | 666 | // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.: |
| 667 | // enum X { A, B, C } D; D should chain to X. |
| 668 | New = new EnumDecl(Loc, Name, 0); |
Chris Lattner | 5f52150 | 2007-01-25 06:27:24 +0000 | [diff] [blame] | 669 | // If this is an undefined enum, warn. |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 670 | if (TK != TK_Definition) Diag(Loc, diag::ext_forward_ref_enum); |
Chris Lattner | 5f52150 | 2007-01-25 06:27:24 +0000 | [diff] [blame] | 671 | break; |
Chris Lattner | 720a054 | 2007-01-25 00:44:24 +0000 | [diff] [blame] | 672 | case Decl::Union: |
| 673 | case Decl::Struct: |
| 674 | case Decl::Class: |
Chris Lattner | 776fac8 | 2007-06-09 00:53:06 +0000 | [diff] [blame] | 675 | // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.: |
| 676 | // struct X { int A; } D; D should chain to X. |
| 677 | New = new RecordDecl(Kind, Loc, Name, 0); |
Chris Lattner | 720a054 | 2007-01-25 00:44:24 +0000 | [diff] [blame] | 678 | break; |
| 679 | } |
Chris Lattner | 18b1962 | 2007-01-22 07:39:13 +0000 | [diff] [blame] | 680 | |
| 681 | // If this has an identifier, add it to the scope stack. |
| 682 | if (Name) { |
| 683 | New->setNext(Name->getFETokenInfo<Decl>()); |
| 684 | Name->setFETokenInfo(New); |
| 685 | S->AddDecl(New); |
| 686 | } |
| 687 | |
| 688 | return New; |
| 689 | } |
Chris Lattner | 1300fb9 | 2007-01-23 23:42:53 +0000 | [diff] [blame] | 690 | |
| 691 | /// ParseField - Each field of a struct/union/class is passed into this in order |
| 692 | /// to create a FieldDecl object for it. |
| 693 | Sema::DeclTy *Sema::ParseField(Scope *S, DeclTy *TagDecl, |
| 694 | SourceLocation DeclStart, |
| 695 | Declarator &D, ExprTy *BitfieldWidth) { |
| 696 | IdentifierInfo *II = D.getIdentifier(); |
| 697 | Expr *BitWidth = (Expr*)BitfieldWidth; |
| 698 | |
| 699 | SourceLocation Loc = DeclStart; |
| 700 | if (II) Loc = D.getIdentifierLoc(); |
| 701 | |
Chris Lattner | 62d2e66 | 2007-01-28 00:21:37 +0000 | [diff] [blame] | 702 | // FIXME: Unnamed fields can be handled in various different ways, for |
| 703 | // example, unnamed unions inject all members into the struct namespace! |
| 704 | |
| 705 | |
Chris Lattner | 1300fb9 | 2007-01-23 23:42:53 +0000 | [diff] [blame] | 706 | if (BitWidth) { |
| 707 | // TODO: Validate. |
Steve Naroff | f84d11f | 2007-05-23 21:48:04 +0000 | [diff] [blame] | 708 | //printf("WARNING: BITFIELDS IGNORED!\n"); |
Chris Lattner | 1300fb9 | 2007-01-23 23:42:53 +0000 | [diff] [blame] | 709 | |
| 710 | // 6.7.2.1p3 |
| 711 | // 6.7.2.1p4 |
| 712 | |
| 713 | } else { |
| 714 | // Not a bitfield. |
| 715 | |
| 716 | // validate II. |
| 717 | |
| 718 | } |
| 719 | |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 720 | QualType T = GetTypeForDeclarator(D, S); |
Chris Lattner | 01a7c53 | 2007-01-25 23:09:03 +0000 | [diff] [blame] | 721 | if (T.isNull()) return 0; |
Steve Naroff | 8eeeb13 | 2007-05-08 21:09:37 +0000 | [diff] [blame] | 722 | |
| 723 | // C99 6.7.2.1p8: A member of a structure or union may have any type other |
| 724 | // than a variably modified type. |
Chris Lattner | 6046e00 | 2007-06-05 06:39:23 +0000 | [diff] [blame] | 725 | if (ArrayType *ary = dyn_cast<ArrayType>(T.getCanonicalType())) { |
| 726 | if (VerifyConstantArrayType(ary, Loc)) |
Steve Naroff | 8eeeb13 | 2007-05-08 21:09:37 +0000 | [diff] [blame] | 727 | return 0; |
| 728 | } |
Chris Lattner | 776fac8 | 2007-06-09 00:53:06 +0000 | [diff] [blame] | 729 | |
| 730 | // FIXME: Chain fielddecls together. |
| 731 | return new FieldDecl(Loc, II, T, 0); |
Chris Lattner | 1300fb9 | 2007-01-23 23:42:53 +0000 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | void Sema::ParseRecordBody(SourceLocation RecLoc, DeclTy *RecDecl, |
| 735 | DeclTy **Fields, unsigned NumFields) { |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 736 | RecordDecl *Record = cast<RecordDecl>(static_cast<Decl*>(RecDecl)); |
Chris Lattner | 1300fb9 | 2007-01-23 23:42:53 +0000 | [diff] [blame] | 737 | if (Record->isDefinition()) { |
| 738 | // Diagnose code like: |
Chris Lattner | bdf8b8d | 2007-01-24 02:11:17 +0000 | [diff] [blame] | 739 | // struct S { struct S {} X; }; |
Chris Lattner | 1300fb9 | 2007-01-23 23:42:53 +0000 | [diff] [blame] | 740 | // We discover this when we complete the outer S. Reject and ignore the |
| 741 | // outer S. |
| 742 | Diag(Record->getLocation(), diag::err_nested_redefinition, |
| 743 | Record->getKindName()); |
| 744 | Diag(RecLoc, diag::err_previous_definition); |
| 745 | return; |
| 746 | } |
Chris Lattner | bdf8b8d | 2007-01-24 02:11:17 +0000 | [diff] [blame] | 747 | |
Chris Lattner | bdf8b8d | 2007-01-24 02:11:17 +0000 | [diff] [blame] | 748 | // Verify that all the fields are okay. |
Chris Lattner | 8262560 | 2007-01-24 02:26:21 +0000 | [diff] [blame] | 749 | unsigned NumNamedMembers = 0; |
Steve Naroff | cc32142 | 2007-03-26 23:09:51 +0000 | [diff] [blame] | 750 | SmallVector<FieldDecl*, 32> RecFields; |
Chris Lattner | e5a6656 | 2007-01-25 22:48:42 +0000 | [diff] [blame] | 751 | SmallSet<const IdentifierInfo*, 32> FieldIDs; |
| 752 | |
Chris Lattner | bdf8b8d | 2007-01-24 02:11:17 +0000 | [diff] [blame] | 753 | for (unsigned i = 0; i != NumFields; ++i) { |
| 754 | FieldDecl *FD = cast_or_null<FieldDecl>(static_cast<Decl*>(Fields[i])); |
| 755 | if (!FD) continue; // Already issued a diagnostic. |
Chris Lattner | 720a054 | 2007-01-25 00:44:24 +0000 | [diff] [blame] | 756 | |
| 757 | // Get the type for the field. |
Steve Naroff | d50c88e | 2007-04-05 21:15:20 +0000 | [diff] [blame] | 758 | Type *FDTy = FD->getType().getCanonicalType().getTypePtr(); |
Chris Lattner | 720a054 | 2007-01-25 00:44:24 +0000 | [diff] [blame] | 759 | |
Chris Lattner | bdf8b8d | 2007-01-24 02:11:17 +0000 | [diff] [blame] | 760 | // C99 6.7.2.1p2 - A field may not be a function type. |
Chris Lattner | 720a054 | 2007-01-25 00:44:24 +0000 | [diff] [blame] | 761 | if (isa<FunctionType>(FDTy)) { |
Chris Lattner | bdf8b8d | 2007-01-24 02:11:17 +0000 | [diff] [blame] | 762 | Diag(FD->getLocation(), diag::err_field_declared_as_function, |
| 763 | FD->getName()); |
Chris Lattner | 8262560 | 2007-01-24 02:26:21 +0000 | [diff] [blame] | 764 | delete FD; |
Chris Lattner | bdf8b8d | 2007-01-24 02:11:17 +0000 | [diff] [blame] | 765 | continue; |
| 766 | } |
| 767 | |
Chris Lattner | 8262560 | 2007-01-24 02:26:21 +0000 | [diff] [blame] | 768 | // C99 6.7.2.1p2 - A field may not be an incomplete type except... |
Chris Lattner | 720a054 | 2007-01-25 00:44:24 +0000 | [diff] [blame] | 769 | if (FDTy->isIncompleteType()) { |
Chris Lattner | 8262560 | 2007-01-24 02:26:21 +0000 | [diff] [blame] | 770 | if (i != NumFields-1 || // ... that the last member ... |
| 771 | Record->getKind() != Decl::Struct || // ... of a structure ... |
Chris Lattner | 720a054 | 2007-01-25 00:44:24 +0000 | [diff] [blame] | 772 | !isa<ArrayType>(FDTy)) { //... may have incomplete array type. |
Chris Lattner | 8262560 | 2007-01-24 02:26:21 +0000 | [diff] [blame] | 773 | Diag(FD->getLocation(), diag::err_field_incomplete, FD->getName()); |
| 774 | delete FD; |
| 775 | continue; |
| 776 | } |
Chris Lattner | 720a054 | 2007-01-25 00:44:24 +0000 | [diff] [blame] | 777 | if (NumNamedMembers < 1) { //... must have more than named member ... |
Chris Lattner | 8262560 | 2007-01-24 02:26:21 +0000 | [diff] [blame] | 778 | Diag(FD->getLocation(), diag::err_flexible_array_empty_struct, |
| 779 | FD->getName()); |
| 780 | delete FD; |
| 781 | continue; |
| 782 | } |
Chris Lattner | 720a054 | 2007-01-25 00:44:24 +0000 | [diff] [blame] | 783 | |
| 784 | // Okay, we have a legal flexible array member at the end of the struct. |
Chris Lattner | 4194315 | 2007-01-25 04:52:46 +0000 | [diff] [blame] | 785 | Record->setHasFlexibleArrayMember(true); |
Chris Lattner | bdf8b8d | 2007-01-24 02:11:17 +0000 | [diff] [blame] | 786 | } |
Chris Lattner | 720a054 | 2007-01-25 00:44:24 +0000 | [diff] [blame] | 787 | |
| 788 | |
| 789 | /// C99 6.7.2.1p2 - a struct ending in a flexible array member cannot be the |
| 790 | /// field of another structure or the element of an array. |
| 791 | if (RecordType *FDTTy = dyn_cast<RecordType>(FDTy)) { |
| 792 | if (FDTTy->getDecl()->hasFlexibleArrayMember()) { |
| 793 | // If this is a member of a union, then entire union becomes "flexible". |
| 794 | if (Record->getKind() == Decl::Union) { |
Chris Lattner | 4194315 | 2007-01-25 04:52:46 +0000 | [diff] [blame] | 795 | Record->setHasFlexibleArrayMember(true); |
Chris Lattner | 720a054 | 2007-01-25 00:44:24 +0000 | [diff] [blame] | 796 | } else { |
| 797 | // If this is a struct/class and this is not the last element, reject |
| 798 | // it. Note that GCC supports variable sized arrays in the middle of |
| 799 | // structures. |
| 800 | if (i != NumFields-1) { |
| 801 | Diag(FD->getLocation(), diag::err_variable_sized_type_in_struct, |
| 802 | FD->getName()); |
| 803 | delete FD; |
| 804 | continue; |
| 805 | } |
| 806 | |
| 807 | // We support flexible arrays at the end of structs in other structs |
| 808 | // as an extension. |
| 809 | Diag(FD->getLocation(), diag::ext_flexible_array_in_struct, |
| 810 | FD->getName()); |
Chris Lattner | 4194315 | 2007-01-25 04:52:46 +0000 | [diff] [blame] | 811 | Record->setHasFlexibleArrayMember(true); |
Chris Lattner | 720a054 | 2007-01-25 00:44:24 +0000 | [diff] [blame] | 812 | } |
| 813 | } |
| 814 | } |
| 815 | |
Chris Lattner | 8262560 | 2007-01-24 02:26:21 +0000 | [diff] [blame] | 816 | // Keep track of the number of named members. |
Chris Lattner | e5a6656 | 2007-01-25 22:48:42 +0000 | [diff] [blame] | 817 | if (IdentifierInfo *II = FD->getIdentifier()) { |
| 818 | // Detect duplicate member names. |
Chris Lattner | baf3366 | 2007-01-27 02:14:08 +0000 | [diff] [blame] | 819 | if (!FieldIDs.insert(II)) { |
Chris Lattner | e5a6656 | 2007-01-25 22:48:42 +0000 | [diff] [blame] | 820 | Diag(FD->getLocation(), diag::err_duplicate_member, II->getName()); |
| 821 | // Find the previous decl. |
| 822 | SourceLocation PrevLoc; |
| 823 | for (unsigned i = 0, e = RecFields.size(); ; ++i) { |
| 824 | assert(i != e && "Didn't find previous def!"); |
| 825 | if (RecFields[i]->getIdentifier() == II) { |
| 826 | PrevLoc = RecFields[i]->getLocation(); |
| 827 | break; |
| 828 | } |
| 829 | } |
| 830 | Diag(PrevLoc, diag::err_previous_definition); |
| 831 | delete FD; |
| 832 | continue; |
| 833 | } |
Chris Lattner | 8262560 | 2007-01-24 02:26:21 +0000 | [diff] [blame] | 834 | ++NumNamedMembers; |
Chris Lattner | e5a6656 | 2007-01-25 22:48:42 +0000 | [diff] [blame] | 835 | } |
Chris Lattner | 4194315 | 2007-01-25 04:52:46 +0000 | [diff] [blame] | 836 | |
| 837 | // Remember good fields. |
| 838 | RecFields.push_back(FD); |
Chris Lattner | bdf8b8d | 2007-01-24 02:11:17 +0000 | [diff] [blame] | 839 | } |
Chris Lattner | 8262560 | 2007-01-24 02:26:21 +0000 | [diff] [blame] | 840 | |
| 841 | |
| 842 | // Okay, we successfully defined 'Record'. |
Chris Lattner | 4194315 | 2007-01-25 04:52:46 +0000 | [diff] [blame] | 843 | Record->defineBody(&RecFields[0], RecFields.size()); |
Chris Lattner | 1300fb9 | 2007-01-23 23:42:53 +0000 | [diff] [blame] | 844 | } |
| 845 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 846 | Sema::DeclTy *Sema::ParseEnumConstant(Scope *S, DeclTy *EnumDeclX, |
| 847 | SourceLocation IdLoc, IdentifierInfo *Id, |
| 848 | SourceLocation EqualLoc, ExprTy *Val) { |
| 849 | EnumDecl *TheEnumDecl = cast<EnumDecl>(static_cast<Decl*>(EnumDeclX)); |
Chris Lattner | 8116d1b | 2007-01-25 22:38:29 +0000 | [diff] [blame] | 850 | |
| 851 | // Verify that there isn't already something declared with this name in this |
| 852 | // scope. |
Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 853 | if (Decl *PrevDecl = LookupScopedDecl(Id, Decl::IDNS_Ordinary, IdLoc, S)) { |
Chris Lattner | 8116d1b | 2007-01-25 22:38:29 +0000 | [diff] [blame] | 854 | if (S->isDeclScope(PrevDecl)) { |
| 855 | if (isa<EnumConstantDecl>(PrevDecl)) |
| 856 | Diag(IdLoc, diag::err_redefinition_of_enumerator, Id->getName()); |
| 857 | else |
| 858 | Diag(IdLoc, diag::err_redefinition, Id->getName()); |
| 859 | Diag(PrevDecl->getLocation(), diag::err_previous_definition); |
| 860 | return 0; |
| 861 | } |
| 862 | } |
Steve Naroff | 72cada0 | 2007-05-18 00:18:52 +0000 | [diff] [blame] | 863 | SourceLocation expLoc; |
Steve Naroff | 6396921 | 2007-05-07 21:22:42 +0000 | [diff] [blame] | 864 | // C99 6.7.2.2p2: Make sure we have an integer constant expression. |
Chris Lattner | 6046e00 | 2007-06-05 06:39:23 +0000 | [diff] [blame] | 865 | // FIXME: Capture this value in the enumconstantdecl. |
Chris Lattner | 238cbc5 | 2007-06-02 04:48:48 +0000 | [diff] [blame] | 866 | if (Val && !((Expr *)Val)->isIntegerConstantExpr(&expLoc)) { |
Steve Naroff | 72cada0 | 2007-05-18 00:18:52 +0000 | [diff] [blame] | 867 | Diag(expLoc, diag::err_enum_value_not_integer_constant_expr, Id->getName()); |
Steve Naroff | 6396921 | 2007-05-07 21:22:42 +0000 | [diff] [blame] | 868 | return 0; |
| 869 | } |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 870 | QualType Ty = Context.getTagDeclType(TheEnumDecl); |
Chris Lattner | 776fac8 | 2007-06-09 00:53:06 +0000 | [diff] [blame] | 871 | // FIXME: Chain EnumConstantDecl's together. |
| 872 | EnumConstantDecl *New = new EnumConstantDecl(IdLoc, Id, Ty, (Expr *)Val, 0); |
Chris Lattner | 8116d1b | 2007-01-25 22:38:29 +0000 | [diff] [blame] | 873 | |
| 874 | // Register this decl in the current scope stack. |
| 875 | New->setNext(Id->getFETokenInfo<Decl>()); |
| 876 | Id->setFETokenInfo(New); |
| 877 | S->AddDecl(New); |
| 878 | return New; |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | void Sema::ParseEnumBody(SourceLocation EnumLoc, DeclTy *EnumDeclX, |
| 882 | DeclTy **Elements, unsigned NumElements) { |
| 883 | EnumDecl *Enum = cast<EnumDecl>(static_cast<Decl*>(EnumDeclX)); |
| 884 | assert(!Enum->isDefinition() && "Enum redefinitions can't reach here"); |
| 885 | |
| 886 | // Verify that all the values are okay. |
| 887 | SmallVector<EnumConstantDecl*, 32> Values; |
| 888 | for (unsigned i = 0; i != NumElements; ++i) { |
| 889 | EnumConstantDecl *ECD = |
| 890 | cast_or_null<EnumConstantDecl>(static_cast<Decl*>(Elements[i])); |
| 891 | if (!ECD) continue; // Already issued a diagnostic. |
| 892 | |
| 893 | Values.push_back(ECD); |
| 894 | } |
| 895 | |
| 896 | Enum->defineElements(&Values[0], Values.size()); |
| 897 | } |
Chris Lattner | 1300fb9 | 2007-01-23 23:42:53 +0000 | [diff] [blame] | 898 | |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 899 | void Sema::AddTopLevelDecl(Decl *current, Decl *last) { |
| 900 | if (!current) return; |
| 901 | |
| 902 | // If this is a top-level decl that is chained to some other (e.g. int A,B,C;) |
| 903 | // remember this in the LastInGroupList list. |
Chris Lattner | 776fac8 | 2007-06-09 00:53:06 +0000 | [diff] [blame] | 904 | if (last) |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 905 | LastInGroupList.push_back((Decl*)last); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 906 | } |