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 | f84a79c | 2006-11-11 22:59:23 +0000 | [diff] [blame] | 17 | #include "clang/AST/Type.h" |
Chris Lattner | 591a675 | 2006-11-19 23:16:18 +0000 | [diff] [blame] | 18 | #include "clang/Parse/DeclSpec.h" |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 19 | #include "clang/Parse/Scope.h" |
| 20 | #include "clang/Lex/IdentifierTable.h" |
Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 21 | #include "clang/Basic/LangOptions.h" |
Chris Lattner | 697e5d6 | 2006-11-09 06:32:27 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | using namespace clang; |
| 24 | |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 25 | |
Chris Lattner | 2ebe4bb | 2006-11-20 01:29:42 +0000 | [diff] [blame] | 26 | Sema::DeclTy *Sema::isTypeName(const IdentifierInfo &II, Scope *S) const { |
| 27 | return dyn_cast_or_null<TypeDecl>(II.getFETokenInfo<Decl>()); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 28 | } |
| 29 | |
Chris Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 30 | void Sema::PopScope(SourceLocation Loc, Scope *S) { |
| 31 | for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end(); |
| 32 | I != E; ++I) { |
Chris Lattner | 99d3177 | 2007-01-21 22:37:37 +0000 | [diff] [blame] | 33 | Decl *D = static_cast<Decl*>(*I); |
Chris Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 34 | assert(D && "This decl didn't get pushed??"); |
Chris Lattner | ff65b6b | 2007-01-23 01:33:16 +0000 | [diff] [blame] | 35 | IdentifierInfo *II = D->getIdentifier(); |
| 36 | if (!II) continue; |
Chris Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 37 | |
Chris Lattner | ff65b6b | 2007-01-23 01:33:16 +0000 | [diff] [blame] | 38 | // Unlink this decl from the identifier. Because the scope contains decls |
| 39 | // in an unordered collection, and because we have multiple identifier |
| 40 | // namespaces (e.g. tag, normal, label),the decl may not be the first entry. |
| 41 | if (II->getFETokenInfo<Decl>() == D) { |
| 42 | // Normal case, no multiple decls in different namespaces. |
| 43 | II->setFETokenInfo(D->getNext()); |
| 44 | } else { |
| 45 | // Scan ahead. There are only three namespaces in C, so this loop can |
| 46 | // never execute more than 3 times. |
| 47 | Decl *SomeDecl = II->getFETokenInfo<Decl>(); |
| 48 | while (SomeDecl->getNext() != D) { |
| 49 | SomeDecl = SomeDecl->getNext(); |
| 50 | assert(SomeDecl && "Didn't find this decl on its identifier's chain!"); |
| 51 | } |
| 52 | SomeDecl->setNext(D->getNext()); |
| 53 | } |
Chris Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 54 | |
Chris Lattner | 740b2f3 | 2006-11-21 01:32:20 +0000 | [diff] [blame] | 55 | // This will have to be revisited for C++: there we want to nest stuff in |
| 56 | // namespace decls etc. Even for C, we might want a top-level translation |
| 57 | // unit decl or something. |
| 58 | if (!CurFunctionDecl) |
| 59 | continue; |
| 60 | |
| 61 | // Chain this decl to the containing function, it now owns the memory for |
| 62 | // the decl. |
| 63 | D->setNext(CurFunctionDecl->getDeclChain()); |
| 64 | CurFunctionDecl->setDeclChain(D); |
Chris Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
Chris Lattner | 200bdc3 | 2006-11-19 02:43:37 +0000 | [diff] [blame] | 68 | /// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with |
| 69 | /// no declarator (e.g. "struct foo;") is parsed. |
| 70 | Sema::DeclTy *Sema::ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS) { |
| 71 | // TODO: emit error on 'int;' or 'const enum foo;'. |
| 72 | // TODO: emit error on 'typedef int;' |
| 73 | // if (!DS.isMissingDeclaratorOk()) Diag(...); |
| 74 | |
| 75 | // TODO: Register 'struct foo;' with the type system as an opaque struct. |
| 76 | |
| 77 | // TODO: Check that we don't already have 'union foo;' or something else |
| 78 | // that conflicts. |
| 79 | return 0; |
| 80 | } |
| 81 | |
Chris Lattner | 18b1962 | 2007-01-22 07:39:13 +0000 | [diff] [blame] | 82 | /// LookupScopedDecl - Look up the inner-most declaration in the specified |
| 83 | /// namespace. |
| 84 | static Decl *LookupScopedDecl(IdentifierInfo *II, Decl::IdentifierNamespace NS){ |
| 85 | if (II == 0) return 0; |
| 86 | |
| 87 | // Scan up the scope chain looking for a decl that matches this identifier |
| 88 | // that is in the appropriate namespace. This search should not take long, as |
| 89 | // shadowing of names is uncommon, and deep shadowing is extremely uncommon. |
| 90 | for (Decl *D = II->getFETokenInfo<Decl>(); D; D = D->getNext()) |
| 91 | if (D->getIdentifierNamespace() == NS) |
| 92 | return D; |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 97 | Action::DeclTy * |
| 98 | Sema::ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init, |
| 99 | DeclTy *LastInGroup) { |
| 100 | IdentifierInfo *II = D.getIdentifier(); |
Chris Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 101 | |
Chris Lattner | 18b1962 | 2007-01-22 07:39:13 +0000 | [diff] [blame] | 102 | if (Decl *PrevDecl = LookupScopedDecl(II, Decl::IDNS_Ordinary)) { |
Chris Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 103 | // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope. |
Chris Lattner | 99d3177 | 2007-01-21 22:37:37 +0000 | [diff] [blame] | 104 | if (S->isDeclScope(PrevDecl)) { |
| 105 | // TODO: This is totally simplistic. It should handle merging functions |
| 106 | // together etc, merging extern int X; int X; ... |
| 107 | Diag(D.getIdentifierLoc(), diag::err_redefinition, II->getName()); |
| 108 | Diag(PrevDecl->getLocation(), diag::err_previous_definition); |
| 109 | } |
Chris Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 110 | } |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 111 | |
| 112 | Decl *New; |
Chris Lattner | 2114d5e | 2006-12-04 07:40:24 +0000 | [diff] [blame] | 113 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) |
Chris Lattner | 18b1962 | 2007-01-22 07:39:13 +0000 | [diff] [blame] | 114 | New = ParseTypedefDecl(S, D); |
Chris Lattner | 2114d5e | 2006-12-04 07:40:24 +0000 | [diff] [blame] | 115 | else if (D.isFunctionDeclarator()) |
Chris Lattner | 18b1962 | 2007-01-22 07:39:13 +0000 | [diff] [blame] | 116 | New = new FunctionDecl(D.getIdentifierLoc(), II, GetTypeForDeclarator(D,S)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 117 | else |
Chris Lattner | 18b1962 | 2007-01-22 07:39:13 +0000 | [diff] [blame] | 118 | New = new VarDecl(D.getIdentifierLoc(), II, GetTypeForDeclarator(D, S)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 119 | |
Chris Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 120 | if (!New) return 0; |
| 121 | |
| 122 | |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 123 | // If this has an identifier, add it to the scope stack. |
| 124 | if (II) { |
Chris Lattner | 18b1962 | 2007-01-22 07:39:13 +0000 | [diff] [blame] | 125 | New->setNext(II->getFETokenInfo<Decl>()); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 126 | II->setFETokenInfo(New); |
Chris Lattner | 99d3177 | 2007-01-21 22:37:37 +0000 | [diff] [blame] | 127 | S->AddDecl(New); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | // If this is a top-level decl that is chained to some other (e.g. int A,B,C;) |
| 131 | // remember this in the LastInGroupList list. |
| 132 | if (LastInGroup && S->getParent() == 0) |
| 133 | LastInGroupList.push_back((Decl*)LastInGroup); |
| 134 | |
| 135 | return New; |
| 136 | } |
| 137 | |
Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 138 | VarDecl * |
| 139 | Sema::ParseParamDeclarator(DeclaratorChunk &FTI, unsigned ArgNo, |
| 140 | Scope *FnScope) { |
| 141 | const DeclaratorChunk::ParamInfo &PI = FTI.Fun.ArgInfo[ArgNo]; |
Chris Lattner | 200bdc3 | 2006-11-19 02:43:37 +0000 | [diff] [blame] | 142 | |
Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 143 | IdentifierInfo *II = PI.Ident; |
Chris Lattner | c284e9b | 2007-01-23 05:14:32 +0000 | [diff] [blame] | 144 | // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope. |
| 145 | // Can this happen for params? We already checked that they don't conflict |
| 146 | // among each other. Here they can only shadow globals, which is ok. |
Chris Lattner | 18b1962 | 2007-01-22 07:39:13 +0000 | [diff] [blame] | 147 | if (Decl *PrevDecl = LookupScopedDecl(II, Decl::IDNS_Ordinary)) { |
Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 148 | |
Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Chris Lattner | 18b1962 | 2007-01-22 07:39:13 +0000 | [diff] [blame] | 151 | VarDecl *New = new VarDecl(PI.IdentLoc, II, static_cast<Type*>(PI.TypeInfo)); |
Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 152 | |
| 153 | // If this has an identifier, add it to the scope stack. |
| 154 | if (II) { |
Chris Lattner | 18b1962 | 2007-01-22 07:39:13 +0000 | [diff] [blame] | 155 | New->setNext(II->getFETokenInfo<Decl>()); |
Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 156 | II->setFETokenInfo(New); |
Chris Lattner | 99d3177 | 2007-01-21 22:37:37 +0000 | [diff] [blame] | 157 | FnScope->AddDecl(New); |
Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 158 | } |
Chris Lattner | 229ce60 | 2006-11-21 01:21:07 +0000 | [diff] [blame] | 159 | |
Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 160 | return New; |
| 161 | } |
| 162 | |
| 163 | |
| 164 | Sema::DeclTy *Sema::ParseStartOfFunctionDef(Scope *FnBodyScope, Declarator &D) { |
Chris Lattner | 229ce60 | 2006-11-21 01:21:07 +0000 | [diff] [blame] | 165 | assert(CurFunctionDecl == 0 && "Function parsing confused"); |
Chris Lattner | 5c5fbcc | 2006-12-03 08:41:30 +0000 | [diff] [blame] | 166 | assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function && |
| 167 | "Not a function declarator!"); |
| 168 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 169 | |
| 170 | // Verify 6.9.1p6: 'every identifier in the identifier list shall be declared' |
| 171 | // for a K&R function. |
| 172 | if (!FTI.hasPrototype) { |
| 173 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) { |
| 174 | if (FTI.ArgInfo[i].TypeInfo == 0) { |
| 175 | Diag(FTI.ArgInfo[i].IdentLoc, diag::err_param_not_declared, |
| 176 | FTI.ArgInfo[i].Ident->getName()); |
| 177 | // Implicitly declare the argument as type 'int' for lack of a better |
| 178 | // type. |
| 179 | FTI.ArgInfo[i].TypeInfo = Context.IntTy.getAsOpaquePtr(); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | // Since this is a function definition, act as though we have information |
| 184 | // about the arguments. |
| 185 | FTI.hasPrototype = true; |
Chris Lattner | 2114d5e | 2006-12-04 07:40:24 +0000 | [diff] [blame] | 186 | } else { |
| 187 | // FIXME: Diagnose arguments without names in C. |
| 188 | |
Chris Lattner | 5c5fbcc | 2006-12-03 08:41:30 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 191 | Scope *GlobalScope = FnBodyScope->getParent(); |
| 192 | |
| 193 | FunctionDecl *FD = |
| 194 | static_cast<FunctionDecl*>(ParseDeclarator(GlobalScope, D, 0, 0)); |
Chris Lattner | 229ce60 | 2006-11-21 01:21:07 +0000 | [diff] [blame] | 195 | CurFunctionDecl = FD; |
Chris Lattner | 2114d5e | 2006-12-04 07:40:24 +0000 | [diff] [blame] | 196 | |
Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 197 | // Create Decl objects for each parameter, adding them to the FunctionDecl. |
| 198 | SmallVector<VarDecl*, 16> Params; |
Chris Lattner | f61c8a8 | 2007-01-21 19:04:43 +0000 | [diff] [blame] | 199 | |
| 200 | // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs function that takes |
| 201 | // no arguments, not a function that takes a single void argument. |
| 202 | if (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
| 203 | FTI.ArgInfo[0].TypeInfo == Context.VoidTy.getAsOpaquePtr()) { |
Chris Lattner | 99d3177 | 2007-01-21 22:37:37 +0000 | [diff] [blame] | 204 | // empty arg list, don't push any params. |
Chris Lattner | f61c8a8 | 2007-01-21 19:04:43 +0000 | [diff] [blame] | 205 | } else { |
| 206 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) |
| 207 | Params.push_back(ParseParamDeclarator(D.getTypeObject(0), i,FnBodyScope)); |
| 208 | } |
Chris Lattner | 2114d5e | 2006-12-04 07:40:24 +0000 | [diff] [blame] | 209 | |
Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 210 | FD->setParams(&Params[0], Params.size()); |
Chris Lattner | 2114d5e | 2006-12-04 07:40:24 +0000 | [diff] [blame] | 211 | |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 212 | return FD; |
| 213 | } |
| 214 | |
Chris Lattner | 229ce60 | 2006-11-21 01:21:07 +0000 | [diff] [blame] | 215 | Sema::DeclTy *Sema::ParseFunctionDefBody(DeclTy *D, StmtTy *Body) { |
| 216 | FunctionDecl *FD = static_cast<FunctionDecl*>(D); |
| 217 | FD->setBody((Stmt*)Body); |
| 218 | |
| 219 | assert(FD == CurFunctionDecl && "Function parsing confused"); |
| 220 | CurFunctionDecl = 0; |
| 221 | return FD; |
| 222 | } |
| 223 | |
| 224 | |
Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 225 | /// ImplicitlyDefineFunction - An undeclared identifier was used in a function |
| 226 | /// call, forming a call to an implicitly defined function (per C99 6.5.1p2). |
| 227 | Decl *Sema::ImplicitlyDefineFunction(SourceLocation Loc, IdentifierInfo &II, |
| 228 | Scope *S) { |
| 229 | if (getLangOptions().C99) // Extension in C99. |
| 230 | Diag(Loc, diag::ext_implicit_function_decl, II.getName()); |
| 231 | else // Legal in C90, but warn about it. |
| 232 | Diag(Loc, diag::warn_implicit_function_decl, II.getName()); |
| 233 | |
| 234 | // FIXME: handle stuff like: |
| 235 | // void foo() { extern float X(); } |
| 236 | // void bar() { X(); } <-- implicit decl for X in another scope. |
| 237 | |
| 238 | // Set a Declarator for the implicit definition: int foo(); |
Chris Lattner | 353f574 | 2006-11-28 04:50:12 +0000 | [diff] [blame] | 239 | const char *Dummy; |
Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 240 | DeclSpec DS; |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 241 | bool Error = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, Dummy); |
Chris Lattner | 353f574 | 2006-11-28 04:50:12 +0000 | [diff] [blame] | 242 | assert(!Error && "Error setting up implicit decl!"); |
Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 243 | Declarator D(DS, Declarator::BlockContext); |
Chris Lattner | cbc426d | 2006-12-02 06:43:02 +0000 | [diff] [blame] | 244 | D.AddTypeInfo(DeclaratorChunk::getFunction(false, false, 0, 0, Loc)); |
Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 245 | D.SetIdentifier(&II, Loc); |
| 246 | |
| 247 | Decl *Result = static_cast<Decl*>(ParseDeclarator(S, D, 0, 0)); |
| 248 | |
| 249 | // Visit this implicit declaration like any other top-level form. |
| 250 | LastInGroupList.push_back(Result); |
| 251 | return Result; |
| 252 | } |
| 253 | |
Chris Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 254 | |
Chris Lattner | 18b1962 | 2007-01-22 07:39:13 +0000 | [diff] [blame] | 255 | Decl *Sema::ParseTypedefDecl(Scope *S, Declarator &D) { |
Chris Lattner | da8aa7b | 2006-11-19 23:12:30 +0000 | [diff] [blame] | 256 | assert(D.getIdentifier() && "Wrong callback for declspec withotu declarator"); |
Chris Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 257 | |
Chris Lattner | 5ca17df | 2006-11-19 23:32:49 +0000 | [diff] [blame] | 258 | TypeRef T = GetTypeForDeclarator(D, S); |
Chris Lattner | 0d8b1a1 | 2006-11-20 04:34:45 +0000 | [diff] [blame] | 259 | if (T.isNull()) return 0; |
| 260 | |
Chris Lattner | 18b1962 | 2007-01-22 07:39:13 +0000 | [diff] [blame] | 261 | // Scope manipulation handled by caller. |
| 262 | return new TypedefDecl(D.getIdentifierLoc(), D.getIdentifier(), T); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Chris Lattner | 18b1962 | 2007-01-22 07:39:13 +0000 | [diff] [blame] | 265 | |
| 266 | /// ParseStructUnionTag - This is invoked when we see 'struct foo' or |
| 267 | /// 'struct {'. In the former case, Name will be non-null. In the later case, |
Chris Lattner | 7b9ace6 | 2007-01-23 20:11:08 +0000 | [diff] [blame^] | 268 | /// Name will be null. TagType indicates what kind of tag this is. TK indicates |
| 269 | /// whether this is a reference/declaration/definition of a tag. |
| 270 | Sema::DeclTy *Sema::ParseTag(Scope *S, unsigned TagType, TagKind TK, |
Chris Lattner | f34c4da | 2007-01-23 04:08:05 +0000 | [diff] [blame] | 271 | SourceLocation KWLoc, IdentifierInfo *Name, |
| 272 | SourceLocation NameLoc) { |
Chris Lattner | 8799cf2 | 2007-01-23 01:57:16 +0000 | [diff] [blame] | 273 | // 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^] | 274 | assert((Name != 0 || TK == TK_Definition) && |
| 275 | "Nameless record must be a definition!"); |
Chris Lattner | 8799cf2 | 2007-01-23 01:57:16 +0000 | [diff] [blame] | 276 | |
Chris Lattner | f34c4da | 2007-01-23 04:08:05 +0000 | [diff] [blame] | 277 | Decl::Kind Kind; |
Chris Lattner | bf0b798 | 2007-01-23 04:27:41 +0000 | [diff] [blame] | 278 | switch (TagType) { |
Chris Lattner | f34c4da | 2007-01-23 04:08:05 +0000 | [diff] [blame] | 279 | default: assert(0 && "Unknown tag type!"); |
Chris Lattner | bf0b798 | 2007-01-23 04:27:41 +0000 | [diff] [blame] | 280 | case DeclSpec::TST_struct: Kind = Decl::Struct; break; |
| 281 | case DeclSpec::TST_union: Kind = Decl::Union; break; |
| 282 | //case DeclSpec::TST_class: Kind = Decl::Class; break; |
| 283 | case DeclSpec::TST_enum: Kind = Decl::Enum; break; |
Chris Lattner | f34c4da | 2007-01-23 04:08:05 +0000 | [diff] [blame] | 284 | } |
Chris Lattner | 7e783a1 | 2007-01-23 02:05:42 +0000 | [diff] [blame] | 285 | |
Chris Lattner | 18b1962 | 2007-01-22 07:39:13 +0000 | [diff] [blame] | 286 | // If this is a named struct, check to see if there was a previous forward |
| 287 | // declaration or definition. |
Chris Lattner | 7b9ace6 | 2007-01-23 20:11:08 +0000 | [diff] [blame^] | 288 | if (TagDecl *PrevDecl = |
| 289 | dyn_cast_or_null<TagDecl>(LookupScopedDecl(Name, Decl::IDNS_Tag))) { |
Chris Lattner | 8799cf2 | 2007-01-23 01:57:16 +0000 | [diff] [blame] | 290 | |
| 291 | // If this is a use of a previous tag, or if the tag is already declared in |
| 292 | // the same scope (so that the definition/declaration completes or |
| 293 | // rementions the tag), reuse the decl. |
Chris Lattner | 7b9ace6 | 2007-01-23 20:11:08 +0000 | [diff] [blame^] | 294 | if (TK == TK_Reference || S->isDeclScope(PrevDecl)) { |
Chris Lattner | 7e783a1 | 2007-01-23 02:05:42 +0000 | [diff] [blame] | 295 | // Make sure that this wasn't declared as an enum and now used as a struct |
| 296 | // or something similar. |
| 297 | if (PrevDecl->getKind() != Kind) { |
Chris Lattner | f34c4da | 2007-01-23 04:08:05 +0000 | [diff] [blame] | 298 | Diag(KWLoc, diag::err_use_with_wrong_tag, Name->getName()); |
Chris Lattner | 7e783a1 | 2007-01-23 02:05:42 +0000 | [diff] [blame] | 299 | Diag(PrevDecl->getLocation(), diag::err_previous_use); |
| 300 | } |
Chris Lattner | 7b9ace6 | 2007-01-23 20:11:08 +0000 | [diff] [blame^] | 301 | |
| 302 | // If this is a use or a forward declaration, we're good. |
| 303 | if (TK != TK_Definition) |
| 304 | return PrevDecl; |
Chris Lattner | f34c4da | 2007-01-23 04:08:05 +0000 | [diff] [blame] | 305 | |
Chris Lattner | 7b9ace6 | 2007-01-23 20:11:08 +0000 | [diff] [blame^] | 306 | // Diagnose attempts to redefine a tag. |
| 307 | if (PrevDecl->isDefinition()) { |
| 308 | Diag(NameLoc, diag::err_redefinition, Name->getName()); |
| 309 | Diag(PrevDecl->getLocation(), diag::err_previous_definition); |
| 310 | // If this is a redefinition, recover by making this struct be |
| 311 | // anonymous, which will make any later references get the previous |
| 312 | // definition. |
| 313 | Name = 0; |
| 314 | } else { |
| 315 | // Okay, this is definition of a previously declared or referenced tag. |
| 316 | // Move the location of the decl to be the definition site. |
| 317 | PrevDecl->setLocation(NameLoc); |
| 318 | PrevDecl->setDefinition(true); |
| 319 | return PrevDecl; |
| 320 | } |
Chris Lattner | 8799cf2 | 2007-01-23 01:57:16 +0000 | [diff] [blame] | 321 | } |
Chris Lattner | f34c4da | 2007-01-23 04:08:05 +0000 | [diff] [blame] | 322 | // If we get here, this is a definition of a new struct type in a nested |
| 323 | // scope, e.g. "struct foo; void bar() { struct foo; }", just create a new |
| 324 | // type. |
Chris Lattner | 18b1962 | 2007-01-22 07:39:13 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Chris Lattner | bf0b798 | 2007-01-23 04:27:41 +0000 | [diff] [blame] | 327 | // If there is an identifier, use the location of the identifier as the |
| 328 | // location of the decl, otherwise use the location of the struct/union |
| 329 | // keyword. |
| 330 | SourceLocation Loc = NameLoc.isValid() ? NameLoc : KWLoc; |
| 331 | |
Chris Lattner | 18b1962 | 2007-01-22 07:39:13 +0000 | [diff] [blame] | 332 | // 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^] | 333 | TagDecl *New; |
Chris Lattner | f34c4da | 2007-01-23 04:08:05 +0000 | [diff] [blame] | 334 | if (Kind != Decl::Enum) |
| 335 | New = new RecordDecl(Kind, Loc, Name); |
| 336 | else |
| 337 | assert(0 && "Enum tags not implemented yet!"); |
Chris Lattner | 18b1962 | 2007-01-22 07:39:13 +0000 | [diff] [blame] | 338 | |
Chris Lattner | 7b9ace6 | 2007-01-23 20:11:08 +0000 | [diff] [blame^] | 339 | if (TK == TK_Definition) |
| 340 | New->setDefinition(true); |
| 341 | |
Chris Lattner | 18b1962 | 2007-01-22 07:39:13 +0000 | [diff] [blame] | 342 | // If this has an identifier, add it to the scope stack. |
| 343 | if (Name) { |
| 344 | New->setNext(Name->getFETokenInfo<Decl>()); |
| 345 | Name->setFETokenInfo(New); |
| 346 | S->AddDecl(New); |
| 347 | } |
| 348 | |
| 349 | return New; |
| 350 | } |