Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- SemaDecl.cpp - Semantic Analysis for Declarations ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 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" |
| 22 | #include "clang/Lex/IdentifierTable.h" |
| 23 | #include "clang/Basic/LangOptions.h" |
| 24 | #include "clang/Basic/TargetInfo.h" |
| 25 | #include "llvm/ADT/SmallSet.h" |
| 26 | using namespace clang; |
| 27 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 28 | Sema::DeclTy *Sema::isTypeName(const IdentifierInfo &II, Scope *S) const { |
| 29 | return dyn_cast_or_null<TypedefDecl>(II.getFETokenInfo<Decl>()); |
| 30 | } |
| 31 | |
| 32 | void Sema::PopScope(SourceLocation Loc, Scope *S) { |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 33 | if (S->decl_empty()) return; |
| 34 | assert((S->getFlags() & Scope::DeclScope) &&"Scope shouldn't contain decls!"); |
| 35 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 36 | for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end(); |
| 37 | I != E; ++I) { |
| 38 | Decl *D = static_cast<Decl*>(*I); |
| 39 | assert(D && "This decl didn't get pushed??"); |
| 40 | IdentifierInfo *II = D->getIdentifier(); |
| 41 | if (!II) continue; |
| 42 | |
| 43 | // Unlink this decl from the identifier. Because the scope contains decls |
| 44 | // in an unordered collection, and because we have multiple identifier |
| 45 | // namespaces (e.g. tag, normal, label),the decl may not be the first entry. |
| 46 | if (II->getFETokenInfo<Decl>() == D) { |
| 47 | // Normal case, no multiple decls in different namespaces. |
| 48 | II->setFETokenInfo(D->getNext()); |
| 49 | } else { |
| 50 | // Scan ahead. There are only three namespaces in C, so this loop can |
| 51 | // never execute more than 3 times. |
| 52 | Decl *SomeDecl = II->getFETokenInfo<Decl>(); |
| 53 | while (SomeDecl->getNext() != D) { |
| 54 | SomeDecl = SomeDecl->getNext(); |
| 55 | assert(SomeDecl && "Didn't find this decl on its identifier's chain!"); |
| 56 | } |
| 57 | SomeDecl->setNext(D->getNext()); |
| 58 | } |
| 59 | |
| 60 | // This will have to be revisited for C++: there we want to nest stuff in |
| 61 | // namespace decls etc. Even for C, we might want a top-level translation |
| 62 | // unit decl or something. |
| 63 | if (!CurFunctionDecl) |
| 64 | continue; |
| 65 | |
| 66 | // Chain this decl to the containing function, it now owns the memory for |
| 67 | // the decl. |
| 68 | D->setNext(CurFunctionDecl->getDeclChain()); |
| 69 | CurFunctionDecl->setDeclChain(D); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /// LookupScopedDecl - Look up the inner-most declaration in the specified |
| 74 | /// namespace. |
| 75 | Decl *Sema::LookupScopedDecl(IdentifierInfo *II, unsigned NSI, |
| 76 | SourceLocation IdLoc, Scope *S) { |
| 77 | if (II == 0) return 0; |
| 78 | Decl::IdentifierNamespace NS = (Decl::IdentifierNamespace)NSI; |
| 79 | |
| 80 | // Scan up the scope chain looking for a decl that matches this identifier |
| 81 | // that is in the appropriate namespace. This search should not take long, as |
| 82 | // shadowing of names is uncommon, and deep shadowing is extremely uncommon. |
| 83 | for (Decl *D = II->getFETokenInfo<Decl>(); D; D = D->getNext()) |
| 84 | if (D->getIdentifierNamespace() == NS) |
| 85 | return D; |
| 86 | |
| 87 | // If we didn't find a use of this identifier, and if the identifier |
| 88 | // corresponds to a compiler builtin, create the decl object for the builtin |
| 89 | // now, injecting it into translation unit scope, and return it. |
| 90 | if (NS == Decl::IDNS_Ordinary) { |
| 91 | // If this is a builtin on some other target, or if this builtin varies |
| 92 | // across targets (e.g. in type), emit a diagnostic and mark the translation |
| 93 | // unit non-portable for using it. |
| 94 | if (II->isNonPortableBuiltin()) { |
| 95 | // Only emit this diagnostic once for this builtin. |
| 96 | II->setNonPortableBuiltin(false); |
| 97 | Context.Target.DiagnoseNonPortability(IdLoc, |
| 98 | diag::port_target_builtin_use); |
| 99 | } |
| 100 | // If this is a builtin on this (or all) targets, create the decl. |
| 101 | if (unsigned BuiltinID = II->getBuiltinID()) |
| 102 | return LazilyCreateBuiltin(II, BuiltinID, S); |
| 103 | } |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | /// LazilyCreateBuiltin - The specified Builtin-ID was first used at file scope. |
| 108 | /// lazily create a decl for it. |
| 109 | Decl *Sema::LazilyCreateBuiltin(IdentifierInfo *II, unsigned bid, Scope *S) { |
| 110 | Builtin::ID BID = (Builtin::ID)bid; |
| 111 | |
| 112 | QualType R = Context.BuiltinInfo.GetBuiltinType(BID, Context); |
| 113 | FunctionDecl *New = new FunctionDecl(SourceLocation(), II, R, |
Chris Lattner | 70c8b2e | 2007-08-26 04:02:13 +0000 | [diff] [blame] | 114 | FunctionDecl::Extern, false, 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 115 | |
| 116 | // Find translation-unit scope to insert this function into. |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 117 | if (Scope *FnS = S->getFnParent()) |
| 118 | S = FnS->getParent(); // Skip all scopes in a function at once. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 119 | while (S->getParent()) |
| 120 | S = S->getParent(); |
| 121 | S->AddDecl(New); |
| 122 | |
| 123 | // Add this decl to the end of the identifier info. |
| 124 | if (Decl *LastDecl = II->getFETokenInfo<Decl>()) { |
| 125 | // Scan until we find the last (outermost) decl in the id chain. |
| 126 | while (LastDecl->getNext()) |
| 127 | LastDecl = LastDecl->getNext(); |
| 128 | // Insert before (outside) it. |
| 129 | LastDecl->setNext(New); |
| 130 | } else { |
| 131 | II->setFETokenInfo(New); |
| 132 | } |
| 133 | // Make sure clients iterating over decls see this. |
| 134 | LastInGroupList.push_back(New); |
| 135 | |
| 136 | return New; |
| 137 | } |
| 138 | |
| 139 | /// MergeTypeDefDecl - We just parsed a typedef 'New' which has the same name |
| 140 | /// and scope as a previous declaration 'Old'. Figure out how to resolve this |
| 141 | /// situation, merging decls or emitting diagnostics as appropriate. |
| 142 | /// |
| 143 | TypedefDecl *Sema::MergeTypeDefDecl(TypedefDecl *New, Decl *OldD) { |
| 144 | // Verify the old decl was also a typedef. |
| 145 | TypedefDecl *Old = dyn_cast<TypedefDecl>(OldD); |
| 146 | if (!Old) { |
| 147 | Diag(New->getLocation(), diag::err_redefinition_different_kind, |
| 148 | New->getName()); |
| 149 | Diag(OldD->getLocation(), diag::err_previous_definition); |
| 150 | return New; |
| 151 | } |
| 152 | |
| 153 | // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope. |
| 154 | // TODO: This is totally simplistic. It should handle merging functions |
| 155 | // together etc, merging extern int X; int X; ... |
| 156 | Diag(New->getLocation(), diag::err_redefinition, New->getName()); |
| 157 | Diag(Old->getLocation(), diag::err_previous_definition); |
| 158 | return New; |
| 159 | } |
| 160 | |
| 161 | /// MergeFunctionDecl - We just parsed a function 'New' which has the same name |
| 162 | /// and scope as a previous declaration 'Old'. Figure out how to resolve this |
| 163 | /// situation, merging decls or emitting diagnostics as appropriate. |
| 164 | /// |
| 165 | FunctionDecl *Sema::MergeFunctionDecl(FunctionDecl *New, Decl *OldD) { |
| 166 | // Verify the old decl was also a function. |
| 167 | FunctionDecl *Old = dyn_cast<FunctionDecl>(OldD); |
| 168 | if (!Old) { |
| 169 | Diag(New->getLocation(), diag::err_redefinition_different_kind, |
| 170 | New->getName()); |
| 171 | Diag(OldD->getLocation(), diag::err_previous_definition); |
| 172 | return New; |
| 173 | } |
| 174 | |
| 175 | // This is not right, but it's a start. If 'Old' is a function prototype with |
| 176 | // the same type as 'New', silently allow this. FIXME: We should link up decl |
| 177 | // objects here. |
| 178 | if (Old->getBody() == 0 && |
| 179 | Old->getCanonicalType() == New->getCanonicalType()) { |
| 180 | return New; |
| 181 | } |
| 182 | |
| 183 | // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope. |
| 184 | // TODO: This is totally simplistic. It should handle merging functions |
| 185 | // together etc, merging extern int X; int X; ... |
| 186 | Diag(New->getLocation(), diag::err_redefinition, New->getName()); |
| 187 | Diag(Old->getLocation(), diag::err_previous_definition); |
| 188 | return New; |
| 189 | } |
| 190 | |
| 191 | /// MergeVarDecl - We just parsed a variable 'New' which has the same name |
| 192 | /// and scope as a previous declaration 'Old'. Figure out how to resolve this |
| 193 | /// situation, merging decls or emitting diagnostics as appropriate. |
| 194 | /// |
| 195 | /// FIXME: Need to carefully consider tentative definition rules (C99 6.9.2p2). |
| 196 | /// For example, we incorrectly complain about i1, i4 from C99 6.9.2p4. |
| 197 | /// |
| 198 | VarDecl *Sema::MergeVarDecl(VarDecl *New, Decl *OldD) { |
| 199 | // Verify the old decl was also a variable. |
| 200 | VarDecl *Old = dyn_cast<VarDecl>(OldD); |
| 201 | if (!Old) { |
| 202 | Diag(New->getLocation(), diag::err_redefinition_different_kind, |
| 203 | New->getName()); |
| 204 | Diag(OldD->getLocation(), diag::err_previous_definition); |
| 205 | return New; |
| 206 | } |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 207 | FileVarDecl *OldFSDecl = dyn_cast<FileVarDecl>(Old); |
| 208 | FileVarDecl *NewFSDecl = dyn_cast<FileVarDecl>(New); |
| 209 | bool OldIsTentative = false; |
| 210 | |
| 211 | if (OldFSDecl && NewFSDecl) { // C99 6.9.2 |
| 212 | // Handle C "tentative" external object definitions. FIXME: finish! |
| 213 | if (!OldFSDecl->getInit() && |
| 214 | (OldFSDecl->getStorageClass() == VarDecl::None || |
| 215 | OldFSDecl->getStorageClass() == VarDecl::Static)) |
| 216 | OldIsTentative = true; |
| 217 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 218 | // Verify the types match. |
| 219 | if (Old->getCanonicalType() != New->getCanonicalType()) { |
| 220 | Diag(New->getLocation(), diag::err_redefinition, New->getName()); |
| 221 | Diag(Old->getLocation(), diag::err_previous_definition); |
| 222 | return New; |
| 223 | } |
| 224 | // We've verified the types match, now check if Old is "extern". |
| 225 | if (Old->getStorageClass() != VarDecl::Extern) { |
| 226 | Diag(New->getLocation(), diag::err_redefinition, New->getName()); |
| 227 | Diag(Old->getLocation(), diag::err_previous_definition); |
| 228 | } |
| 229 | return New; |
| 230 | } |
| 231 | |
| 232 | /// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with |
| 233 | /// no declarator (e.g. "struct foo;") is parsed. |
| 234 | Sema::DeclTy *Sema::ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS) { |
| 235 | // TODO: emit error on 'int;' or 'const enum foo;'. |
| 236 | // TODO: emit error on 'typedef int;' |
| 237 | // if (!DS.isMissingDeclaratorOk()) Diag(...); |
| 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | |
Steve Naroff | 9e8925e | 2007-09-04 14:36:54 +0000 | [diff] [blame] | 242 | bool Sema::CheckSingleInitializer(Expr *&Init, QualType DeclType) { |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 243 | AssignmentCheckResult result; |
| 244 | SourceLocation loc = Init->getLocStart(); |
| 245 | // Get the type before calling CheckSingleAssignmentConstraints(), since |
| 246 | // it can promote the expression. |
| 247 | QualType rhsType = Init->getType(); |
| 248 | |
| 249 | result = CheckSingleAssignmentConstraints(DeclType, Init); |
| 250 | |
| 251 | // decode the result (notice that extensions still return a type). |
| 252 | switch (result) { |
| 253 | case Compatible: |
| 254 | break; |
| 255 | case Incompatible: |
Steve Naroff | 6f9f307 | 2007-09-02 15:34:30 +0000 | [diff] [blame] | 256 | // FIXME: tighten up this check which should allow: |
| 257 | // char s[] = "abc", which is identical to char s[] = { 'a', 'b', 'c' }; |
| 258 | if (rhsType == Context.getPointerType(Context.CharTy)) |
| 259 | break; |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 260 | Diag(loc, diag::err_typecheck_assign_incompatible, |
| 261 | DeclType.getAsString(), rhsType.getAsString(), |
| 262 | Init->getSourceRange()); |
| 263 | return true; |
| 264 | case PointerFromInt: |
| 265 | // check for null pointer constant (C99 6.3.2.3p3) |
| 266 | if (!Init->isNullPointerConstant(Context)) { |
| 267 | Diag(loc, diag::ext_typecheck_assign_pointer_int, |
| 268 | DeclType.getAsString(), rhsType.getAsString(), |
| 269 | Init->getSourceRange()); |
| 270 | return true; |
| 271 | } |
| 272 | break; |
| 273 | case IntFromPointer: |
| 274 | Diag(loc, diag::ext_typecheck_assign_pointer_int, |
| 275 | DeclType.getAsString(), rhsType.getAsString(), |
| 276 | Init->getSourceRange()); |
| 277 | break; |
| 278 | case IncompatiblePointer: |
| 279 | Diag(loc, diag::ext_typecheck_assign_incompatible_pointer, |
| 280 | DeclType.getAsString(), rhsType.getAsString(), |
| 281 | Init->getSourceRange()); |
| 282 | break; |
| 283 | case CompatiblePointerDiscardsQualifiers: |
| 284 | Diag(loc, diag::ext_typecheck_assign_discards_qualifiers, |
| 285 | DeclType.getAsString(), rhsType.getAsString(), |
| 286 | Init->getSourceRange()); |
| 287 | break; |
| 288 | } |
| 289 | return false; |
| 290 | } |
| 291 | |
Steve Naroff | 9e8925e | 2007-09-04 14:36:54 +0000 | [diff] [blame] | 292 | bool Sema::CheckInitExpr(Expr *expr, InitListExpr *IList, unsigned slot, |
| 293 | bool isStatic, QualType ElementType) { |
Steve Naroff | 371227d | 2007-09-04 02:20:04 +0000 | [diff] [blame] | 294 | SourceLocation loc; |
Steve Naroff | 9e8925e | 2007-09-04 14:36:54 +0000 | [diff] [blame] | 295 | Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer. |
Steve Naroff | 371227d | 2007-09-04 02:20:04 +0000 | [diff] [blame] | 296 | |
| 297 | if (isStatic && !expr->isConstantExpr(Context, &loc)) { // C99 6.7.8p4. |
| 298 | Diag(loc, diag::err_init_element_not_constant, expr->getSourceRange()); |
| 299 | return true; |
| 300 | } else if (CheckSingleInitializer(expr, ElementType)) { |
| 301 | return true; // types weren't compatible. |
| 302 | } |
Steve Naroff | 9e8925e | 2007-09-04 14:36:54 +0000 | [diff] [blame] | 303 | if (savExpr != expr) // The type was promoted, update initializer list. |
| 304 | IList->setInit(slot, expr); |
Steve Naroff | 371227d | 2007-09-04 02:20:04 +0000 | [diff] [blame] | 305 | return false; |
| 306 | } |
| 307 | |
| 308 | void Sema::CheckVariableInitList(QualType DeclType, InitListExpr *IList, |
| 309 | QualType ElementType, bool isStatic, |
| 310 | int &nInitializers, bool &hadError) { |
Steve Naroff | 6f9f307 | 2007-09-02 15:34:30 +0000 | [diff] [blame] | 311 | for (unsigned i = 0; i < IList->getNumInits(); i++) { |
| 312 | Expr *expr = IList->getInit(i); |
| 313 | |
Steve Naroff | 371227d | 2007-09-04 02:20:04 +0000 | [diff] [blame] | 314 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(expr)) { |
| 315 | if (const ConstantArrayType *CAT = DeclType->getAsConstantArrayType()) { |
Steve Naroff | 7cf8c44 | 2007-09-04 21:13:33 +0000 | [diff] [blame] | 316 | int maxElements = CAT->getMaximumElements(); |
Steve Naroff | 371227d | 2007-09-04 02:20:04 +0000 | [diff] [blame] | 317 | CheckConstantInitList(DeclType, InitList, ElementType, isStatic, |
| 318 | maxElements, hadError); |
Steve Naroff | 6f9f307 | 2007-09-02 15:34:30 +0000 | [diff] [blame] | 319 | } |
Steve Naroff | 371227d | 2007-09-04 02:20:04 +0000 | [diff] [blame] | 320 | } else { |
Steve Naroff | 9e8925e | 2007-09-04 14:36:54 +0000 | [diff] [blame] | 321 | hadError = CheckInitExpr(expr, IList, i, isStatic, ElementType); |
Steve Naroff | 6f9f307 | 2007-09-02 15:34:30 +0000 | [diff] [blame] | 322 | } |
Steve Naroff | 371227d | 2007-09-04 02:20:04 +0000 | [diff] [blame] | 323 | nInitializers++; |
| 324 | } |
| 325 | return; |
| 326 | } |
| 327 | |
| 328 | // FIXME: Doesn't deal with arrays of structures yet. |
| 329 | void Sema::CheckConstantInitList(QualType DeclType, InitListExpr *IList, |
| 330 | QualType ElementType, bool isStatic, |
| 331 | int &totalInits, bool &hadError) { |
| 332 | int maxElementsAtThisLevel = 0; |
| 333 | int nInitsAtLevel = 0; |
| 334 | |
| 335 | if (const ConstantArrayType *CAT = DeclType->getAsConstantArrayType()) { |
| 336 | // We have a constant array type, compute maxElements *at this level*. |
Steve Naroff | 7cf8c44 | 2007-09-04 21:13:33 +0000 | [diff] [blame] | 337 | maxElementsAtThisLevel = CAT->getMaximumElements(); |
| 338 | // Set DeclType, used below to recurse (for multi-dimensional arrays). |
| 339 | DeclType = CAT->getElementType(); |
Steve Naroff | 371227d | 2007-09-04 02:20:04 +0000 | [diff] [blame] | 340 | } else if (DeclType->isScalarType()) { |
| 341 | Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init, |
| 342 | IList->getSourceRange()); |
| 343 | maxElementsAtThisLevel = 1; |
| 344 | } |
| 345 | // The empty init list "{ }" is treated specially below. |
| 346 | unsigned numInits = IList->getNumInits(); |
| 347 | if (numInits) { |
| 348 | for (unsigned i = 0; i < numInits; i++) { |
| 349 | Expr *expr = IList->getInit(i); |
| 350 | |
| 351 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(expr)) { |
| 352 | CheckConstantInitList(DeclType, InitList, ElementType, isStatic, |
| 353 | totalInits, hadError); |
| 354 | } else { |
Steve Naroff | 9e8925e | 2007-09-04 14:36:54 +0000 | [diff] [blame] | 355 | hadError = CheckInitExpr(expr, IList, i, isStatic, ElementType); |
Steve Naroff | 371227d | 2007-09-04 02:20:04 +0000 | [diff] [blame] | 356 | nInitsAtLevel++; // increment the number of initializers at this level. |
| 357 | totalInits--; // decrement the total number of initializers. |
| 358 | |
| 359 | // Check if we have space for another initializer. |
| 360 | if ((nInitsAtLevel > maxElementsAtThisLevel) || (totalInits < 0)) |
| 361 | Diag(expr->getLocStart(), diag::warn_excess_initializers, |
| 362 | expr->getSourceRange()); |
| 363 | } |
| 364 | } |
| 365 | if (nInitsAtLevel < maxElementsAtThisLevel) // fill the remaining elements. |
| 366 | totalInits -= (maxElementsAtThisLevel - nInitsAtLevel); |
| 367 | } else { |
| 368 | // we have an initializer list with no elements. |
| 369 | totalInits -= maxElementsAtThisLevel; |
| 370 | if (totalInits < 0) |
| 371 | Diag(IList->getLocStart(), diag::warn_excess_initializers, |
| 372 | IList->getSourceRange()); |
Steve Naroff | 6f9f307 | 2007-09-02 15:34:30 +0000 | [diff] [blame] | 373 | } |
Steve Naroff | d35005e | 2007-09-03 01:24:23 +0000 | [diff] [blame] | 374 | return; |
Steve Naroff | 6f9f307 | 2007-09-02 15:34:30 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Steve Naroff | 9e8925e | 2007-09-04 14:36:54 +0000 | [diff] [blame] | 377 | bool Sema::CheckInitializer(Expr *&Init, QualType &DeclType, bool isStatic) { |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 378 | InitListExpr *InitList = dyn_cast<InitListExpr>(Init); |
Steve Naroff | d35005e | 2007-09-03 01:24:23 +0000 | [diff] [blame] | 379 | if (!InitList) |
| 380 | return CheckSingleInitializer(Init, DeclType); |
| 381 | |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 382 | // We have an InitListExpr, make sure we set the type. |
| 383 | Init->setType(DeclType); |
Steve Naroff | d35005e | 2007-09-03 01:24:23 +0000 | [diff] [blame] | 384 | |
| 385 | bool hadError = false; |
Steve Naroff | 6f9f307 | 2007-09-02 15:34:30 +0000 | [diff] [blame] | 386 | |
Steve Naroff | 38374b0 | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 387 | // C99 6.7.8p3: The type of the entity to be initialized shall be an array |
| 388 | // of unknown size ("[]") or an object type that is not a variable array type. |
| 389 | if (const VariableArrayType *VAT = DeclType->getAsVariableArrayType()) { |
| 390 | Expr *expr = VAT->getSizeExpr(); |
Steve Naroff | d35005e | 2007-09-03 01:24:23 +0000 | [diff] [blame] | 391 | if (expr) |
| 392 | return Diag(expr->getLocStart(), diag::err_variable_object_no_init, |
| 393 | expr->getSourceRange()); |
| 394 | |
Steve Naroff | 7cf8c44 | 2007-09-04 21:13:33 +0000 | [diff] [blame] | 395 | // We have a VariableArrayType with unknown size. Note that only the first |
| 396 | // array can have unknown size. For example, "int [][]" is illegal. |
Steve Naroff | 371227d | 2007-09-04 02:20:04 +0000 | [diff] [blame] | 397 | int numInits = 0; |
Steve Naroff | 7cf8c44 | 2007-09-04 21:13:33 +0000 | [diff] [blame] | 398 | CheckVariableInitList(VAT->getElementType(), InitList, VAT->getBaseType(), |
| 399 | isStatic, numInits, hadError); |
Steve Naroff | d35005e | 2007-09-03 01:24:23 +0000 | [diff] [blame] | 400 | if (!hadError) { |
| 401 | // Return a new array type from the number of initializers (C99 6.7.8p22). |
| 402 | llvm::APSInt ConstVal(32); |
Steve Naroff | 371227d | 2007-09-04 02:20:04 +0000 | [diff] [blame] | 403 | ConstVal = numInits; |
| 404 | DeclType = Context.getConstantArrayType(DeclType, ConstVal, |
Steve Naroff | d35005e | 2007-09-03 01:24:23 +0000 | [diff] [blame] | 405 | ArrayType::Normal, 0); |
| 406 | } |
| 407 | return hadError; |
| 408 | } |
| 409 | if (const ConstantArrayType *CAT = DeclType->getAsConstantArrayType()) { |
Steve Naroff | 7cf8c44 | 2007-09-04 21:13:33 +0000 | [diff] [blame] | 410 | int maxElements = CAT->getMaximumElements(); |
| 411 | CheckConstantInitList(DeclType, InitList, CAT->getBaseType(), |
| 412 | isStatic, maxElements, hadError); |
Steve Naroff | d35005e | 2007-09-03 01:24:23 +0000 | [diff] [blame] | 413 | return hadError; |
| 414 | } |
Steve Naroff | 371227d | 2007-09-04 02:20:04 +0000 | [diff] [blame] | 415 | if (DeclType->isScalarType()) { // C99 6.7.8p11: Allow "int x = { 1, 2 };" |
| 416 | int maxElements = 1; |
| 417 | CheckConstantInitList(DeclType, InitList, DeclType, isStatic, maxElements, |
| 418 | hadError); |
Steve Naroff | d35005e | 2007-09-03 01:24:23 +0000 | [diff] [blame] | 419 | return hadError; |
Steve Naroff | 38374b0 | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 420 | } |
| 421 | // FIXME: Handle struct/union types. |
Steve Naroff | d35005e | 2007-09-03 01:24:23 +0000 | [diff] [blame] | 422 | return hadError; |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 425 | Sema::DeclTy * |
Chris Lattner | 24c3990 | 2007-07-12 00:36:32 +0000 | [diff] [blame] | 426 | Sema::ParseDeclarator(Scope *S, Declarator &D, ExprTy *init, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 427 | DeclTy *lastDeclarator) { |
| 428 | Decl *LastDeclarator = (Decl*)lastDeclarator; |
Chris Lattner | 24c3990 | 2007-07-12 00:36:32 +0000 | [diff] [blame] | 429 | Expr *Init = static_cast<Expr*>(init); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 430 | IdentifierInfo *II = D.getIdentifier(); |
| 431 | |
Chris Lattner | e80a59c | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 432 | // All of these full declarators require an identifier. If it doesn't have |
| 433 | // one, the ParsedFreeStandingDeclSpec action should be used. |
| 434 | if (II == 0) { |
Chris Lattner | 98e0863 | 2007-08-28 06:17:15 +0000 | [diff] [blame] | 435 | Diag(D.getDeclSpec().getSourceRange().Begin(), |
| 436 | diag::err_declarator_need_ident, |
Chris Lattner | e80a59c | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 437 | D.getDeclSpec().getSourceRange(), D.getSourceRange()); |
| 438 | return 0; |
| 439 | } |
| 440 | |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 441 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 442 | // we find one that is. |
| 443 | while ((S->getFlags() & Scope::DeclScope) == 0) |
| 444 | S = S->getParent(); |
| 445 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 446 | // See if this is a redefinition of a variable in the same scope. |
| 447 | Decl *PrevDecl = LookupScopedDecl(II, Decl::IDNS_Ordinary, |
| 448 | D.getIdentifierLoc(), S); |
| 449 | if (PrevDecl && !S->isDeclScope(PrevDecl)) |
| 450 | PrevDecl = 0; // If in outer scope, it isn't the same thing. |
| 451 | |
| 452 | Decl *New; |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 453 | bool InvalidDecl = false; |
| 454 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 455 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Chris Lattner | 24c3990 | 2007-07-12 00:36:32 +0000 | [diff] [blame] | 456 | assert(Init == 0 && "Can't have initializer for a typedef!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 457 | TypedefDecl *NewTD = ParseTypedefDecl(S, D, LastDeclarator); |
| 458 | if (!NewTD) return 0; |
| 459 | |
| 460 | // Handle attributes prior to checking for duplicates in MergeVarDecl |
| 461 | HandleDeclAttributes(NewTD, D.getDeclSpec().getAttributes(), |
| 462 | D.getAttributes()); |
| 463 | // Merge the decl with the existing one if appropriate. |
| 464 | if (PrevDecl) { |
| 465 | NewTD = MergeTypeDefDecl(NewTD, PrevDecl); |
| 466 | if (NewTD == 0) return 0; |
| 467 | } |
| 468 | New = NewTD; |
| 469 | if (S->getParent() == 0) { |
| 470 | // C99 6.7.7p2: If a typedef name specifies a variably modified type |
| 471 | // then it shall have block scope. |
Steve Naroff | d7444aa | 2007-08-31 17:20:07 +0000 | [diff] [blame] | 472 | if (const VariableArrayType *VAT = |
| 473 | NewTD->getUnderlyingType()->getAsVariablyModifiedType()) { |
| 474 | Diag(D.getIdentifierLoc(), diag::err_typecheck_illegal_vla, |
| 475 | VAT->getSizeExpr()->getSourceRange()); |
| 476 | InvalidDecl = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | } else if (D.isFunctionDeclarator()) { |
Chris Lattner | 24c3990 | 2007-07-12 00:36:32 +0000 | [diff] [blame] | 480 | assert(Init == 0 && "Can't have an initializer for a functiondecl!"); |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 481 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 482 | QualType R = GetTypeForDeclarator(D, S); |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 483 | assert(!R.isNull() && "GetTypeForDeclarator() returned null type"); |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 484 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 485 | FunctionDecl::StorageClass SC; |
| 486 | switch (D.getDeclSpec().getStorageClassSpec()) { |
| 487 | default: assert(0 && "Unknown storage class!"); |
| 488 | case DeclSpec::SCS_auto: |
| 489 | case DeclSpec::SCS_register: |
| 490 | Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_func, |
| 491 | R.getAsString()); |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 492 | InvalidDecl = true; |
| 493 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 494 | case DeclSpec::SCS_unspecified: SC = FunctionDecl::None; break; |
| 495 | case DeclSpec::SCS_extern: SC = FunctionDecl::Extern; break; |
| 496 | case DeclSpec::SCS_static: SC = FunctionDecl::Static; break; |
| 497 | } |
| 498 | |
| 499 | FunctionDecl *NewFD = new FunctionDecl(D.getIdentifierLoc(), II, R, SC, |
Chris Lattner | 70c8b2e | 2007-08-26 04:02:13 +0000 | [diff] [blame] | 500 | D.getDeclSpec().isInlineSpecified(), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 501 | LastDeclarator); |
| 502 | |
| 503 | // Merge the decl with the existing one if appropriate. |
| 504 | if (PrevDecl) { |
| 505 | NewFD = MergeFunctionDecl(NewFD, PrevDecl); |
| 506 | if (NewFD == 0) return 0; |
| 507 | } |
| 508 | New = NewFD; |
| 509 | } else { |
| 510 | QualType R = GetTypeForDeclarator(D, S); |
Steve Naroff | 53a3234 | 2007-08-28 18:45:29 +0000 | [diff] [blame] | 511 | assert(!R.isNull() && "GetTypeForDeclarator() returned null type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 512 | |
| 513 | VarDecl *NewVD; |
| 514 | VarDecl::StorageClass SC; |
| 515 | switch (D.getDeclSpec().getStorageClassSpec()) { |
| 516 | default: assert(0 && "Unknown storage class!"); |
| 517 | case DeclSpec::SCS_unspecified: SC = VarDecl::None; break; |
| 518 | case DeclSpec::SCS_extern: SC = VarDecl::Extern; break; |
| 519 | case DeclSpec::SCS_static: SC = VarDecl::Static; break; |
| 520 | case DeclSpec::SCS_auto: SC = VarDecl::Auto; break; |
| 521 | case DeclSpec::SCS_register: SC = VarDecl::Register; break; |
| 522 | } |
| 523 | if (S->getParent() == 0) { |
Steve Naroff | 6f9f307 | 2007-09-02 15:34:30 +0000 | [diff] [blame] | 524 | if (Init) { |
| 525 | if (SC == VarDecl::Extern) |
| 526 | Diag(D.getIdentifierLoc(), diag::warn_extern_init); |
Steve Naroff | 371227d | 2007-09-04 02:20:04 +0000 | [diff] [blame] | 527 | if (!D.getInvalidType()) |
| 528 | CheckInitializer(Init, R, true); |
Steve Naroff | 6f9f307 | 2007-09-02 15:34:30 +0000 | [diff] [blame] | 529 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 530 | // File scope. C99 6.9.2p2: A declaration of an identifier for and |
| 531 | // object that has file scope without an initializer, and without a |
| 532 | // storage-class specifier or with the storage-class specifier "static", |
| 533 | // constitutes a tentative definition. Note: A tentative definition with |
| 534 | // external linkage is valid (C99 6.2.2p5). |
| 535 | if (!Init && SC == VarDecl::Static) { |
| 536 | // C99 6.9.2p3: If the declaration of an identifier for an object is |
| 537 | // a tentative definition and has internal linkage (C99 6.2.2p3), the |
| 538 | // declared type shall not be an incomplete type. |
| 539 | if (R->isIncompleteType()) { |
| 540 | Diag(D.getIdentifierLoc(), diag::err_typecheck_decl_incomplete_type, |
| 541 | R.getAsString()); |
Steve Naroff | 53a3234 | 2007-08-28 18:45:29 +0000 | [diff] [blame] | 542 | InvalidDecl = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 543 | } |
| 544 | } |
| 545 | // C99 6.9p2: The storage-class specifiers auto and register shall not |
| 546 | // appear in the declaration specifiers in an external declaration. |
| 547 | if (SC == VarDecl::Auto || SC == VarDecl::Register) { |
| 548 | Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_fscope, |
| 549 | R.getAsString()); |
Steve Naroff | 53a3234 | 2007-08-28 18:45:29 +0000 | [diff] [blame] | 550 | InvalidDecl = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 551 | } |
Steve Naroff | d7444aa | 2007-08-31 17:20:07 +0000 | [diff] [blame] | 552 | if (SC == VarDecl::Static) { |
| 553 | // C99 6.7.5.2p2: If an identifier is declared to be an object with |
| 554 | // static storage duration, it shall not have a variable length array. |
| 555 | if (const VariableArrayType *VLA = R->getAsVariableArrayType()) { |
| 556 | Expr *Size = VLA->getSizeExpr(); |
| 557 | if (Size || (!Size && !Init)) { |
| 558 | // FIXME: Since we don't support initializers yet, we only emit this |
| 559 | // error when we don't have an initializer. Once initializers are |
| 560 | // implemented, the VLA will change to a CLA. |
| 561 | Diag(D.getIdentifierLoc(), diag::err_typecheck_illegal_vla); |
| 562 | InvalidDecl = true; |
| 563 | } |
| 564 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 565 | } |
| 566 | NewVD = new FileVarDecl(D.getIdentifierLoc(), II, R, SC, LastDeclarator); |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 567 | } else { |
| 568 | if (Init) { |
Steve Naroff | 6f9f307 | 2007-09-02 15:34:30 +0000 | [diff] [blame] | 569 | if (SC == VarDecl::Extern) { // C99 6.7.8p5 |
| 570 | Diag(D.getIdentifierLoc(), diag::err_block_extern_cant_init); |
| 571 | InvalidDecl = true; |
Steve Naroff | 371227d | 2007-09-04 02:20:04 +0000 | [diff] [blame] | 572 | } else if (!D.getInvalidType()) { |
Steve Naroff | 6f9f307 | 2007-09-02 15:34:30 +0000 | [diff] [blame] | 573 | CheckInitializer(Init, R, SC == VarDecl::Static); |
| 574 | } |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 575 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 576 | // Block scope. C99 6.7p7: If an identifier for an object is declared with |
| 577 | // no linkage (C99 6.2.2p6), the type for the object shall be complete... |
| 578 | if (SC != VarDecl::Extern) { |
| 579 | if (R->isIncompleteType()) { |
| 580 | Diag(D.getIdentifierLoc(), diag::err_typecheck_decl_incomplete_type, |
| 581 | R.getAsString()); |
Steve Naroff | 53a3234 | 2007-08-28 18:45:29 +0000 | [diff] [blame] | 582 | InvalidDecl = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 583 | } |
| 584 | } |
| 585 | if (SC == VarDecl::Static) { |
| 586 | // C99 6.7.5.2p2: If an identifier is declared to be an object with |
| 587 | // static storage duration, it shall not have a variable length array. |
Steve Naroff | d7444aa | 2007-08-31 17:20:07 +0000 | [diff] [blame] | 588 | if (const VariableArrayType *VLA = R->getAsVariableArrayType()) { |
| 589 | Expr *Size = VLA->getSizeExpr(); |
| 590 | if (Size || (!Size && !Init)) { |
| 591 | // FIXME: Since we don't support initializers yet, we only emit this |
| 592 | // error when we don't have an initializer. Once initializers are |
| 593 | // implemented, the VLA will change to a CLA. |
| 594 | Diag(D.getIdentifierLoc(), diag::err_typecheck_illegal_vla); |
Steve Naroff | 53a3234 | 2007-08-28 18:45:29 +0000 | [diff] [blame] | 595 | InvalidDecl = true; |
Steve Naroff | d7444aa | 2007-08-31 17:20:07 +0000 | [diff] [blame] | 596 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 597 | } |
| 598 | } |
| 599 | NewVD = new BlockVarDecl(D.getIdentifierLoc(), II, R, SC, LastDeclarator); |
Steve Naroff | 53a3234 | 2007-08-28 18:45:29 +0000 | [diff] [blame] | 600 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 601 | // Handle attributes prior to checking for duplicates in MergeVarDecl |
| 602 | HandleDeclAttributes(NewVD, D.getDeclSpec().getAttributes(), |
| 603 | D.getAttributes()); |
| 604 | |
| 605 | // Merge the decl with the existing one if appropriate. |
| 606 | if (PrevDecl) { |
| 607 | NewVD = MergeVarDecl(NewVD, PrevDecl); |
| 608 | if (NewVD == 0) return 0; |
| 609 | } |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 610 | if (Init) { // FIXME: This will likely move up above...for now, it stays. |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 611 | NewVD->setInit(Init); |
| 612 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 613 | New = NewVD; |
| 614 | } |
| 615 | |
| 616 | // If this has an identifier, add it to the scope stack. |
| 617 | if (II) { |
| 618 | New->setNext(II->getFETokenInfo<Decl>()); |
| 619 | II->setFETokenInfo(New); |
| 620 | S->AddDecl(New); |
| 621 | } |
| 622 | |
| 623 | if (S->getParent() == 0) |
| 624 | AddTopLevelDecl(New, LastDeclarator); |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 625 | |
| 626 | // If any semantic error occurred, mark the decl as invalid. |
| 627 | if (D.getInvalidType() || InvalidDecl) |
| 628 | New->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 629 | |
| 630 | return New; |
| 631 | } |
| 632 | |
| 633 | /// The declarators are chained together backwards, reverse the list. |
| 634 | Sema::DeclTy *Sema::FinalizeDeclaratorGroup(Scope *S, DeclTy *group) { |
| 635 | // Often we have single declarators, handle them quickly. |
| 636 | Decl *Group = static_cast<Decl*>(group); |
| 637 | if (Group == 0 || Group->getNextDeclarator() == 0) return Group; |
| 638 | |
| 639 | Decl *NewGroup = 0; |
| 640 | while (Group) { |
| 641 | Decl *Next = Group->getNextDeclarator(); |
| 642 | Group->setNextDeclarator(NewGroup); |
| 643 | NewGroup = Group; |
| 644 | Group = Next; |
| 645 | } |
| 646 | return NewGroup; |
| 647 | } |
Steve Naroff | e1223f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 648 | |
| 649 | // Called from Sema::ParseStartOfFunctionDef(). |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 650 | ParmVarDecl * |
| 651 | Sema::ParseParamDeclarator(DeclaratorChunk &FTI, unsigned ArgNo, |
| 652 | Scope *FnScope) { |
| 653 | const DeclaratorChunk::ParamInfo &PI = FTI.Fun.ArgInfo[ArgNo]; |
| 654 | |
| 655 | IdentifierInfo *II = PI.Ident; |
| 656 | // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope. |
| 657 | // Can this happen for params? We already checked that they don't conflict |
| 658 | // among each other. Here they can only shadow globals, which is ok. |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 659 | if (/*Decl *PrevDecl = */LookupScopedDecl(II, Decl::IDNS_Ordinary, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 660 | PI.IdentLoc, FnScope)) { |
| 661 | |
| 662 | } |
| 663 | |
| 664 | // FIXME: Handle storage class (auto, register). No declarator? |
| 665 | // TODO: Chain to previous parameter with the prevdeclarator chain? |
Steve Naroff | 6a9f3e3 | 2007-08-07 22:44:21 +0000 | [diff] [blame] | 666 | |
| 667 | // Perform the default function/array conversion (C99 6.7.5.3p[7,8]). |
| 668 | // Doing the promotion here has a win and a loss. The win is the type for |
| 669 | // both Decl's and DeclRefExpr's will match (a convenient invariant for the |
| 670 | // code generator). The loss is the orginal type isn't preserved. For example: |
| 671 | // |
| 672 | // void func(int parmvardecl[5]) { // convert "int [5]" to "int *" |
| 673 | // int blockvardecl[5]; |
| 674 | // sizeof(parmvardecl); // size == 4 |
| 675 | // sizeof(blockvardecl); // size == 20 |
| 676 | // } |
| 677 | // |
| 678 | // For expressions, all implicit conversions are captured using the |
| 679 | // ImplicitCastExpr AST node (we have no such mechanism for Decl's). |
| 680 | // |
| 681 | // FIXME: If a source translation tool needs to see the original type, then |
| 682 | // we need to consider storing both types (in ParmVarDecl)... |
| 683 | // |
| 684 | QualType parmDeclType = QualType::getFromOpaquePtr(PI.TypeInfo); |
| 685 | if (const ArrayType *AT = parmDeclType->getAsArrayType()) |
| 686 | parmDeclType = Context.getPointerType(AT->getElementType()); |
| 687 | else if (parmDeclType->isFunctionType()) |
| 688 | parmDeclType = Context.getPointerType(parmDeclType); |
| 689 | |
| 690 | ParmVarDecl *New = new ParmVarDecl(PI.IdentLoc, II, parmDeclType, |
Steve Naroff | 53a3234 | 2007-08-28 18:45:29 +0000 | [diff] [blame] | 691 | VarDecl::None, 0); |
| 692 | if (PI.InvalidType) |
| 693 | New->setInvalidDecl(); |
| 694 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 695 | // If this has an identifier, add it to the scope stack. |
| 696 | if (II) { |
| 697 | New->setNext(II->getFETokenInfo<Decl>()); |
| 698 | II->setFETokenInfo(New); |
| 699 | FnScope->AddDecl(New); |
| 700 | } |
| 701 | |
| 702 | return New; |
| 703 | } |
| 704 | |
| 705 | |
| 706 | Sema::DeclTy *Sema::ParseStartOfFunctionDef(Scope *FnBodyScope, Declarator &D) { |
| 707 | assert(CurFunctionDecl == 0 && "Function parsing confused"); |
| 708 | assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function && |
| 709 | "Not a function declarator!"); |
| 710 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 711 | |
| 712 | // Verify 6.9.1p6: 'every identifier in the identifier list shall be declared' |
| 713 | // for a K&R function. |
| 714 | if (!FTI.hasPrototype) { |
| 715 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) { |
| 716 | if (FTI.ArgInfo[i].TypeInfo == 0) { |
| 717 | Diag(FTI.ArgInfo[i].IdentLoc, diag::ext_param_not_declared, |
| 718 | FTI.ArgInfo[i].Ident->getName()); |
| 719 | // Implicitly declare the argument as type 'int' for lack of a better |
| 720 | // type. |
| 721 | FTI.ArgInfo[i].TypeInfo = Context.IntTy.getAsOpaquePtr(); |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | // Since this is a function definition, act as though we have information |
| 726 | // about the arguments. |
| 727 | FTI.hasPrototype = true; |
| 728 | } else { |
| 729 | // FIXME: Diagnose arguments without names in C. |
| 730 | |
| 731 | } |
| 732 | |
| 733 | Scope *GlobalScope = FnBodyScope->getParent(); |
| 734 | |
| 735 | FunctionDecl *FD = |
| 736 | static_cast<FunctionDecl*>(ParseDeclarator(GlobalScope, D, 0, 0)); |
| 737 | CurFunctionDecl = FD; |
| 738 | |
| 739 | // Create Decl objects for each parameter, adding them to the FunctionDecl. |
| 740 | llvm::SmallVector<ParmVarDecl*, 16> Params; |
| 741 | |
| 742 | // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs function that takes |
| 743 | // no arguments, not a function that takes a single void argument. |
| 744 | if (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
| 745 | FTI.ArgInfo[0].TypeInfo == Context.VoidTy.getAsOpaquePtr()) { |
| 746 | // empty arg list, don't push any params. |
| 747 | } else { |
| 748 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) |
| 749 | Params.push_back(ParseParamDeclarator(D.getTypeObject(0), i,FnBodyScope)); |
| 750 | } |
| 751 | |
| 752 | FD->setParams(&Params[0], Params.size()); |
| 753 | |
| 754 | return FD; |
| 755 | } |
| 756 | |
| 757 | Sema::DeclTy *Sema::ParseFunctionDefBody(DeclTy *D, StmtTy *Body) { |
| 758 | FunctionDecl *FD = static_cast<FunctionDecl*>(D); |
| 759 | FD->setBody((Stmt*)Body); |
| 760 | |
| 761 | assert(FD == CurFunctionDecl && "Function parsing confused"); |
| 762 | CurFunctionDecl = 0; |
| 763 | |
| 764 | // Verify and clean out per-function state. |
| 765 | |
| 766 | // Check goto/label use. |
| 767 | for (llvm::DenseMap<IdentifierInfo*, LabelStmt*>::iterator |
| 768 | I = LabelMap.begin(), E = LabelMap.end(); I != E; ++I) { |
| 769 | // Verify that we have no forward references left. If so, there was a goto |
| 770 | // or address of a label taken, but no definition of it. Label fwd |
| 771 | // definitions are indicated with a null substmt. |
| 772 | if (I->second->getSubStmt() == 0) { |
| 773 | LabelStmt *L = I->second; |
| 774 | // Emit error. |
| 775 | Diag(L->getIdentLoc(), diag::err_undeclared_label_use, L->getName()); |
| 776 | |
| 777 | // At this point, we have gotos that use the bogus label. Stitch it into |
| 778 | // the function body so that they aren't leaked and that the AST is well |
| 779 | // formed. |
| 780 | L->setSubStmt(new NullStmt(L->getIdentLoc())); |
| 781 | cast<CompoundStmt>((Stmt*)Body)->push_back(L); |
| 782 | } |
| 783 | } |
| 784 | LabelMap.clear(); |
| 785 | |
| 786 | return FD; |
| 787 | } |
| 788 | |
| 789 | |
| 790 | /// ImplicitlyDefineFunction - An undeclared identifier was used in a function |
| 791 | /// call, forming a call to an implicitly defined function (per C99 6.5.1p2). |
| 792 | Decl *Sema::ImplicitlyDefineFunction(SourceLocation Loc, IdentifierInfo &II, |
| 793 | Scope *S) { |
| 794 | if (getLangOptions().C99) // Extension in C99. |
| 795 | Diag(Loc, diag::ext_implicit_function_decl, II.getName()); |
| 796 | else // Legal in C90, but warn about it. |
| 797 | Diag(Loc, diag::warn_implicit_function_decl, II.getName()); |
| 798 | |
| 799 | // FIXME: handle stuff like: |
| 800 | // void foo() { extern float X(); } |
| 801 | // void bar() { X(); } <-- implicit decl for X in another scope. |
| 802 | |
| 803 | // Set a Declarator for the implicit definition: int foo(); |
| 804 | const char *Dummy; |
| 805 | DeclSpec DS; |
| 806 | bool Error = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, Dummy); |
| 807 | Error = Error; // Silence warning. |
| 808 | assert(!Error && "Error setting up implicit decl!"); |
| 809 | Declarator D(DS, Declarator::BlockContext); |
| 810 | D.AddTypeInfo(DeclaratorChunk::getFunction(false, false, 0, 0, Loc)); |
| 811 | D.SetIdentifier(&II, Loc); |
| 812 | |
| 813 | // Find translation-unit scope to insert this function into. |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 814 | if (Scope *FnS = S->getFnParent()) |
| 815 | S = FnS->getParent(); // Skip all scopes in a function at once. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 816 | while (S->getParent()) |
| 817 | S = S->getParent(); |
| 818 | |
| 819 | return static_cast<Decl*>(ParseDeclarator(S, D, 0, 0)); |
| 820 | } |
| 821 | |
| 822 | |
| 823 | TypedefDecl *Sema::ParseTypedefDecl(Scope *S, Declarator &D, |
| 824 | Decl *LastDeclarator) { |
| 825 | assert(D.getIdentifier() && "Wrong callback for declspec without declarator"); |
| 826 | |
| 827 | QualType T = GetTypeForDeclarator(D, S); |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 828 | assert(!T.isNull() && "GetTypeForDeclarator() returned null type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 829 | |
| 830 | // Scope manipulation handled by caller. |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 831 | TypedefDecl *NewTD = new TypedefDecl(D.getIdentifierLoc(), D.getIdentifier(), |
| 832 | T, LastDeclarator); |
| 833 | if (D.getInvalidType()) |
| 834 | NewTD->setInvalidDecl(); |
| 835 | return NewTD; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | |
| 839 | /// ParseTag - This is invoked when we see 'struct foo' or 'struct {'. In the |
| 840 | /// former case, Name will be non-null. In the later case, Name will be null. |
| 841 | /// TagType indicates what kind of tag this is. TK indicates whether this is a |
| 842 | /// reference/declaration/definition of a tag. |
| 843 | Sema::DeclTy *Sema::ParseTag(Scope *S, unsigned TagType, TagKind TK, |
| 844 | SourceLocation KWLoc, IdentifierInfo *Name, |
| 845 | SourceLocation NameLoc, AttributeList *Attr) { |
| 846 | // If this is a use of an existing tag, it must have a name. |
| 847 | assert((Name != 0 || TK == TK_Definition) && |
| 848 | "Nameless record must be a definition!"); |
| 849 | |
| 850 | Decl::Kind Kind; |
| 851 | switch (TagType) { |
| 852 | default: assert(0 && "Unknown tag type!"); |
| 853 | case DeclSpec::TST_struct: Kind = Decl::Struct; break; |
| 854 | case DeclSpec::TST_union: Kind = Decl::Union; break; |
| 855 | //case DeclSpec::TST_class: Kind = Decl::Class; break; |
| 856 | case DeclSpec::TST_enum: Kind = Decl::Enum; break; |
| 857 | } |
| 858 | |
| 859 | // If this is a named struct, check to see if there was a previous forward |
| 860 | // declaration or definition. |
| 861 | if (TagDecl *PrevDecl = |
| 862 | dyn_cast_or_null<TagDecl>(LookupScopedDecl(Name, Decl::IDNS_Tag, |
| 863 | NameLoc, S))) { |
| 864 | |
| 865 | // If this is a use of a previous tag, or if the tag is already declared in |
| 866 | // the same scope (so that the definition/declaration completes or |
| 867 | // rementions the tag), reuse the decl. |
| 868 | if (TK == TK_Reference || S->isDeclScope(PrevDecl)) { |
| 869 | // Make sure that this wasn't declared as an enum and now used as a struct |
| 870 | // or something similar. |
| 871 | if (PrevDecl->getKind() != Kind) { |
| 872 | Diag(KWLoc, diag::err_use_with_wrong_tag, Name->getName()); |
| 873 | Diag(PrevDecl->getLocation(), diag::err_previous_use); |
| 874 | } |
| 875 | |
| 876 | // If this is a use or a forward declaration, we're good. |
| 877 | if (TK != TK_Definition) |
| 878 | return PrevDecl; |
| 879 | |
| 880 | // Diagnose attempts to redefine a tag. |
| 881 | if (PrevDecl->isDefinition()) { |
| 882 | Diag(NameLoc, diag::err_redefinition, Name->getName()); |
| 883 | Diag(PrevDecl->getLocation(), diag::err_previous_definition); |
| 884 | // If this is a redefinition, recover by making this struct be |
| 885 | // anonymous, which will make any later references get the previous |
| 886 | // definition. |
| 887 | Name = 0; |
| 888 | } else { |
| 889 | // Okay, this is definition of a previously declared or referenced tag. |
| 890 | // Move the location of the decl to be the definition site. |
| 891 | PrevDecl->setLocation(NameLoc); |
| 892 | return PrevDecl; |
| 893 | } |
| 894 | } |
| 895 | // If we get here, this is a definition of a new struct type in a nested |
| 896 | // scope, e.g. "struct foo; void bar() { struct foo; }", just create a new |
| 897 | // type. |
| 898 | } |
| 899 | |
| 900 | // If there is an identifier, use the location of the identifier as the |
| 901 | // location of the decl, otherwise use the location of the struct/union |
| 902 | // keyword. |
| 903 | SourceLocation Loc = NameLoc.isValid() ? NameLoc : KWLoc; |
| 904 | |
| 905 | // Otherwise, if this is the first time we've seen this tag, create the decl. |
| 906 | TagDecl *New; |
| 907 | switch (Kind) { |
| 908 | default: assert(0 && "Unknown tag kind!"); |
| 909 | case Decl::Enum: |
| 910 | // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.: |
| 911 | // enum X { A, B, C } D; D should chain to X. |
| 912 | New = new EnumDecl(Loc, Name, 0); |
| 913 | // If this is an undefined enum, warn. |
| 914 | if (TK != TK_Definition) Diag(Loc, diag::ext_forward_ref_enum); |
| 915 | break; |
| 916 | case Decl::Union: |
| 917 | case Decl::Struct: |
| 918 | case Decl::Class: |
| 919 | // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.: |
| 920 | // struct X { int A; } D; D should chain to X. |
| 921 | New = new RecordDecl(Kind, Loc, Name, 0); |
| 922 | break; |
| 923 | } |
| 924 | |
| 925 | // If this has an identifier, add it to the scope stack. |
| 926 | if (Name) { |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 927 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 928 | // we find one that is. |
| 929 | while ((S->getFlags() & Scope::DeclScope) == 0) |
| 930 | S = S->getParent(); |
| 931 | |
| 932 | // Add it to the decl chain. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 933 | New->setNext(Name->getFETokenInfo<Decl>()); |
| 934 | Name->setFETokenInfo(New); |
| 935 | S->AddDecl(New); |
| 936 | } |
| 937 | |
| 938 | return New; |
| 939 | } |
| 940 | |
| 941 | /// ParseField - Each field of a struct/union/class is passed into this in order |
| 942 | /// to create a FieldDecl object for it. |
| 943 | Sema::DeclTy *Sema::ParseField(Scope *S, DeclTy *TagDecl, |
| 944 | SourceLocation DeclStart, |
| 945 | Declarator &D, ExprTy *BitfieldWidth) { |
| 946 | IdentifierInfo *II = D.getIdentifier(); |
| 947 | Expr *BitWidth = (Expr*)BitfieldWidth; |
| 948 | |
| 949 | SourceLocation Loc = DeclStart; |
| 950 | if (II) Loc = D.getIdentifierLoc(); |
| 951 | |
| 952 | // FIXME: Unnamed fields can be handled in various different ways, for |
| 953 | // example, unnamed unions inject all members into the struct namespace! |
| 954 | |
| 955 | |
| 956 | if (BitWidth) { |
| 957 | // TODO: Validate. |
| 958 | //printf("WARNING: BITFIELDS IGNORED!\n"); |
| 959 | |
| 960 | // 6.7.2.1p3 |
| 961 | // 6.7.2.1p4 |
| 962 | |
| 963 | } else { |
| 964 | // Not a bitfield. |
| 965 | |
| 966 | // validate II. |
| 967 | |
| 968 | } |
| 969 | |
| 970 | QualType T = GetTypeForDeclarator(D, S); |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 971 | assert(!T.isNull() && "GetTypeForDeclarator() returned null type"); |
| 972 | bool InvalidDecl = false; |
Steve Naroff | d7444aa | 2007-08-31 17:20:07 +0000 | [diff] [blame] | 973 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 974 | // C99 6.7.2.1p8: A member of a structure or union may have any type other |
| 975 | // than a variably modified type. |
Steve Naroff | d7444aa | 2007-08-31 17:20:07 +0000 | [diff] [blame] | 976 | if (const VariableArrayType *VAT = T->getAsVariablyModifiedType()) { |
| 977 | Diag(Loc, diag::err_typecheck_illegal_vla, |
| 978 | VAT->getSizeExpr()->getSourceRange()); |
| 979 | InvalidDecl = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 980 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 981 | // FIXME: Chain fielddecls together. |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 982 | FieldDecl *NewFD = new FieldDecl(Loc, II, T, 0); |
| 983 | if (D.getInvalidType() || InvalidDecl) |
| 984 | NewFD->setInvalidDecl(); |
| 985 | return NewFD; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | void Sema::ParseRecordBody(SourceLocation RecLoc, DeclTy *RecDecl, |
| 989 | DeclTy **Fields, unsigned NumFields) { |
| 990 | RecordDecl *Record = cast<RecordDecl>(static_cast<Decl*>(RecDecl)); |
| 991 | if (Record->isDefinition()) { |
| 992 | // Diagnose code like: |
| 993 | // struct S { struct S {} X; }; |
| 994 | // We discover this when we complete the outer S. Reject and ignore the |
| 995 | // outer S. |
| 996 | Diag(Record->getLocation(), diag::err_nested_redefinition, |
| 997 | Record->getKindName()); |
| 998 | Diag(RecLoc, diag::err_previous_definition); |
| 999 | return; |
| 1000 | } |
| 1001 | |
| 1002 | // Verify that all the fields are okay. |
| 1003 | unsigned NumNamedMembers = 0; |
| 1004 | llvm::SmallVector<FieldDecl*, 32> RecFields; |
| 1005 | llvm::SmallSet<const IdentifierInfo*, 32> FieldIDs; |
| 1006 | |
| 1007 | for (unsigned i = 0; i != NumFields; ++i) { |
| 1008 | FieldDecl *FD = cast_or_null<FieldDecl>(static_cast<Decl*>(Fields[i])); |
| 1009 | if (!FD) continue; // Already issued a diagnostic. |
| 1010 | |
| 1011 | // Get the type for the field. |
Chris Lattner | 02c642e | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 1012 | Type *FDTy = FD->getType().getTypePtr(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1013 | |
| 1014 | // C99 6.7.2.1p2 - A field may not be a function type. |
Chris Lattner | 02c642e | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 1015 | if (FDTy->isFunctionType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1016 | Diag(FD->getLocation(), diag::err_field_declared_as_function, |
| 1017 | FD->getName()); |
| 1018 | delete FD; |
| 1019 | continue; |
| 1020 | } |
| 1021 | |
| 1022 | // C99 6.7.2.1p2 - A field may not be an incomplete type except... |
| 1023 | if (FDTy->isIncompleteType()) { |
| 1024 | if (i != NumFields-1 || // ... that the last member ... |
| 1025 | Record->getKind() != Decl::Struct || // ... of a structure ... |
Chris Lattner | 02c642e | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 1026 | !FDTy->isArrayType()) { //... may have incomplete array type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1027 | Diag(FD->getLocation(), diag::err_field_incomplete, FD->getName()); |
| 1028 | delete FD; |
| 1029 | continue; |
| 1030 | } |
| 1031 | if (NumNamedMembers < 1) { //... must have more than named member ... |
| 1032 | Diag(FD->getLocation(), diag::err_flexible_array_empty_struct, |
| 1033 | FD->getName()); |
| 1034 | delete FD; |
| 1035 | continue; |
| 1036 | } |
| 1037 | |
| 1038 | // Okay, we have a legal flexible array member at the end of the struct. |
| 1039 | Record->setHasFlexibleArrayMember(true); |
| 1040 | } |
| 1041 | |
| 1042 | |
| 1043 | /// C99 6.7.2.1p2 - a struct ending in a flexible array member cannot be the |
| 1044 | /// field of another structure or the element of an array. |
Chris Lattner | 02c642e | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 1045 | if (const RecordType *FDTTy = FDTy->getAsRecordType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1046 | if (FDTTy->getDecl()->hasFlexibleArrayMember()) { |
| 1047 | // If this is a member of a union, then entire union becomes "flexible". |
| 1048 | if (Record->getKind() == Decl::Union) { |
| 1049 | Record->setHasFlexibleArrayMember(true); |
| 1050 | } else { |
| 1051 | // If this is a struct/class and this is not the last element, reject |
| 1052 | // it. Note that GCC supports variable sized arrays in the middle of |
| 1053 | // structures. |
| 1054 | if (i != NumFields-1) { |
| 1055 | Diag(FD->getLocation(), diag::err_variable_sized_type_in_struct, |
| 1056 | FD->getName()); |
| 1057 | delete FD; |
| 1058 | continue; |
| 1059 | } |
| 1060 | |
| 1061 | // We support flexible arrays at the end of structs in other structs |
| 1062 | // as an extension. |
| 1063 | Diag(FD->getLocation(), diag::ext_flexible_array_in_struct, |
| 1064 | FD->getName()); |
| 1065 | Record->setHasFlexibleArrayMember(true); |
| 1066 | } |
| 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | // Keep track of the number of named members. |
| 1071 | if (IdentifierInfo *II = FD->getIdentifier()) { |
| 1072 | // Detect duplicate member names. |
| 1073 | if (!FieldIDs.insert(II)) { |
| 1074 | Diag(FD->getLocation(), diag::err_duplicate_member, II->getName()); |
| 1075 | // Find the previous decl. |
| 1076 | SourceLocation PrevLoc; |
| 1077 | for (unsigned i = 0, e = RecFields.size(); ; ++i) { |
| 1078 | assert(i != e && "Didn't find previous def!"); |
| 1079 | if (RecFields[i]->getIdentifier() == II) { |
| 1080 | PrevLoc = RecFields[i]->getLocation(); |
| 1081 | break; |
| 1082 | } |
| 1083 | } |
| 1084 | Diag(PrevLoc, diag::err_previous_definition); |
| 1085 | delete FD; |
| 1086 | continue; |
| 1087 | } |
| 1088 | ++NumNamedMembers; |
| 1089 | } |
| 1090 | |
| 1091 | // Remember good fields. |
| 1092 | RecFields.push_back(FD); |
| 1093 | } |
| 1094 | |
| 1095 | |
| 1096 | // Okay, we successfully defined 'Record'. |
| 1097 | Record->defineBody(&RecFields[0], RecFields.size()); |
| 1098 | } |
| 1099 | |
| 1100 | Sema::DeclTy *Sema::ParseEnumConstant(Scope *S, DeclTy *theEnumDecl, |
| 1101 | DeclTy *lastEnumConst, |
| 1102 | SourceLocation IdLoc, IdentifierInfo *Id, |
| 1103 | SourceLocation EqualLoc, ExprTy *val) { |
| 1104 | theEnumDecl = theEnumDecl; // silence unused warning. |
| 1105 | EnumConstantDecl *LastEnumConst = |
| 1106 | cast_or_null<EnumConstantDecl>(static_cast<Decl*>(lastEnumConst)); |
| 1107 | Expr *Val = static_cast<Expr*>(val); |
| 1108 | |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 1109 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 1110 | // we find one that is. |
| 1111 | while ((S->getFlags() & Scope::DeclScope) == 0) |
| 1112 | S = S->getParent(); |
| 1113 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1114 | // Verify that there isn't already something declared with this name in this |
| 1115 | // scope. |
| 1116 | if (Decl *PrevDecl = LookupScopedDecl(Id, Decl::IDNS_Ordinary, IdLoc, S)) { |
| 1117 | if (S->isDeclScope(PrevDecl)) { |
| 1118 | if (isa<EnumConstantDecl>(PrevDecl)) |
| 1119 | Diag(IdLoc, diag::err_redefinition_of_enumerator, Id->getName()); |
| 1120 | else |
| 1121 | Diag(IdLoc, diag::err_redefinition, Id->getName()); |
| 1122 | Diag(PrevDecl->getLocation(), diag::err_previous_definition); |
| 1123 | // FIXME: Don't leak memory: delete Val; |
| 1124 | return 0; |
| 1125 | } |
| 1126 | } |
| 1127 | |
| 1128 | llvm::APSInt EnumVal(32); |
| 1129 | QualType EltTy; |
| 1130 | if (Val) { |
Chris Lattner | 421a23d | 2007-08-27 21:16:18 +0000 | [diff] [blame] | 1131 | // Make sure to promote the operand type to int. |
| 1132 | UsualUnaryConversions(Val); |
| 1133 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1134 | // C99 6.7.2.2p2: Make sure we have an integer constant expression. |
| 1135 | SourceLocation ExpLoc; |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 1136 | if (!Val->isIntegerConstantExpr(EnumVal, Context, &ExpLoc)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1137 | Diag(ExpLoc, diag::err_enum_value_not_integer_constant_expr, |
| 1138 | Id->getName()); |
| 1139 | // FIXME: Don't leak memory: delete Val; |
Chris Lattner | b7416f9 | 2007-08-27 17:37:24 +0000 | [diff] [blame] | 1140 | Val = 0; // Just forget about it. |
Chris Lattner | e9ca851 | 2007-08-29 16:03:41 +0000 | [diff] [blame] | 1141 | } else { |
| 1142 | EltTy = Val->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1143 | } |
Chris Lattner | b7416f9 | 2007-08-27 17:37:24 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
| 1146 | if (!Val) { |
| 1147 | if (LastEnumConst) { |
| 1148 | // Assign the last value + 1. |
| 1149 | EnumVal = LastEnumConst->getInitVal(); |
| 1150 | ++EnumVal; |
Chris Lattner | 421a23d | 2007-08-27 21:16:18 +0000 | [diff] [blame] | 1151 | |
| 1152 | // Check for overflow on increment. |
| 1153 | if (EnumVal < LastEnumConst->getInitVal()) |
| 1154 | Diag(IdLoc, diag::warn_enum_value_overflow); |
| 1155 | |
Chris Lattner | b7416f9 | 2007-08-27 17:37:24 +0000 | [diff] [blame] | 1156 | EltTy = LastEnumConst->getType(); |
| 1157 | } else { |
| 1158 | // First value, set to zero. |
| 1159 | EltTy = Context.IntTy; |
Chris Lattner | 701e5eb | 2007-09-04 02:45:27 +0000 | [diff] [blame] | 1160 | EnumVal.zextOrTrunc( |
| 1161 | static_cast<uint32_t>(Context.getTypeSize(EltTy, IdLoc))); |
Chris Lattner | b7416f9 | 2007-08-27 17:37:24 +0000 | [diff] [blame] | 1162 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1165 | EnumConstantDecl *New = new EnumConstantDecl(IdLoc, Id, EltTy, Val, EnumVal, |
| 1166 | LastEnumConst); |
| 1167 | |
| 1168 | // Register this decl in the current scope stack. |
| 1169 | New->setNext(Id->getFETokenInfo<Decl>()); |
| 1170 | Id->setFETokenInfo(New); |
| 1171 | S->AddDecl(New); |
| 1172 | return New; |
| 1173 | } |
| 1174 | |
| 1175 | void Sema::ParseEnumBody(SourceLocation EnumLoc, DeclTy *EnumDeclX, |
| 1176 | DeclTy **Elements, unsigned NumElements) { |
| 1177 | EnumDecl *Enum = cast<EnumDecl>(static_cast<Decl*>(EnumDeclX)); |
| 1178 | assert(!Enum->isDefinition() && "Enum redefinitions can't reach here"); |
| 1179 | |
Chris Lattner | e37f0be | 2007-08-28 05:10:31 +0000 | [diff] [blame] | 1180 | // TODO: If the result value doesn't fit in an int, it must be a long or long |
| 1181 | // long value. ISO C does not support this, but GCC does as an extension, |
| 1182 | // emit a warning. |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1183 | unsigned IntWidth = Context.Target.getIntWidth(Enum->getLocation()); |
Chris Lattner | e37f0be | 2007-08-28 05:10:31 +0000 | [diff] [blame] | 1184 | |
| 1185 | |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1186 | // Verify that all the values are okay, compute the size of the values, and |
| 1187 | // reverse the list. |
| 1188 | unsigned NumNegativeBits = 0; |
| 1189 | unsigned NumPositiveBits = 0; |
| 1190 | |
| 1191 | // Keep track of whether all elements have type int. |
| 1192 | bool AllElementsInt = true; |
| 1193 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1194 | EnumConstantDecl *EltList = 0; |
| 1195 | for (unsigned i = 0; i != NumElements; ++i) { |
| 1196 | EnumConstantDecl *ECD = |
| 1197 | cast_or_null<EnumConstantDecl>(static_cast<Decl*>(Elements[i])); |
| 1198 | if (!ECD) continue; // Already issued a diagnostic. |
Chris Lattner | 211a30e | 2007-08-28 05:27:00 +0000 | [diff] [blame] | 1199 | |
| 1200 | // If the enum value doesn't fit in an int, emit an extension warning. |
| 1201 | assert(ECD->getInitVal().getBitWidth() >= IntWidth && |
| 1202 | "Should have promoted value to int"); |
| 1203 | const llvm::APSInt &InitVal = ECD->getInitVal(); |
| 1204 | if (InitVal.getBitWidth() > IntWidth) { |
| 1205 | llvm::APSInt V(InitVal); |
| 1206 | V.trunc(IntWidth); |
| 1207 | V.extend(InitVal.getBitWidth()); |
| 1208 | if (V != InitVal) |
| 1209 | Diag(ECD->getLocation(), diag::ext_enum_value_not_int, |
| 1210 | InitVal.toString()); |
| 1211 | } |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1212 | |
| 1213 | // Keep track of the size of positive and negative values. |
| 1214 | if (InitVal.isUnsigned() || !InitVal.isNegative()) |
| 1215 | NumPositiveBits = std::max(NumPositiveBits, InitVal.getActiveBits()); |
| 1216 | else |
| 1217 | NumNegativeBits = std::max(NumNegativeBits, InitVal.getMinSignedBits()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1218 | |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1219 | // Keep track of whether every enum element has type int (very commmon). |
| 1220 | if (AllElementsInt) |
| 1221 | AllElementsInt = ECD->getType() == Context.IntTy; |
| 1222 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1223 | ECD->setNextDeclarator(EltList); |
| 1224 | EltList = ECD; |
| 1225 | } |
| 1226 | |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1227 | // Figure out the type that should be used for this enum. |
| 1228 | // FIXME: Support attribute(packed) on enums and -fshort-enums. |
| 1229 | QualType BestType; |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 1230 | unsigned BestWidth; |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1231 | |
| 1232 | if (NumNegativeBits) { |
| 1233 | // If there is a negative value, figure out the smallest integer type (of |
| 1234 | // int/long/longlong) that fits. |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 1235 | if (NumNegativeBits <= IntWidth && NumPositiveBits < IntWidth) { |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1236 | BestType = Context.IntTy; |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 1237 | BestWidth = IntWidth; |
| 1238 | } else { |
| 1239 | BestWidth = Context.Target.getLongWidth(Enum->getLocation()); |
| 1240 | if (NumNegativeBits <= BestWidth && NumPositiveBits < BestWidth) |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1241 | BestType = Context.LongTy; |
| 1242 | else { |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 1243 | BestWidth = Context.Target.getLongLongWidth(Enum->getLocation()); |
| 1244 | if (NumNegativeBits > BestWidth || NumPositiveBits >= BestWidth) |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1245 | Diag(Enum->getLocation(), diag::warn_enum_too_large); |
| 1246 | BestType = Context.LongLongTy; |
| 1247 | } |
| 1248 | } |
| 1249 | } else { |
| 1250 | // If there is no negative value, figure out which of uint, ulong, ulonglong |
| 1251 | // fits. |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 1252 | if (NumPositiveBits <= IntWidth) { |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1253 | BestType = Context.UnsignedIntTy; |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 1254 | BestWidth = IntWidth; |
| 1255 | } else if (NumPositiveBits <= |
| 1256 | (BestWidth = Context.Target.getLongWidth(Enum->getLocation()))) |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1257 | BestType = Context.UnsignedLongTy; |
| 1258 | else { |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 1259 | BestWidth = Context.Target.getLongLongWidth(Enum->getLocation()); |
| 1260 | assert(NumPositiveBits <= BestWidth && |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1261 | "How could an initializer get larger than ULL?"); |
| 1262 | BestType = Context.UnsignedLongLongTy; |
| 1263 | } |
| 1264 | } |
| 1265 | |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 1266 | // Loop over all of the enumerator constants, changing their types to match |
| 1267 | // the type of the enum if needed. |
| 1268 | for (unsigned i = 0; i != NumElements; ++i) { |
| 1269 | EnumConstantDecl *ECD = |
| 1270 | cast_or_null<EnumConstantDecl>(static_cast<Decl*>(Elements[i])); |
| 1271 | if (!ECD) continue; // Already issued a diagnostic. |
| 1272 | |
| 1273 | // Standard C says the enumerators have int type, but we allow, as an |
| 1274 | // extension, the enumerators to be larger than int size. If each |
| 1275 | // enumerator value fits in an int, type it as an int, otherwise type it the |
| 1276 | // same as the enumerator decl itself. This means that in "enum { X = 1U }" |
| 1277 | // that X has type 'int', not 'unsigned'. |
| 1278 | if (ECD->getType() == Context.IntTy) |
| 1279 | continue; // Already int type. |
| 1280 | |
| 1281 | // Determine whether the value fits into an int. |
| 1282 | llvm::APSInt InitVal = ECD->getInitVal(); |
| 1283 | bool FitsInInt; |
| 1284 | if (InitVal.isUnsigned() || !InitVal.isNegative()) |
| 1285 | FitsInInt = InitVal.getActiveBits() < IntWidth; |
| 1286 | else |
| 1287 | FitsInInt = InitVal.getMinSignedBits() <= IntWidth; |
| 1288 | |
| 1289 | // If it fits into an integer type, force it. Otherwise force it to match |
| 1290 | // the enum decl type. |
| 1291 | QualType NewTy; |
| 1292 | unsigned NewWidth; |
| 1293 | bool NewSign; |
| 1294 | if (FitsInInt) { |
| 1295 | NewTy = Context.IntTy; |
| 1296 | NewWidth = IntWidth; |
| 1297 | NewSign = true; |
| 1298 | } else if (ECD->getType() == BestType) { |
| 1299 | // Already the right type! |
| 1300 | continue; |
| 1301 | } else { |
| 1302 | NewTy = BestType; |
| 1303 | NewWidth = BestWidth; |
| 1304 | NewSign = BestType->isSignedIntegerType(); |
| 1305 | } |
| 1306 | |
| 1307 | // Adjust the APSInt value. |
| 1308 | InitVal.extOrTrunc(NewWidth); |
| 1309 | InitVal.setIsSigned(NewSign); |
| 1310 | ECD->setInitVal(InitVal); |
| 1311 | |
| 1312 | // Adjust the Expr initializer and type. |
| 1313 | ECD->setInitExpr(new ImplicitCastExpr(NewTy, ECD->getInitExpr())); |
| 1314 | ECD->setType(NewTy); |
| 1315 | } |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1316 | |
Chris Lattner | e00b18c | 2007-08-28 18:24:31 +0000 | [diff] [blame] | 1317 | Enum->defineElements(EltList, BestType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | void Sema::AddTopLevelDecl(Decl *current, Decl *last) { |
| 1321 | if (!current) return; |
| 1322 | |
| 1323 | // If this is a top-level decl that is chained to some other (e.g. int A,B,C;) |
| 1324 | // remember this in the LastInGroupList list. |
| 1325 | if (last) |
| 1326 | LastInGroupList.push_back((Decl*)last); |
| 1327 | } |
| 1328 | |
| 1329 | void Sema::HandleDeclAttribute(Decl *New, AttributeList *rawAttr) { |
| 1330 | if (strcmp(rawAttr->getAttributeName()->getName(), "vector_size") == 0) { |
| 1331 | if (ValueDecl *vDecl = dyn_cast<ValueDecl>(New)) { |
| 1332 | QualType newType = HandleVectorTypeAttribute(vDecl->getType(), rawAttr); |
| 1333 | if (!newType.isNull()) // install the new vector type into the decl |
| 1334 | vDecl->setType(newType); |
| 1335 | } |
| 1336 | if (TypedefDecl *tDecl = dyn_cast<TypedefDecl>(New)) { |
| 1337 | QualType newType = HandleVectorTypeAttribute(tDecl->getUnderlyingType(), |
| 1338 | rawAttr); |
| 1339 | if (!newType.isNull()) // install the new vector type into the decl |
| 1340 | tDecl->setUnderlyingType(newType); |
| 1341 | } |
| 1342 | } |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1343 | if (strcmp(rawAttr->getAttributeName()->getName(), "ocu_vector_type") == 0) { |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1344 | if (TypedefDecl *tDecl = dyn_cast<TypedefDecl>(New)) |
| 1345 | HandleOCUVectorTypeAttribute(tDecl, rawAttr); |
| 1346 | else |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1347 | Diag(rawAttr->getAttributeLoc(), |
| 1348 | diag::err_typecheck_ocu_vector_not_typedef); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1349 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1350 | // FIXME: add other attributes... |
| 1351 | } |
| 1352 | |
| 1353 | void Sema::HandleDeclAttributes(Decl *New, AttributeList *declspec_prefix, |
| 1354 | AttributeList *declarator_postfix) { |
| 1355 | while (declspec_prefix) { |
| 1356 | HandleDeclAttribute(New, declspec_prefix); |
| 1357 | declspec_prefix = declspec_prefix->getNext(); |
| 1358 | } |
| 1359 | while (declarator_postfix) { |
| 1360 | HandleDeclAttribute(New, declarator_postfix); |
| 1361 | declarator_postfix = declarator_postfix->getNext(); |
| 1362 | } |
| 1363 | } |
| 1364 | |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1365 | void Sema::HandleOCUVectorTypeAttribute(TypedefDecl *tDecl, |
| 1366 | AttributeList *rawAttr) { |
| 1367 | QualType curType = tDecl->getUnderlyingType(); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1368 | // check the attribute arugments. |
| 1369 | if (rawAttr->getNumArgs() != 1) { |
| 1370 | Diag(rawAttr->getAttributeLoc(), diag::err_attribute_wrong_number_arguments, |
| 1371 | std::string("1")); |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1372 | return; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1373 | } |
| 1374 | Expr *sizeExpr = static_cast<Expr *>(rawAttr->getArg(0)); |
| 1375 | llvm::APSInt vecSize(32); |
| 1376 | if (!sizeExpr->isIntegerConstantExpr(vecSize, Context)) { |
| 1377 | Diag(rawAttr->getAttributeLoc(), diag::err_attribute_vector_size_not_int, |
| 1378 | sizeExpr->getSourceRange()); |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1379 | return; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1380 | } |
| 1381 | // unlike gcc's vector_size attribute, we do not allow vectors to be defined |
| 1382 | // in conjunction with complex types (pointers, arrays, functions, etc.). |
| 1383 | Type *canonType = curType.getCanonicalType().getTypePtr(); |
| 1384 | if (!(canonType->isIntegerType() || canonType->isRealFloatingType())) { |
| 1385 | Diag(rawAttr->getAttributeLoc(), diag::err_attribute_invalid_vector_type, |
| 1386 | curType.getCanonicalType().getAsString()); |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1387 | return; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1388 | } |
| 1389 | // unlike gcc's vector_size attribute, the size is specified as the |
| 1390 | // number of elements, not the number of bytes. |
Chris Lattner | 701e5eb | 2007-09-04 02:45:27 +0000 | [diff] [blame] | 1391 | unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue()); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1392 | |
| 1393 | if (vectorSize == 0) { |
| 1394 | Diag(rawAttr->getAttributeLoc(), diag::err_attribute_zero_size, |
| 1395 | sizeExpr->getSourceRange()); |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1396 | return; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1397 | } |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1398 | // Instantiate/Install the vector type, the number of elements is > 0. |
| 1399 | tDecl->setUnderlyingType(Context.getOCUVectorType(curType, vectorSize)); |
| 1400 | // Remember this typedef decl, we will need it later for diagnostics. |
| 1401 | OCUVectorDecls.push_back(tDecl); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1402 | } |
| 1403 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1404 | QualType Sema::HandleVectorTypeAttribute(QualType curType, |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1405 | AttributeList *rawAttr) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1406 | // check the attribute arugments. |
| 1407 | if (rawAttr->getNumArgs() != 1) { |
| 1408 | Diag(rawAttr->getAttributeLoc(), diag::err_attribute_wrong_number_arguments, |
| 1409 | std::string("1")); |
| 1410 | return QualType(); |
| 1411 | } |
| 1412 | Expr *sizeExpr = static_cast<Expr *>(rawAttr->getArg(0)); |
| 1413 | llvm::APSInt vecSize(32); |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 1414 | if (!sizeExpr->isIntegerConstantExpr(vecSize, Context)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1415 | Diag(rawAttr->getAttributeLoc(), diag::err_attribute_vector_size_not_int, |
| 1416 | sizeExpr->getSourceRange()); |
| 1417 | return QualType(); |
| 1418 | } |
| 1419 | // navigate to the base type - we need to provide for vector pointers, |
| 1420 | // vector arrays, and functions returning vectors. |
| 1421 | Type *canonType = curType.getCanonicalType().getTypePtr(); |
| 1422 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1423 | if (canonType->isPointerType() || canonType->isArrayType() || |
| 1424 | canonType->isFunctionType()) { |
| 1425 | assert(1 && "HandleVector(): Complex type construction unimplemented"); |
| 1426 | /* FIXME: rebuild the type from the inside out, vectorizing the inner type. |
| 1427 | do { |
| 1428 | if (PointerType *PT = dyn_cast<PointerType>(canonType)) |
| 1429 | canonType = PT->getPointeeType().getTypePtr(); |
| 1430 | else if (ArrayType *AT = dyn_cast<ArrayType>(canonType)) |
| 1431 | canonType = AT->getElementType().getTypePtr(); |
| 1432 | else if (FunctionType *FT = dyn_cast<FunctionType>(canonType)) |
| 1433 | canonType = FT->getResultType().getTypePtr(); |
| 1434 | } while (canonType->isPointerType() || canonType->isArrayType() || |
| 1435 | canonType->isFunctionType()); |
| 1436 | */ |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1437 | } |
| 1438 | // the base type must be integer or float. |
| 1439 | if (!(canonType->isIntegerType() || canonType->isRealFloatingType())) { |
| 1440 | Diag(rawAttr->getAttributeLoc(), diag::err_attribute_invalid_vector_type, |
| 1441 | curType.getCanonicalType().getAsString()); |
| 1442 | return QualType(); |
| 1443 | } |
Chris Lattner | 701e5eb | 2007-09-04 02:45:27 +0000 | [diff] [blame] | 1444 | unsigned typeSize = static_cast<unsigned>( |
| 1445 | Context.getTypeSize(curType, rawAttr->getAttributeLoc())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1446 | // vecSize is specified in bytes - convert to bits. |
Chris Lattner | 701e5eb | 2007-09-04 02:45:27 +0000 | [diff] [blame] | 1447 | unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1448 | |
| 1449 | // the vector size needs to be an integral multiple of the type size. |
| 1450 | if (vectorSize % typeSize) { |
| 1451 | Diag(rawAttr->getAttributeLoc(), diag::err_attribute_invalid_size, |
| 1452 | sizeExpr->getSourceRange()); |
| 1453 | return QualType(); |
| 1454 | } |
| 1455 | if (vectorSize == 0) { |
| 1456 | Diag(rawAttr->getAttributeLoc(), diag::err_attribute_zero_size, |
| 1457 | sizeExpr->getSourceRange()); |
| 1458 | return QualType(); |
| 1459 | } |
| 1460 | // Since OpenCU requires 3 element vectors (OpenCU 5.1.2), we don't restrict |
| 1461 | // the number of elements to be a power of two (unlike GCC). |
| 1462 | // Instantiate the vector type, the number of elements is > 0. |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1463 | return Context.getVectorType(curType, vectorSize/typeSize); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1464 | } |
| 1465 | |