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