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