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