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) { |
| 33 | IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I); |
| 34 | Decl *D = II.getFETokenInfo<Decl>(); |
| 35 | assert(D && "This decl didn't get pushed??"); |
| 36 | |
Chris Lattner | 229ce60 | 2006-11-21 01:21:07 +0000 | [diff] [blame] | 37 | II.setFETokenInfo(D->getNext()); |
Chris Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 38 | |
Chris Lattner | 740b2f3 | 2006-11-21 01:32:20 +0000 | [diff] [blame] | 39 | // This will have to be revisited for C++: there we want to nest stuff in |
| 40 | // namespace decls etc. Even for C, we might want a top-level translation |
| 41 | // unit decl or something. |
| 42 | if (!CurFunctionDecl) |
| 43 | continue; |
| 44 | |
| 45 | // Chain this decl to the containing function, it now owns the memory for |
| 46 | // the decl. |
| 47 | D->setNext(CurFunctionDecl->getDeclChain()); |
| 48 | CurFunctionDecl->setDeclChain(D); |
Chris Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
Chris Lattner | 200bdc3 | 2006-11-19 02:43:37 +0000 | [diff] [blame] | 52 | /// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with |
| 53 | /// no declarator (e.g. "struct foo;") is parsed. |
| 54 | Sema::DeclTy *Sema::ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS) { |
| 55 | // TODO: emit error on 'int;' or 'const enum foo;'. |
| 56 | // TODO: emit error on 'typedef int;' |
| 57 | // if (!DS.isMissingDeclaratorOk()) Diag(...); |
| 58 | |
| 59 | // TODO: Register 'struct foo;' with the type system as an opaque struct. |
| 60 | |
| 61 | // TODO: Check that we don't already have 'union foo;' or something else |
| 62 | // that conflicts. |
| 63 | return 0; |
| 64 | } |
| 65 | |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 66 | Action::DeclTy * |
| 67 | Sema::ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init, |
| 68 | DeclTy *LastInGroup) { |
| 69 | IdentifierInfo *II = D.getIdentifier(); |
Chris Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 70 | Decl *PrevDecl = 0; |
| 71 | |
| 72 | if (II) { |
| 73 | PrevDecl = II->getFETokenInfo<Decl>(); |
| 74 | |
| 75 | // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope. |
| 76 | } |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 77 | |
| 78 | Decl *New; |
Chris Lattner | 2114d5e | 2006-12-04 07:40:24 +0000 | [diff] [blame] | 79 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) |
Chris Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 80 | New = ParseTypedefDecl(S, D, PrevDecl); |
Chris Lattner | 2114d5e | 2006-12-04 07:40:24 +0000 | [diff] [blame] | 81 | else if (D.isFunctionDeclarator()) |
Chris Lattner | 5ca17df | 2006-11-19 23:32:49 +0000 | [diff] [blame] | 82 | New = new FunctionDecl(II, GetTypeForDeclarator(D, S), PrevDecl); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 83 | else |
Chris Lattner | 5ca17df | 2006-11-19 23:32:49 +0000 | [diff] [blame] | 84 | New = new VarDecl(II, GetTypeForDeclarator(D, S), PrevDecl); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 85 | |
Chris Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 86 | if (!New) return 0; |
| 87 | |
| 88 | |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 89 | // If this has an identifier, add it to the scope stack. |
| 90 | if (II) { |
| 91 | // If PrevDecl includes conflicting name here, emit a diagnostic. |
| 92 | II->setFETokenInfo(New); |
| 93 | S->AddDecl(II); |
| 94 | } |
| 95 | |
| 96 | // If this is a top-level decl that is chained to some other (e.g. int A,B,C;) |
| 97 | // remember this in the LastInGroupList list. |
| 98 | if (LastInGroup && S->getParent() == 0) |
| 99 | LastInGroupList.push_back((Decl*)LastInGroup); |
| 100 | |
| 101 | return New; |
| 102 | } |
| 103 | |
Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame^] | 104 | VarDecl * |
| 105 | Sema::ParseParamDeclarator(DeclaratorChunk &FTI, unsigned ArgNo, |
| 106 | Scope *FnScope) { |
| 107 | const DeclaratorChunk::ParamInfo &PI = FTI.Fun.ArgInfo[ArgNo]; |
Chris Lattner | 200bdc3 | 2006-11-19 02:43:37 +0000 | [diff] [blame] | 108 | |
Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame^] | 109 | IdentifierInfo *II = PI.Ident; |
| 110 | Decl *PrevDecl = 0; |
| 111 | |
| 112 | if (II) { |
| 113 | PrevDecl = II->getFETokenInfo<Decl>(); |
| 114 | |
| 115 | // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope. |
| 116 | } |
| 117 | |
| 118 | VarDecl *New = new VarDecl(II, static_cast<Type*>(PI.TypeInfo), PrevDecl); |
| 119 | |
| 120 | // If this has an identifier, add it to the scope stack. |
| 121 | if (II) { |
| 122 | // If PrevDecl includes conflicting name here, emit a diagnostic. |
| 123 | II->setFETokenInfo(New); |
| 124 | FnScope->AddDecl(II); |
| 125 | } |
Chris Lattner | 229ce60 | 2006-11-21 01:21:07 +0000 | [diff] [blame] | 126 | |
Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame^] | 127 | return New; |
| 128 | } |
| 129 | |
| 130 | |
| 131 | Sema::DeclTy *Sema::ParseStartOfFunctionDef(Scope *FnBodyScope, Declarator &D) { |
Chris Lattner | 229ce60 | 2006-11-21 01:21:07 +0000 | [diff] [blame] | 132 | assert(CurFunctionDecl == 0 && "Function parsing confused"); |
Chris Lattner | 5c5fbcc | 2006-12-03 08:41:30 +0000 | [diff] [blame] | 133 | assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function && |
| 134 | "Not a function declarator!"); |
| 135 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 136 | |
| 137 | // Verify 6.9.1p6: 'every identifier in the identifier list shall be declared' |
| 138 | // for a K&R function. |
| 139 | if (!FTI.hasPrototype) { |
| 140 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) { |
| 141 | if (FTI.ArgInfo[i].TypeInfo == 0) { |
| 142 | Diag(FTI.ArgInfo[i].IdentLoc, diag::err_param_not_declared, |
| 143 | FTI.ArgInfo[i].Ident->getName()); |
| 144 | // Implicitly declare the argument as type 'int' for lack of a better |
| 145 | // type. |
| 146 | FTI.ArgInfo[i].TypeInfo = Context.IntTy.getAsOpaquePtr(); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // Since this is a function definition, act as though we have information |
| 151 | // about the arguments. |
| 152 | FTI.hasPrototype = true; |
Chris Lattner | 2114d5e | 2006-12-04 07:40:24 +0000 | [diff] [blame] | 153 | } else { |
| 154 | // FIXME: Diagnose arguments without names in C. |
| 155 | |
Chris Lattner | 5c5fbcc | 2006-12-03 08:41:30 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame^] | 158 | Scope *GlobalScope = FnBodyScope->getParent(); |
| 159 | |
| 160 | FunctionDecl *FD = |
| 161 | static_cast<FunctionDecl*>(ParseDeclarator(GlobalScope, D, 0, 0)); |
Chris Lattner | 229ce60 | 2006-11-21 01:21:07 +0000 | [diff] [blame] | 162 | CurFunctionDecl = FD; |
Chris Lattner | 2114d5e | 2006-12-04 07:40:24 +0000 | [diff] [blame] | 163 | |
Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame^] | 164 | // Create Decl objects for each parameter, adding them to the FunctionDecl. |
| 165 | SmallVector<VarDecl*, 16> Params; |
| 166 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) |
| 167 | Params.push_back(ParseParamDeclarator(D.getTypeObject(0), i, FnBodyScope)); |
Chris Lattner | 2114d5e | 2006-12-04 07:40:24 +0000 | [diff] [blame] | 168 | |
Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame^] | 169 | FD->setParams(&Params[0], Params.size()); |
Chris Lattner | 2114d5e | 2006-12-04 07:40:24 +0000 | [diff] [blame] | 170 | |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 171 | return FD; |
| 172 | } |
| 173 | |
Chris Lattner | 229ce60 | 2006-11-21 01:21:07 +0000 | [diff] [blame] | 174 | Sema::DeclTy *Sema::ParseFunctionDefBody(DeclTy *D, StmtTy *Body) { |
| 175 | FunctionDecl *FD = static_cast<FunctionDecl*>(D); |
| 176 | FD->setBody((Stmt*)Body); |
| 177 | |
| 178 | assert(FD == CurFunctionDecl && "Function parsing confused"); |
| 179 | CurFunctionDecl = 0; |
| 180 | return FD; |
| 181 | } |
| 182 | |
| 183 | |
Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 184 | /// ImplicitlyDefineFunction - An undeclared identifier was used in a function |
| 185 | /// call, forming a call to an implicitly defined function (per C99 6.5.1p2). |
| 186 | Decl *Sema::ImplicitlyDefineFunction(SourceLocation Loc, IdentifierInfo &II, |
| 187 | Scope *S) { |
| 188 | if (getLangOptions().C99) // Extension in C99. |
| 189 | Diag(Loc, diag::ext_implicit_function_decl, II.getName()); |
| 190 | else // Legal in C90, but warn about it. |
| 191 | Diag(Loc, diag::warn_implicit_function_decl, II.getName()); |
| 192 | |
| 193 | // FIXME: handle stuff like: |
| 194 | // void foo() { extern float X(); } |
| 195 | // void bar() { X(); } <-- implicit decl for X in another scope. |
| 196 | |
| 197 | // Set a Declarator for the implicit definition: int foo(); |
Chris Lattner | 353f574 | 2006-11-28 04:50:12 +0000 | [diff] [blame] | 198 | const char *Dummy; |
Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 199 | DeclSpec DS; |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 200 | bool Error = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, Dummy); |
Chris Lattner | 353f574 | 2006-11-28 04:50:12 +0000 | [diff] [blame] | 201 | assert(!Error && "Error setting up implicit decl!"); |
Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 202 | Declarator D(DS, Declarator::BlockContext); |
Chris Lattner | cbc426d | 2006-12-02 06:43:02 +0000 | [diff] [blame] | 203 | D.AddTypeInfo(DeclaratorChunk::getFunction(false, false, 0, 0, Loc)); |
Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 204 | D.SetIdentifier(&II, Loc); |
| 205 | |
| 206 | Decl *Result = static_cast<Decl*>(ParseDeclarator(S, D, 0, 0)); |
| 207 | |
| 208 | // Visit this implicit declaration like any other top-level form. |
| 209 | LastInGroupList.push_back(Result); |
| 210 | return Result; |
| 211 | } |
| 212 | |
Chris Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 213 | |
| 214 | Decl *Sema::ParseTypedefDecl(Scope *S, Declarator &D, Decl *PrevDecl) { |
Chris Lattner | da8aa7b | 2006-11-19 23:12:30 +0000 | [diff] [blame] | 215 | assert(D.getIdentifier() && "Wrong callback for declspec withotu declarator"); |
Chris Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 216 | |
Chris Lattner | 5ca17df | 2006-11-19 23:32:49 +0000 | [diff] [blame] | 217 | TypeRef T = GetTypeForDeclarator(D, S); |
Chris Lattner | 0d8b1a1 | 2006-11-20 04:34:45 +0000 | [diff] [blame] | 218 | if (T.isNull()) return 0; |
| 219 | |
Chris Lattner | 5ca17df | 2006-11-19 23:32:49 +0000 | [diff] [blame] | 220 | return new TypedefDecl(D.getIdentifier(), T, PrevDecl); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 221 | } |
| 222 | |