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