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