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