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