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