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