Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- SemaDecl.cpp - Semantic Analysis for Declarations ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for declarations. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
Chris Lattner | e1e7985 | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTConsumer.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
| 17 | #include "clang/AST/Builtins.h" |
| 18 | #include "clang/AST/Decl.h" |
| 19 | #include "clang/AST/Expr.h" |
| 20 | #include "clang/AST/Type.h" |
| 21 | #include "clang/Parse/DeclSpec.h" |
| 22 | #include "clang/Parse/Scope.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 23 | #include "clang/Basic/LangOptions.h" |
| 24 | #include "clang/Basic/TargetInfo.h" |
Steve Naroff | 4c49a6c | 2008-01-30 23:46:05 +0000 | [diff] [blame] | 25 | #include "clang/Basic/SourceManager.h" |
| 26 | // FIXME: layering (ideally, Sema shouldn't be dependent on Lex API's) |
Chris Lattner | e1e7985 | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 27 | #include "clang/Lex/Preprocessor.h" |
Steve Naroff | 4c49a6c | 2008-01-30 23:46:05 +0000 | [diff] [blame] | 28 | #include "clang/Lex/HeaderSearch.h" |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/SmallString.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/SmallSet.h" |
Fariborz Jahanian | 85ff264 | 2007-10-05 18:00:57 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/DenseSet.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 32 | using namespace clang; |
| 33 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 34 | Sema::DeclTy *Sema::isTypeName(const IdentifierInfo &II, Scope *S) const { |
Fariborz Jahanian | bece4ac | 2007-10-12 16:34:10 +0000 | [diff] [blame] | 35 | Decl *IIDecl = II.getFETokenInfo<Decl>(); |
| 36 | // Find first occurance of none-tagged declaration |
| 37 | while(IIDecl && IIDecl->getIdentifierNamespace() != Decl::IDNS_Ordinary) |
| 38 | IIDecl = cast<ScopedDecl>(IIDecl)->getNext(); |
| 39 | if (!IIDecl) |
| 40 | return 0; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 41 | if (isa<TypedefDecl>(IIDecl) || isa<ObjCInterfaceDecl>(IIDecl)) |
Fariborz Jahanian | bece4ac | 2007-10-12 16:34:10 +0000 | [diff] [blame] | 42 | return IIDecl; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 43 | if (ObjCCompatibleAliasDecl *ADecl = |
| 44 | dyn_cast<ObjCCompatibleAliasDecl>(IIDecl)) |
Fariborz Jahanian | bece4ac | 2007-10-12 16:34:10 +0000 | [diff] [blame] | 45 | return ADecl->getClassInterface(); |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 46 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Steve Naroff | b216c88 | 2007-10-09 22:01:59 +0000 | [diff] [blame] | 49 | void Sema::ActOnPopScope(SourceLocation Loc, Scope *S) { |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 50 | if (S->decl_empty()) return; |
| 51 | assert((S->getFlags() & Scope::DeclScope) &&"Scope shouldn't contain decls!"); |
| 52 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 53 | for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end(); |
| 54 | I != E; ++I) { |
Steve Naroff | c752d04 | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 55 | Decl *TmpD = static_cast<Decl*>(*I); |
| 56 | assert(TmpD && "This decl didn't get pushed??"); |
| 57 | ScopedDecl *D = dyn_cast<ScopedDecl>(TmpD); |
| 58 | assert(D && "This decl isn't a ScopedDecl?"); |
| 59 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 60 | IdentifierInfo *II = D->getIdentifier(); |
| 61 | if (!II) continue; |
| 62 | |
| 63 | // Unlink this decl from the identifier. Because the scope contains decls |
| 64 | // in an unordered collection, and because we have multiple identifier |
| 65 | // namespaces (e.g. tag, normal, label),the decl may not be the first entry. |
| 66 | if (II->getFETokenInfo<Decl>() == D) { |
| 67 | // Normal case, no multiple decls in different namespaces. |
| 68 | II->setFETokenInfo(D->getNext()); |
| 69 | } else { |
| 70 | // Scan ahead. There are only three namespaces in C, so this loop can |
| 71 | // never execute more than 3 times. |
Steve Naroff | c752d04 | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 72 | ScopedDecl *SomeDecl = II->getFETokenInfo<ScopedDecl>(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 73 | while (SomeDecl->getNext() != D) { |
| 74 | SomeDecl = SomeDecl->getNext(); |
| 75 | assert(SomeDecl && "Didn't find this decl on its identifier's chain!"); |
| 76 | } |
| 77 | SomeDecl->setNext(D->getNext()); |
| 78 | } |
| 79 | |
| 80 | // This will have to be revisited for C++: there we want to nest stuff in |
| 81 | // namespace decls etc. Even for C, we might want a top-level translation |
| 82 | // unit decl or something. |
| 83 | if (!CurFunctionDecl) |
| 84 | continue; |
| 85 | |
| 86 | // Chain this decl to the containing function, it now owns the memory for |
| 87 | // the decl. |
| 88 | D->setNext(CurFunctionDecl->getDeclChain()); |
| 89 | CurFunctionDecl->setDeclChain(D); |
| 90 | } |
| 91 | } |
| 92 | |
Fariborz Jahanian | 4cabdfc | 2007-10-12 19:38:20 +0000 | [diff] [blame] | 93 | /// LookupInterfaceDecl - Lookup interface declaration in the scope chain. |
| 94 | /// Return the first declaration found (which may or may not be a class |
Fariborz Jahanian | 3fe44e4 | 2007-10-12 19:53:08 +0000 | [diff] [blame] | 95 | /// declaration. Caller is responsible for handling the none-class case. |
Fariborz Jahanian | 4cabdfc | 2007-10-12 19:38:20 +0000 | [diff] [blame] | 96 | /// Bypassing the alias of a class by returning the aliased class. |
| 97 | ScopedDecl *Sema::LookupInterfaceDecl(IdentifierInfo *ClassName) { |
| 98 | ScopedDecl *IDecl; |
| 99 | // Scan up the scope chain looking for a decl that matches this identifier |
| 100 | // that is in the appropriate namespace. |
| 101 | for (IDecl = ClassName->getFETokenInfo<ScopedDecl>(); IDecl; |
| 102 | IDecl = IDecl->getNext()) |
| 103 | if (IDecl->getIdentifierNamespace() == Decl::IDNS_Ordinary) |
| 104 | break; |
| 105 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 106 | if (ObjCCompatibleAliasDecl *ADecl = |
| 107 | dyn_cast_or_null<ObjCCompatibleAliasDecl>(IDecl)) |
Fariborz Jahanian | 4cabdfc | 2007-10-12 19:38:20 +0000 | [diff] [blame] | 108 | return ADecl->getClassInterface(); |
| 109 | return IDecl; |
| 110 | } |
| 111 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 112 | /// getObjCInterfaceDecl - Look up a for a class declaration in the scope. |
Fariborz Jahanian | 9d048ff | 2007-09-29 00:54:24 +0000 | [diff] [blame] | 113 | /// return 0 if one not found. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 114 | ObjCInterfaceDecl *Sema::getObjCInterfaceDecl(IdentifierInfo *Id) { |
Fariborz Jahanian | 4cabdfc | 2007-10-12 19:38:20 +0000 | [diff] [blame] | 115 | ScopedDecl *IdDecl = LookupInterfaceDecl(Id); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 116 | return cast_or_null<ObjCInterfaceDecl>(IdDecl); |
Fariborz Jahanian | 9d048ff | 2007-09-29 00:54:24 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 119 | /// LookupScopedDecl - Look up the inner-most declaration in the specified |
| 120 | /// namespace. |
Steve Naroff | c752d04 | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 121 | ScopedDecl *Sema::LookupScopedDecl(IdentifierInfo *II, unsigned NSI, |
| 122 | SourceLocation IdLoc, Scope *S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 123 | if (II == 0) return 0; |
| 124 | Decl::IdentifierNamespace NS = (Decl::IdentifierNamespace)NSI; |
| 125 | |
| 126 | // Scan up the scope chain looking for a decl that matches this identifier |
| 127 | // that is in the appropriate namespace. This search should not take long, as |
| 128 | // shadowing of names is uncommon, and deep shadowing is extremely uncommon. |
Steve Naroff | c752d04 | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 129 | for (ScopedDecl *D = II->getFETokenInfo<ScopedDecl>(); D; D = D->getNext()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 130 | if (D->getIdentifierNamespace() == NS) |
| 131 | return D; |
| 132 | |
| 133 | // If we didn't find a use of this identifier, and if the identifier |
| 134 | // corresponds to a compiler builtin, create the decl object for the builtin |
| 135 | // now, injecting it into translation unit scope, and return it. |
| 136 | if (NS == Decl::IDNS_Ordinary) { |
| 137 | // If this is a builtin on some other target, or if this builtin varies |
| 138 | // across targets (e.g. in type), emit a diagnostic and mark the translation |
| 139 | // unit non-portable for using it. |
| 140 | if (II->isNonPortableBuiltin()) { |
| 141 | // Only emit this diagnostic once for this builtin. |
| 142 | II->setNonPortableBuiltin(false); |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 143 | Context.Target.DiagnoseNonPortability(Context.getFullLoc(IdLoc), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 144 | diag::port_target_builtin_use); |
| 145 | } |
| 146 | // If this is a builtin on this (or all) targets, create the decl. |
| 147 | if (unsigned BuiltinID = II->getBuiltinID()) |
| 148 | return LazilyCreateBuiltin(II, BuiltinID, S); |
| 149 | } |
| 150 | return 0; |
| 151 | } |
| 152 | |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 153 | void Sema::InitBuiltinVaListType() |
| 154 | { |
| 155 | if (!Context.getBuiltinVaListType().isNull()) |
| 156 | return; |
| 157 | |
| 158 | IdentifierInfo *VaIdent = &Context.Idents.get("__builtin_va_list"); |
| 159 | ScopedDecl *VaDecl = LookupScopedDecl(VaIdent, Decl::IDNS_Ordinary, |
| 160 | SourceLocation(), TUScope); |
Steve Naroff | 733002f | 2007-10-18 22:17:45 +0000 | [diff] [blame] | 161 | TypedefDecl *VaTypedef = cast<TypedefDecl>(VaDecl); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 162 | Context.setBuiltinVaListType(Context.getTypedefType(VaTypedef)); |
| 163 | } |
| 164 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 165 | /// LazilyCreateBuiltin - The specified Builtin-ID was first used at file scope. |
| 166 | /// lazily create a decl for it. |
Chris Lattner | 22b73ba | 2007-10-10 23:42:28 +0000 | [diff] [blame] | 167 | ScopedDecl *Sema::LazilyCreateBuiltin(IdentifierInfo *II, unsigned bid, |
| 168 | Scope *S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 169 | Builtin::ID BID = (Builtin::ID)bid; |
| 170 | |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 171 | if (BID == Builtin::BI__builtin_va_start || |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 172 | BID == Builtin::BI__builtin_va_copy || |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 173 | BID == Builtin::BI__builtin_va_end) |
| 174 | InitBuiltinVaListType(); |
| 175 | |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 176 | QualType R = Context.BuiltinInfo.GetBuiltinType(BID, Context); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 177 | FunctionDecl *New = new FunctionDecl(SourceLocation(), II, R, |
Chris Lattner | 70c8b2e | 2007-08-26 04:02:13 +0000 | [diff] [blame] | 178 | FunctionDecl::Extern, false, 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 179 | |
| 180 | // Find translation-unit scope to insert this function into. |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 181 | if (Scope *FnS = S->getFnParent()) |
| 182 | S = FnS->getParent(); // Skip all scopes in a function at once. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 183 | while (S->getParent()) |
| 184 | S = S->getParent(); |
| 185 | S->AddDecl(New); |
| 186 | |
| 187 | // Add this decl to the end of the identifier info. |
Steve Naroff | c752d04 | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 188 | if (ScopedDecl *LastDecl = II->getFETokenInfo<ScopedDecl>()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 189 | // Scan until we find the last (outermost) decl in the id chain. |
| 190 | while (LastDecl->getNext()) |
| 191 | LastDecl = LastDecl->getNext(); |
| 192 | // Insert before (outside) it. |
| 193 | LastDecl->setNext(New); |
| 194 | } else { |
| 195 | II->setFETokenInfo(New); |
| 196 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 197 | return New; |
| 198 | } |
| 199 | |
| 200 | /// MergeTypeDefDecl - We just parsed a typedef 'New' which has the same name |
| 201 | /// and scope as a previous declaration 'Old'. Figure out how to resolve this |
| 202 | /// situation, merging decls or emitting diagnostics as appropriate. |
| 203 | /// |
Steve Naroff | 8e74c93 | 2007-09-13 21:41:19 +0000 | [diff] [blame] | 204 | TypedefDecl *Sema::MergeTypeDefDecl(TypedefDecl *New, ScopedDecl *OldD) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 205 | // Verify the old decl was also a typedef. |
| 206 | TypedefDecl *Old = dyn_cast<TypedefDecl>(OldD); |
| 207 | if (!Old) { |
| 208 | Diag(New->getLocation(), diag::err_redefinition_different_kind, |
| 209 | New->getName()); |
| 210 | Diag(OldD->getLocation(), diag::err_previous_definition); |
| 211 | return New; |
| 212 | } |
| 213 | |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 214 | // Allow multiple definitions for ObjC built-in typedefs. |
| 215 | // FIXME: Verify the underlying types are equivalent! |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 216 | if (getLangOptions().ObjC1 && isBuiltinObjCType(New)) |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 217 | return Old; |
Steve Naroff | 4c49a6c | 2008-01-30 23:46:05 +0000 | [diff] [blame] | 218 | |
| 219 | // Redeclaration of a type is a constraint violation (6.7.2.3p1). |
| 220 | // Apparently GCC, Intel, and Sun all silently ignore the redeclaration if |
| 221 | // *either* declaration is in a system header. The code below implements |
| 222 | // this adhoc compatibility rule. FIXME: The following code will not |
| 223 | // work properly when compiling ".i" files (containing preprocessed output). |
| 224 | SourceManager &SrcMgr = Context.getSourceManager(); |
| 225 | const FileEntry *OldDeclFile = SrcMgr.getFileEntryForLoc(Old->getLocation()); |
| 226 | const FileEntry *NewDeclFile = SrcMgr.getFileEntryForLoc(New->getLocation()); |
| 227 | HeaderSearch &HdrInfo = PP.getHeaderSearchInfo(); |
| 228 | DirectoryLookup::DirType OldDirType = HdrInfo.getFileDirFlavor(OldDeclFile); |
| 229 | DirectoryLookup::DirType NewDirType = HdrInfo.getFileDirFlavor(NewDeclFile); |
| 230 | |
Steve Naroff | d62701b | 2008-02-07 03:50:06 +0000 | [diff] [blame] | 231 | if ((OldDirType == DirectoryLookup::ExternCSystemHeaderDir || |
| 232 | NewDirType == DirectoryLookup::ExternCSystemHeaderDir) || |
| 233 | getLangOptions().Microsoft) |
Steve Naroff | 4c49a6c | 2008-01-30 23:46:05 +0000 | [diff] [blame] | 234 | return New; |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 235 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 236 | // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope. |
| 237 | // TODO: This is totally simplistic. It should handle merging functions |
| 238 | // together etc, merging extern int X; int X; ... |
| 239 | Diag(New->getLocation(), diag::err_redefinition, New->getName()); |
| 240 | Diag(Old->getLocation(), diag::err_previous_definition); |
| 241 | return New; |
| 242 | } |
| 243 | |
| 244 | /// MergeFunctionDecl - We just parsed a function 'New' which has the same name |
| 245 | /// and scope as a previous declaration 'Old'. Figure out how to resolve this |
| 246 | /// situation, merging decls or emitting diagnostics as appropriate. |
| 247 | /// |
Steve Naroff | 8e74c93 | 2007-09-13 21:41:19 +0000 | [diff] [blame] | 248 | FunctionDecl *Sema::MergeFunctionDecl(FunctionDecl *New, ScopedDecl *OldD) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 249 | // Verify the old decl was also a function. |
| 250 | FunctionDecl *Old = dyn_cast<FunctionDecl>(OldD); |
| 251 | if (!Old) { |
| 252 | Diag(New->getLocation(), diag::err_redefinition_different_kind, |
| 253 | New->getName()); |
| 254 | Diag(OldD->getLocation(), diag::err_previous_definition); |
| 255 | return New; |
| 256 | } |
| 257 | |
Chris Lattner | 5519644 | 2007-11-20 19:04:50 +0000 | [diff] [blame] | 258 | QualType OldQType = Old->getCanonicalType(); |
| 259 | QualType NewQType = New->getCanonicalType(); |
| 260 | |
| 261 | // This is not right, but it's a start. |
| 262 | // If Old is a function prototype with no defined arguments we only compare |
| 263 | // the return type; If arguments are defined on the prototype we validate the |
| 264 | // entire function type. |
| 265 | // FIXME: We should link up decl objects here. |
| 266 | if (Old->getBody() == 0) { |
| 267 | if (OldQType.getTypePtr()->getTypeClass() == Type::FunctionNoProto && |
| 268 | Old->getResultType() == New->getResultType()) |
| 269 | return New; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 270 | } |
Steve Naroff | adbbd0c | 2008-01-14 20:51:29 +0000 | [diff] [blame] | 271 | // Function types need to be compatible, not identical. This handles |
| 272 | // duplicate function decls like "void f(int); void f(enum X);" properly. |
| 273 | if (Context.functionTypesAreCompatible(OldQType, NewQType)) |
| 274 | return New; |
Chris Lattner | e3995fe | 2007-11-06 06:07:26 +0000 | [diff] [blame] | 275 | |
Steve Naroff | 837618c | 2008-01-16 15:01:34 +0000 | [diff] [blame] | 276 | // A function that has already been declared has been redeclared or defined |
| 277 | // with a different type- show appropriate diagnostic |
| 278 | diag::kind PrevDiag = Old->getBody() ? diag::err_previous_definition : |
| 279 | diag::err_previous_declaration; |
| 280 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 281 | // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope. |
| 282 | // TODO: This is totally simplistic. It should handle merging functions |
| 283 | // together etc, merging extern int X; int X; ... |
Steve Naroff | 837618c | 2008-01-16 15:01:34 +0000 | [diff] [blame] | 284 | Diag(New->getLocation(), diag::err_conflicting_types, New->getName()); |
| 285 | Diag(Old->getLocation(), PrevDiag); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 286 | return New; |
| 287 | } |
| 288 | |
Chris Lattner | fcc2d26 | 2007-11-06 04:28:31 +0000 | [diff] [blame] | 289 | |
| 290 | /// hasUndefinedLength - Used by equivalentArrayTypes to determine whether the |
| 291 | /// the outermost VariableArrayType has no size defined. |
| 292 | static bool hasUndefinedLength(const ArrayType *Array) { |
| 293 | const VariableArrayType *VAT = Array->getAsVariableArrayType(); |
| 294 | return VAT && !VAT->getSizeExpr(); |
| 295 | } |
| 296 | |
| 297 | /// equivalentArrayTypes - Used to determine whether two array types are |
| 298 | /// equivalent. |
| 299 | /// We need to check this explicitly as an incomplete array definition is |
| 300 | /// considered a VariableArrayType, so will not match a complete array |
| 301 | /// definition that would be otherwise equivalent. |
| 302 | static bool areEquivalentArrayTypes(QualType NewQType, QualType OldQType) { |
| 303 | const ArrayType *NewAT = NewQType->getAsArrayType(); |
| 304 | const ArrayType *OldAT = OldQType->getAsArrayType(); |
| 305 | |
| 306 | if (!NewAT || !OldAT) |
| 307 | return false; |
| 308 | |
| 309 | // If either (or both) array types in incomplete we need to strip off the |
| 310 | // outer VariableArrayType. Once the outer VAT is removed the remaining |
| 311 | // types must be identical if the array types are to be considered |
| 312 | // equivalent. |
| 313 | // eg. int[][1] and int[1][1] become |
| 314 | // VAT(null, CAT(1, int)) and CAT(1, CAT(1, int)) |
| 315 | // removing the outermost VAT gives |
| 316 | // CAT(1, int) and CAT(1, int) |
| 317 | // which are equal, therefore the array types are equivalent. |
| 318 | if (hasUndefinedLength(NewAT) || hasUndefinedLength(OldAT)) { |
| 319 | if (NewAT->getIndexTypeQualifier() != OldAT->getIndexTypeQualifier()) |
| 320 | return false; |
Eli Friedman | 0493025 | 2008-01-29 07:51:12 +0000 | [diff] [blame] | 321 | NewQType = NewAT->getElementType().getCanonicalType(); |
| 322 | OldQType = OldAT->getElementType().getCanonicalType(); |
Chris Lattner | fcc2d26 | 2007-11-06 04:28:31 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | return NewQType == OldQType; |
| 326 | } |
| 327 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 328 | /// MergeVarDecl - We just parsed a variable 'New' which has the same name |
| 329 | /// and scope as a previous declaration 'Old'. Figure out how to resolve this |
| 330 | /// situation, merging decls or emitting diagnostics as appropriate. |
| 331 | /// |
| 332 | /// FIXME: Need to carefully consider tentative definition rules (C99 6.9.2p2). |
| 333 | /// For example, we incorrectly complain about i1, i4 from C99 6.9.2p4. |
| 334 | /// |
Steve Naroff | 8e74c93 | 2007-09-13 21:41:19 +0000 | [diff] [blame] | 335 | VarDecl *Sema::MergeVarDecl(VarDecl *New, ScopedDecl *OldD) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 336 | // Verify the old decl was also a variable. |
| 337 | VarDecl *Old = dyn_cast<VarDecl>(OldD); |
| 338 | if (!Old) { |
| 339 | Diag(New->getLocation(), diag::err_redefinition_different_kind, |
| 340 | New->getName()); |
| 341 | Diag(OldD->getLocation(), diag::err_previous_definition); |
| 342 | return New; |
| 343 | } |
| 344 | // Verify the types match. |
Chris Lattner | fcc2d26 | 2007-11-06 04:28:31 +0000 | [diff] [blame] | 345 | if (Old->getCanonicalType() != New->getCanonicalType() && |
| 346 | !areEquivalentArrayTypes(New->getCanonicalType(), Old->getCanonicalType())) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 347 | Diag(New->getLocation(), diag::err_redefinition, New->getName()); |
| 348 | Diag(Old->getLocation(), diag::err_previous_definition); |
| 349 | return New; |
| 350 | } |
Steve Naroff | b7b032e | 2008-01-30 00:44:01 +0000 | [diff] [blame] | 351 | // C99 6.2.2p4: Check if we have a static decl followed by a non-static. |
| 352 | if (New->getStorageClass() == VarDecl::Static && |
| 353 | (Old->getStorageClass() == VarDecl::None || |
| 354 | Old->getStorageClass() == VarDecl::Extern)) { |
| 355 | Diag(New->getLocation(), diag::err_static_non_static, New->getName()); |
| 356 | Diag(Old->getLocation(), diag::err_previous_definition); |
| 357 | return New; |
| 358 | } |
| 359 | // C99 6.2.2p4: Check if we have a non-static decl followed by a static. |
| 360 | if (New->getStorageClass() != VarDecl::Static && |
| 361 | Old->getStorageClass() == VarDecl::Static) { |
| 362 | Diag(New->getLocation(), diag::err_non_static_static, New->getName()); |
| 363 | Diag(Old->getLocation(), diag::err_previous_definition); |
| 364 | return New; |
| 365 | } |
| 366 | // We've verified the types match, now handle "tentative" definitions. |
| 367 | FileVarDecl *OldFSDecl = dyn_cast<FileVarDecl>(Old); |
| 368 | FileVarDecl *NewFSDecl = dyn_cast<FileVarDecl>(New); |
| 369 | |
| 370 | if (OldFSDecl && NewFSDecl) { |
| 371 | // Handle C "tentative" external object definitions (C99 6.9.2). |
| 372 | bool OldIsTentative = false; |
| 373 | bool NewIsTentative = false; |
| 374 | |
| 375 | if (!OldFSDecl->getInit() && |
| 376 | (OldFSDecl->getStorageClass() == VarDecl::None || |
| 377 | OldFSDecl->getStorageClass() == VarDecl::Static)) |
| 378 | OldIsTentative = true; |
| 379 | |
| 380 | // FIXME: this check doesn't work (since the initializer hasn't been |
| 381 | // attached yet). This check should be moved to FinalizeDeclaratorGroup. |
| 382 | // Unfortunately, by the time we get to FinializeDeclaratorGroup, we've |
| 383 | // thrown out the old decl. |
| 384 | if (!NewFSDecl->getInit() && |
| 385 | (NewFSDecl->getStorageClass() == VarDecl::None || |
| 386 | NewFSDecl->getStorageClass() == VarDecl::Static)) |
| 387 | ; // change to NewIsTentative = true; once the code is moved. |
| 388 | |
| 389 | if (NewIsTentative || OldIsTentative) |
| 390 | return New; |
| 391 | } |
| 392 | if (Old->getStorageClass() != VarDecl::Extern && |
| 393 | New->getStorageClass() != VarDecl::Extern) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 394 | Diag(New->getLocation(), diag::err_redefinition, New->getName()); |
| 395 | Diag(Old->getLocation(), diag::err_previous_definition); |
| 396 | } |
| 397 | return New; |
| 398 | } |
| 399 | |
| 400 | /// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with |
| 401 | /// no declarator (e.g. "struct foo;") is parsed. |
| 402 | Sema::DeclTy *Sema::ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS) { |
| 403 | // TODO: emit error on 'int;' or 'const enum foo;'. |
| 404 | // TODO: emit error on 'typedef int;' |
| 405 | // if (!DS.isMissingDeclaratorOk()) Diag(...); |
| 406 | |
Steve Naroff | 9219928 | 2007-11-17 21:37:36 +0000 | [diff] [blame] | 407 | return dyn_cast_or_null<TagDecl>(static_cast<Decl *>(DS.getTypeRep())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 410 | bool Sema::CheckSingleInitializer(Expr *&Init, QualType DeclType) { |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 411 | // Get the type before calling CheckSingleAssignmentConstraints(), since |
| 412 | // it can promote the expression. |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 413 | QualType InitType = Init->getType(); |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 414 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 415 | AssignConvertType ConvTy = CheckSingleAssignmentConstraints(DeclType, Init); |
| 416 | return DiagnoseAssignmentResult(ConvTy, Init->getLocStart(), DeclType, |
| 417 | InitType, Init, "initializing"); |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Steve Naroff | 9e8925e | 2007-09-04 14:36:54 +0000 | [diff] [blame] | 420 | bool Sema::CheckInitExpr(Expr *expr, InitListExpr *IList, unsigned slot, |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 421 | QualType ElementType) { |
Chris Lattner | 33b7b06 | 2007-12-11 23:15:04 +0000 | [diff] [blame] | 422 | Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer. |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 423 | if (CheckSingleInitializer(expr, ElementType)) |
Chris Lattner | 33b7b06 | 2007-12-11 23:15:04 +0000 | [diff] [blame] | 424 | return true; // types weren't compatible. |
| 425 | |
Steve Naroff | 9e8925e | 2007-09-04 14:36:54 +0000 | [diff] [blame] | 426 | if (savExpr != expr) // The type was promoted, update initializer list. |
| 427 | IList->setInit(slot, expr); |
Steve Naroff | 371227d | 2007-09-04 02:20:04 +0000 | [diff] [blame] | 428 | return false; |
| 429 | } |
| 430 | |
Steve Naroff | a49e1fa | 2008-01-22 00:55:40 +0000 | [diff] [blame] | 431 | bool Sema::CheckStringLiteralInit(StringLiteral *strLiteral, QualType &DeclT) { |
| 432 | if (const VariableArrayType *VAT = DeclT->getAsIncompleteArrayType()) { |
| 433 | // C99 6.7.8p14. We have an array of character type with unknown size |
| 434 | // being initialized to a string literal. |
| 435 | llvm::APSInt ConstVal(32); |
| 436 | ConstVal = strLiteral->getByteLength() + 1; |
| 437 | // Return a new array type (C99 6.7.8p22). |
| 438 | DeclT = Context.getConstantArrayType(VAT->getElementType(), ConstVal, |
| 439 | ArrayType::Normal, 0); |
| 440 | } else if (const ConstantArrayType *CAT = DeclT->getAsConstantArrayType()) { |
| 441 | // C99 6.7.8p14. We have an array of character type with known size. |
| 442 | if (strLiteral->getByteLength() > (unsigned)CAT->getMaximumElements()) |
| 443 | Diag(strLiteral->getSourceRange().getBegin(), |
| 444 | diag::warn_initializer_string_for_char_array_too_long, |
| 445 | strLiteral->getSourceRange()); |
| 446 | } else { |
| 447 | assert(0 && "HandleStringLiteralInit(): Invalid array type"); |
| 448 | } |
| 449 | // Set type from "char *" to "constant array of char". |
| 450 | strLiteral->setType(DeclT); |
| 451 | // For now, we always return false (meaning success). |
| 452 | return false; |
| 453 | } |
| 454 | |
| 455 | StringLiteral *Sema::IsStringLiteralInit(Expr *Init, QualType DeclType) { |
Steve Naroff | a49e1fa | 2008-01-22 00:55:40 +0000 | [diff] [blame] | 456 | const ArrayType *AT = DeclType->getAsArrayType(); |
Steve Naroff | a996033 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 457 | if (AT && AT->getElementType()->isCharType()) { |
| 458 | return dyn_cast<StringLiteral>(Init); |
| 459 | } |
Steve Naroff | a49e1fa | 2008-01-22 00:55:40 +0000 | [diff] [blame] | 460 | return 0; |
| 461 | } |
| 462 | |
Steve Naroff | a996033 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 463 | // CheckInitializerListTypes - Checks the types of elements of an initializer |
| 464 | // list. This function is recursive: it calls itself to initialize subelements |
| 465 | // of aggregate types. Note that the topLevel parameter essentially refers to |
| 466 | // whether this expression "owns" the initializer list passed in, or if this |
| 467 | // initialization is taking elements out of a parent initializer. Each |
| 468 | // call to this function adds zero or more to startIndex, reports any errors, |
| 469 | // and returns true if it found any inconsistent types. |
| 470 | bool Sema::CheckInitializerListTypes(InitListExpr*& IList, QualType &DeclType, |
| 471 | bool topLevel, unsigned& startIndex) { |
Steve Naroff | 2fdc374 | 2007-12-10 22:44:33 +0000 | [diff] [blame] | 472 | bool hadError = false; |
Steve Naroff | a996033 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 473 | |
| 474 | if (DeclType->isScalarType()) { |
| 475 | // The simplest case: initializing a single scalar |
| 476 | if (topLevel) { |
| 477 | Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init, |
| 478 | IList->getSourceRange()); |
| 479 | } |
| 480 | if (startIndex < IList->getNumInits()) { |
| 481 | Expr* expr = IList->getInit(startIndex); |
| 482 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
| 483 | // FIXME: Should an error be reported here instead? |
| 484 | unsigned newIndex = 0; |
| 485 | CheckInitializerListTypes(SubInitList, DeclType, true, newIndex); |
| 486 | } else { |
| 487 | hadError |= CheckInitExpr(expr, IList, startIndex, DeclType); |
| 488 | } |
| 489 | ++startIndex; |
| 490 | } |
| 491 | // FIXME: Should an error be reported for empty initializer list + scalar? |
| 492 | } else if (DeclType->isVectorType()) { |
| 493 | if (startIndex < IList->getNumInits()) { |
| 494 | const VectorType *VT = DeclType->getAsVectorType(); |
| 495 | int maxElements = VT->getNumElements(); |
| 496 | QualType elementType = VT->getElementType(); |
| 497 | |
| 498 | for (int i = 0; i < maxElements; ++i) { |
| 499 | // Don't attempt to go past the end of the init list |
| 500 | if (startIndex >= IList->getNumInits()) |
| 501 | break; |
| 502 | Expr* expr = IList->getInit(startIndex); |
| 503 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
| 504 | unsigned newIndex = 0; |
| 505 | hadError |= CheckInitializerListTypes(SubInitList, elementType, |
| 506 | true, newIndex); |
| 507 | ++startIndex; |
| 508 | } else { |
| 509 | hadError |= CheckInitializerListTypes(IList, elementType, |
| 510 | false, startIndex); |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | } else if (DeclType->isAggregateType() || DeclType->isUnionType()) { |
| 515 | if (DeclType->isStructureType() || DeclType->isUnionType()) { |
Steve Naroff | 578edc6 | 2008-01-28 02:00:41 +0000 | [diff] [blame] | 516 | if (startIndex < IList->getNumInits() && !topLevel && |
| 517 | Context.typesAreCompatible(IList->getInit(startIndex)->getType(), |
| 518 | DeclType)) { |
Steve Naroff | a996033 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 519 | // We found a compatible struct; per the standard, this initializes the |
| 520 | // struct. (The C standard technically says that this only applies for |
| 521 | // initializers for declarations with automatic scope; however, this |
| 522 | // construct is unambiguous anyway because a struct cannot contain |
| 523 | // a type compatible with itself. We'll output an error when we check |
| 524 | // if the initializer is constant.) |
| 525 | // FIXME: Is a call to CheckSingleInitializer required here? |
| 526 | ++startIndex; |
| 527 | } else { |
| 528 | RecordDecl* structDecl = DeclType->getAsRecordType()->getDecl(); |
Steve Naroff | b43eaa5 | 2008-02-11 00:06:17 +0000 | [diff] [blame] | 529 | |
Steve Naroff | 406db93 | 2008-02-11 21:52:37 +0000 | [diff] [blame^] | 530 | // If the record is invalid, some of it's members are invalid. To avoid |
| 531 | // confusion, we forgo checking the intializer for the entire record. |
Steve Naroff | b43eaa5 | 2008-02-11 00:06:17 +0000 | [diff] [blame] | 532 | if (structDecl->isInvalidDecl()) |
| 533 | return true; |
| 534 | |
Steve Naroff | a996033 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 535 | // If structDecl is a forward declaration, this loop won't do anything; |
| 536 | // That's okay, because an error should get printed out elsewhere. It |
| 537 | // might be worthwhile to skip over the rest of the initializer, though. |
| 538 | int numMembers = structDecl->getNumMembers() - |
| 539 | structDecl->hasFlexibleArrayMember(); |
| 540 | for (int i = 0; i < numMembers; i++) { |
| 541 | // Don't attempt to go past the end of the init list |
| 542 | if (startIndex >= IList->getNumInits()) |
| 543 | break; |
| 544 | FieldDecl * curField = structDecl->getMember(i); |
| 545 | if (!curField->getIdentifier()) { |
| 546 | // Don't initialize unnamed fields, e.g. "int : 20;" |
| 547 | continue; |
| 548 | } |
| 549 | QualType fieldType = curField->getType(); |
| 550 | Expr* expr = IList->getInit(startIndex); |
| 551 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
| 552 | unsigned newStart = 0; |
| 553 | hadError |= CheckInitializerListTypes(SubInitList, fieldType, |
| 554 | true, newStart); |
| 555 | ++startIndex; |
| 556 | } else { |
| 557 | hadError |= CheckInitializerListTypes(IList, fieldType, |
| 558 | false, startIndex); |
| 559 | } |
| 560 | if (DeclType->isUnionType()) |
| 561 | break; |
| 562 | } |
| 563 | // FIXME: Implement flexible array initialization GCC extension (it's a |
| 564 | // really messy extension to implement, unfortunately...the necessary |
| 565 | // information isn't actually even here!) |
| 566 | } |
| 567 | } else if (DeclType->isArrayType()) { |
| 568 | // Check for the special-case of initializing an array with a string. |
| 569 | if (startIndex < IList->getNumInits()) { |
| 570 | if (StringLiteral *lit = IsStringLiteralInit(IList->getInit(startIndex), |
| 571 | DeclType)) { |
| 572 | CheckStringLiteralInit(lit, DeclType); |
| 573 | ++startIndex; |
| 574 | if (topLevel && startIndex < IList->getNumInits()) { |
| 575 | // We have leftover initializers; warn |
| 576 | Diag(IList->getInit(startIndex)->getLocStart(), |
| 577 | diag::err_excess_initializers_in_char_array_initializer, |
| 578 | IList->getInit(startIndex)->getSourceRange()); |
| 579 | } |
| 580 | return false; |
| 581 | } |
| 582 | } |
| 583 | int maxElements; |
| 584 | if (const VariableArrayType *VAT = DeclType->getAsVariableArrayType()) { |
| 585 | // FIXME: use a proper constant |
| 586 | maxElements = 0x7FFFFFFF; |
| 587 | // Check for VLAs; in standard C it would be possible to check this |
| 588 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 589 | // them in all sorts of strange places). |
| 590 | if (const Expr *expr = VAT->getSizeExpr()) { |
| 591 | Diag(expr->getLocStart(), diag::err_variable_object_no_init, |
| 592 | expr->getSourceRange()); |
| 593 | hadError = true; |
| 594 | } |
| 595 | } else { |
| 596 | const ConstantArrayType *CAT = DeclType->getAsConstantArrayType(); |
| 597 | maxElements = static_cast<int>(CAT->getSize().getZExtValue()); |
| 598 | } |
| 599 | QualType elementType = DeclType->getAsArrayType()->getElementType(); |
| 600 | int numElements = 0; |
| 601 | for (int i = 0; i < maxElements; ++i, ++numElements) { |
| 602 | // Don't attempt to go past the end of the init list |
| 603 | if (startIndex >= IList->getNumInits()) |
| 604 | break; |
| 605 | Expr* expr = IList->getInit(startIndex); |
| 606 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
| 607 | unsigned newIndex = 0; |
| 608 | hadError |= CheckInitializerListTypes(SubInitList, elementType, |
| 609 | true, newIndex); |
| 610 | ++startIndex; |
| 611 | } else { |
| 612 | hadError |= CheckInitializerListTypes(IList, elementType, |
| 613 | false, startIndex); |
| 614 | } |
| 615 | } |
| 616 | if (DeclType->getAsVariableArrayType()) { |
| 617 | // If this is an incomplete array type, the actual type needs to |
| 618 | // be calculated here |
| 619 | if (numElements == 0) { |
| 620 | // Sizing an array implicitly to zero is not allowed |
| 621 | // (It could in theory be allowed, but it doesn't really matter.) |
| 622 | Diag(IList->getLocStart(), |
| 623 | diag::err_at_least_one_initializer_needed_to_size_array); |
| 624 | hadError = true; |
| 625 | } else { |
| 626 | llvm::APSInt ConstVal(32); |
| 627 | ConstVal = numElements; |
| 628 | DeclType = Context.getConstantArrayType(elementType, ConstVal, |
| 629 | ArrayType::Normal, 0); |
| 630 | } |
| 631 | } |
| 632 | } else { |
| 633 | assert(0 && "Aggregate that isn't a function or array?!"); |
| 634 | } |
| 635 | } else { |
| 636 | // In C, all types are either scalars or aggregates, but |
| 637 | // additional handling is needed here for C++ (and possibly others?). |
| 638 | assert(0 && "Unsupported initializer type"); |
| 639 | } |
| 640 | |
| 641 | // If this init list is a base list, we set the type; an initializer doesn't |
| 642 | // fundamentally have a type, but this makes the ASTs a bit easier to read |
| 643 | if (topLevel) |
| 644 | IList->setType(DeclType); |
| 645 | |
| 646 | if (topLevel && startIndex < IList->getNumInits()) { |
| 647 | // We have leftover initializers; warn |
| 648 | Diag(IList->getInit(startIndex)->getLocStart(), |
| 649 | diag::warn_excess_initializers, |
| 650 | IList->getInit(startIndex)->getSourceRange()); |
| 651 | } |
| 652 | return hadError; |
| 653 | } |
| 654 | |
| 655 | bool Sema::CheckInitializerTypes(Expr *&Init, QualType &DeclType) { |
Steve Naroff | ca10730 | 2008-01-21 23:53:58 +0000 | [diff] [blame] | 656 | // C99 6.7.8p3: The type of the entity to be initialized shall be an array |
| 657 | // of unknown size ("[]") or an object type that is not a variable array type. |
| 658 | if (const VariableArrayType *VAT = DeclType->getAsVariablyModifiedType()) |
| 659 | return Diag(VAT->getSizeExpr()->getLocStart(), |
| 660 | diag::err_variable_object_no_init, |
| 661 | VAT->getSizeExpr()->getSourceRange()); |
| 662 | |
Steve Naroff | 2fdc374 | 2007-12-10 22:44:33 +0000 | [diff] [blame] | 663 | InitListExpr *InitList = dyn_cast<InitListExpr>(Init); |
| 664 | if (!InitList) { |
Steve Naroff | a49e1fa | 2008-01-22 00:55:40 +0000 | [diff] [blame] | 665 | // FIXME: Handle wide strings |
| 666 | if (StringLiteral *strLiteral = IsStringLiteralInit(Init, DeclType)) |
| 667 | return CheckStringLiteralInit(strLiteral, DeclType); |
Eli Friedman | a312ce2 | 2008-02-08 00:48:24 +0000 | [diff] [blame] | 668 | |
| 669 | if (DeclType->isArrayType()) |
| 670 | return Diag(Init->getLocStart(), |
| 671 | diag::err_array_init_list_required, |
| 672 | Init->getSourceRange()); |
| 673 | |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 674 | return CheckSingleInitializer(Init, DeclType); |
Steve Naroff | 2fdc374 | 2007-12-10 22:44:33 +0000 | [diff] [blame] | 675 | } |
Steve Naroff | a996033 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 676 | unsigned newIndex = 0; |
| 677 | return CheckInitializerListTypes(InitList, DeclType, true, newIndex); |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 680 | Sema::DeclTy * |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 681 | Sema::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *lastDecl) { |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 682 | ScopedDecl *LastDeclarator = dyn_cast_or_null<ScopedDecl>((Decl *)lastDecl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 683 | IdentifierInfo *II = D.getIdentifier(); |
| 684 | |
Chris Lattner | e80a59c | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 685 | // All of these full declarators require an identifier. If it doesn't have |
| 686 | // one, the ParsedFreeStandingDeclSpec action should be used. |
| 687 | if (II == 0) { |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 688 | Diag(D.getDeclSpec().getSourceRange().getBegin(), |
Chris Lattner | 98e0863 | 2007-08-28 06:17:15 +0000 | [diff] [blame] | 689 | diag::err_declarator_need_ident, |
Chris Lattner | e80a59c | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 690 | D.getDeclSpec().getSourceRange(), D.getSourceRange()); |
| 691 | return 0; |
| 692 | } |
| 693 | |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 694 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 695 | // we find one that is. |
| 696 | while ((S->getFlags() & Scope::DeclScope) == 0) |
| 697 | S = S->getParent(); |
| 698 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 699 | // See if this is a redefinition of a variable in the same scope. |
Steve Naroff | c752d04 | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 700 | ScopedDecl *PrevDecl = LookupScopedDecl(II, Decl::IDNS_Ordinary, |
| 701 | D.getIdentifierLoc(), S); |
Steve Naroff | c752d04 | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 702 | ScopedDecl *New; |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 703 | bool InvalidDecl = false; |
| 704 | |
Chris Lattner | 41af093 | 2007-11-14 06:34:38 +0000 | [diff] [blame] | 705 | QualType R = GetTypeForDeclarator(D, S); |
| 706 | assert(!R.isNull() && "GetTypeForDeclarator() returned null type"); |
| 707 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 708 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Chris Lattner | 41af093 | 2007-11-14 06:34:38 +0000 | [diff] [blame] | 709 | TypedefDecl *NewTD = ParseTypedefDecl(S, D, R, LastDeclarator); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 710 | if (!NewTD) return 0; |
| 711 | |
| 712 | // Handle attributes prior to checking for duplicates in MergeVarDecl |
| 713 | HandleDeclAttributes(NewTD, D.getDeclSpec().getAttributes(), |
| 714 | D.getAttributes()); |
Steve Naroff | ffce4d5 | 2008-01-09 23:34:55 +0000 | [diff] [blame] | 715 | // Merge the decl with the existing one if appropriate. If the decl is |
| 716 | // in an outer scope, it isn't the same thing. |
| 717 | if (PrevDecl && S->isDeclScope(PrevDecl)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 718 | NewTD = MergeTypeDefDecl(NewTD, PrevDecl); |
| 719 | if (NewTD == 0) return 0; |
| 720 | } |
| 721 | New = NewTD; |
| 722 | if (S->getParent() == 0) { |
| 723 | // C99 6.7.7p2: If a typedef name specifies a variably modified type |
| 724 | // then it shall have block scope. |
Steve Naroff | d7444aa | 2007-08-31 17:20:07 +0000 | [diff] [blame] | 725 | if (const VariableArrayType *VAT = |
| 726 | NewTD->getUnderlyingType()->getAsVariablyModifiedType()) { |
| 727 | Diag(D.getIdentifierLoc(), diag::err_typecheck_illegal_vla, |
| 728 | VAT->getSizeExpr()->getSourceRange()); |
| 729 | InvalidDecl = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 730 | } |
| 731 | } |
Chris Lattner | 41af093 | 2007-11-14 06:34:38 +0000 | [diff] [blame] | 732 | } else if (R.getTypePtr()->isFunctionType()) { |
Chris Lattner | 271f1a6 | 2007-09-27 15:15:46 +0000 | [diff] [blame] | 733 | FunctionDecl::StorageClass SC = FunctionDecl::None; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 734 | switch (D.getDeclSpec().getStorageClassSpec()) { |
| 735 | default: assert(0 && "Unknown storage class!"); |
| 736 | case DeclSpec::SCS_auto: |
| 737 | case DeclSpec::SCS_register: |
| 738 | Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_func, |
| 739 | R.getAsString()); |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 740 | InvalidDecl = true; |
| 741 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 742 | case DeclSpec::SCS_unspecified: SC = FunctionDecl::None; break; |
| 743 | case DeclSpec::SCS_extern: SC = FunctionDecl::Extern; break; |
| 744 | case DeclSpec::SCS_static: SC = FunctionDecl::Static; break; |
Steve Naroff | 7dd0bd4 | 2008-01-28 21:57:15 +0000 | [diff] [blame] | 745 | case DeclSpec::SCS_private_extern: SC = FunctionDecl::PrivateExtern;break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | FunctionDecl *NewFD = new FunctionDecl(D.getIdentifierLoc(), II, R, SC, |
Chris Lattner | 70c8b2e | 2007-08-26 04:02:13 +0000 | [diff] [blame] | 749 | D.getDeclSpec().isInlineSpecified(), |
Nate Begeman | 1b4e251 | 2007-11-13 22:14:47 +0000 | [diff] [blame] | 750 | LastDeclarator, |
| 751 | D.getDeclSpec().getAttributes()); |
| 752 | |
| 753 | // Transfer ownership of DeclSpec attributes to FunctionDecl |
| 754 | D.getDeclSpec().clearAttributes(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 755 | |
Steve Naroff | ffce4d5 | 2008-01-09 23:34:55 +0000 | [diff] [blame] | 756 | // Merge the decl with the existing one if appropriate. Since C functions |
| 757 | // are in a flat namespace, make sure we consider decls in outer scopes. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 758 | if (PrevDecl) { |
| 759 | NewFD = MergeFunctionDecl(NewFD, PrevDecl); |
| 760 | if (NewFD == 0) return 0; |
| 761 | } |
| 762 | New = NewFD; |
| 763 | } else { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 764 | if (R.getTypePtr()->isObjCInterfaceType()) { |
Fariborz Jahanian | e7f64cc | 2007-10-12 22:10:42 +0000 | [diff] [blame] | 765 | Diag(D.getIdentifierLoc(), diag::err_statically_allocated_object, |
| 766 | D.getIdentifier()->getName()); |
| 767 | InvalidDecl = true; |
| 768 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 769 | |
| 770 | VarDecl *NewVD; |
| 771 | VarDecl::StorageClass SC; |
| 772 | switch (D.getDeclSpec().getStorageClassSpec()) { |
| 773 | default: assert(0 && "Unknown storage class!"); |
Steve Naroff | d6326c6 | 2008-01-25 22:14:40 +0000 | [diff] [blame] | 774 | case DeclSpec::SCS_unspecified: SC = VarDecl::None; break; |
| 775 | case DeclSpec::SCS_extern: SC = VarDecl::Extern; break; |
| 776 | case DeclSpec::SCS_static: SC = VarDecl::Static; break; |
| 777 | case DeclSpec::SCS_auto: SC = VarDecl::Auto; break; |
| 778 | case DeclSpec::SCS_register: SC = VarDecl::Register; break; |
| 779 | case DeclSpec::SCS_private_extern: SC = VarDecl::PrivateExtern; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 780 | } |
| 781 | if (S->getParent() == 0) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 782 | // C99 6.9p2: The storage-class specifiers auto and register shall not |
| 783 | // appear in the declaration specifiers in an external declaration. |
| 784 | if (SC == VarDecl::Auto || SC == VarDecl::Register) { |
| 785 | Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_fscope, |
| 786 | R.getAsString()); |
Steve Naroff | 53a3234 | 2007-08-28 18:45:29 +0000 | [diff] [blame] | 787 | InvalidDecl = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 788 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 789 | NewVD = new FileVarDecl(D.getIdentifierLoc(), II, R, SC, LastDeclarator); |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 790 | } else { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 791 | NewVD = new BlockVarDecl(D.getIdentifierLoc(), II, R, SC, LastDeclarator); |
Steve Naroff | 53a3234 | 2007-08-28 18:45:29 +0000 | [diff] [blame] | 792 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 793 | // Handle attributes prior to checking for duplicates in MergeVarDecl |
| 794 | HandleDeclAttributes(NewVD, D.getDeclSpec().getAttributes(), |
| 795 | D.getAttributes()); |
| 796 | |
Steve Naroff | ffce4d5 | 2008-01-09 23:34:55 +0000 | [diff] [blame] | 797 | // Merge the decl with the existing one if appropriate. If the decl is |
| 798 | // in an outer scope, it isn't the same thing. |
| 799 | if (PrevDecl && S->isDeclScope(PrevDecl)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 800 | NewVD = MergeVarDecl(NewVD, PrevDecl); |
| 801 | if (NewVD == 0) return 0; |
| 802 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 803 | New = NewVD; |
| 804 | } |
| 805 | |
| 806 | // If this has an identifier, add it to the scope stack. |
| 807 | if (II) { |
Steve Naroff | c752d04 | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 808 | New->setNext(II->getFETokenInfo<ScopedDecl>()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 809 | II->setFETokenInfo(New); |
| 810 | S->AddDecl(New); |
| 811 | } |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 812 | // If any semantic error occurred, mark the decl as invalid. |
| 813 | if (D.getInvalidType() || InvalidDecl) |
| 814 | New->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 815 | |
| 816 | return New; |
| 817 | } |
| 818 | |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 819 | bool Sema::CheckForConstantInitializer(Expr *Init, QualType DclT) { |
| 820 | SourceLocation loc; |
| 821 | // FIXME: Remove the isReference check and handle assignment to a reference. |
| 822 | if (!DclT->isReferenceType() && !Init->isConstantExpr(Context, &loc)) { |
| 823 | assert(loc.isValid() && "isConstantExpr didn't return a loc!"); |
| 824 | Diag(loc, diag::err_init_element_not_constant, Init->getSourceRange()); |
| 825 | return true; |
| 826 | } |
| 827 | return false; |
| 828 | } |
| 829 | |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 830 | void Sema::AddInitializerToDecl(DeclTy *dcl, ExprTy *init) { |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 831 | Decl *RealDecl = static_cast<Decl *>(dcl); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 832 | Expr *Init = static_cast<Expr *>(init); |
Chris Lattner | 9a11b9a | 2007-10-19 20:10:30 +0000 | [diff] [blame] | 833 | assert(Init && "missing initializer"); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 834 | |
Chris Lattner | 9a11b9a | 2007-10-19 20:10:30 +0000 | [diff] [blame] | 835 | // If there is no declaration, there was an error parsing it. Just ignore |
| 836 | // the initializer. |
| 837 | if (RealDecl == 0) { |
| 838 | delete Init; |
| 839 | return; |
| 840 | } |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 841 | |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 842 | VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl); |
| 843 | if (!VDecl) { |
Steve Naroff | 8e74c93 | 2007-09-13 21:41:19 +0000 | [diff] [blame] | 844 | Diag(dyn_cast<ScopedDecl>(RealDecl)->getLocation(), |
| 845 | diag::err_illegal_initializer); |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 846 | RealDecl->setInvalidDecl(); |
| 847 | return; |
| 848 | } |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 849 | // Get the decls type and save a reference for later, since |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 850 | // CheckInitializerTypes may change it. |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 851 | QualType DclT = VDecl->getType(), SavT = DclT; |
| 852 | if (BlockVarDecl *BVD = dyn_cast<BlockVarDecl>(VDecl)) { |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 853 | VarDecl::StorageClass SC = BVD->getStorageClass(); |
| 854 | if (SC == VarDecl::Extern) { // C99 6.7.8p5 |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 855 | Diag(VDecl->getLocation(), diag::err_block_extern_cant_init); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 856 | BVD->setInvalidDecl(); |
| 857 | } else if (!BVD->isInvalidDecl()) { |
Steve Naroff | a996033 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 858 | if (CheckInitializerTypes(Init, DclT)) |
| 859 | BVD->setInvalidDecl(); |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 860 | if (SC == VarDecl::Static) // C99 6.7.8p4. |
| 861 | CheckForConstantInitializer(Init, DclT); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 862 | } |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 863 | } else if (FileVarDecl *FVD = dyn_cast<FileVarDecl>(VDecl)) { |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 864 | if (FVD->getStorageClass() == VarDecl::Extern) |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 865 | Diag(VDecl->getLocation(), diag::warn_extern_init); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 866 | if (!FVD->isInvalidDecl()) |
Steve Naroff | a996033 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 867 | if (CheckInitializerTypes(Init, DclT)) |
| 868 | FVD->setInvalidDecl(); |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 869 | |
| 870 | // C99 6.7.8p4. All file scoped initializers need to be constant. |
| 871 | CheckForConstantInitializer(Init, DclT); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 872 | } |
| 873 | // If the type changed, it means we had an incomplete type that was |
| 874 | // completed by the initializer. For example: |
| 875 | // int ary[] = { 1, 3, 5 }; |
| 876 | // "ary" transitions from a VariableArrayType to a ConstantArrayType. |
Christopher Lamb | 48b1239 | 2007-11-29 19:09:19 +0000 | [diff] [blame] | 877 | if (!VDecl->isInvalidDecl() && (DclT != SavT)) { |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 878 | VDecl->setType(DclT); |
Christopher Lamb | 48b1239 | 2007-11-29 19:09:19 +0000 | [diff] [blame] | 879 | Init->setType(DclT); |
| 880 | } |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 881 | |
| 882 | // Attach the initializer to the decl. |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 883 | VDecl->setInit(Init); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 884 | return; |
| 885 | } |
| 886 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 887 | /// The declarators are chained together backwards, reverse the list. |
| 888 | Sema::DeclTy *Sema::FinalizeDeclaratorGroup(Scope *S, DeclTy *group) { |
| 889 | // Often we have single declarators, handle them quickly. |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 890 | Decl *GroupDecl = static_cast<Decl*>(group); |
| 891 | if (GroupDecl == 0) |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 892 | return 0; |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 893 | |
| 894 | ScopedDecl *Group = dyn_cast<ScopedDecl>(GroupDecl); |
| 895 | ScopedDecl *NewGroup = 0; |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 896 | if (Group->getNextDeclarator() == 0) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 897 | NewGroup = Group; |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 898 | else { // reverse the list. |
| 899 | while (Group) { |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 900 | ScopedDecl *Next = Group->getNextDeclarator(); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 901 | Group->setNextDeclarator(NewGroup); |
| 902 | NewGroup = Group; |
| 903 | Group = Next; |
| 904 | } |
| 905 | } |
| 906 | // Perform semantic analysis that depends on having fully processed both |
| 907 | // the declarator and initializer. |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 908 | for (ScopedDecl *ID = NewGroup; ID; ID = ID->getNextDeclarator()) { |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 909 | VarDecl *IDecl = dyn_cast<VarDecl>(ID); |
| 910 | if (!IDecl) |
| 911 | continue; |
| 912 | FileVarDecl *FVD = dyn_cast<FileVarDecl>(IDecl); |
| 913 | BlockVarDecl *BVD = dyn_cast<BlockVarDecl>(IDecl); |
| 914 | QualType T = IDecl->getType(); |
| 915 | |
| 916 | // C99 6.7.5.2p2: If an identifier is declared to be an object with |
| 917 | // static storage duration, it shall not have a variable length array. |
| 918 | if ((FVD || BVD) && IDecl->getStorageClass() == VarDecl::Static) { |
| 919 | if (const VariableArrayType *VLA = T->getAsVariableArrayType()) { |
| 920 | if (VLA->getSizeExpr()) { |
| 921 | Diag(IDecl->getLocation(), diag::err_typecheck_illegal_vla); |
| 922 | IDecl->setInvalidDecl(); |
| 923 | } |
| 924 | } |
| 925 | } |
| 926 | // Block scope. C99 6.7p7: If an identifier for an object is declared with |
| 927 | // no linkage (C99 6.2.2p6), the type for the object shall be complete... |
| 928 | if (BVD && IDecl->getStorageClass() != VarDecl::Extern) { |
| 929 | if (T->isIncompleteType()) { |
Chris Lattner | 8b1be77 | 2007-12-02 07:50:03 +0000 | [diff] [blame] | 930 | Diag(IDecl->getLocation(), diag::err_typecheck_decl_incomplete_type, |
| 931 | T.getAsString()); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 932 | IDecl->setInvalidDecl(); |
| 933 | } |
| 934 | } |
| 935 | // File scope. C99 6.9.2p2: A declaration of an identifier for and |
| 936 | // object that has file scope without an initializer, and without a |
| 937 | // storage-class specifier or with the storage-class specifier "static", |
| 938 | // constitutes a tentative definition. Note: A tentative definition with |
| 939 | // external linkage is valid (C99 6.2.2p5). |
Steve Naroff | d3cd1e5 | 2008-01-18 00:39:39 +0000 | [diff] [blame] | 940 | if (FVD && !FVD->getInit() && (FVD->getStorageClass() == VarDecl::Static || |
| 941 | FVD->getStorageClass() == VarDecl::None)) { |
Steve Naroff | 9a75f8a | 2008-01-18 20:40:52 +0000 | [diff] [blame] | 942 | const VariableArrayType *VAT = T->getAsVariableArrayType(); |
| 943 | |
| 944 | if (VAT && VAT->getSizeExpr() == 0) { |
| 945 | // C99 6.9.2 (p2, p5): Implicit initialization causes an incomplete |
| 946 | // array to be completed. Don't issue a diagnostic. |
| 947 | } else if (T->isIncompleteType()) { |
| 948 | // C99 6.9.2p3: If the declaration of an identifier for an object is |
| 949 | // a tentative definition and has internal linkage (C99 6.2.2p3), the |
| 950 | // declared type shall not be an incomplete type. |
Chris Lattner | 8b1be77 | 2007-12-02 07:50:03 +0000 | [diff] [blame] | 951 | Diag(IDecl->getLocation(), diag::err_typecheck_decl_incomplete_type, |
| 952 | T.getAsString()); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 953 | IDecl->setInvalidDecl(); |
| 954 | } |
| 955 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 956 | } |
| 957 | return NewGroup; |
| 958 | } |
Steve Naroff | e1223f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 959 | |
| 960 | // Called from Sema::ParseStartOfFunctionDef(). |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 961 | ParmVarDecl * |
Nate Begeman | bff5f5c | 2007-11-13 21:49:48 +0000 | [diff] [blame] | 962 | Sema::ActOnParamDeclarator(struct DeclaratorChunk::ParamInfo &PI, Scope *FnScope) |
Steve Naroff | 6649992 | 2007-11-12 03:44:46 +0000 | [diff] [blame] | 963 | { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 964 | IdentifierInfo *II = PI.Ident; |
| 965 | // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope. |
| 966 | // Can this happen for params? We already checked that they don't conflict |
| 967 | // among each other. Here they can only shadow globals, which is ok. |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 968 | if (/*Decl *PrevDecl = */LookupScopedDecl(II, Decl::IDNS_Ordinary, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 969 | PI.IdentLoc, FnScope)) { |
| 970 | |
| 971 | } |
| 972 | |
| 973 | // FIXME: Handle storage class (auto, register). No declarator? |
| 974 | // TODO: Chain to previous parameter with the prevdeclarator chain? |
Steve Naroff | 6a9f3e3 | 2007-08-07 22:44:21 +0000 | [diff] [blame] | 975 | |
| 976 | // Perform the default function/array conversion (C99 6.7.5.3p[7,8]). |
| 977 | // Doing the promotion here has a win and a loss. The win is the type for |
| 978 | // both Decl's and DeclRefExpr's will match (a convenient invariant for the |
| 979 | // code generator). The loss is the orginal type isn't preserved. For example: |
| 980 | // |
| 981 | // void func(int parmvardecl[5]) { // convert "int [5]" to "int *" |
| 982 | // int blockvardecl[5]; |
| 983 | // sizeof(parmvardecl); // size == 4 |
| 984 | // sizeof(blockvardecl); // size == 20 |
| 985 | // } |
| 986 | // |
| 987 | // For expressions, all implicit conversions are captured using the |
| 988 | // ImplicitCastExpr AST node (we have no such mechanism for Decl's). |
| 989 | // |
| 990 | // FIXME: If a source translation tool needs to see the original type, then |
| 991 | // we need to consider storing both types (in ParmVarDecl)... |
| 992 | // |
| 993 | QualType parmDeclType = QualType::getFromOpaquePtr(PI.TypeInfo); |
Chris Lattner | 529bd02 | 2008-01-02 22:50:48 +0000 | [diff] [blame] | 994 | if (const ArrayType *AT = parmDeclType->getAsArrayType()) { |
| 995 | // int x[restrict 4] -> int *restrict |
Steve Naroff | 6a9f3e3 | 2007-08-07 22:44:21 +0000 | [diff] [blame] | 996 | parmDeclType = Context.getPointerType(AT->getElementType()); |
Chris Lattner | 529bd02 | 2008-01-02 22:50:48 +0000 | [diff] [blame] | 997 | parmDeclType = parmDeclType.getQualifiedType(AT->getIndexTypeQualifier()); |
| 998 | } else if (parmDeclType->isFunctionType()) |
Steve Naroff | 6a9f3e3 | 2007-08-07 22:44:21 +0000 | [diff] [blame] | 999 | parmDeclType = Context.getPointerType(parmDeclType); |
| 1000 | |
| 1001 | ParmVarDecl *New = new ParmVarDecl(PI.IdentLoc, II, parmDeclType, |
Nate Begeman | 1b4e251 | 2007-11-13 22:14:47 +0000 | [diff] [blame] | 1002 | VarDecl::None, 0, PI.AttrList); |
Steve Naroff | 53a3234 | 2007-08-28 18:45:29 +0000 | [diff] [blame] | 1003 | if (PI.InvalidType) |
| 1004 | New->setInvalidDecl(); |
| 1005 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1006 | // If this has an identifier, add it to the scope stack. |
| 1007 | if (II) { |
Steve Naroff | c752d04 | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 1008 | New->setNext(II->getFETokenInfo<ScopedDecl>()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1009 | II->setFETokenInfo(New); |
| 1010 | FnScope->AddDecl(New); |
| 1011 | } |
| 1012 | |
| 1013 | return New; |
| 1014 | } |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 1015 | |
Chris Lattner | b652cea | 2007-10-09 17:14:05 +0000 | [diff] [blame] | 1016 | Sema::DeclTy *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Declarator &D) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1017 | assert(CurFunctionDecl == 0 && "Function parsing confused"); |
| 1018 | assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function && |
| 1019 | "Not a function declarator!"); |
| 1020 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 1021 | |
| 1022 | // Verify 6.9.1p6: 'every identifier in the identifier list shall be declared' |
| 1023 | // for a K&R function. |
| 1024 | if (!FTI.hasPrototype) { |
| 1025 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) { |
| 1026 | if (FTI.ArgInfo[i].TypeInfo == 0) { |
| 1027 | Diag(FTI.ArgInfo[i].IdentLoc, diag::ext_param_not_declared, |
| 1028 | FTI.ArgInfo[i].Ident->getName()); |
| 1029 | // Implicitly declare the argument as type 'int' for lack of a better |
| 1030 | // type. |
| 1031 | FTI.ArgInfo[i].TypeInfo = Context.IntTy.getAsOpaquePtr(); |
| 1032 | } |
| 1033 | } |
| 1034 | |
| 1035 | // Since this is a function definition, act as though we have information |
| 1036 | // about the arguments. |
| 1037 | FTI.hasPrototype = true; |
| 1038 | } else { |
| 1039 | // FIXME: Diagnose arguments without names in C. |
| 1040 | |
| 1041 | } |
| 1042 | |
| 1043 | Scope *GlobalScope = FnBodyScope->getParent(); |
Steve Naroff | adbbd0c | 2008-01-14 20:51:29 +0000 | [diff] [blame] | 1044 | |
| 1045 | // See if this is a redefinition. |
| 1046 | ScopedDecl *PrevDcl = LookupScopedDecl(D.getIdentifier(), Decl::IDNS_Ordinary, |
| 1047 | D.getIdentifierLoc(), GlobalScope); |
| 1048 | if (FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(PrevDcl)) { |
| 1049 | if (FD->getBody()) { |
| 1050 | Diag(D.getIdentifierLoc(), diag::err_redefinition, |
| 1051 | D.getIdentifier()->getName()); |
| 1052 | Diag(FD->getLocation(), diag::err_previous_definition); |
| 1053 | } |
| 1054 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1055 | FunctionDecl *FD = |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1056 | static_cast<FunctionDecl*>(ActOnDeclarator(GlobalScope, D, 0)); |
Steve Naroff | adbbd0c | 2008-01-14 20:51:29 +0000 | [diff] [blame] | 1057 | assert(FD != 0 && "ActOnDeclarator() didn't return a FunctionDecl"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1058 | CurFunctionDecl = FD; |
| 1059 | |
| 1060 | // Create Decl objects for each parameter, adding them to the FunctionDecl. |
| 1061 | llvm::SmallVector<ParmVarDecl*, 16> Params; |
| 1062 | |
| 1063 | // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs function that takes |
| 1064 | // no arguments, not a function that takes a single void argument. |
| 1065 | if (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
Chris Lattner | b751c28 | 2007-11-28 18:51:29 +0000 | [diff] [blame] | 1066 | !QualType::getFromOpaquePtr(FTI.ArgInfo[0].TypeInfo).getQualifiers() && |
| 1067 | QualType::getFromOpaquePtr(FTI.ArgInfo[0].TypeInfo)->isVoidType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1068 | // empty arg list, don't push any params. |
| 1069 | } else { |
Steve Naroff | 6649992 | 2007-11-12 03:44:46 +0000 | [diff] [blame] | 1070 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) { |
Nate Begeman | bff5f5c | 2007-11-13 21:49:48 +0000 | [diff] [blame] | 1071 | Params.push_back(ActOnParamDeclarator(D.getTypeObject(0).Fun.ArgInfo[i], |
Steve Naroff | 6649992 | 2007-11-12 03:44:46 +0000 | [diff] [blame] | 1072 | FnBodyScope)); |
| 1073 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | FD->setParams(&Params[0], Params.size()); |
| 1077 | |
| 1078 | return FD; |
| 1079 | } |
| 1080 | |
Steve Naroff | d6d054d | 2007-11-11 23:20:51 +0000 | [diff] [blame] | 1081 | Sema::DeclTy *Sema::ActOnFinishFunctionBody(DeclTy *D, StmtTy *Body) { |
| 1082 | Decl *dcl = static_cast<Decl *>(D); |
| 1083 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(dcl)) { |
| 1084 | FD->setBody((Stmt*)Body); |
| 1085 | assert(FD == CurFunctionDecl && "Function parsing confused"); |
Steve Naroff | 4d83220 | 2007-12-13 18:18:56 +0000 | [diff] [blame] | 1086 | CurFunctionDecl = 0; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1087 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(dcl)) { |
Steve Naroff | d6d054d | 2007-11-11 23:20:51 +0000 | [diff] [blame] | 1088 | MD->setBody((Stmt*)Body); |
Steve Naroff | 0330071 | 2007-11-12 13:56:41 +0000 | [diff] [blame] | 1089 | CurMethodDecl = 0; |
Steve Naroff | 4d83220 | 2007-12-13 18:18:56 +0000 | [diff] [blame] | 1090 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1091 | // Verify and clean out per-function state. |
| 1092 | |
| 1093 | // Check goto/label use. |
| 1094 | for (llvm::DenseMap<IdentifierInfo*, LabelStmt*>::iterator |
| 1095 | I = LabelMap.begin(), E = LabelMap.end(); I != E; ++I) { |
| 1096 | // Verify that we have no forward references left. If so, there was a goto |
| 1097 | // or address of a label taken, but no definition of it. Label fwd |
| 1098 | // definitions are indicated with a null substmt. |
| 1099 | if (I->second->getSubStmt() == 0) { |
| 1100 | LabelStmt *L = I->second; |
| 1101 | // Emit error. |
| 1102 | Diag(L->getIdentLoc(), diag::err_undeclared_label_use, L->getName()); |
| 1103 | |
| 1104 | // At this point, we have gotos that use the bogus label. Stitch it into |
| 1105 | // the function body so that they aren't leaked and that the AST is well |
| 1106 | // formed. |
Chris Lattner | 0cbc215 | 2008-01-25 00:01:10 +0000 | [diff] [blame] | 1107 | if (Body) { |
| 1108 | L->setSubStmt(new NullStmt(L->getIdentLoc())); |
| 1109 | cast<CompoundStmt>((Stmt*)Body)->push_back(L); |
| 1110 | } else { |
| 1111 | // The whole function wasn't parsed correctly, just delete this. |
| 1112 | delete L; |
| 1113 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1114 | } |
| 1115 | } |
| 1116 | LabelMap.clear(); |
| 1117 | |
Steve Naroff | d6d054d | 2007-11-11 23:20:51 +0000 | [diff] [blame] | 1118 | return D; |
Fariborz Jahanian | 60fbca0 | 2007-11-10 16:31:34 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1121 | /// ImplicitlyDefineFunction - An undeclared identifier was used in a function |
| 1122 | /// call, forming a call to an implicitly defined function (per C99 6.5.1p2). |
Steve Naroff | 8c9f13e | 2007-09-16 16:16:00 +0000 | [diff] [blame] | 1123 | ScopedDecl *Sema::ImplicitlyDefineFunction(SourceLocation Loc, |
| 1124 | IdentifierInfo &II, Scope *S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1125 | if (getLangOptions().C99) // Extension in C99. |
| 1126 | Diag(Loc, diag::ext_implicit_function_decl, II.getName()); |
| 1127 | else // Legal in C90, but warn about it. |
| 1128 | Diag(Loc, diag::warn_implicit_function_decl, II.getName()); |
| 1129 | |
| 1130 | // FIXME: handle stuff like: |
| 1131 | // void foo() { extern float X(); } |
| 1132 | // void bar() { X(); } <-- implicit decl for X in another scope. |
| 1133 | |
| 1134 | // Set a Declarator for the implicit definition: int foo(); |
| 1135 | const char *Dummy; |
| 1136 | DeclSpec DS; |
| 1137 | bool Error = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, Dummy); |
| 1138 | Error = Error; // Silence warning. |
| 1139 | assert(!Error && "Error setting up implicit decl!"); |
| 1140 | Declarator D(DS, Declarator::BlockContext); |
| 1141 | D.AddTypeInfo(DeclaratorChunk::getFunction(false, false, 0, 0, Loc)); |
| 1142 | D.SetIdentifier(&II, Loc); |
| 1143 | |
| 1144 | // Find translation-unit scope to insert this function into. |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 1145 | if (Scope *FnS = S->getFnParent()) |
| 1146 | S = FnS->getParent(); // Skip all scopes in a function at once. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1147 | while (S->getParent()) |
| 1148 | S = S->getParent(); |
| 1149 | |
Steve Naroff | 8c9f13e | 2007-09-16 16:16:00 +0000 | [diff] [blame] | 1150 | return dyn_cast<ScopedDecl>(static_cast<Decl*>(ActOnDeclarator(S, D, 0))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | |
Chris Lattner | 41af093 | 2007-11-14 06:34:38 +0000 | [diff] [blame] | 1154 | TypedefDecl *Sema::ParseTypedefDecl(Scope *S, Declarator &D, QualType T, |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 1155 | ScopedDecl *LastDeclarator) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1156 | assert(D.getIdentifier() && "Wrong callback for declspec without declarator"); |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 1157 | assert(!T.isNull() && "GetTypeForDeclarator() returned null type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1158 | |
| 1159 | // Scope manipulation handled by caller. |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 1160 | TypedefDecl *NewTD = new TypedefDecl(D.getIdentifierLoc(), D.getIdentifier(), |
| 1161 | T, LastDeclarator); |
| 1162 | if (D.getInvalidType()) |
| 1163 | NewTD->setInvalidDecl(); |
| 1164 | return NewTD; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1167 | /// ActOnTag - This is invoked when we see 'struct foo' or 'struct {'. In the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1168 | /// former case, Name will be non-null. In the later case, Name will be null. |
| 1169 | /// TagType indicates what kind of tag this is. TK indicates whether this is a |
| 1170 | /// reference/declaration/definition of a tag. |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1171 | Sema::DeclTy *Sema::ActOnTag(Scope *S, unsigned TagType, TagKind TK, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1172 | SourceLocation KWLoc, IdentifierInfo *Name, |
| 1173 | SourceLocation NameLoc, AttributeList *Attr) { |
| 1174 | // If this is a use of an existing tag, it must have a name. |
| 1175 | assert((Name != 0 || TK == TK_Definition) && |
| 1176 | "Nameless record must be a definition!"); |
| 1177 | |
| 1178 | Decl::Kind Kind; |
| 1179 | switch (TagType) { |
| 1180 | default: assert(0 && "Unknown tag type!"); |
| 1181 | case DeclSpec::TST_struct: Kind = Decl::Struct; break; |
| 1182 | case DeclSpec::TST_union: Kind = Decl::Union; break; |
| 1183 | //case DeclSpec::TST_class: Kind = Decl::Class; break; |
| 1184 | case DeclSpec::TST_enum: Kind = Decl::Enum; break; |
| 1185 | } |
| 1186 | |
| 1187 | // If this is a named struct, check to see if there was a previous forward |
| 1188 | // declaration or definition. |
| 1189 | if (TagDecl *PrevDecl = |
| 1190 | dyn_cast_or_null<TagDecl>(LookupScopedDecl(Name, Decl::IDNS_Tag, |
| 1191 | NameLoc, S))) { |
| 1192 | |
| 1193 | // If this is a use of a previous tag, or if the tag is already declared in |
| 1194 | // the same scope (so that the definition/declaration completes or |
| 1195 | // rementions the tag), reuse the decl. |
| 1196 | if (TK == TK_Reference || S->isDeclScope(PrevDecl)) { |
| 1197 | // Make sure that this wasn't declared as an enum and now used as a struct |
| 1198 | // or something similar. |
| 1199 | if (PrevDecl->getKind() != Kind) { |
| 1200 | Diag(KWLoc, diag::err_use_with_wrong_tag, Name->getName()); |
| 1201 | Diag(PrevDecl->getLocation(), diag::err_previous_use); |
| 1202 | } |
| 1203 | |
| 1204 | // If this is a use or a forward declaration, we're good. |
| 1205 | if (TK != TK_Definition) |
| 1206 | return PrevDecl; |
| 1207 | |
| 1208 | // Diagnose attempts to redefine a tag. |
| 1209 | if (PrevDecl->isDefinition()) { |
| 1210 | Diag(NameLoc, diag::err_redefinition, Name->getName()); |
| 1211 | Diag(PrevDecl->getLocation(), diag::err_previous_definition); |
| 1212 | // If this is a redefinition, recover by making this struct be |
| 1213 | // anonymous, which will make any later references get the previous |
| 1214 | // definition. |
| 1215 | Name = 0; |
| 1216 | } else { |
| 1217 | // Okay, this is definition of a previously declared or referenced tag. |
| 1218 | // Move the location of the decl to be the definition site. |
| 1219 | PrevDecl->setLocation(NameLoc); |
| 1220 | return PrevDecl; |
| 1221 | } |
| 1222 | } |
| 1223 | // If we get here, this is a definition of a new struct type in a nested |
| 1224 | // scope, e.g. "struct foo; void bar() { struct foo; }", just create a new |
| 1225 | // type. |
| 1226 | } |
| 1227 | |
| 1228 | // If there is an identifier, use the location of the identifier as the |
| 1229 | // location of the decl, otherwise use the location of the struct/union |
| 1230 | // keyword. |
| 1231 | SourceLocation Loc = NameLoc.isValid() ? NameLoc : KWLoc; |
| 1232 | |
| 1233 | // Otherwise, if this is the first time we've seen this tag, create the decl. |
| 1234 | TagDecl *New; |
| 1235 | switch (Kind) { |
| 1236 | default: assert(0 && "Unknown tag kind!"); |
| 1237 | case Decl::Enum: |
| 1238 | // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.: |
| 1239 | // enum X { A, B, C } D; D should chain to X. |
| 1240 | New = new EnumDecl(Loc, Name, 0); |
| 1241 | // If this is an undefined enum, warn. |
| 1242 | if (TK != TK_Definition) Diag(Loc, diag::ext_forward_ref_enum); |
| 1243 | break; |
| 1244 | case Decl::Union: |
| 1245 | case Decl::Struct: |
| 1246 | case Decl::Class: |
| 1247 | // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.: |
| 1248 | // struct X { int A; } D; D should chain to X. |
| 1249 | New = new RecordDecl(Kind, Loc, Name, 0); |
| 1250 | break; |
| 1251 | } |
| 1252 | |
| 1253 | // If this has an identifier, add it to the scope stack. |
| 1254 | if (Name) { |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 1255 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 1256 | // we find one that is. |
| 1257 | while ((S->getFlags() & Scope::DeclScope) == 0) |
| 1258 | S = S->getParent(); |
| 1259 | |
| 1260 | // Add it to the decl chain. |
Steve Naroff | c752d04 | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 1261 | New->setNext(Name->getFETokenInfo<ScopedDecl>()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1262 | Name->setFETokenInfo(New); |
| 1263 | S->AddDecl(New); |
| 1264 | } |
Chris Lattner | e1e7985 | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 1265 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1266 | return New; |
| 1267 | } |
| 1268 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1269 | /// ActOnField - Each field of a struct/union/class is passed into this in order |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1270 | /// to create a FieldDecl object for it. |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1271 | Sema::DeclTy *Sema::ActOnField(Scope *S, DeclTy *TagDecl, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1272 | SourceLocation DeclStart, |
| 1273 | Declarator &D, ExprTy *BitfieldWidth) { |
| 1274 | IdentifierInfo *II = D.getIdentifier(); |
| 1275 | Expr *BitWidth = (Expr*)BitfieldWidth; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1276 | SourceLocation Loc = DeclStart; |
| 1277 | if (II) Loc = D.getIdentifierLoc(); |
| 1278 | |
| 1279 | // FIXME: Unnamed fields can be handled in various different ways, for |
| 1280 | // example, unnamed unions inject all members into the struct namespace! |
| 1281 | |
| 1282 | |
| 1283 | if (BitWidth) { |
| 1284 | // TODO: Validate. |
| 1285 | //printf("WARNING: BITFIELDS IGNORED!\n"); |
| 1286 | |
| 1287 | // 6.7.2.1p3 |
| 1288 | // 6.7.2.1p4 |
| 1289 | |
| 1290 | } else { |
| 1291 | // Not a bitfield. |
| 1292 | |
| 1293 | // validate II. |
| 1294 | |
| 1295 | } |
| 1296 | |
| 1297 | QualType T = GetTypeForDeclarator(D, S); |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 1298 | assert(!T.isNull() && "GetTypeForDeclarator() returned null type"); |
| 1299 | bool InvalidDecl = false; |
Steve Naroff | d7444aa | 2007-08-31 17:20:07 +0000 | [diff] [blame] | 1300 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1301 | // C99 6.7.2.1p8: A member of a structure or union may have any type other |
| 1302 | // than a variably modified type. |
Steve Naroff | d7444aa | 2007-08-31 17:20:07 +0000 | [diff] [blame] | 1303 | if (const VariableArrayType *VAT = T->getAsVariablyModifiedType()) { |
| 1304 | Diag(Loc, diag::err_typecheck_illegal_vla, |
| 1305 | VAT->getSizeExpr()->getSourceRange()); |
| 1306 | InvalidDecl = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1307 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1308 | // FIXME: Chain fielddecls together. |
Steve Naroff | 4473921 | 2007-09-11 21:17:26 +0000 | [diff] [blame] | 1309 | FieldDecl *NewFD; |
| 1310 | |
| 1311 | if (isa<RecordDecl>(static_cast<Decl *>(TagDecl))) |
Devang Patel | 54e6235 | 2007-11-01 16:29:56 +0000 | [diff] [blame] | 1312 | NewFD = new FieldDecl(Loc, II, T, BitWidth); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1313 | else if (isa<ObjCInterfaceDecl>(static_cast<Decl *>(TagDecl)) || |
| 1314 | isa<ObjCImplementationDecl>(static_cast<Decl *>(TagDecl)) || |
| 1315 | isa<ObjCCategoryDecl>(static_cast<Decl *>(TagDecl)) || |
Steve Naroff | ddd600f | 2007-11-14 14:15:31 +0000 | [diff] [blame] | 1316 | // FIXME: ivars are currently used to model properties, and |
| 1317 | // properties can appear within a protocol. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1318 | // See corresponding FIXME in DeclObjC.h:ObjCPropertyDecl. |
| 1319 | isa<ObjCProtocolDecl>(static_cast<Decl *>(TagDecl))) |
| 1320 | NewFD = new ObjCIvarDecl(Loc, II, T); |
Steve Naroff | 4473921 | 2007-09-11 21:17:26 +0000 | [diff] [blame] | 1321 | else |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1322 | assert(0 && "Sema::ActOnField(): Unknown TagDecl"); |
Steve Naroff | 4473921 | 2007-09-11 21:17:26 +0000 | [diff] [blame] | 1323 | |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 1324 | if (D.getInvalidType() || InvalidDecl) |
| 1325 | NewFD->setInvalidDecl(); |
| 1326 | return NewFD; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1327 | } |
| 1328 | |
Fariborz Jahanian | 89204a1 | 2007-10-01 16:53:59 +0000 | [diff] [blame] | 1329 | /// TranslateIvarVisibility - Translate visibility from a token ID to an |
| 1330 | /// AST enum value. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1331 | static ObjCIvarDecl::AccessControl |
Fariborz Jahanian | 89204a1 | 2007-10-01 16:53:59 +0000 | [diff] [blame] | 1332 | TranslateIvarVisibility(tok::ObjCKeywordKind ivarVisibility) { |
Steve Naroff | f13271f | 2007-09-14 23:09:53 +0000 | [diff] [blame] | 1333 | switch (ivarVisibility) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1334 | case tok::objc_private: return ObjCIvarDecl::Private; |
| 1335 | case tok::objc_public: return ObjCIvarDecl::Public; |
| 1336 | case tok::objc_protected: return ObjCIvarDecl::Protected; |
| 1337 | case tok::objc_package: return ObjCIvarDecl::Package; |
Fariborz Jahanian | 89204a1 | 2007-10-01 16:53:59 +0000 | [diff] [blame] | 1338 | default: assert(false && "Unknown visitibility kind"); |
Steve Naroff | f13271f | 2007-09-14 23:09:53 +0000 | [diff] [blame] | 1339 | } |
| 1340 | } |
| 1341 | |
Fariborz Jahanian | 9d048ff | 2007-09-29 00:54:24 +0000 | [diff] [blame] | 1342 | void Sema::ActOnFields(Scope* S, |
Fariborz Jahanian | 3f5faf7 | 2007-10-04 00:45:27 +0000 | [diff] [blame] | 1343 | SourceLocation RecLoc, DeclTy *RecDecl, |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1344 | DeclTy **Fields, unsigned NumFields, |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 1345 | SourceLocation LBrac, SourceLocation RBrac, |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1346 | tok::ObjCKeywordKind *visibility) { |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 1347 | Decl *EnclosingDecl = static_cast<Decl*>(RecDecl); |
| 1348 | assert(EnclosingDecl && "missing record or interface decl"); |
| 1349 | RecordDecl *Record = dyn_cast<RecordDecl>(EnclosingDecl); |
| 1350 | |
Fariborz Jahanian | e267ab6 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 1351 | if (Record && Record->isDefinition()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1352 | // Diagnose code like: |
| 1353 | // struct S { struct S {} X; }; |
| 1354 | // We discover this when we complete the outer S. Reject and ignore the |
| 1355 | // outer S. |
| 1356 | Diag(Record->getLocation(), diag::err_nested_redefinition, |
| 1357 | Record->getKindName()); |
| 1358 | Diag(RecLoc, diag::err_previous_definition); |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 1359 | Record->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1360 | return; |
| 1361 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1362 | // Verify that all the fields are okay. |
| 1363 | unsigned NumNamedMembers = 0; |
| 1364 | llvm::SmallVector<FieldDecl*, 32> RecFields; |
| 1365 | llvm::SmallSet<const IdentifierInfo*, 32> FieldIDs; |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 1366 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1367 | for (unsigned i = 0; i != NumFields; ++i) { |
Fariborz Jahanian | e267ab6 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 1368 | |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 1369 | FieldDecl *FD = cast_or_null<FieldDecl>(static_cast<Decl*>(Fields[i])); |
| 1370 | assert(FD && "missing field decl"); |
| 1371 | |
| 1372 | // Remember all fields. |
| 1373 | RecFields.push_back(FD); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1374 | |
| 1375 | // Get the type for the field. |
Chris Lattner | 02c642e | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 1376 | Type *FDTy = FD->getType().getTypePtr(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1377 | |
Steve Naroff | f13271f | 2007-09-14 23:09:53 +0000 | [diff] [blame] | 1378 | // If we have visibility info, make sure the AST is set accordingly. |
| 1379 | if (visibility) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1380 | cast<ObjCIvarDecl>(FD)->setAccessControl( |
Fariborz Jahanian | 89204a1 | 2007-10-01 16:53:59 +0000 | [diff] [blame] | 1381 | TranslateIvarVisibility(visibility[i])); |
Steve Naroff | f13271f | 2007-09-14 23:09:53 +0000 | [diff] [blame] | 1382 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1383 | // C99 6.7.2.1p2 - A field may not be a function type. |
Chris Lattner | 02c642e | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 1384 | if (FDTy->isFunctionType()) { |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 1385 | Diag(FD->getLocation(), diag::err_field_declared_as_function, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1386 | FD->getName()); |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 1387 | FD->setInvalidDecl(); |
| 1388 | EnclosingDecl->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1389 | continue; |
| 1390 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1391 | // C99 6.7.2.1p2 - A field may not be an incomplete type except... |
| 1392 | if (FDTy->isIncompleteType()) { |
Fariborz Jahanian | e267ab6 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 1393 | if (!Record) { // Incomplete ivar type is always an error. |
Fariborz Jahanian | 3f5faf7 | 2007-10-04 00:45:27 +0000 | [diff] [blame] | 1394 | Diag(FD->getLocation(), diag::err_field_incomplete, FD->getName()); |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 1395 | FD->setInvalidDecl(); |
| 1396 | EnclosingDecl->setInvalidDecl(); |
Fariborz Jahanian | 3f5faf7 | 2007-10-04 00:45:27 +0000 | [diff] [blame] | 1397 | continue; |
Fariborz Jahanian | e267ab6 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 1398 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1399 | if (i != NumFields-1 || // ... that the last member ... |
| 1400 | Record->getKind() != Decl::Struct || // ... of a structure ... |
Chris Lattner | 02c642e | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 1401 | !FDTy->isArrayType()) { //... may have incomplete array type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1402 | Diag(FD->getLocation(), diag::err_field_incomplete, FD->getName()); |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 1403 | FD->setInvalidDecl(); |
| 1404 | EnclosingDecl->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1405 | continue; |
| 1406 | } |
Fariborz Jahanian | e267ab6 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 1407 | if (NumNamedMembers < 1) { //... must have more than named member ... |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1408 | Diag(FD->getLocation(), diag::err_flexible_array_empty_struct, |
| 1409 | FD->getName()); |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 1410 | FD->setInvalidDecl(); |
| 1411 | EnclosingDecl->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1412 | continue; |
| 1413 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1414 | // Okay, we have a legal flexible array member at the end of the struct. |
Fariborz Jahanian | e267ab6 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 1415 | if (Record) |
| 1416 | Record->setHasFlexibleArrayMember(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1417 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1418 | /// C99 6.7.2.1p2 - a struct ending in a flexible array member cannot be the |
| 1419 | /// field of another structure or the element of an array. |
Chris Lattner | 02c642e | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 1420 | if (const RecordType *FDTTy = FDTy->getAsRecordType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1421 | if (FDTTy->getDecl()->hasFlexibleArrayMember()) { |
| 1422 | // If this is a member of a union, then entire union becomes "flexible". |
Fariborz Jahanian | e267ab6 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 1423 | if (Record && Record->getKind() == Decl::Union) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1424 | Record->setHasFlexibleArrayMember(true); |
| 1425 | } else { |
| 1426 | // If this is a struct/class and this is not the last element, reject |
| 1427 | // it. Note that GCC supports variable sized arrays in the middle of |
| 1428 | // structures. |
| 1429 | if (i != NumFields-1) { |
| 1430 | Diag(FD->getLocation(), diag::err_variable_sized_type_in_struct, |
| 1431 | FD->getName()); |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 1432 | FD->setInvalidDecl(); |
| 1433 | EnclosingDecl->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1434 | continue; |
| 1435 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1436 | // We support flexible arrays at the end of structs in other structs |
| 1437 | // as an extension. |
| 1438 | Diag(FD->getLocation(), diag::ext_flexible_array_in_struct, |
| 1439 | FD->getName()); |
Fariborz Jahanian | 3f5faf7 | 2007-10-04 00:45:27 +0000 | [diff] [blame] | 1440 | if (Record) |
Fariborz Jahanian | e267ab6 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 1441 | Record->setHasFlexibleArrayMember(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1442 | } |
| 1443 | } |
| 1444 | } |
Fariborz Jahanian | e7f64cc | 2007-10-12 22:10:42 +0000 | [diff] [blame] | 1445 | /// A field cannot be an Objective-c object |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1446 | if (FDTy->isObjCInterfaceType()) { |
Fariborz Jahanian | e7f64cc | 2007-10-12 22:10:42 +0000 | [diff] [blame] | 1447 | Diag(FD->getLocation(), diag::err_statically_allocated_object, |
| 1448 | FD->getName()); |
| 1449 | FD->setInvalidDecl(); |
| 1450 | EnclosingDecl->setInvalidDecl(); |
| 1451 | continue; |
| 1452 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1453 | // Keep track of the number of named members. |
| 1454 | if (IdentifierInfo *II = FD->getIdentifier()) { |
| 1455 | // Detect duplicate member names. |
| 1456 | if (!FieldIDs.insert(II)) { |
| 1457 | Diag(FD->getLocation(), diag::err_duplicate_member, II->getName()); |
| 1458 | // Find the previous decl. |
| 1459 | SourceLocation PrevLoc; |
| 1460 | for (unsigned i = 0, e = RecFields.size(); ; ++i) { |
| 1461 | assert(i != e && "Didn't find previous def!"); |
| 1462 | if (RecFields[i]->getIdentifier() == II) { |
| 1463 | PrevLoc = RecFields[i]->getLocation(); |
| 1464 | break; |
| 1465 | } |
| 1466 | } |
| 1467 | Diag(PrevLoc, diag::err_previous_definition); |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 1468 | FD->setInvalidDecl(); |
| 1469 | EnclosingDecl->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1470 | continue; |
| 1471 | } |
| 1472 | ++NumNamedMembers; |
| 1473 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1476 | // Okay, we successfully defined 'Record'. |
Chris Lattner | e1e7985 | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 1477 | if (Record) { |
Fariborz Jahanian | e267ab6 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 1478 | Record->defineBody(&RecFields[0], RecFields.size()); |
Chris Lattner | e1e7985 | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 1479 | Consumer.HandleTagDeclDefinition(Record); |
| 1480 | } else { |
Chris Lattner | a91d381 | 2008-02-05 22:40:55 +0000 | [diff] [blame] | 1481 | ObjCIvarDecl **ClsFields = reinterpret_cast<ObjCIvarDecl**>(&RecFields[0]); |
| 1482 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(EnclosingDecl)) |
| 1483 | ID->addInstanceVariablesToClass(ClsFields, RecFields.size(), RBrac); |
| 1484 | else if (ObjCImplementationDecl *IMPDecl = |
| 1485 | dyn_cast<ObjCImplementationDecl>(EnclosingDecl)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1486 | assert(IMPDecl && "ActOnFields - missing ObjCImplementationDecl"); |
| 1487 | IMPDecl->ObjCAddInstanceVariablesToClassImpl(ClsFields, RecFields.size()); |
Fariborz Jahanian | 3a3ca1b | 2007-10-31 18:48:14 +0000 | [diff] [blame] | 1488 | CheckImplementationIvars(IMPDecl, ClsFields, RecFields.size(), RBrac); |
Fariborz Jahanian | d0b90bf | 2007-09-26 18:27:25 +0000 | [diff] [blame] | 1489 | } |
Fariborz Jahanian | b04a021 | 2007-09-14 21:08:27 +0000 | [diff] [blame] | 1490 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1491 | } |
| 1492 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1493 | Sema::DeclTy *Sema::ActOnEnumConstant(Scope *S, DeclTy *theEnumDecl, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1494 | DeclTy *lastEnumConst, |
| 1495 | SourceLocation IdLoc, IdentifierInfo *Id, |
| 1496 | SourceLocation EqualLoc, ExprTy *val) { |
| 1497 | theEnumDecl = theEnumDecl; // silence unused warning. |
| 1498 | EnumConstantDecl *LastEnumConst = |
| 1499 | cast_or_null<EnumConstantDecl>(static_cast<Decl*>(lastEnumConst)); |
| 1500 | Expr *Val = static_cast<Expr*>(val); |
| 1501 | |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 1502 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 1503 | // we find one that is. |
| 1504 | while ((S->getFlags() & Scope::DeclScope) == 0) |
| 1505 | S = S->getParent(); |
| 1506 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1507 | // Verify that there isn't already something declared with this name in this |
| 1508 | // scope. |
Steve Naroff | 8e74c93 | 2007-09-13 21:41:19 +0000 | [diff] [blame] | 1509 | if (ScopedDecl *PrevDecl = LookupScopedDecl(Id, Decl::IDNS_Ordinary, |
| 1510 | IdLoc, S)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1511 | if (S->isDeclScope(PrevDecl)) { |
| 1512 | if (isa<EnumConstantDecl>(PrevDecl)) |
| 1513 | Diag(IdLoc, diag::err_redefinition_of_enumerator, Id->getName()); |
| 1514 | else |
| 1515 | Diag(IdLoc, diag::err_redefinition, Id->getName()); |
| 1516 | Diag(PrevDecl->getLocation(), diag::err_previous_definition); |
| 1517 | // FIXME: Don't leak memory: delete Val; |
| 1518 | return 0; |
| 1519 | } |
| 1520 | } |
| 1521 | |
| 1522 | llvm::APSInt EnumVal(32); |
| 1523 | QualType EltTy; |
| 1524 | if (Val) { |
Chris Lattner | 421a23d | 2007-08-27 21:16:18 +0000 | [diff] [blame] | 1525 | // Make sure to promote the operand type to int. |
| 1526 | UsualUnaryConversions(Val); |
| 1527 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1528 | // C99 6.7.2.2p2: Make sure we have an integer constant expression. |
| 1529 | SourceLocation ExpLoc; |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 1530 | if (!Val->isIntegerConstantExpr(EnumVal, Context, &ExpLoc)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1531 | Diag(ExpLoc, diag::err_enum_value_not_integer_constant_expr, |
| 1532 | Id->getName()); |
| 1533 | // FIXME: Don't leak memory: delete Val; |
Chris Lattner | b7416f9 | 2007-08-27 17:37:24 +0000 | [diff] [blame] | 1534 | Val = 0; // Just forget about it. |
Chris Lattner | e9ca851 | 2007-08-29 16:03:41 +0000 | [diff] [blame] | 1535 | } else { |
| 1536 | EltTy = Val->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1537 | } |
Chris Lattner | b7416f9 | 2007-08-27 17:37:24 +0000 | [diff] [blame] | 1538 | } |
| 1539 | |
| 1540 | if (!Val) { |
| 1541 | if (LastEnumConst) { |
| 1542 | // Assign the last value + 1. |
| 1543 | EnumVal = LastEnumConst->getInitVal(); |
| 1544 | ++EnumVal; |
Chris Lattner | 421a23d | 2007-08-27 21:16:18 +0000 | [diff] [blame] | 1545 | |
| 1546 | // Check for overflow on increment. |
| 1547 | if (EnumVal < LastEnumConst->getInitVal()) |
| 1548 | Diag(IdLoc, diag::warn_enum_value_overflow); |
| 1549 | |
Chris Lattner | b7416f9 | 2007-08-27 17:37:24 +0000 | [diff] [blame] | 1550 | EltTy = LastEnumConst->getType(); |
| 1551 | } else { |
| 1552 | // First value, set to zero. |
| 1553 | EltTy = Context.IntTy; |
Chris Lattner | 701e5eb | 2007-09-04 02:45:27 +0000 | [diff] [blame] | 1554 | EnumVal.zextOrTrunc( |
| 1555 | static_cast<uint32_t>(Context.getTypeSize(EltTy, IdLoc))); |
Chris Lattner | b7416f9 | 2007-08-27 17:37:24 +0000 | [diff] [blame] | 1556 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1557 | } |
| 1558 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1559 | EnumConstantDecl *New = new EnumConstantDecl(IdLoc, Id, EltTy, Val, EnumVal, |
| 1560 | LastEnumConst); |
| 1561 | |
| 1562 | // Register this decl in the current scope stack. |
Steve Naroff | c752d04 | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 1563 | New->setNext(Id->getFETokenInfo<ScopedDecl>()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1564 | Id->setFETokenInfo(New); |
| 1565 | S->AddDecl(New); |
| 1566 | return New; |
| 1567 | } |
| 1568 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1569 | void Sema::ActOnEnumBody(SourceLocation EnumLoc, DeclTy *EnumDeclX, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1570 | DeclTy **Elements, unsigned NumElements) { |
| 1571 | EnumDecl *Enum = cast<EnumDecl>(static_cast<Decl*>(EnumDeclX)); |
| 1572 | assert(!Enum->isDefinition() && "Enum redefinitions can't reach here"); |
| 1573 | |
Chris Lattner | e37f0be | 2007-08-28 05:10:31 +0000 | [diff] [blame] | 1574 | // TODO: If the result value doesn't fit in an int, it must be a long or long |
| 1575 | // long value. ISO C does not support this, but GCC does as an extension, |
| 1576 | // emit a warning. |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 1577 | unsigned IntWidth = |
| 1578 | Context.Target.getIntWidth(Context.getFullLoc(Enum->getLocation())); |
Chris Lattner | e37f0be | 2007-08-28 05:10:31 +0000 | [diff] [blame] | 1579 | |
| 1580 | |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1581 | // Verify that all the values are okay, compute the size of the values, and |
| 1582 | // reverse the list. |
| 1583 | unsigned NumNegativeBits = 0; |
| 1584 | unsigned NumPositiveBits = 0; |
| 1585 | |
| 1586 | // Keep track of whether all elements have type int. |
| 1587 | bool AllElementsInt = true; |
| 1588 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1589 | EnumConstantDecl *EltList = 0; |
| 1590 | for (unsigned i = 0; i != NumElements; ++i) { |
| 1591 | EnumConstantDecl *ECD = |
| 1592 | cast_or_null<EnumConstantDecl>(static_cast<Decl*>(Elements[i])); |
| 1593 | if (!ECD) continue; // Already issued a diagnostic. |
Chris Lattner | 211a30e | 2007-08-28 05:27:00 +0000 | [diff] [blame] | 1594 | |
| 1595 | // If the enum value doesn't fit in an int, emit an extension warning. |
| 1596 | assert(ECD->getInitVal().getBitWidth() >= IntWidth && |
| 1597 | "Should have promoted value to int"); |
| 1598 | const llvm::APSInt &InitVal = ECD->getInitVal(); |
| 1599 | if (InitVal.getBitWidth() > IntWidth) { |
| 1600 | llvm::APSInt V(InitVal); |
| 1601 | V.trunc(IntWidth); |
| 1602 | V.extend(InitVal.getBitWidth()); |
| 1603 | if (V != InitVal) |
| 1604 | Diag(ECD->getLocation(), diag::ext_enum_value_not_int, |
| 1605 | InitVal.toString()); |
| 1606 | } |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1607 | |
| 1608 | // Keep track of the size of positive and negative values. |
| 1609 | if (InitVal.isUnsigned() || !InitVal.isNegative()) |
Chris Lattner | 21dd821 | 2008-01-14 21:47:29 +0000 | [diff] [blame] | 1610 | NumPositiveBits = std::max(NumPositiveBits, |
| 1611 | (unsigned)InitVal.getActiveBits()); |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1612 | else |
Chris Lattner | 21dd821 | 2008-01-14 21:47:29 +0000 | [diff] [blame] | 1613 | NumNegativeBits = std::max(NumNegativeBits, |
| 1614 | (unsigned)InitVal.getMinSignedBits()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1615 | |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1616 | // Keep track of whether every enum element has type int (very commmon). |
| 1617 | if (AllElementsInt) |
| 1618 | AllElementsInt = ECD->getType() == Context.IntTy; |
| 1619 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1620 | ECD->setNextDeclarator(EltList); |
| 1621 | EltList = ECD; |
| 1622 | } |
| 1623 | |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1624 | // Figure out the type that should be used for this enum. |
| 1625 | // FIXME: Support attribute(packed) on enums and -fshort-enums. |
| 1626 | QualType BestType; |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 1627 | unsigned BestWidth; |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1628 | |
| 1629 | if (NumNegativeBits) { |
| 1630 | // If there is a negative value, figure out the smallest integer type (of |
| 1631 | // int/long/longlong) that fits. |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 1632 | if (NumNegativeBits <= IntWidth && NumPositiveBits < IntWidth) { |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1633 | BestType = Context.IntTy; |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 1634 | BestWidth = IntWidth; |
| 1635 | } else { |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 1636 | BestWidth = |
| 1637 | Context.Target.getLongWidth(Context.getFullLoc(Enum->getLocation())); |
| 1638 | |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 1639 | if (NumNegativeBits <= BestWidth && NumPositiveBits < BestWidth) |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1640 | BestType = Context.LongTy; |
| 1641 | else { |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 1642 | BestWidth = Context.Target.getLongLongWidth( |
| 1643 | Context.getFullLoc(Enum->getLocation())); |
| 1644 | |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 1645 | if (NumNegativeBits > BestWidth || NumPositiveBits >= BestWidth) |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1646 | Diag(Enum->getLocation(), diag::warn_enum_too_large); |
| 1647 | BestType = Context.LongLongTy; |
| 1648 | } |
| 1649 | } |
| 1650 | } else { |
| 1651 | // If there is no negative value, figure out which of uint, ulong, ulonglong |
| 1652 | // fits. |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 1653 | if (NumPositiveBits <= IntWidth) { |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1654 | BestType = Context.UnsignedIntTy; |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 1655 | BestWidth = IntWidth; |
| 1656 | } else if (NumPositiveBits <= |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 1657 | (BestWidth = Context.Target.getLongWidth( |
| 1658 | Context.getFullLoc(Enum->getLocation())))) |
| 1659 | |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1660 | BestType = Context.UnsignedLongTy; |
| 1661 | else { |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 1662 | BestWidth = |
| 1663 | Context.Target.getLongLongWidth(Context.getFullLoc(Enum->getLocation())); |
| 1664 | |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 1665 | assert(NumPositiveBits <= BestWidth && |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1666 | "How could an initializer get larger than ULL?"); |
| 1667 | BestType = Context.UnsignedLongLongTy; |
| 1668 | } |
| 1669 | } |
| 1670 | |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 1671 | // Loop over all of the enumerator constants, changing their types to match |
| 1672 | // the type of the enum if needed. |
| 1673 | for (unsigned i = 0; i != NumElements; ++i) { |
| 1674 | EnumConstantDecl *ECD = |
| 1675 | cast_or_null<EnumConstantDecl>(static_cast<Decl*>(Elements[i])); |
| 1676 | if (!ECD) continue; // Already issued a diagnostic. |
| 1677 | |
| 1678 | // Standard C says the enumerators have int type, but we allow, as an |
| 1679 | // extension, the enumerators to be larger than int size. If each |
| 1680 | // enumerator value fits in an int, type it as an int, otherwise type it the |
| 1681 | // same as the enumerator decl itself. This means that in "enum { X = 1U }" |
| 1682 | // that X has type 'int', not 'unsigned'. |
| 1683 | if (ECD->getType() == Context.IntTy) |
| 1684 | continue; // Already int type. |
| 1685 | |
| 1686 | // Determine whether the value fits into an int. |
| 1687 | llvm::APSInt InitVal = ECD->getInitVal(); |
| 1688 | bool FitsInInt; |
| 1689 | if (InitVal.isUnsigned() || !InitVal.isNegative()) |
| 1690 | FitsInInt = InitVal.getActiveBits() < IntWidth; |
| 1691 | else |
| 1692 | FitsInInt = InitVal.getMinSignedBits() <= IntWidth; |
| 1693 | |
| 1694 | // If it fits into an integer type, force it. Otherwise force it to match |
| 1695 | // the enum decl type. |
| 1696 | QualType NewTy; |
| 1697 | unsigned NewWidth; |
| 1698 | bool NewSign; |
| 1699 | if (FitsInInt) { |
| 1700 | NewTy = Context.IntTy; |
| 1701 | NewWidth = IntWidth; |
| 1702 | NewSign = true; |
| 1703 | } else if (ECD->getType() == BestType) { |
| 1704 | // Already the right type! |
| 1705 | continue; |
| 1706 | } else { |
| 1707 | NewTy = BestType; |
| 1708 | NewWidth = BestWidth; |
| 1709 | NewSign = BestType->isSignedIntegerType(); |
| 1710 | } |
| 1711 | |
| 1712 | // Adjust the APSInt value. |
| 1713 | InitVal.extOrTrunc(NewWidth); |
| 1714 | InitVal.setIsSigned(NewSign); |
| 1715 | ECD->setInitVal(InitVal); |
| 1716 | |
| 1717 | // Adjust the Expr initializer and type. |
| 1718 | ECD->setInitExpr(new ImplicitCastExpr(NewTy, ECD->getInitExpr())); |
| 1719 | ECD->setType(NewTy); |
| 1720 | } |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1721 | |
Chris Lattner | e00b18c | 2007-08-28 18:24:31 +0000 | [diff] [blame] | 1722 | Enum->defineElements(EltList, BestType); |
Chris Lattner | e1e7985 | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 1723 | Consumer.HandleTagDeclDefinition(Enum); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1724 | } |
| 1725 | |
Anders Carlsson | dfab6cb | 2008-02-08 00:33:21 +0000 | [diff] [blame] | 1726 | Sema::DeclTy *Sema::ActOnFileScopeAsmDecl(SourceLocation Loc, |
| 1727 | ExprTy *expr) { |
| 1728 | StringLiteral *AsmString = cast<StringLiteral>((Expr*)expr); |
| 1729 | |
| 1730 | return new FileScopeAsmDecl(Loc, AsmString); |
| 1731 | } |
| 1732 | |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 1733 | Sema::DeclTy* Sema::ActOnLinkageSpec(SourceLocation Loc, |
| 1734 | SourceLocation LBrace, |
| 1735 | SourceLocation RBrace, |
| 1736 | const char *Lang, |
| 1737 | unsigned StrSize, |
| 1738 | DeclTy *D) { |
| 1739 | LinkageSpecDecl::LanguageIDs Language; |
| 1740 | Decl *dcl = static_cast<Decl *>(D); |
| 1741 | if (strncmp(Lang, "\"C\"", StrSize) == 0) |
| 1742 | Language = LinkageSpecDecl::lang_c; |
| 1743 | else if (strncmp(Lang, "\"C++\"", StrSize) == 0) |
| 1744 | Language = LinkageSpecDecl::lang_cxx; |
| 1745 | else { |
| 1746 | Diag(Loc, diag::err_bad_language); |
| 1747 | return 0; |
| 1748 | } |
| 1749 | |
| 1750 | // FIXME: Add all the various semantics of linkage specifications |
| 1751 | return new LinkageSpecDecl(Loc, Language, dcl); |
| 1752 | } |
| 1753 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1754 | void Sema::HandleDeclAttribute(Decl *New, AttributeList *rawAttr) { |
Anders Carlsson | 6ede0ff | 2007-12-19 06:16:30 +0000 | [diff] [blame] | 1755 | const char *attrName = rawAttr->getAttributeName()->getName(); |
| 1756 | unsigned attrLen = rawAttr->getAttributeName()->getLength(); |
| 1757 | |
Anders Carlsson | abf5ad0 | 2007-12-19 17:43:24 +0000 | [diff] [blame] | 1758 | // Normalize the attribute name, __foo__ becomes foo. |
| 1759 | if (attrLen > 4 && attrName[0] == '_' && attrName[1] == '_' && |
| 1760 | attrName[attrLen - 2] == '_' && attrName[attrLen - 1] == '_') { |
| 1761 | attrName += 2; |
| 1762 | attrLen -= 4; |
| 1763 | } |
| 1764 | |
| 1765 | if (attrLen == 11 && !memcmp(attrName, "vector_size", 11)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1766 | if (ValueDecl *vDecl = dyn_cast<ValueDecl>(New)) { |
| 1767 | QualType newType = HandleVectorTypeAttribute(vDecl->getType(), rawAttr); |
| 1768 | if (!newType.isNull()) // install the new vector type into the decl |
| 1769 | vDecl->setType(newType); |
| 1770 | } |
| 1771 | if (TypedefDecl *tDecl = dyn_cast<TypedefDecl>(New)) { |
| 1772 | QualType newType = HandleVectorTypeAttribute(tDecl->getUnderlyingType(), |
| 1773 | rawAttr); |
| 1774 | if (!newType.isNull()) // install the new vector type into the decl |
| 1775 | tDecl->setUnderlyingType(newType); |
| 1776 | } |
Anders Carlsson | abf5ad0 | 2007-12-19 17:43:24 +0000 | [diff] [blame] | 1777 | } else if (attrLen == 15 && !memcmp(attrName, "ocu_vector_type", 15)) { |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1778 | if (TypedefDecl *tDecl = dyn_cast<TypedefDecl>(New)) |
| 1779 | HandleOCUVectorTypeAttribute(tDecl, rawAttr); |
| 1780 | else |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1781 | Diag(rawAttr->getAttributeLoc(), |
| 1782 | diag::err_typecheck_ocu_vector_not_typedef); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1783 | } else if (attrLen == 13 && !memcmp(attrName, "address_space", 13)) { |
| 1784 | if (TypedefDecl *tDecl = dyn_cast<TypedefDecl>(New)) { |
| 1785 | QualType newType = HandleAddressSpaceTypeAttribute( |
| 1786 | tDecl->getUnderlyingType(), |
| 1787 | rawAttr); |
| 1788 | if (!newType.isNull()) // install the new addr spaced type into the decl |
| 1789 | tDecl->setUnderlyingType(newType); |
| 1790 | } else if (ValueDecl *vDecl = dyn_cast<ValueDecl>(New)) { |
| 1791 | QualType newType = HandleAddressSpaceTypeAttribute(vDecl->getType(), |
| 1792 | rawAttr); |
| 1793 | if (!newType.isNull()) // install the new addr spaced type into the decl |
| 1794 | vDecl->setType(newType); |
| 1795 | } |
Anders Carlsson | 78aaae9 | 2007-12-19 07:19:40 +0000 | [diff] [blame] | 1796 | } else if (attrLen == 7 && !memcmp(attrName, "aligned", 7)) { |
| 1797 | HandleAlignedAttribute(New, rawAttr); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1798 | } |
Anders Carlsson | 78aaae9 | 2007-12-19 07:19:40 +0000 | [diff] [blame] | 1799 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1800 | // FIXME: add other attributes... |
| 1801 | } |
| 1802 | |
| 1803 | void Sema::HandleDeclAttributes(Decl *New, AttributeList *declspec_prefix, |
| 1804 | AttributeList *declarator_postfix) { |
| 1805 | while (declspec_prefix) { |
| 1806 | HandleDeclAttribute(New, declspec_prefix); |
| 1807 | declspec_prefix = declspec_prefix->getNext(); |
| 1808 | } |
| 1809 | while (declarator_postfix) { |
| 1810 | HandleDeclAttribute(New, declarator_postfix); |
| 1811 | declarator_postfix = declarator_postfix->getNext(); |
| 1812 | } |
| 1813 | } |
| 1814 | |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1815 | QualType Sema::HandleAddressSpaceTypeAttribute(QualType curType, |
| 1816 | AttributeList *rawAttr) { |
| 1817 | // check the attribute arugments. |
| 1818 | if (rawAttr->getNumArgs() != 1) { |
| 1819 | Diag(rawAttr->getAttributeLoc(), diag::err_attribute_wrong_number_arguments, |
| 1820 | std::string("1")); |
| 1821 | return QualType(); |
| 1822 | } |
| 1823 | Expr *addrSpaceExpr = static_cast<Expr *>(rawAttr->getArg(0)); |
| 1824 | llvm::APSInt addrSpace(32); |
| 1825 | if (!addrSpaceExpr->isIntegerConstantExpr(addrSpace, Context)) { |
| 1826 | Diag(rawAttr->getAttributeLoc(), diag::err_attribute_address_space_not_int, |
| 1827 | addrSpaceExpr->getSourceRange()); |
| 1828 | return QualType(); |
| 1829 | } |
| 1830 | unsigned addressSpace = static_cast<unsigned>(addrSpace.getZExtValue()); |
| 1831 | |
| 1832 | // Zero is the default memory space, so no qualification is needed |
| 1833 | if (addressSpace == 0) |
| 1834 | return curType; |
| 1835 | |
| 1836 | // TODO: Should we convert contained types of address space |
| 1837 | // qualified types here or or where they directly participate in conversions |
| 1838 | // (i.e. elsewhere) |
| 1839 | |
| 1840 | return Context.getASQualType(curType, addressSpace); |
| 1841 | } |
| 1842 | |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1843 | void Sema::HandleOCUVectorTypeAttribute(TypedefDecl *tDecl, |
| 1844 | AttributeList *rawAttr) { |
| 1845 | QualType curType = tDecl->getUnderlyingType(); |
Anders Carlsson | 78aaae9 | 2007-12-19 07:19:40 +0000 | [diff] [blame] | 1846 | // check the attribute arguments. |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1847 | if (rawAttr->getNumArgs() != 1) { |
| 1848 | Diag(rawAttr->getAttributeLoc(), diag::err_attribute_wrong_number_arguments, |
| 1849 | std::string("1")); |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1850 | return; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1851 | } |
| 1852 | Expr *sizeExpr = static_cast<Expr *>(rawAttr->getArg(0)); |
| 1853 | llvm::APSInt vecSize(32); |
| 1854 | if (!sizeExpr->isIntegerConstantExpr(vecSize, Context)) { |
| 1855 | Diag(rawAttr->getAttributeLoc(), diag::err_attribute_vector_size_not_int, |
| 1856 | sizeExpr->getSourceRange()); |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1857 | return; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1858 | } |
| 1859 | // unlike gcc's vector_size attribute, we do not allow vectors to be defined |
| 1860 | // in conjunction with complex types (pointers, arrays, functions, etc.). |
| 1861 | Type *canonType = curType.getCanonicalType().getTypePtr(); |
| 1862 | if (!(canonType->isIntegerType() || canonType->isRealFloatingType())) { |
| 1863 | Diag(rawAttr->getAttributeLoc(), diag::err_attribute_invalid_vector_type, |
| 1864 | curType.getCanonicalType().getAsString()); |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1865 | return; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1866 | } |
| 1867 | // unlike gcc's vector_size attribute, the size is specified as the |
| 1868 | // number of elements, not the number of bytes. |
Chris Lattner | 701e5eb | 2007-09-04 02:45:27 +0000 | [diff] [blame] | 1869 | unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue()); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1870 | |
| 1871 | if (vectorSize == 0) { |
| 1872 | Diag(rawAttr->getAttributeLoc(), diag::err_attribute_zero_size, |
| 1873 | sizeExpr->getSourceRange()); |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1874 | return; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1875 | } |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1876 | // Instantiate/Install the vector type, the number of elements is > 0. |
| 1877 | tDecl->setUnderlyingType(Context.getOCUVectorType(curType, vectorSize)); |
| 1878 | // Remember this typedef decl, we will need it later for diagnostics. |
| 1879 | OCUVectorDecls.push_back(tDecl); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1880 | } |
| 1881 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1882 | QualType Sema::HandleVectorTypeAttribute(QualType curType, |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1883 | AttributeList *rawAttr) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1884 | // check the attribute arugments. |
| 1885 | if (rawAttr->getNumArgs() != 1) { |
| 1886 | Diag(rawAttr->getAttributeLoc(), diag::err_attribute_wrong_number_arguments, |
| 1887 | std::string("1")); |
| 1888 | return QualType(); |
| 1889 | } |
| 1890 | Expr *sizeExpr = static_cast<Expr *>(rawAttr->getArg(0)); |
| 1891 | llvm::APSInt vecSize(32); |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 1892 | if (!sizeExpr->isIntegerConstantExpr(vecSize, Context)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1893 | Diag(rawAttr->getAttributeLoc(), diag::err_attribute_vector_size_not_int, |
| 1894 | sizeExpr->getSourceRange()); |
| 1895 | return QualType(); |
| 1896 | } |
| 1897 | // navigate to the base type - we need to provide for vector pointers, |
| 1898 | // vector arrays, and functions returning vectors. |
| 1899 | Type *canonType = curType.getCanonicalType().getTypePtr(); |
| 1900 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1901 | if (canonType->isPointerType() || canonType->isArrayType() || |
| 1902 | canonType->isFunctionType()) { |
Chris Lattner | 54b263b | 2007-12-19 05:38:06 +0000 | [diff] [blame] | 1903 | assert(0 && "HandleVector(): Complex type construction unimplemented"); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1904 | /* FIXME: rebuild the type from the inside out, vectorizing the inner type. |
| 1905 | do { |
| 1906 | if (PointerType *PT = dyn_cast<PointerType>(canonType)) |
| 1907 | canonType = PT->getPointeeType().getTypePtr(); |
| 1908 | else if (ArrayType *AT = dyn_cast<ArrayType>(canonType)) |
| 1909 | canonType = AT->getElementType().getTypePtr(); |
| 1910 | else if (FunctionType *FT = dyn_cast<FunctionType>(canonType)) |
| 1911 | canonType = FT->getResultType().getTypePtr(); |
| 1912 | } while (canonType->isPointerType() || canonType->isArrayType() || |
| 1913 | canonType->isFunctionType()); |
| 1914 | */ |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1915 | } |
| 1916 | // the base type must be integer or float. |
| 1917 | if (!(canonType->isIntegerType() || canonType->isRealFloatingType())) { |
| 1918 | Diag(rawAttr->getAttributeLoc(), diag::err_attribute_invalid_vector_type, |
| 1919 | curType.getCanonicalType().getAsString()); |
| 1920 | return QualType(); |
| 1921 | } |
Chris Lattner | 701e5eb | 2007-09-04 02:45:27 +0000 | [diff] [blame] | 1922 | unsigned typeSize = static_cast<unsigned>( |
| 1923 | Context.getTypeSize(curType, rawAttr->getAttributeLoc())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1924 | // vecSize is specified in bytes - convert to bits. |
Chris Lattner | 701e5eb | 2007-09-04 02:45:27 +0000 | [diff] [blame] | 1925 | unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1926 | |
| 1927 | // the vector size needs to be an integral multiple of the type size. |
| 1928 | if (vectorSize % typeSize) { |
| 1929 | Diag(rawAttr->getAttributeLoc(), diag::err_attribute_invalid_size, |
| 1930 | sizeExpr->getSourceRange()); |
| 1931 | return QualType(); |
| 1932 | } |
| 1933 | if (vectorSize == 0) { |
| 1934 | Diag(rawAttr->getAttributeLoc(), diag::err_attribute_zero_size, |
| 1935 | sizeExpr->getSourceRange()); |
| 1936 | return QualType(); |
| 1937 | } |
| 1938 | // Since OpenCU requires 3 element vectors (OpenCU 5.1.2), we don't restrict |
| 1939 | // the number of elements to be a power of two (unlike GCC). |
| 1940 | // Instantiate the vector type, the number of elements is > 0. |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1941 | return Context.getVectorType(curType, vectorSize/typeSize); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1942 | } |
| 1943 | |
Anders Carlsson | 78aaae9 | 2007-12-19 07:19:40 +0000 | [diff] [blame] | 1944 | void Sema::HandleAlignedAttribute(Decl *d, AttributeList *rawAttr) |
| 1945 | { |
| 1946 | // check the attribute arguments. |
Eli Friedman | 4ca0867 | 2008-01-30 17:38:42 +0000 | [diff] [blame] | 1947 | if (rawAttr->getNumArgs() > 1) { |
Anders Carlsson | 78aaae9 | 2007-12-19 07:19:40 +0000 | [diff] [blame] | 1948 | Diag(rawAttr->getAttributeLoc(), diag::err_attribute_wrong_number_arguments, |
| 1949 | std::string("1")); |
| 1950 | return; |
| 1951 | } |
Eli Friedman | 4ca0867 | 2008-01-30 17:38:42 +0000 | [diff] [blame] | 1952 | |
Devang Patel | 6c751c2 | 2008-01-30 18:00:07 +0000 | [diff] [blame] | 1953 | // TODO: We probably need to actually do something with aligned attribute. |
Eli Friedman | 4ca0867 | 2008-01-30 17:38:42 +0000 | [diff] [blame] | 1954 | if (rawAttr->getNumArgs() == 0) |
| 1955 | return; |
| 1956 | |
Anders Carlsson | 78aaae9 | 2007-12-19 07:19:40 +0000 | [diff] [blame] | 1957 | Expr *alignmentExpr = static_cast<Expr *>(rawAttr->getArg(0)); |
| 1958 | llvm::APSInt alignment(32); |
| 1959 | if (!alignmentExpr->isIntegerConstantExpr(alignment, Context)) { |
| 1960 | Diag(rawAttr->getAttributeLoc(), diag::err_attribute_vector_size_not_int, |
| 1961 | alignmentExpr->getSourceRange()); |
| 1962 | return; |
| 1963 | } |
| 1964 | } |