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