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