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