Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- SemaDecl.cpp - Semantic Analysis for Declarations ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for declarations. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
Chris Lattner | e1e7985 | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTConsumer.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Anders Carlsson | f78915f | 2008-02-15 07:04:12 +0000 | [diff] [blame] | 17 | #include "clang/AST/Attr.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 18 | #include "clang/AST/Builtins.h" |
| 19 | #include "clang/AST/Decl.h" |
| 20 | #include "clang/AST/Expr.h" |
| 21 | #include "clang/AST/Type.h" |
| 22 | #include "clang/Parse/DeclSpec.h" |
| 23 | #include "clang/Parse/Scope.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 24 | #include "clang/Basic/LangOptions.h" |
| 25 | #include "clang/Basic/TargetInfo.h" |
Steve Naroff | 4c49a6c | 2008-01-30 23:46:05 +0000 | [diff] [blame] | 26 | #include "clang/Basic/SourceManager.h" |
| 27 | // FIXME: layering (ideally, Sema shouldn't be dependent on Lex API's) |
Chris Lattner | e1e7985 | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 28 | #include "clang/Lex/Preprocessor.h" |
Steve Naroff | 4c49a6c | 2008-01-30 23:46:05 +0000 | [diff] [blame] | 29 | #include "clang/Lex/HeaderSearch.h" |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/SmallString.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/SmallSet.h" |
Fariborz Jahanian | 85ff264 | 2007-10-05 18:00:57 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/DenseSet.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 33 | using namespace clang; |
| 34 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 35 | Sema::DeclTy *Sema::isTypeName(const IdentifierInfo &II, Scope *S) const { |
Fariborz Jahanian | bece4ac | 2007-10-12 16:34:10 +0000 | [diff] [blame] | 36 | Decl *IIDecl = II.getFETokenInfo<Decl>(); |
| 37 | // Find first occurance of none-tagged declaration |
| 38 | while(IIDecl && IIDecl->getIdentifierNamespace() != Decl::IDNS_Ordinary) |
| 39 | IIDecl = cast<ScopedDecl>(IIDecl)->getNext(); |
| 40 | if (!IIDecl) |
| 41 | return 0; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 42 | if (isa<TypedefDecl>(IIDecl) || isa<ObjCInterfaceDecl>(IIDecl)) |
Fariborz Jahanian | bece4ac | 2007-10-12 16:34:10 +0000 | [diff] [blame] | 43 | return IIDecl; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 44 | if (ObjCCompatibleAliasDecl *ADecl = |
| 45 | dyn_cast<ObjCCompatibleAliasDecl>(IIDecl)) |
Fariborz Jahanian | bece4ac | 2007-10-12 16:34:10 +0000 | [diff] [blame] | 46 | return ADecl->getClassInterface(); |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 47 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Steve Naroff | b216c88 | 2007-10-09 22:01:59 +0000 | [diff] [blame] | 50 | void Sema::ActOnPopScope(SourceLocation Loc, Scope *S) { |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 51 | if (S->decl_empty()) return; |
| 52 | assert((S->getFlags() & Scope::DeclScope) &&"Scope shouldn't contain decls!"); |
| 53 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 54 | for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end(); |
| 55 | I != E; ++I) { |
Steve Naroff | c752d04 | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 56 | Decl *TmpD = static_cast<Decl*>(*I); |
| 57 | assert(TmpD && "This decl didn't get pushed??"); |
| 58 | ScopedDecl *D = dyn_cast<ScopedDecl>(TmpD); |
| 59 | assert(D && "This decl isn't a ScopedDecl?"); |
| 60 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 61 | IdentifierInfo *II = D->getIdentifier(); |
| 62 | if (!II) continue; |
| 63 | |
| 64 | // Unlink this decl from the identifier. Because the scope contains decls |
| 65 | // in an unordered collection, and because we have multiple identifier |
| 66 | // namespaces (e.g. tag, normal, label),the decl may not be the first entry. |
| 67 | if (II->getFETokenInfo<Decl>() == D) { |
| 68 | // Normal case, no multiple decls in different namespaces. |
| 69 | II->setFETokenInfo(D->getNext()); |
| 70 | } else { |
| 71 | // Scan ahead. There are only three namespaces in C, so this loop can |
| 72 | // never execute more than 3 times. |
Steve Naroff | c752d04 | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 73 | ScopedDecl *SomeDecl = II->getFETokenInfo<ScopedDecl>(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 74 | while (SomeDecl->getNext() != D) { |
| 75 | SomeDecl = SomeDecl->getNext(); |
| 76 | assert(SomeDecl && "Didn't find this decl on its identifier's chain!"); |
| 77 | } |
| 78 | SomeDecl->setNext(D->getNext()); |
| 79 | } |
| 80 | |
| 81 | // This will have to be revisited for C++: there we want to nest stuff in |
| 82 | // namespace decls etc. Even for C, we might want a top-level translation |
| 83 | // unit decl or something. |
| 84 | if (!CurFunctionDecl) |
| 85 | continue; |
| 86 | |
| 87 | // Chain this decl to the containing function, it now owns the memory for |
| 88 | // the decl. |
| 89 | D->setNext(CurFunctionDecl->getDeclChain()); |
| 90 | CurFunctionDecl->setDeclChain(D); |
| 91 | } |
| 92 | } |
| 93 | |
Fariborz Jahanian | 4cabdfc | 2007-10-12 19:38:20 +0000 | [diff] [blame] | 94 | /// LookupInterfaceDecl - Lookup interface declaration in the scope chain. |
| 95 | /// Return the first declaration found (which may or may not be a class |
Fariborz Jahanian | 3fe44e4 | 2007-10-12 19:53:08 +0000 | [diff] [blame] | 96 | /// declaration. Caller is responsible for handling the none-class case. |
Fariborz Jahanian | 4cabdfc | 2007-10-12 19:38:20 +0000 | [diff] [blame] | 97 | /// Bypassing the alias of a class by returning the aliased class. |
| 98 | ScopedDecl *Sema::LookupInterfaceDecl(IdentifierInfo *ClassName) { |
| 99 | ScopedDecl *IDecl; |
| 100 | // Scan up the scope chain looking for a decl that matches this identifier |
| 101 | // that is in the appropriate namespace. |
| 102 | for (IDecl = ClassName->getFETokenInfo<ScopedDecl>(); IDecl; |
| 103 | IDecl = IDecl->getNext()) |
| 104 | if (IDecl->getIdentifierNamespace() == Decl::IDNS_Ordinary) |
| 105 | break; |
| 106 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 107 | if (ObjCCompatibleAliasDecl *ADecl = |
| 108 | dyn_cast_or_null<ObjCCompatibleAliasDecl>(IDecl)) |
Fariborz Jahanian | 4cabdfc | 2007-10-12 19:38:20 +0000 | [diff] [blame] | 109 | return ADecl->getClassInterface(); |
| 110 | return IDecl; |
| 111 | } |
| 112 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 113 | /// getObjCInterfaceDecl - Look up a for a class declaration in the scope. |
Fariborz Jahanian | 9d048ff | 2007-09-29 00:54:24 +0000 | [diff] [blame] | 114 | /// return 0 if one not found. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 115 | ObjCInterfaceDecl *Sema::getObjCInterfaceDecl(IdentifierInfo *Id) { |
Fariborz Jahanian | 4cabdfc | 2007-10-12 19:38:20 +0000 | [diff] [blame] | 116 | ScopedDecl *IdDecl = LookupInterfaceDecl(Id); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 117 | return cast_or_null<ObjCInterfaceDecl>(IdDecl); |
Fariborz Jahanian | 9d048ff | 2007-09-29 00:54:24 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 120 | /// LookupScopedDecl - Look up the inner-most declaration in the specified |
| 121 | /// namespace. |
Steve Naroff | c752d04 | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 122 | ScopedDecl *Sema::LookupScopedDecl(IdentifierInfo *II, unsigned NSI, |
| 123 | SourceLocation IdLoc, Scope *S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 124 | if (II == 0) return 0; |
| 125 | Decl::IdentifierNamespace NS = (Decl::IdentifierNamespace)NSI; |
| 126 | |
| 127 | // Scan up the scope chain looking for a decl that matches this identifier |
| 128 | // that is in the appropriate namespace. This search should not take long, as |
| 129 | // shadowing of names is uncommon, and deep shadowing is extremely uncommon. |
Steve Naroff | c752d04 | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 130 | for (ScopedDecl *D = II->getFETokenInfo<ScopedDecl>(); D; D = D->getNext()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 131 | if (D->getIdentifierNamespace() == NS) |
| 132 | return D; |
| 133 | |
| 134 | // If we didn't find a use of this identifier, and if the identifier |
| 135 | // corresponds to a compiler builtin, create the decl object for the builtin |
| 136 | // now, injecting it into translation unit scope, and return it. |
| 137 | if (NS == Decl::IDNS_Ordinary) { |
| 138 | // If this is a builtin on some other target, or if this builtin varies |
| 139 | // across targets (e.g. in type), emit a diagnostic and mark the translation |
| 140 | // unit non-portable for using it. |
| 141 | if (II->isNonPortableBuiltin()) { |
| 142 | // Only emit this diagnostic once for this builtin. |
| 143 | II->setNonPortableBuiltin(false); |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 144 | Context.Target.DiagnoseNonPortability(Context.getFullLoc(IdLoc), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 145 | diag::port_target_builtin_use); |
| 146 | } |
| 147 | // If this is a builtin on this (or all) targets, create the decl. |
| 148 | if (unsigned BuiltinID = II->getBuiltinID()) |
| 149 | return LazilyCreateBuiltin(II, BuiltinID, S); |
| 150 | } |
| 151 | return 0; |
| 152 | } |
| 153 | |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 154 | void Sema::InitBuiltinVaListType() |
| 155 | { |
| 156 | if (!Context.getBuiltinVaListType().isNull()) |
| 157 | return; |
| 158 | |
| 159 | IdentifierInfo *VaIdent = &Context.Idents.get("__builtin_va_list"); |
| 160 | ScopedDecl *VaDecl = LookupScopedDecl(VaIdent, Decl::IDNS_Ordinary, |
| 161 | SourceLocation(), TUScope); |
Steve Naroff | 733002f | 2007-10-18 22:17:45 +0000 | [diff] [blame] | 162 | TypedefDecl *VaTypedef = cast<TypedefDecl>(VaDecl); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 163 | Context.setBuiltinVaListType(Context.getTypedefType(VaTypedef)); |
| 164 | } |
| 165 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 166 | /// LazilyCreateBuiltin - The specified Builtin-ID was first used at file scope. |
| 167 | /// lazily create a decl for it. |
Chris Lattner | 22b73ba | 2007-10-10 23:42:28 +0000 | [diff] [blame] | 168 | ScopedDecl *Sema::LazilyCreateBuiltin(IdentifierInfo *II, unsigned bid, |
| 169 | Scope *S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 170 | Builtin::ID BID = (Builtin::ID)bid; |
| 171 | |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 172 | if (BID == Builtin::BI__builtin_va_start || |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 173 | BID == Builtin::BI__builtin_va_copy || |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 174 | BID == Builtin::BI__builtin_va_end) |
| 175 | InitBuiltinVaListType(); |
| 176 | |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 177 | QualType R = Context.BuiltinInfo.GetBuiltinType(BID, Context); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 178 | FunctionDecl *New = new FunctionDecl(SourceLocation(), II, R, |
Chris Lattner | 70c8b2e | 2007-08-26 04:02:13 +0000 | [diff] [blame] | 179 | FunctionDecl::Extern, false, 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 180 | |
| 181 | // Find translation-unit scope to insert this function into. |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 182 | if (Scope *FnS = S->getFnParent()) |
| 183 | S = FnS->getParent(); // Skip all scopes in a function at once. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 184 | while (S->getParent()) |
| 185 | S = S->getParent(); |
| 186 | S->AddDecl(New); |
| 187 | |
| 188 | // Add this decl to the end of the identifier info. |
Steve Naroff | c752d04 | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 189 | if (ScopedDecl *LastDecl = II->getFETokenInfo<ScopedDecl>()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 190 | // Scan until we find the last (outermost) decl in the id chain. |
| 191 | while (LastDecl->getNext()) |
| 192 | LastDecl = LastDecl->getNext(); |
| 193 | // Insert before (outside) it. |
| 194 | LastDecl->setNext(New); |
| 195 | } else { |
| 196 | II->setFETokenInfo(New); |
| 197 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 198 | return New; |
| 199 | } |
| 200 | |
| 201 | /// MergeTypeDefDecl - We just parsed a typedef 'New' which has the same name |
| 202 | /// and scope as a previous declaration 'Old'. Figure out how to resolve this |
| 203 | /// situation, merging decls or emitting diagnostics as appropriate. |
| 204 | /// |
Steve Naroff | 8e74c93 | 2007-09-13 21:41:19 +0000 | [diff] [blame] | 205 | TypedefDecl *Sema::MergeTypeDefDecl(TypedefDecl *New, ScopedDecl *OldD) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 206 | // Verify the old decl was also a typedef. |
| 207 | TypedefDecl *Old = dyn_cast<TypedefDecl>(OldD); |
| 208 | if (!Old) { |
| 209 | Diag(New->getLocation(), diag::err_redefinition_different_kind, |
| 210 | New->getName()); |
| 211 | Diag(OldD->getLocation(), diag::err_previous_definition); |
| 212 | return New; |
| 213 | } |
| 214 | |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 215 | // Allow multiple definitions for ObjC built-in typedefs. |
| 216 | // FIXME: Verify the underlying types are equivalent! |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 217 | if (getLangOptions().ObjC1 && isBuiltinObjCType(New)) |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 218 | return Old; |
Steve Naroff | 4c49a6c | 2008-01-30 23:46:05 +0000 | [diff] [blame] | 219 | |
| 220 | // Redeclaration of a type is a constraint violation (6.7.2.3p1). |
| 221 | // Apparently GCC, Intel, and Sun all silently ignore the redeclaration if |
| 222 | // *either* declaration is in a system header. The code below implements |
| 223 | // this adhoc compatibility rule. FIXME: The following code will not |
| 224 | // work properly when compiling ".i" files (containing preprocessed output). |
| 225 | SourceManager &SrcMgr = Context.getSourceManager(); |
| 226 | const FileEntry *OldDeclFile = SrcMgr.getFileEntryForLoc(Old->getLocation()); |
| 227 | const FileEntry *NewDeclFile = SrcMgr.getFileEntryForLoc(New->getLocation()); |
| 228 | HeaderSearch &HdrInfo = PP.getHeaderSearchInfo(); |
| 229 | DirectoryLookup::DirType OldDirType = HdrInfo.getFileDirFlavor(OldDeclFile); |
| 230 | DirectoryLookup::DirType NewDirType = HdrInfo.getFileDirFlavor(NewDeclFile); |
| 231 | |
Steve Naroff | d62701b | 2008-02-07 03:50:06 +0000 | [diff] [blame] | 232 | if ((OldDirType == DirectoryLookup::ExternCSystemHeaderDir || |
| 233 | NewDirType == DirectoryLookup::ExternCSystemHeaderDir) || |
| 234 | getLangOptions().Microsoft) |
Steve Naroff | 4c49a6c | 2008-01-30 23:46:05 +0000 | [diff] [blame] | 235 | return New; |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 236 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 237 | // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope. |
| 238 | // TODO: This is totally simplistic. It should handle merging functions |
| 239 | // together etc, merging extern int X; int X; ... |
| 240 | Diag(New->getLocation(), diag::err_redefinition, New->getName()); |
| 241 | Diag(Old->getLocation(), diag::err_previous_definition); |
| 242 | return New; |
| 243 | } |
| 244 | |
| 245 | /// MergeFunctionDecl - We just parsed a function 'New' which has the same name |
| 246 | /// and scope as a previous declaration 'Old'. Figure out how to resolve this |
| 247 | /// situation, merging decls or emitting diagnostics as appropriate. |
| 248 | /// |
Steve Naroff | 8e74c93 | 2007-09-13 21:41:19 +0000 | [diff] [blame] | 249 | FunctionDecl *Sema::MergeFunctionDecl(FunctionDecl *New, ScopedDecl *OldD) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 250 | // Verify the old decl was also a function. |
| 251 | FunctionDecl *Old = dyn_cast<FunctionDecl>(OldD); |
| 252 | if (!Old) { |
| 253 | Diag(New->getLocation(), diag::err_redefinition_different_kind, |
| 254 | New->getName()); |
| 255 | Diag(OldD->getLocation(), diag::err_previous_definition); |
| 256 | return New; |
| 257 | } |
| 258 | |
Chris Lattner | 5519644 | 2007-11-20 19:04:50 +0000 | [diff] [blame] | 259 | QualType OldQType = Old->getCanonicalType(); |
| 260 | QualType NewQType = New->getCanonicalType(); |
| 261 | |
Steve Naroff | adbbd0c | 2008-01-14 20:51:29 +0000 | [diff] [blame] | 262 | // Function types need to be compatible, not identical. This handles |
| 263 | // duplicate function decls like "void f(int); void f(enum X);" properly. |
| 264 | if (Context.functionTypesAreCompatible(OldQType, NewQType)) |
| 265 | return New; |
Chris Lattner | e3995fe | 2007-11-06 06:07:26 +0000 | [diff] [blame] | 266 | |
Steve Naroff | 837618c | 2008-01-16 15:01:34 +0000 | [diff] [blame] | 267 | // A function that has already been declared has been redeclared or defined |
| 268 | // with a different type- show appropriate diagnostic |
| 269 | diag::kind PrevDiag = Old->getBody() ? diag::err_previous_definition : |
| 270 | diag::err_previous_declaration; |
| 271 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 272 | // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope. |
| 273 | // TODO: This is totally simplistic. It should handle merging functions |
| 274 | // together etc, merging extern int X; int X; ... |
Steve Naroff | 837618c | 2008-01-16 15:01:34 +0000 | [diff] [blame] | 275 | Diag(New->getLocation(), diag::err_conflicting_types, New->getName()); |
| 276 | Diag(Old->getLocation(), PrevDiag); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 277 | return New; |
| 278 | } |
| 279 | |
Chris Lattner | fcc2d26 | 2007-11-06 04:28:31 +0000 | [diff] [blame] | 280 | /// equivalentArrayTypes - Used to determine whether two array types are |
| 281 | /// equivalent. |
| 282 | /// We need to check this explicitly as an incomplete array definition is |
| 283 | /// considered a VariableArrayType, so will not match a complete array |
| 284 | /// definition that would be otherwise equivalent. |
| 285 | static bool areEquivalentArrayTypes(QualType NewQType, QualType OldQType) { |
| 286 | const ArrayType *NewAT = NewQType->getAsArrayType(); |
| 287 | const ArrayType *OldAT = OldQType->getAsArrayType(); |
| 288 | |
| 289 | if (!NewAT || !OldAT) |
| 290 | return false; |
| 291 | |
| 292 | // If either (or both) array types in incomplete we need to strip off the |
| 293 | // outer VariableArrayType. Once the outer VAT is removed the remaining |
| 294 | // types must be identical if the array types are to be considered |
| 295 | // equivalent. |
| 296 | // eg. int[][1] and int[1][1] become |
| 297 | // VAT(null, CAT(1, int)) and CAT(1, CAT(1, int)) |
| 298 | // removing the outermost VAT gives |
| 299 | // CAT(1, int) and CAT(1, int) |
| 300 | // which are equal, therefore the array types are equivalent. |
Eli Friedman | 9db1397 | 2008-02-15 12:53:51 +0000 | [diff] [blame] | 301 | if (NewAT->isIncompleteArrayType() || OldAT->isIncompleteArrayType()) { |
Chris Lattner | fcc2d26 | 2007-11-06 04:28:31 +0000 | [diff] [blame] | 302 | if (NewAT->getIndexTypeQualifier() != OldAT->getIndexTypeQualifier()) |
| 303 | return false; |
Eli Friedman | 0493025 | 2008-01-29 07:51:12 +0000 | [diff] [blame] | 304 | NewQType = NewAT->getElementType().getCanonicalType(); |
| 305 | OldQType = OldAT->getElementType().getCanonicalType(); |
Chris Lattner | fcc2d26 | 2007-11-06 04:28:31 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | return NewQType == OldQType; |
| 309 | } |
| 310 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 311 | /// MergeVarDecl - We just parsed a variable 'New' which has the same name |
| 312 | /// and scope as a previous declaration 'Old'. Figure out how to resolve this |
| 313 | /// situation, merging decls or emitting diagnostics as appropriate. |
| 314 | /// |
| 315 | /// FIXME: Need to carefully consider tentative definition rules (C99 6.9.2p2). |
| 316 | /// For example, we incorrectly complain about i1, i4 from C99 6.9.2p4. |
| 317 | /// |
Steve Naroff | 8e74c93 | 2007-09-13 21:41:19 +0000 | [diff] [blame] | 318 | VarDecl *Sema::MergeVarDecl(VarDecl *New, ScopedDecl *OldD) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 319 | // Verify the old decl was also a variable. |
| 320 | VarDecl *Old = dyn_cast<VarDecl>(OldD); |
| 321 | if (!Old) { |
| 322 | Diag(New->getLocation(), diag::err_redefinition_different_kind, |
| 323 | New->getName()); |
| 324 | Diag(OldD->getLocation(), diag::err_previous_definition); |
| 325 | return New; |
| 326 | } |
| 327 | // Verify the types match. |
Chris Lattner | fcc2d26 | 2007-11-06 04:28:31 +0000 | [diff] [blame] | 328 | if (Old->getCanonicalType() != New->getCanonicalType() && |
| 329 | !areEquivalentArrayTypes(New->getCanonicalType(), Old->getCanonicalType())) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 330 | Diag(New->getLocation(), diag::err_redefinition, New->getName()); |
| 331 | Diag(Old->getLocation(), diag::err_previous_definition); |
| 332 | return New; |
| 333 | } |
Steve Naroff | b7b032e | 2008-01-30 00:44:01 +0000 | [diff] [blame] | 334 | // C99 6.2.2p4: Check if we have a static decl followed by a non-static. |
| 335 | if (New->getStorageClass() == VarDecl::Static && |
| 336 | (Old->getStorageClass() == VarDecl::None || |
| 337 | Old->getStorageClass() == VarDecl::Extern)) { |
| 338 | Diag(New->getLocation(), diag::err_static_non_static, New->getName()); |
| 339 | Diag(Old->getLocation(), diag::err_previous_definition); |
| 340 | return New; |
| 341 | } |
| 342 | // C99 6.2.2p4: Check if we have a non-static decl followed by a static. |
| 343 | if (New->getStorageClass() != VarDecl::Static && |
| 344 | Old->getStorageClass() == VarDecl::Static) { |
| 345 | Diag(New->getLocation(), diag::err_non_static_static, New->getName()); |
| 346 | Diag(Old->getLocation(), diag::err_previous_definition); |
| 347 | return New; |
| 348 | } |
| 349 | // We've verified the types match, now handle "tentative" definitions. |
| 350 | FileVarDecl *OldFSDecl = dyn_cast<FileVarDecl>(Old); |
| 351 | FileVarDecl *NewFSDecl = dyn_cast<FileVarDecl>(New); |
| 352 | |
| 353 | if (OldFSDecl && NewFSDecl) { |
| 354 | // Handle C "tentative" external object definitions (C99 6.9.2). |
| 355 | bool OldIsTentative = false; |
| 356 | bool NewIsTentative = false; |
| 357 | |
| 358 | if (!OldFSDecl->getInit() && |
| 359 | (OldFSDecl->getStorageClass() == VarDecl::None || |
| 360 | OldFSDecl->getStorageClass() == VarDecl::Static)) |
| 361 | OldIsTentative = true; |
| 362 | |
| 363 | // FIXME: this check doesn't work (since the initializer hasn't been |
| 364 | // attached yet). This check should be moved to FinalizeDeclaratorGroup. |
| 365 | // Unfortunately, by the time we get to FinializeDeclaratorGroup, we've |
| 366 | // thrown out the old decl. |
| 367 | if (!NewFSDecl->getInit() && |
| 368 | (NewFSDecl->getStorageClass() == VarDecl::None || |
| 369 | NewFSDecl->getStorageClass() == VarDecl::Static)) |
| 370 | ; // change to NewIsTentative = true; once the code is moved. |
| 371 | |
| 372 | if (NewIsTentative || OldIsTentative) |
| 373 | return New; |
| 374 | } |
| 375 | if (Old->getStorageClass() != VarDecl::Extern && |
| 376 | New->getStorageClass() != VarDecl::Extern) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 377 | Diag(New->getLocation(), diag::err_redefinition, New->getName()); |
| 378 | Diag(Old->getLocation(), diag::err_previous_definition); |
| 379 | } |
| 380 | return New; |
| 381 | } |
| 382 | |
| 383 | /// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with |
| 384 | /// no declarator (e.g. "struct foo;") is parsed. |
| 385 | Sema::DeclTy *Sema::ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS) { |
| 386 | // TODO: emit error on 'int;' or 'const enum foo;'. |
| 387 | // TODO: emit error on 'typedef int;' |
| 388 | // if (!DS.isMissingDeclaratorOk()) Diag(...); |
| 389 | |
Steve Naroff | 9219928 | 2007-11-17 21:37:36 +0000 | [diff] [blame] | 390 | return dyn_cast_or_null<TagDecl>(static_cast<Decl *>(DS.getTypeRep())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 393 | bool Sema::CheckSingleInitializer(Expr *&Init, QualType DeclType) { |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 394 | // Get the type before calling CheckSingleAssignmentConstraints(), since |
| 395 | // it can promote the expression. |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 396 | QualType InitType = Init->getType(); |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 397 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 398 | AssignConvertType ConvTy = CheckSingleAssignmentConstraints(DeclType, Init); |
| 399 | return DiagnoseAssignmentResult(ConvTy, Init->getLocStart(), DeclType, |
| 400 | InitType, Init, "initializing"); |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Steve Naroff | 9e8925e | 2007-09-04 14:36:54 +0000 | [diff] [blame] | 403 | bool Sema::CheckInitExpr(Expr *expr, InitListExpr *IList, unsigned slot, |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 404 | QualType ElementType) { |
Chris Lattner | 33b7b06 | 2007-12-11 23:15:04 +0000 | [diff] [blame] | 405 | Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer. |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 406 | if (CheckSingleInitializer(expr, ElementType)) |
Chris Lattner | 33b7b06 | 2007-12-11 23:15:04 +0000 | [diff] [blame] | 407 | return true; // types weren't compatible. |
| 408 | |
Steve Naroff | 9e8925e | 2007-09-04 14:36:54 +0000 | [diff] [blame] | 409 | if (savExpr != expr) // The type was promoted, update initializer list. |
| 410 | IList->setInit(slot, expr); |
Steve Naroff | 371227d | 2007-09-04 02:20:04 +0000 | [diff] [blame] | 411 | return false; |
| 412 | } |
| 413 | |
Steve Naroff | a49e1fa | 2008-01-22 00:55:40 +0000 | [diff] [blame] | 414 | bool Sema::CheckStringLiteralInit(StringLiteral *strLiteral, QualType &DeclT) { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 415 | if (const IncompleteArrayType *IAT = DeclT->getAsIncompleteArrayType()) { |
Steve Naroff | a49e1fa | 2008-01-22 00:55:40 +0000 | [diff] [blame] | 416 | // C99 6.7.8p14. We have an array of character type with unknown size |
| 417 | // being initialized to a string literal. |
| 418 | llvm::APSInt ConstVal(32); |
| 419 | ConstVal = strLiteral->getByteLength() + 1; |
| 420 | // Return a new array type (C99 6.7.8p22). |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 421 | DeclT = Context.getConstantArrayType(IAT->getElementType(), ConstVal, |
Steve Naroff | a49e1fa | 2008-01-22 00:55:40 +0000 | [diff] [blame] | 422 | ArrayType::Normal, 0); |
| 423 | } else if (const ConstantArrayType *CAT = DeclT->getAsConstantArrayType()) { |
| 424 | // C99 6.7.8p14. We have an array of character type with known size. |
| 425 | if (strLiteral->getByteLength() > (unsigned)CAT->getMaximumElements()) |
| 426 | Diag(strLiteral->getSourceRange().getBegin(), |
| 427 | diag::warn_initializer_string_for_char_array_too_long, |
| 428 | strLiteral->getSourceRange()); |
| 429 | } else { |
| 430 | assert(0 && "HandleStringLiteralInit(): Invalid array type"); |
| 431 | } |
| 432 | // Set type from "char *" to "constant array of char". |
| 433 | strLiteral->setType(DeclT); |
| 434 | // For now, we always return false (meaning success). |
| 435 | return false; |
| 436 | } |
| 437 | |
| 438 | StringLiteral *Sema::IsStringLiteralInit(Expr *Init, QualType DeclType) { |
Steve Naroff | a49e1fa | 2008-01-22 00:55:40 +0000 | [diff] [blame] | 439 | const ArrayType *AT = DeclType->getAsArrayType(); |
Steve Naroff | a996033 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 440 | if (AT && AT->getElementType()->isCharType()) { |
| 441 | return dyn_cast<StringLiteral>(Init); |
| 442 | } |
Steve Naroff | a49e1fa | 2008-01-22 00:55:40 +0000 | [diff] [blame] | 443 | return 0; |
| 444 | } |
| 445 | |
Steve Naroff | a996033 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 446 | // CheckInitializerListTypes - Checks the types of elements of an initializer |
| 447 | // list. This function is recursive: it calls itself to initialize subelements |
| 448 | // of aggregate types. Note that the topLevel parameter essentially refers to |
| 449 | // whether this expression "owns" the initializer list passed in, or if this |
| 450 | // initialization is taking elements out of a parent initializer. Each |
| 451 | // call to this function adds zero or more to startIndex, reports any errors, |
| 452 | // and returns true if it found any inconsistent types. |
| 453 | bool Sema::CheckInitializerListTypes(InitListExpr*& IList, QualType &DeclType, |
| 454 | bool topLevel, unsigned& startIndex) { |
Steve Naroff | 2fdc374 | 2007-12-10 22:44:33 +0000 | [diff] [blame] | 455 | bool hadError = false; |
Steve Naroff | a996033 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 456 | |
| 457 | if (DeclType->isScalarType()) { |
| 458 | // The simplest case: initializing a single scalar |
| 459 | if (topLevel) { |
| 460 | Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init, |
| 461 | IList->getSourceRange()); |
| 462 | } |
| 463 | if (startIndex < IList->getNumInits()) { |
| 464 | Expr* expr = IList->getInit(startIndex); |
| 465 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
| 466 | // FIXME: Should an error be reported here instead? |
| 467 | unsigned newIndex = 0; |
| 468 | CheckInitializerListTypes(SubInitList, DeclType, true, newIndex); |
| 469 | } else { |
| 470 | hadError |= CheckInitExpr(expr, IList, startIndex, DeclType); |
| 471 | } |
| 472 | ++startIndex; |
| 473 | } |
| 474 | // FIXME: Should an error be reported for empty initializer list + scalar? |
| 475 | } else if (DeclType->isVectorType()) { |
| 476 | if (startIndex < IList->getNumInits()) { |
| 477 | const VectorType *VT = DeclType->getAsVectorType(); |
| 478 | int maxElements = VT->getNumElements(); |
| 479 | QualType elementType = VT->getElementType(); |
| 480 | |
| 481 | for (int i = 0; i < maxElements; ++i) { |
| 482 | // Don't attempt to go past the end of the init list |
| 483 | if (startIndex >= IList->getNumInits()) |
| 484 | break; |
| 485 | Expr* expr = IList->getInit(startIndex); |
| 486 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
| 487 | unsigned newIndex = 0; |
| 488 | hadError |= CheckInitializerListTypes(SubInitList, elementType, |
| 489 | true, newIndex); |
| 490 | ++startIndex; |
| 491 | } else { |
| 492 | hadError |= CheckInitializerListTypes(IList, elementType, |
| 493 | false, startIndex); |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | } else if (DeclType->isAggregateType() || DeclType->isUnionType()) { |
| 498 | if (DeclType->isStructureType() || DeclType->isUnionType()) { |
Steve Naroff | 578edc6 | 2008-01-28 02:00:41 +0000 | [diff] [blame] | 499 | if (startIndex < IList->getNumInits() && !topLevel && |
| 500 | Context.typesAreCompatible(IList->getInit(startIndex)->getType(), |
| 501 | DeclType)) { |
Steve Naroff | a996033 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 502 | // We found a compatible struct; per the standard, this initializes the |
| 503 | // struct. (The C standard technically says that this only applies for |
| 504 | // initializers for declarations with automatic scope; however, this |
| 505 | // construct is unambiguous anyway because a struct cannot contain |
| 506 | // a type compatible with itself. We'll output an error when we check |
| 507 | // if the initializer is constant.) |
| 508 | // FIXME: Is a call to CheckSingleInitializer required here? |
| 509 | ++startIndex; |
| 510 | } else { |
| 511 | RecordDecl* structDecl = DeclType->getAsRecordType()->getDecl(); |
Steve Naroff | b43eaa5 | 2008-02-11 00:06:17 +0000 | [diff] [blame] | 512 | |
Steve Naroff | 406db93 | 2008-02-11 21:52:37 +0000 | [diff] [blame] | 513 | // If the record is invalid, some of it's members are invalid. To avoid |
| 514 | // confusion, we forgo checking the intializer for the entire record. |
Steve Naroff | b43eaa5 | 2008-02-11 00:06:17 +0000 | [diff] [blame] | 515 | if (structDecl->isInvalidDecl()) |
| 516 | return true; |
| 517 | |
Steve Naroff | a996033 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 518 | // If structDecl is a forward declaration, this loop won't do anything; |
| 519 | // That's okay, because an error should get printed out elsewhere. It |
| 520 | // might be worthwhile to skip over the rest of the initializer, though. |
| 521 | int numMembers = structDecl->getNumMembers() - |
| 522 | structDecl->hasFlexibleArrayMember(); |
| 523 | for (int i = 0; i < numMembers; i++) { |
| 524 | // Don't attempt to go past the end of the init list |
| 525 | if (startIndex >= IList->getNumInits()) |
| 526 | break; |
| 527 | FieldDecl * curField = structDecl->getMember(i); |
| 528 | if (!curField->getIdentifier()) { |
| 529 | // Don't initialize unnamed fields, e.g. "int : 20;" |
| 530 | continue; |
| 531 | } |
| 532 | QualType fieldType = curField->getType(); |
| 533 | Expr* expr = IList->getInit(startIndex); |
| 534 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
| 535 | unsigned newStart = 0; |
| 536 | hadError |= CheckInitializerListTypes(SubInitList, fieldType, |
| 537 | true, newStart); |
| 538 | ++startIndex; |
| 539 | } else { |
| 540 | hadError |= CheckInitializerListTypes(IList, fieldType, |
| 541 | false, startIndex); |
| 542 | } |
| 543 | if (DeclType->isUnionType()) |
| 544 | break; |
| 545 | } |
| 546 | // FIXME: Implement flexible array initialization GCC extension (it's a |
| 547 | // really messy extension to implement, unfortunately...the necessary |
| 548 | // information isn't actually even here!) |
| 549 | } |
| 550 | } else if (DeclType->isArrayType()) { |
| 551 | // Check for the special-case of initializing an array with a string. |
| 552 | if (startIndex < IList->getNumInits()) { |
| 553 | if (StringLiteral *lit = IsStringLiteralInit(IList->getInit(startIndex), |
| 554 | DeclType)) { |
| 555 | CheckStringLiteralInit(lit, DeclType); |
| 556 | ++startIndex; |
| 557 | if (topLevel && startIndex < IList->getNumInits()) { |
| 558 | // We have leftover initializers; warn |
| 559 | Diag(IList->getInit(startIndex)->getLocStart(), |
| 560 | diag::err_excess_initializers_in_char_array_initializer, |
| 561 | IList->getInit(startIndex)->getSourceRange()); |
| 562 | } |
| 563 | return false; |
| 564 | } |
| 565 | } |
| 566 | int maxElements; |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 567 | if (DeclType->isIncompleteArrayType()) { |
Steve Naroff | a996033 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 568 | // FIXME: use a proper constant |
| 569 | maxElements = 0x7FFFFFFF; |
Chris Lattner | 212839c | 2008-02-20 23:17:35 +0000 | [diff] [blame] | 570 | } else if (const VariableArrayType *VAT = |
| 571 | DeclType->getAsVariableArrayType()) { |
Steve Naroff | a996033 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 572 | // Check for VLAs; in standard C it would be possible to check this |
| 573 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 574 | // them in all sorts of strange places). |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 575 | Diag(VAT->getSizeExpr()->getLocStart(), |
| 576 | diag::err_variable_object_no_init, |
| 577 | VAT->getSizeExpr()->getSourceRange()); |
| 578 | hadError = true; |
| 579 | maxElements = 0x7FFFFFFF; |
Steve Naroff | a996033 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 580 | } else { |
| 581 | const ConstantArrayType *CAT = DeclType->getAsConstantArrayType(); |
| 582 | maxElements = static_cast<int>(CAT->getSize().getZExtValue()); |
| 583 | } |
| 584 | QualType elementType = DeclType->getAsArrayType()->getElementType(); |
| 585 | int numElements = 0; |
| 586 | for (int i = 0; i < maxElements; ++i, ++numElements) { |
| 587 | // Don't attempt to go past the end of the init list |
| 588 | if (startIndex >= IList->getNumInits()) |
| 589 | break; |
| 590 | Expr* expr = IList->getInit(startIndex); |
| 591 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
| 592 | unsigned newIndex = 0; |
| 593 | hadError |= CheckInitializerListTypes(SubInitList, elementType, |
| 594 | true, newIndex); |
| 595 | ++startIndex; |
| 596 | } else { |
| 597 | hadError |= CheckInitializerListTypes(IList, elementType, |
| 598 | false, startIndex); |
| 599 | } |
| 600 | } |
Eli Friedman | 9db1397 | 2008-02-15 12:53:51 +0000 | [diff] [blame] | 601 | if (DeclType->isIncompleteArrayType()) { |
Steve Naroff | a996033 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 602 | // If this is an incomplete array type, the actual type needs to |
| 603 | // be calculated here |
| 604 | if (numElements == 0) { |
| 605 | // Sizing an array implicitly to zero is not allowed |
| 606 | // (It could in theory be allowed, but it doesn't really matter.) |
| 607 | Diag(IList->getLocStart(), |
| 608 | diag::err_at_least_one_initializer_needed_to_size_array); |
| 609 | hadError = true; |
| 610 | } else { |
| 611 | llvm::APSInt ConstVal(32); |
| 612 | ConstVal = numElements; |
| 613 | DeclType = Context.getConstantArrayType(elementType, ConstVal, |
| 614 | ArrayType::Normal, 0); |
| 615 | } |
| 616 | } |
| 617 | } else { |
| 618 | assert(0 && "Aggregate that isn't a function or array?!"); |
| 619 | } |
| 620 | } else { |
| 621 | // In C, all types are either scalars or aggregates, but |
| 622 | // additional handling is needed here for C++ (and possibly others?). |
| 623 | assert(0 && "Unsupported initializer type"); |
| 624 | } |
| 625 | |
| 626 | // If this init list is a base list, we set the type; an initializer doesn't |
| 627 | // fundamentally have a type, but this makes the ASTs a bit easier to read |
| 628 | if (topLevel) |
| 629 | IList->setType(DeclType); |
| 630 | |
| 631 | if (topLevel && startIndex < IList->getNumInits()) { |
| 632 | // We have leftover initializers; warn |
| 633 | Diag(IList->getInit(startIndex)->getLocStart(), |
| 634 | diag::warn_excess_initializers, |
| 635 | IList->getInit(startIndex)->getSourceRange()); |
| 636 | } |
| 637 | return hadError; |
| 638 | } |
| 639 | |
| 640 | bool Sema::CheckInitializerTypes(Expr *&Init, QualType &DeclType) { |
Steve Naroff | ca10730 | 2008-01-21 23:53:58 +0000 | [diff] [blame] | 641 | // C99 6.7.8p3: The type of the entity to be initialized shall be an array |
| 642 | // of unknown size ("[]") or an object type that is not a variable array type. |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 643 | if (const VariableArrayType *VAT = DeclType->getAsVariableArrayType()) |
Steve Naroff | ca10730 | 2008-01-21 23:53:58 +0000 | [diff] [blame] | 644 | return Diag(VAT->getSizeExpr()->getLocStart(), |
| 645 | diag::err_variable_object_no_init, |
| 646 | VAT->getSizeExpr()->getSourceRange()); |
| 647 | |
Steve Naroff | 2fdc374 | 2007-12-10 22:44:33 +0000 | [diff] [blame] | 648 | InitListExpr *InitList = dyn_cast<InitListExpr>(Init); |
| 649 | if (!InitList) { |
Steve Naroff | a49e1fa | 2008-01-22 00:55:40 +0000 | [diff] [blame] | 650 | // FIXME: Handle wide strings |
| 651 | if (StringLiteral *strLiteral = IsStringLiteralInit(Init, DeclType)) |
| 652 | return CheckStringLiteralInit(strLiteral, DeclType); |
Eli Friedman | a312ce2 | 2008-02-08 00:48:24 +0000 | [diff] [blame] | 653 | |
| 654 | if (DeclType->isArrayType()) |
| 655 | return Diag(Init->getLocStart(), |
| 656 | diag::err_array_init_list_required, |
| 657 | Init->getSourceRange()); |
| 658 | |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 659 | return CheckSingleInitializer(Init, DeclType); |
Steve Naroff | 2fdc374 | 2007-12-10 22:44:33 +0000 | [diff] [blame] | 660 | } |
Steve Naroff | a996033 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 661 | unsigned newIndex = 0; |
| 662 | return CheckInitializerListTypes(InitList, DeclType, true, newIndex); |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 663 | } |
| 664 | |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 665 | Sema::DeclTy * |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 666 | Sema::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *lastDecl) { |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 667 | ScopedDecl *LastDeclarator = dyn_cast_or_null<ScopedDecl>((Decl *)lastDecl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 668 | IdentifierInfo *II = D.getIdentifier(); |
| 669 | |
Chris Lattner | e80a59c | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 670 | // All of these full declarators require an identifier. If it doesn't have |
| 671 | // one, the ParsedFreeStandingDeclSpec action should be used. |
| 672 | if (II == 0) { |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 673 | Diag(D.getDeclSpec().getSourceRange().getBegin(), |
Chris Lattner | 98e0863 | 2007-08-28 06:17:15 +0000 | [diff] [blame] | 674 | diag::err_declarator_need_ident, |
Chris Lattner | e80a59c | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 675 | D.getDeclSpec().getSourceRange(), D.getSourceRange()); |
| 676 | return 0; |
| 677 | } |
| 678 | |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 679 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 680 | // we find one that is. |
| 681 | while ((S->getFlags() & Scope::DeclScope) == 0) |
| 682 | S = S->getParent(); |
| 683 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 684 | // See if this is a redefinition of a variable in the same scope. |
Steve Naroff | c752d04 | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 685 | ScopedDecl *PrevDecl = LookupScopedDecl(II, Decl::IDNS_Ordinary, |
| 686 | D.getIdentifierLoc(), S); |
Steve Naroff | c752d04 | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 687 | ScopedDecl *New; |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 688 | bool InvalidDecl = false; |
| 689 | |
Chris Lattner | 41af093 | 2007-11-14 06:34:38 +0000 | [diff] [blame] | 690 | QualType R = GetTypeForDeclarator(D, S); |
| 691 | assert(!R.isNull() && "GetTypeForDeclarator() returned null type"); |
| 692 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 693 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Chris Lattner | 41af093 | 2007-11-14 06:34:38 +0000 | [diff] [blame] | 694 | TypedefDecl *NewTD = ParseTypedefDecl(S, D, R, LastDeclarator); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 695 | if (!NewTD) return 0; |
| 696 | |
| 697 | // Handle attributes prior to checking for duplicates in MergeVarDecl |
| 698 | HandleDeclAttributes(NewTD, D.getDeclSpec().getAttributes(), |
| 699 | D.getAttributes()); |
Steve Naroff | ffce4d5 | 2008-01-09 23:34:55 +0000 | [diff] [blame] | 700 | // Merge the decl with the existing one if appropriate. If the decl is |
| 701 | // in an outer scope, it isn't the same thing. |
| 702 | if (PrevDecl && S->isDeclScope(PrevDecl)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 703 | NewTD = MergeTypeDefDecl(NewTD, PrevDecl); |
| 704 | if (NewTD == 0) return 0; |
| 705 | } |
| 706 | New = NewTD; |
| 707 | if (S->getParent() == 0) { |
| 708 | // C99 6.7.7p2: If a typedef name specifies a variably modified type |
| 709 | // then it shall have block scope. |
Eli Friedman | 9db1397 | 2008-02-15 12:53:51 +0000 | [diff] [blame] | 710 | if (NewTD->getUnderlyingType()->isVariablyModifiedType()) { |
| 711 | // FIXME: Diagnostic needs to be fixed. |
| 712 | Diag(D.getIdentifierLoc(), diag::err_typecheck_illegal_vla); |
Steve Naroff | d7444aa | 2007-08-31 17:20:07 +0000 | [diff] [blame] | 713 | InvalidDecl = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 714 | } |
| 715 | } |
Chris Lattner | 41af093 | 2007-11-14 06:34:38 +0000 | [diff] [blame] | 716 | } else if (R.getTypePtr()->isFunctionType()) { |
Chris Lattner | 271f1a6 | 2007-09-27 15:15:46 +0000 | [diff] [blame] | 717 | FunctionDecl::StorageClass SC = FunctionDecl::None; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 718 | switch (D.getDeclSpec().getStorageClassSpec()) { |
| 719 | default: assert(0 && "Unknown storage class!"); |
| 720 | case DeclSpec::SCS_auto: |
| 721 | case DeclSpec::SCS_register: |
| 722 | Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_func, |
| 723 | R.getAsString()); |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 724 | InvalidDecl = true; |
| 725 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 726 | case DeclSpec::SCS_unspecified: SC = FunctionDecl::None; break; |
| 727 | case DeclSpec::SCS_extern: SC = FunctionDecl::Extern; break; |
| 728 | case DeclSpec::SCS_static: SC = FunctionDecl::Static; break; |
Steve Naroff | 7dd0bd4 | 2008-01-28 21:57:15 +0000 | [diff] [blame] | 729 | case DeclSpec::SCS_private_extern: SC = FunctionDecl::PrivateExtern;break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | FunctionDecl *NewFD = new FunctionDecl(D.getIdentifierLoc(), II, R, SC, |
Chris Lattner | 70c8b2e | 2007-08-26 04:02:13 +0000 | [diff] [blame] | 733 | D.getDeclSpec().isInlineSpecified(), |
Anders Carlsson | f78915f | 2008-02-15 07:04:12 +0000 | [diff] [blame] | 734 | LastDeclarator); |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 735 | // FIXME: Handle attributes: should delete anything left. |
| 736 | D.getDeclSpec().SetAttributes(0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 737 | |
Steve Naroff | ffce4d5 | 2008-01-09 23:34:55 +0000 | [diff] [blame] | 738 | // Merge the decl with the existing one if appropriate. Since C functions |
| 739 | // are in a flat namespace, make sure we consider decls in outer scopes. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 740 | if (PrevDecl) { |
| 741 | NewFD = MergeFunctionDecl(NewFD, PrevDecl); |
| 742 | if (NewFD == 0) return 0; |
| 743 | } |
| 744 | New = NewFD; |
| 745 | } else { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 746 | if (R.getTypePtr()->isObjCInterfaceType()) { |
Fariborz Jahanian | e7f64cc | 2007-10-12 22:10:42 +0000 | [diff] [blame] | 747 | Diag(D.getIdentifierLoc(), diag::err_statically_allocated_object, |
| 748 | D.getIdentifier()->getName()); |
| 749 | InvalidDecl = true; |
| 750 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 751 | |
| 752 | VarDecl *NewVD; |
| 753 | VarDecl::StorageClass SC; |
| 754 | switch (D.getDeclSpec().getStorageClassSpec()) { |
| 755 | default: assert(0 && "Unknown storage class!"); |
Steve Naroff | d6326c6 | 2008-01-25 22:14:40 +0000 | [diff] [blame] | 756 | case DeclSpec::SCS_unspecified: SC = VarDecl::None; break; |
| 757 | case DeclSpec::SCS_extern: SC = VarDecl::Extern; break; |
| 758 | case DeclSpec::SCS_static: SC = VarDecl::Static; break; |
| 759 | case DeclSpec::SCS_auto: SC = VarDecl::Auto; break; |
| 760 | case DeclSpec::SCS_register: SC = VarDecl::Register; break; |
| 761 | case DeclSpec::SCS_private_extern: SC = VarDecl::PrivateExtern; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 762 | } |
| 763 | if (S->getParent() == 0) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 764 | // C99 6.9p2: The storage-class specifiers auto and register shall not |
| 765 | // appear in the declaration specifiers in an external declaration. |
| 766 | if (SC == VarDecl::Auto || SC == VarDecl::Register) { |
| 767 | Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_fscope, |
| 768 | R.getAsString()); |
Steve Naroff | 53a3234 | 2007-08-28 18:45:29 +0000 | [diff] [blame] | 769 | InvalidDecl = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 770 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 771 | NewVD = new FileVarDecl(D.getIdentifierLoc(), II, R, SC, LastDeclarator); |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 772 | } else { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 773 | NewVD = new BlockVarDecl(D.getIdentifierLoc(), II, R, SC, LastDeclarator); |
Steve Naroff | 53a3234 | 2007-08-28 18:45:29 +0000 | [diff] [blame] | 774 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 775 | // Handle attributes prior to checking for duplicates in MergeVarDecl |
| 776 | HandleDeclAttributes(NewVD, D.getDeclSpec().getAttributes(), |
| 777 | D.getAttributes()); |
| 778 | |
Steve Naroff | ffce4d5 | 2008-01-09 23:34:55 +0000 | [diff] [blame] | 779 | // Merge the decl with the existing one if appropriate. If the decl is |
| 780 | // in an outer scope, it isn't the same thing. |
| 781 | if (PrevDecl && S->isDeclScope(PrevDecl)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 782 | NewVD = MergeVarDecl(NewVD, PrevDecl); |
| 783 | if (NewVD == 0) return 0; |
| 784 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 785 | New = NewVD; |
| 786 | } |
| 787 | |
| 788 | // If this has an identifier, add it to the scope stack. |
| 789 | if (II) { |
Steve Naroff | c752d04 | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 790 | New->setNext(II->getFETokenInfo<ScopedDecl>()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 791 | II->setFETokenInfo(New); |
| 792 | S->AddDecl(New); |
| 793 | } |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 794 | // If any semantic error occurred, mark the decl as invalid. |
| 795 | if (D.getInvalidType() || InvalidDecl) |
| 796 | New->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 797 | |
| 798 | return New; |
| 799 | } |
| 800 | |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 801 | bool Sema::CheckForConstantInitializer(Expr *Init, QualType DclT) { |
| 802 | SourceLocation loc; |
| 803 | // FIXME: Remove the isReference check and handle assignment to a reference. |
| 804 | if (!DclT->isReferenceType() && !Init->isConstantExpr(Context, &loc)) { |
| 805 | assert(loc.isValid() && "isConstantExpr didn't return a loc!"); |
| 806 | Diag(loc, diag::err_init_element_not_constant, Init->getSourceRange()); |
| 807 | return true; |
| 808 | } |
| 809 | return false; |
| 810 | } |
| 811 | |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 812 | void Sema::AddInitializerToDecl(DeclTy *dcl, ExprTy *init) { |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 813 | Decl *RealDecl = static_cast<Decl *>(dcl); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 814 | Expr *Init = static_cast<Expr *>(init); |
Chris Lattner | 9a11b9a | 2007-10-19 20:10:30 +0000 | [diff] [blame] | 815 | assert(Init && "missing initializer"); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 816 | |
Chris Lattner | 9a11b9a | 2007-10-19 20:10:30 +0000 | [diff] [blame] | 817 | // If there is no declaration, there was an error parsing it. Just ignore |
| 818 | // the initializer. |
| 819 | if (RealDecl == 0) { |
| 820 | delete Init; |
| 821 | return; |
| 822 | } |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 823 | |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 824 | VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl); |
| 825 | if (!VDecl) { |
Steve Naroff | 8e74c93 | 2007-09-13 21:41:19 +0000 | [diff] [blame] | 826 | Diag(dyn_cast<ScopedDecl>(RealDecl)->getLocation(), |
| 827 | diag::err_illegal_initializer); |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 828 | RealDecl->setInvalidDecl(); |
| 829 | return; |
| 830 | } |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 831 | // Get the decls type and save a reference for later, since |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 832 | // CheckInitializerTypes may change it. |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 833 | QualType DclT = VDecl->getType(), SavT = DclT; |
| 834 | if (BlockVarDecl *BVD = dyn_cast<BlockVarDecl>(VDecl)) { |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 835 | VarDecl::StorageClass SC = BVD->getStorageClass(); |
| 836 | if (SC == VarDecl::Extern) { // C99 6.7.8p5 |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 837 | Diag(VDecl->getLocation(), diag::err_block_extern_cant_init); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 838 | BVD->setInvalidDecl(); |
| 839 | } else if (!BVD->isInvalidDecl()) { |
Steve Naroff | a996033 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 840 | if (CheckInitializerTypes(Init, DclT)) |
| 841 | BVD->setInvalidDecl(); |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 842 | if (SC == VarDecl::Static) // C99 6.7.8p4. |
| 843 | CheckForConstantInitializer(Init, DclT); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 844 | } |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 845 | } else if (FileVarDecl *FVD = dyn_cast<FileVarDecl>(VDecl)) { |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 846 | if (FVD->getStorageClass() == VarDecl::Extern) |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 847 | Diag(VDecl->getLocation(), diag::warn_extern_init); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 848 | if (!FVD->isInvalidDecl()) |
Steve Naroff | a996033 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 849 | if (CheckInitializerTypes(Init, DclT)) |
| 850 | FVD->setInvalidDecl(); |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 851 | |
| 852 | // C99 6.7.8p4. All file scoped initializers need to be constant. |
| 853 | CheckForConstantInitializer(Init, DclT); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 854 | } |
| 855 | // If the type changed, it means we had an incomplete type that was |
| 856 | // completed by the initializer. For example: |
| 857 | // int ary[] = { 1, 3, 5 }; |
| 858 | // "ary" transitions from a VariableArrayType to a ConstantArrayType. |
Christopher Lamb | 48b1239 | 2007-11-29 19:09:19 +0000 | [diff] [blame] | 859 | if (!VDecl->isInvalidDecl() && (DclT != SavT)) { |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 860 | VDecl->setType(DclT); |
Christopher Lamb | 48b1239 | 2007-11-29 19:09:19 +0000 | [diff] [blame] | 861 | Init->setType(DclT); |
| 862 | } |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 863 | |
| 864 | // Attach the initializer to the decl. |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 865 | VDecl->setInit(Init); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 866 | return; |
| 867 | } |
| 868 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 869 | /// The declarators are chained together backwards, reverse the list. |
| 870 | Sema::DeclTy *Sema::FinalizeDeclaratorGroup(Scope *S, DeclTy *group) { |
| 871 | // Often we have single declarators, handle them quickly. |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 872 | Decl *GroupDecl = static_cast<Decl*>(group); |
| 873 | if (GroupDecl == 0) |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 874 | return 0; |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 875 | |
| 876 | ScopedDecl *Group = dyn_cast<ScopedDecl>(GroupDecl); |
| 877 | ScopedDecl *NewGroup = 0; |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 878 | if (Group->getNextDeclarator() == 0) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 879 | NewGroup = Group; |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 880 | else { // reverse the list. |
| 881 | while (Group) { |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 882 | ScopedDecl *Next = Group->getNextDeclarator(); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 883 | Group->setNextDeclarator(NewGroup); |
| 884 | NewGroup = Group; |
| 885 | Group = Next; |
| 886 | } |
| 887 | } |
| 888 | // Perform semantic analysis that depends on having fully processed both |
| 889 | // the declarator and initializer. |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 890 | for (ScopedDecl *ID = NewGroup; ID; ID = ID->getNextDeclarator()) { |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 891 | VarDecl *IDecl = dyn_cast<VarDecl>(ID); |
| 892 | if (!IDecl) |
| 893 | continue; |
| 894 | FileVarDecl *FVD = dyn_cast<FileVarDecl>(IDecl); |
| 895 | BlockVarDecl *BVD = dyn_cast<BlockVarDecl>(IDecl); |
| 896 | QualType T = IDecl->getType(); |
| 897 | |
| 898 | // C99 6.7.5.2p2: If an identifier is declared to be an object with |
| 899 | // static storage duration, it shall not have a variable length array. |
| 900 | if ((FVD || BVD) && IDecl->getStorageClass() == VarDecl::Static) { |
Eli Friedman | 3fe0293 | 2008-02-15 19:53:52 +0000 | [diff] [blame] | 901 | if (T->getAsVariableArrayType()) { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 902 | Diag(IDecl->getLocation(), diag::err_typecheck_illegal_vla); |
| 903 | IDecl->setInvalidDecl(); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 904 | } |
| 905 | } |
| 906 | // Block scope. C99 6.7p7: If an identifier for an object is declared with |
| 907 | // no linkage (C99 6.2.2p6), the type for the object shall be complete... |
| 908 | if (BVD && IDecl->getStorageClass() != VarDecl::Extern) { |
| 909 | if (T->isIncompleteType()) { |
Chris Lattner | 8b1be77 | 2007-12-02 07:50:03 +0000 | [diff] [blame] | 910 | Diag(IDecl->getLocation(), diag::err_typecheck_decl_incomplete_type, |
| 911 | T.getAsString()); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 912 | IDecl->setInvalidDecl(); |
| 913 | } |
| 914 | } |
| 915 | // File scope. C99 6.9.2p2: A declaration of an identifier for and |
| 916 | // object that has file scope without an initializer, and without a |
| 917 | // storage-class specifier or with the storage-class specifier "static", |
| 918 | // constitutes a tentative definition. Note: A tentative definition with |
| 919 | // external linkage is valid (C99 6.2.2p5). |
Steve Naroff | d3cd1e5 | 2008-01-18 00:39:39 +0000 | [diff] [blame] | 920 | if (FVD && !FVD->getInit() && (FVD->getStorageClass() == VarDecl::Static || |
| 921 | FVD->getStorageClass() == VarDecl::None)) { |
Eli Friedman | 9db1397 | 2008-02-15 12:53:51 +0000 | [diff] [blame] | 922 | if (T->isIncompleteArrayType()) { |
Steve Naroff | 9a75f8a | 2008-01-18 20:40:52 +0000 | [diff] [blame] | 923 | // C99 6.9.2 (p2, p5): Implicit initialization causes an incomplete |
| 924 | // array to be completed. Don't issue a diagnostic. |
| 925 | } else if (T->isIncompleteType()) { |
| 926 | // C99 6.9.2p3: If the declaration of an identifier for an object is |
| 927 | // a tentative definition and has internal linkage (C99 6.2.2p3), the |
| 928 | // declared type shall not be an incomplete type. |
Chris Lattner | 8b1be77 | 2007-12-02 07:50:03 +0000 | [diff] [blame] | 929 | Diag(IDecl->getLocation(), diag::err_typecheck_decl_incomplete_type, |
| 930 | T.getAsString()); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 931 | IDecl->setInvalidDecl(); |
| 932 | } |
| 933 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 934 | } |
| 935 | return NewGroup; |
| 936 | } |
Steve Naroff | e1223f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 937 | |
| 938 | // Called from Sema::ParseStartOfFunctionDef(). |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 939 | ParmVarDecl * |
Nate Begeman | 6d20d03 | 2008-02-17 21:02:04 +0000 | [diff] [blame] | 940 | Sema::ActOnParamDeclarator(struct DeclaratorChunk::ParamInfo &PI, |
| 941 | Scope *FnScope) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 942 | IdentifierInfo *II = PI.Ident; |
| 943 | // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope. |
| 944 | // Can this happen for params? We already checked that they don't conflict |
| 945 | // among each other. Here they can only shadow globals, which is ok. |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 946 | if (/*Decl *PrevDecl = */LookupScopedDecl(II, Decl::IDNS_Ordinary, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 947 | PI.IdentLoc, FnScope)) { |
| 948 | |
| 949 | } |
| 950 | |
| 951 | // FIXME: Handle storage class (auto, register). No declarator? |
| 952 | // TODO: Chain to previous parameter with the prevdeclarator chain? |
Steve Naroff | 6a9f3e3 | 2007-08-07 22:44:21 +0000 | [diff] [blame] | 953 | |
| 954 | // Perform the default function/array conversion (C99 6.7.5.3p[7,8]). |
| 955 | // Doing the promotion here has a win and a loss. The win is the type for |
| 956 | // both Decl's and DeclRefExpr's will match (a convenient invariant for the |
| 957 | // code generator). The loss is the orginal type isn't preserved. For example: |
| 958 | // |
| 959 | // void func(int parmvardecl[5]) { // convert "int [5]" to "int *" |
| 960 | // int blockvardecl[5]; |
| 961 | // sizeof(parmvardecl); // size == 4 |
| 962 | // sizeof(blockvardecl); // size == 20 |
| 963 | // } |
| 964 | // |
| 965 | // For expressions, all implicit conversions are captured using the |
| 966 | // ImplicitCastExpr AST node (we have no such mechanism for Decl's). |
| 967 | // |
| 968 | // FIXME: If a source translation tool needs to see the original type, then |
| 969 | // we need to consider storing both types (in ParmVarDecl)... |
| 970 | // |
| 971 | QualType parmDeclType = QualType::getFromOpaquePtr(PI.TypeInfo); |
Chris Lattner | 529bd02 | 2008-01-02 22:50:48 +0000 | [diff] [blame] | 972 | if (const ArrayType *AT = parmDeclType->getAsArrayType()) { |
| 973 | // int x[restrict 4] -> int *restrict |
Steve Naroff | 6a9f3e3 | 2007-08-07 22:44:21 +0000 | [diff] [blame] | 974 | parmDeclType = Context.getPointerType(AT->getElementType()); |
Chris Lattner | 529bd02 | 2008-01-02 22:50:48 +0000 | [diff] [blame] | 975 | parmDeclType = parmDeclType.getQualifiedType(AT->getIndexTypeQualifier()); |
| 976 | } else if (parmDeclType->isFunctionType()) |
Steve Naroff | 6a9f3e3 | 2007-08-07 22:44:21 +0000 | [diff] [blame] | 977 | parmDeclType = Context.getPointerType(parmDeclType); |
| 978 | |
| 979 | ParmVarDecl *New = new ParmVarDecl(PI.IdentLoc, II, parmDeclType, |
Anders Carlsson | f78915f | 2008-02-15 07:04:12 +0000 | [diff] [blame] | 980 | VarDecl::None, 0); |
Anders Carlsson | f78915f | 2008-02-15 07:04:12 +0000 | [diff] [blame] | 981 | |
Steve Naroff | 53a3234 | 2007-08-28 18:45:29 +0000 | [diff] [blame] | 982 | if (PI.InvalidType) |
| 983 | New->setInvalidDecl(); |
| 984 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 985 | // If this has an identifier, add it to the scope stack. |
| 986 | if (II) { |
Steve Naroff | c752d04 | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 987 | New->setNext(II->getFETokenInfo<ScopedDecl>()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 988 | II->setFETokenInfo(New); |
| 989 | FnScope->AddDecl(New); |
| 990 | } |
Nate Begeman | b7894b5 | 2008-02-17 21:20:31 +0000 | [diff] [blame] | 991 | |
| 992 | HandleDeclAttributes(New, PI.AttrList, 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 993 | return New; |
| 994 | } |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 995 | |
Chris Lattner | b652cea | 2007-10-09 17:14:05 +0000 | [diff] [blame] | 996 | Sema::DeclTy *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Declarator &D) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 997 | assert(CurFunctionDecl == 0 && "Function parsing confused"); |
| 998 | assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function && |
| 999 | "Not a function declarator!"); |
| 1000 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 1001 | |
| 1002 | // Verify 6.9.1p6: 'every identifier in the identifier list shall be declared' |
| 1003 | // for a K&R function. |
| 1004 | if (!FTI.hasPrototype) { |
| 1005 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) { |
| 1006 | if (FTI.ArgInfo[i].TypeInfo == 0) { |
| 1007 | Diag(FTI.ArgInfo[i].IdentLoc, diag::ext_param_not_declared, |
| 1008 | FTI.ArgInfo[i].Ident->getName()); |
| 1009 | // Implicitly declare the argument as type 'int' for lack of a better |
| 1010 | // type. |
| 1011 | FTI.ArgInfo[i].TypeInfo = Context.IntTy.getAsOpaquePtr(); |
| 1012 | } |
| 1013 | } |
Chris Lattner | 5280408 | 2008-02-17 19:31:09 +0000 | [diff] [blame] | 1014 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1015 | // Since this is a function definition, act as though we have information |
| 1016 | // about the arguments. |
Chris Lattner | 5280408 | 2008-02-17 19:31:09 +0000 | [diff] [blame] | 1017 | if (FTI.NumArgs) |
| 1018 | FTI.hasPrototype = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1019 | } else { |
| 1020 | // FIXME: Diagnose arguments without names in C. |
| 1021 | |
| 1022 | } |
| 1023 | |
| 1024 | Scope *GlobalScope = FnBodyScope->getParent(); |
Steve Naroff | adbbd0c | 2008-01-14 20:51:29 +0000 | [diff] [blame] | 1025 | |
| 1026 | // See if this is a redefinition. |
| 1027 | ScopedDecl *PrevDcl = LookupScopedDecl(D.getIdentifier(), Decl::IDNS_Ordinary, |
| 1028 | D.getIdentifierLoc(), GlobalScope); |
| 1029 | if (FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(PrevDcl)) { |
| 1030 | if (FD->getBody()) { |
| 1031 | Diag(D.getIdentifierLoc(), diag::err_redefinition, |
| 1032 | D.getIdentifier()->getName()); |
| 1033 | Diag(FD->getLocation(), diag::err_previous_definition); |
| 1034 | } |
| 1035 | } |
Steve Naroff | fabbc34 | 2008-02-12 01:09:36 +0000 | [diff] [blame] | 1036 | Decl *decl = static_cast<Decl*>(ActOnDeclarator(GlobalScope, D, 0)); |
Chris Lattner | e9ba323 | 2008-02-16 01:20:36 +0000 | [diff] [blame] | 1037 | FunctionDecl *FD = cast<FunctionDecl>(decl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1038 | CurFunctionDecl = FD; |
| 1039 | |
| 1040 | // Create Decl objects for each parameter, adding them to the FunctionDecl. |
| 1041 | llvm::SmallVector<ParmVarDecl*, 16> Params; |
| 1042 | |
| 1043 | // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs function that takes |
| 1044 | // no arguments, not a function that takes a single void argument. |
| 1045 | if (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 1046 | !QualType::getFromOpaquePtr(FTI.ArgInfo[0].TypeInfo).getCVRQualifiers() && |
Chris Lattner | b751c28 | 2007-11-28 18:51:29 +0000 | [diff] [blame] | 1047 | QualType::getFromOpaquePtr(FTI.ArgInfo[0].TypeInfo)->isVoidType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1048 | // empty arg list, don't push any params. |
| 1049 | } else { |
Steve Naroff | 6649992 | 2007-11-12 03:44:46 +0000 | [diff] [blame] | 1050 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) { |
Nate Begeman | bff5f5c | 2007-11-13 21:49:48 +0000 | [diff] [blame] | 1051 | Params.push_back(ActOnParamDeclarator(D.getTypeObject(0).Fun.ArgInfo[i], |
Steve Naroff | 6649992 | 2007-11-12 03:44:46 +0000 | [diff] [blame] | 1052 | FnBodyScope)); |
| 1053 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | FD->setParams(&Params[0], Params.size()); |
| 1057 | |
| 1058 | return FD; |
| 1059 | } |
| 1060 | |
Steve Naroff | d6d054d | 2007-11-11 23:20:51 +0000 | [diff] [blame] | 1061 | Sema::DeclTy *Sema::ActOnFinishFunctionBody(DeclTy *D, StmtTy *Body) { |
| 1062 | Decl *dcl = static_cast<Decl *>(D); |
| 1063 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(dcl)) { |
| 1064 | FD->setBody((Stmt*)Body); |
| 1065 | assert(FD == CurFunctionDecl && "Function parsing confused"); |
Steve Naroff | 4d83220 | 2007-12-13 18:18:56 +0000 | [diff] [blame] | 1066 | CurFunctionDecl = 0; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1067 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(dcl)) { |
Steve Naroff | d6d054d | 2007-11-11 23:20:51 +0000 | [diff] [blame] | 1068 | MD->setBody((Stmt*)Body); |
Steve Naroff | 0330071 | 2007-11-12 13:56:41 +0000 | [diff] [blame] | 1069 | CurMethodDecl = 0; |
Steve Naroff | 4d83220 | 2007-12-13 18:18:56 +0000 | [diff] [blame] | 1070 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1071 | // Verify and clean out per-function state. |
| 1072 | |
| 1073 | // Check goto/label use. |
| 1074 | for (llvm::DenseMap<IdentifierInfo*, LabelStmt*>::iterator |
| 1075 | I = LabelMap.begin(), E = LabelMap.end(); I != E; ++I) { |
| 1076 | // Verify that we have no forward references left. If so, there was a goto |
| 1077 | // or address of a label taken, but no definition of it. Label fwd |
| 1078 | // definitions are indicated with a null substmt. |
| 1079 | if (I->second->getSubStmt() == 0) { |
| 1080 | LabelStmt *L = I->second; |
| 1081 | // Emit error. |
| 1082 | Diag(L->getIdentLoc(), diag::err_undeclared_label_use, L->getName()); |
| 1083 | |
| 1084 | // At this point, we have gotos that use the bogus label. Stitch it into |
| 1085 | // the function body so that they aren't leaked and that the AST is well |
| 1086 | // formed. |
Chris Lattner | 0cbc215 | 2008-01-25 00:01:10 +0000 | [diff] [blame] | 1087 | if (Body) { |
| 1088 | L->setSubStmt(new NullStmt(L->getIdentLoc())); |
| 1089 | cast<CompoundStmt>((Stmt*)Body)->push_back(L); |
| 1090 | } else { |
| 1091 | // The whole function wasn't parsed correctly, just delete this. |
| 1092 | delete L; |
| 1093 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1094 | } |
| 1095 | } |
| 1096 | LabelMap.clear(); |
| 1097 | |
Steve Naroff | d6d054d | 2007-11-11 23:20:51 +0000 | [diff] [blame] | 1098 | return D; |
Fariborz Jahanian | 60fbca0 | 2007-11-10 16:31:34 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1101 | /// ImplicitlyDefineFunction - An undeclared identifier was used in a function |
| 1102 | /// call, forming a call to an implicitly defined function (per C99 6.5.1p2). |
Steve Naroff | 8c9f13e | 2007-09-16 16:16:00 +0000 | [diff] [blame] | 1103 | ScopedDecl *Sema::ImplicitlyDefineFunction(SourceLocation Loc, |
| 1104 | IdentifierInfo &II, Scope *S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1105 | if (getLangOptions().C99) // Extension in C99. |
| 1106 | Diag(Loc, diag::ext_implicit_function_decl, II.getName()); |
| 1107 | else // Legal in C90, but warn about it. |
| 1108 | Diag(Loc, diag::warn_implicit_function_decl, II.getName()); |
| 1109 | |
| 1110 | // FIXME: handle stuff like: |
| 1111 | // void foo() { extern float X(); } |
| 1112 | // void bar() { X(); } <-- implicit decl for X in another scope. |
| 1113 | |
| 1114 | // Set a Declarator for the implicit definition: int foo(); |
| 1115 | const char *Dummy; |
| 1116 | DeclSpec DS; |
| 1117 | bool Error = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, Dummy); |
| 1118 | Error = Error; // Silence warning. |
| 1119 | assert(!Error && "Error setting up implicit decl!"); |
| 1120 | Declarator D(DS, Declarator::BlockContext); |
| 1121 | D.AddTypeInfo(DeclaratorChunk::getFunction(false, false, 0, 0, Loc)); |
| 1122 | D.SetIdentifier(&II, Loc); |
| 1123 | |
| 1124 | // Find translation-unit scope to insert this function into. |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 1125 | if (Scope *FnS = S->getFnParent()) |
| 1126 | S = FnS->getParent(); // Skip all scopes in a function at once. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1127 | while (S->getParent()) |
| 1128 | S = S->getParent(); |
| 1129 | |
Steve Naroff | 8c9f13e | 2007-09-16 16:16:00 +0000 | [diff] [blame] | 1130 | return dyn_cast<ScopedDecl>(static_cast<Decl*>(ActOnDeclarator(S, D, 0))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | |
Chris Lattner | 41af093 | 2007-11-14 06:34:38 +0000 | [diff] [blame] | 1134 | TypedefDecl *Sema::ParseTypedefDecl(Scope *S, Declarator &D, QualType T, |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 1135 | ScopedDecl *LastDeclarator) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1136 | assert(D.getIdentifier() && "Wrong callback for declspec without declarator"); |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 1137 | assert(!T.isNull() && "GetTypeForDeclarator() returned null type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1138 | |
| 1139 | // Scope manipulation handled by caller. |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 1140 | TypedefDecl *NewTD = new TypedefDecl(D.getIdentifierLoc(), D.getIdentifier(), |
| 1141 | T, LastDeclarator); |
| 1142 | if (D.getInvalidType()) |
| 1143 | NewTD->setInvalidDecl(); |
| 1144 | return NewTD; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1145 | } |
| 1146 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1147 | /// ActOnTag - This is invoked when we see 'struct foo' or 'struct {'. In the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1148 | /// former case, Name will be non-null. In the later case, Name will be null. |
| 1149 | /// TagType indicates what kind of tag this is. TK indicates whether this is a |
| 1150 | /// reference/declaration/definition of a tag. |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1151 | Sema::DeclTy *Sema::ActOnTag(Scope *S, unsigned TagType, TagKind TK, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1152 | SourceLocation KWLoc, IdentifierInfo *Name, |
| 1153 | SourceLocation NameLoc, AttributeList *Attr) { |
| 1154 | // If this is a use of an existing tag, it must have a name. |
| 1155 | assert((Name != 0 || TK == TK_Definition) && |
| 1156 | "Nameless record must be a definition!"); |
| 1157 | |
| 1158 | Decl::Kind Kind; |
| 1159 | switch (TagType) { |
| 1160 | default: assert(0 && "Unknown tag type!"); |
| 1161 | case DeclSpec::TST_struct: Kind = Decl::Struct; break; |
| 1162 | case DeclSpec::TST_union: Kind = Decl::Union; break; |
| 1163 | //case DeclSpec::TST_class: Kind = Decl::Class; break; |
| 1164 | case DeclSpec::TST_enum: Kind = Decl::Enum; break; |
| 1165 | } |
| 1166 | |
| 1167 | // If this is a named struct, check to see if there was a previous forward |
| 1168 | // declaration or definition. |
| 1169 | if (TagDecl *PrevDecl = |
| 1170 | dyn_cast_or_null<TagDecl>(LookupScopedDecl(Name, Decl::IDNS_Tag, |
| 1171 | NameLoc, S))) { |
| 1172 | |
| 1173 | // If this is a use of a previous tag, or if the tag is already declared in |
| 1174 | // the same scope (so that the definition/declaration completes or |
| 1175 | // rementions the tag), reuse the decl. |
| 1176 | if (TK == TK_Reference || S->isDeclScope(PrevDecl)) { |
| 1177 | // Make sure that this wasn't declared as an enum and now used as a struct |
| 1178 | // or something similar. |
| 1179 | if (PrevDecl->getKind() != Kind) { |
| 1180 | Diag(KWLoc, diag::err_use_with_wrong_tag, Name->getName()); |
| 1181 | Diag(PrevDecl->getLocation(), diag::err_previous_use); |
| 1182 | } |
| 1183 | |
| 1184 | // If this is a use or a forward declaration, we're good. |
| 1185 | if (TK != TK_Definition) |
| 1186 | return PrevDecl; |
| 1187 | |
| 1188 | // Diagnose attempts to redefine a tag. |
| 1189 | if (PrevDecl->isDefinition()) { |
| 1190 | Diag(NameLoc, diag::err_redefinition, Name->getName()); |
| 1191 | Diag(PrevDecl->getLocation(), diag::err_previous_definition); |
| 1192 | // If this is a redefinition, recover by making this struct be |
| 1193 | // anonymous, which will make any later references get the previous |
| 1194 | // definition. |
| 1195 | Name = 0; |
| 1196 | } else { |
| 1197 | // Okay, this is definition of a previously declared or referenced tag. |
| 1198 | // Move the location of the decl to be the definition site. |
| 1199 | PrevDecl->setLocation(NameLoc); |
| 1200 | return PrevDecl; |
| 1201 | } |
| 1202 | } |
| 1203 | // If we get here, this is a definition of a new struct type in a nested |
| 1204 | // scope, e.g. "struct foo; void bar() { struct foo; }", just create a new |
| 1205 | // type. |
| 1206 | } |
| 1207 | |
| 1208 | // If there is an identifier, use the location of the identifier as the |
| 1209 | // location of the decl, otherwise use the location of the struct/union |
| 1210 | // keyword. |
| 1211 | SourceLocation Loc = NameLoc.isValid() ? NameLoc : KWLoc; |
| 1212 | |
| 1213 | // Otherwise, if this is the first time we've seen this tag, create the decl. |
| 1214 | TagDecl *New; |
| 1215 | switch (Kind) { |
| 1216 | default: assert(0 && "Unknown tag kind!"); |
| 1217 | case Decl::Enum: |
| 1218 | // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.: |
| 1219 | // enum X { A, B, C } D; D should chain to X. |
| 1220 | New = new EnumDecl(Loc, Name, 0); |
| 1221 | // If this is an undefined enum, warn. |
| 1222 | if (TK != TK_Definition) Diag(Loc, diag::ext_forward_ref_enum); |
| 1223 | break; |
| 1224 | case Decl::Union: |
| 1225 | case Decl::Struct: |
| 1226 | case Decl::Class: |
| 1227 | // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.: |
| 1228 | // struct X { int A; } D; D should chain to X. |
| 1229 | New = new RecordDecl(Kind, Loc, Name, 0); |
| 1230 | break; |
| 1231 | } |
| 1232 | |
| 1233 | // If this has an identifier, add it to the scope stack. |
| 1234 | if (Name) { |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 1235 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 1236 | // we find one that is. |
| 1237 | while ((S->getFlags() & Scope::DeclScope) == 0) |
| 1238 | S = S->getParent(); |
| 1239 | |
| 1240 | // Add it to the decl chain. |
Steve Naroff | c752d04 | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 1241 | New->setNext(Name->getFETokenInfo<ScopedDecl>()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1242 | Name->setFETokenInfo(New); |
| 1243 | S->AddDecl(New); |
| 1244 | } |
Chris Lattner | e1e7985 | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 1245 | |
Anders Carlsson | ad14806 | 2008-02-16 00:29:18 +0000 | [diff] [blame] | 1246 | HandleDeclAttributes(New, Attr, 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1247 | return New; |
| 1248 | } |
| 1249 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1250 | /// ActOnField - Each field of a struct/union/class is passed into this in order |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1251 | /// to create a FieldDecl object for it. |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1252 | Sema::DeclTy *Sema::ActOnField(Scope *S, DeclTy *TagDecl, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1253 | SourceLocation DeclStart, |
| 1254 | Declarator &D, ExprTy *BitfieldWidth) { |
| 1255 | IdentifierInfo *II = D.getIdentifier(); |
| 1256 | Expr *BitWidth = (Expr*)BitfieldWidth; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1257 | SourceLocation Loc = DeclStart; |
| 1258 | if (II) Loc = D.getIdentifierLoc(); |
| 1259 | |
| 1260 | // FIXME: Unnamed fields can be handled in various different ways, for |
| 1261 | // example, unnamed unions inject all members into the struct namespace! |
| 1262 | |
| 1263 | |
| 1264 | if (BitWidth) { |
| 1265 | // TODO: Validate. |
| 1266 | //printf("WARNING: BITFIELDS IGNORED!\n"); |
| 1267 | |
| 1268 | // 6.7.2.1p3 |
| 1269 | // 6.7.2.1p4 |
| 1270 | |
| 1271 | } else { |
| 1272 | // Not a bitfield. |
| 1273 | |
| 1274 | // validate II. |
| 1275 | |
| 1276 | } |
| 1277 | |
| 1278 | QualType T = GetTypeForDeclarator(D, S); |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 1279 | assert(!T.isNull() && "GetTypeForDeclarator() returned null type"); |
| 1280 | bool InvalidDecl = false; |
Steve Naroff | d7444aa | 2007-08-31 17:20:07 +0000 | [diff] [blame] | 1281 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1282 | // C99 6.7.2.1p8: A member of a structure or union may have any type other |
| 1283 | // than a variably modified type. |
Eli Friedman | 9db1397 | 2008-02-15 12:53:51 +0000 | [diff] [blame] | 1284 | if (T->isVariablyModifiedType()) { |
| 1285 | // FIXME: This diagnostic needs work |
| 1286 | Diag(Loc, diag::err_typecheck_illegal_vla, Loc); |
Steve Naroff | d7444aa | 2007-08-31 17:20:07 +0000 | [diff] [blame] | 1287 | InvalidDecl = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1288 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1289 | // FIXME: Chain fielddecls together. |
Steve Naroff | 4473921 | 2007-09-11 21:17:26 +0000 | [diff] [blame] | 1290 | FieldDecl *NewFD; |
| 1291 | |
| 1292 | if (isa<RecordDecl>(static_cast<Decl *>(TagDecl))) |
Devang Patel | 54e6235 | 2007-11-01 16:29:56 +0000 | [diff] [blame] | 1293 | NewFD = new FieldDecl(Loc, II, T, BitWidth); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1294 | else if (isa<ObjCInterfaceDecl>(static_cast<Decl *>(TagDecl)) || |
| 1295 | isa<ObjCImplementationDecl>(static_cast<Decl *>(TagDecl)) || |
| 1296 | isa<ObjCCategoryDecl>(static_cast<Decl *>(TagDecl)) || |
Steve Naroff | ddd600f | 2007-11-14 14:15:31 +0000 | [diff] [blame] | 1297 | // FIXME: ivars are currently used to model properties, and |
| 1298 | // properties can appear within a protocol. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1299 | // See corresponding FIXME in DeclObjC.h:ObjCPropertyDecl. |
| 1300 | isa<ObjCProtocolDecl>(static_cast<Decl *>(TagDecl))) |
| 1301 | NewFD = new ObjCIvarDecl(Loc, II, T); |
Steve Naroff | 4473921 | 2007-09-11 21:17:26 +0000 | [diff] [blame] | 1302 | else |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1303 | assert(0 && "Sema::ActOnField(): Unknown TagDecl"); |
Steve Naroff | 4473921 | 2007-09-11 21:17:26 +0000 | [diff] [blame] | 1304 | |
Anders Carlsson | ad14806 | 2008-02-16 00:29:18 +0000 | [diff] [blame] | 1305 | HandleDeclAttributes(NewFD, D.getDeclSpec().getAttributes(), |
| 1306 | D.getAttributes()); |
| 1307 | |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 1308 | if (D.getInvalidType() || InvalidDecl) |
| 1309 | NewFD->setInvalidDecl(); |
| 1310 | return NewFD; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1311 | } |
| 1312 | |
Fariborz Jahanian | 89204a1 | 2007-10-01 16:53:59 +0000 | [diff] [blame] | 1313 | /// TranslateIvarVisibility - Translate visibility from a token ID to an |
| 1314 | /// AST enum value. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1315 | static ObjCIvarDecl::AccessControl |
Fariborz Jahanian | 89204a1 | 2007-10-01 16:53:59 +0000 | [diff] [blame] | 1316 | TranslateIvarVisibility(tok::ObjCKeywordKind ivarVisibility) { |
Steve Naroff | f13271f | 2007-09-14 23:09:53 +0000 | [diff] [blame] | 1317 | switch (ivarVisibility) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1318 | case tok::objc_private: return ObjCIvarDecl::Private; |
| 1319 | case tok::objc_public: return ObjCIvarDecl::Public; |
| 1320 | case tok::objc_protected: return ObjCIvarDecl::Protected; |
| 1321 | case tok::objc_package: return ObjCIvarDecl::Package; |
Fariborz Jahanian | 89204a1 | 2007-10-01 16:53:59 +0000 | [diff] [blame] | 1322 | default: assert(false && "Unknown visitibility kind"); |
Steve Naroff | f13271f | 2007-09-14 23:09:53 +0000 | [diff] [blame] | 1323 | } |
| 1324 | } |
| 1325 | |
Fariborz Jahanian | 9d048ff | 2007-09-29 00:54:24 +0000 | [diff] [blame] | 1326 | void Sema::ActOnFields(Scope* S, |
Fariborz Jahanian | 3f5faf7 | 2007-10-04 00:45:27 +0000 | [diff] [blame] | 1327 | SourceLocation RecLoc, DeclTy *RecDecl, |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1328 | DeclTy **Fields, unsigned NumFields, |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 1329 | SourceLocation LBrac, SourceLocation RBrac, |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1330 | tok::ObjCKeywordKind *visibility) { |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 1331 | Decl *EnclosingDecl = static_cast<Decl*>(RecDecl); |
| 1332 | assert(EnclosingDecl && "missing record or interface decl"); |
| 1333 | RecordDecl *Record = dyn_cast<RecordDecl>(EnclosingDecl); |
| 1334 | |
Fariborz Jahanian | e267ab6 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 1335 | if (Record && Record->isDefinition()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1336 | // Diagnose code like: |
| 1337 | // struct S { struct S {} X; }; |
| 1338 | // We discover this when we complete the outer S. Reject and ignore the |
| 1339 | // outer S. |
| 1340 | Diag(Record->getLocation(), diag::err_nested_redefinition, |
| 1341 | Record->getKindName()); |
| 1342 | Diag(RecLoc, diag::err_previous_definition); |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 1343 | Record->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1344 | return; |
| 1345 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1346 | // Verify that all the fields are okay. |
| 1347 | unsigned NumNamedMembers = 0; |
| 1348 | llvm::SmallVector<FieldDecl*, 32> RecFields; |
| 1349 | llvm::SmallSet<const IdentifierInfo*, 32> FieldIDs; |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 1350 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1351 | for (unsigned i = 0; i != NumFields; ++i) { |
Fariborz Jahanian | e267ab6 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 1352 | |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 1353 | FieldDecl *FD = cast_or_null<FieldDecl>(static_cast<Decl*>(Fields[i])); |
| 1354 | assert(FD && "missing field decl"); |
| 1355 | |
| 1356 | // Remember all fields. |
| 1357 | RecFields.push_back(FD); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1358 | |
| 1359 | // Get the type for the field. |
Chris Lattner | 02c642e | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 1360 | Type *FDTy = FD->getType().getTypePtr(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1361 | |
Steve Naroff | f13271f | 2007-09-14 23:09:53 +0000 | [diff] [blame] | 1362 | // If we have visibility info, make sure the AST is set accordingly. |
| 1363 | if (visibility) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1364 | cast<ObjCIvarDecl>(FD)->setAccessControl( |
Fariborz Jahanian | 89204a1 | 2007-10-01 16:53:59 +0000 | [diff] [blame] | 1365 | TranslateIvarVisibility(visibility[i])); |
Steve Naroff | f13271f | 2007-09-14 23:09:53 +0000 | [diff] [blame] | 1366 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1367 | // C99 6.7.2.1p2 - A field may not be a function type. |
Chris Lattner | 02c642e | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 1368 | if (FDTy->isFunctionType()) { |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 1369 | Diag(FD->getLocation(), diag::err_field_declared_as_function, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1370 | FD->getName()); |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 1371 | FD->setInvalidDecl(); |
| 1372 | EnclosingDecl->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1373 | continue; |
| 1374 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1375 | // C99 6.7.2.1p2 - A field may not be an incomplete type except... |
| 1376 | if (FDTy->isIncompleteType()) { |
Fariborz Jahanian | e267ab6 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 1377 | if (!Record) { // Incomplete ivar type is always an error. |
Fariborz Jahanian | 3f5faf7 | 2007-10-04 00:45:27 +0000 | [diff] [blame] | 1378 | Diag(FD->getLocation(), diag::err_field_incomplete, FD->getName()); |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 1379 | FD->setInvalidDecl(); |
| 1380 | EnclosingDecl->setInvalidDecl(); |
Fariborz Jahanian | 3f5faf7 | 2007-10-04 00:45:27 +0000 | [diff] [blame] | 1381 | continue; |
Fariborz Jahanian | e267ab6 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 1382 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1383 | if (i != NumFields-1 || // ... that the last member ... |
| 1384 | Record->getKind() != Decl::Struct || // ... of a structure ... |
Chris Lattner | 02c642e | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 1385 | !FDTy->isArrayType()) { //... may have incomplete array type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1386 | Diag(FD->getLocation(), diag::err_field_incomplete, FD->getName()); |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 1387 | FD->setInvalidDecl(); |
| 1388 | EnclosingDecl->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1389 | continue; |
| 1390 | } |
Fariborz Jahanian | e267ab6 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 1391 | if (NumNamedMembers < 1) { //... must have more than named member ... |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1392 | Diag(FD->getLocation(), diag::err_flexible_array_empty_struct, |
| 1393 | FD->getName()); |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 1394 | FD->setInvalidDecl(); |
| 1395 | EnclosingDecl->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1396 | continue; |
| 1397 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1398 | // Okay, we have a legal flexible array member at the end of the struct. |
Fariborz Jahanian | e267ab6 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 1399 | if (Record) |
| 1400 | Record->setHasFlexibleArrayMember(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1401 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1402 | /// C99 6.7.2.1p2 - a struct ending in a flexible array member cannot be the |
| 1403 | /// field of another structure or the element of an array. |
Chris Lattner | 02c642e | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 1404 | if (const RecordType *FDTTy = FDTy->getAsRecordType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1405 | if (FDTTy->getDecl()->hasFlexibleArrayMember()) { |
| 1406 | // If this is a member of a union, then entire union becomes "flexible". |
Fariborz Jahanian | e267ab6 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 1407 | if (Record && Record->getKind() == Decl::Union) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1408 | Record->setHasFlexibleArrayMember(true); |
| 1409 | } else { |
| 1410 | // If this is a struct/class and this is not the last element, reject |
| 1411 | // it. Note that GCC supports variable sized arrays in the middle of |
| 1412 | // structures. |
| 1413 | if (i != NumFields-1) { |
| 1414 | Diag(FD->getLocation(), diag::err_variable_sized_type_in_struct, |
| 1415 | FD->getName()); |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 1416 | FD->setInvalidDecl(); |
| 1417 | EnclosingDecl->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1418 | continue; |
| 1419 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1420 | // We support flexible arrays at the end of structs in other structs |
| 1421 | // as an extension. |
| 1422 | Diag(FD->getLocation(), diag::ext_flexible_array_in_struct, |
| 1423 | FD->getName()); |
Fariborz Jahanian | 3f5faf7 | 2007-10-04 00:45:27 +0000 | [diff] [blame] | 1424 | if (Record) |
Fariborz Jahanian | e267ab6 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 1425 | Record->setHasFlexibleArrayMember(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1426 | } |
| 1427 | } |
| 1428 | } |
Fariborz Jahanian | e7f64cc | 2007-10-12 22:10:42 +0000 | [diff] [blame] | 1429 | /// A field cannot be an Objective-c object |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1430 | if (FDTy->isObjCInterfaceType()) { |
Fariborz Jahanian | e7f64cc | 2007-10-12 22:10:42 +0000 | [diff] [blame] | 1431 | Diag(FD->getLocation(), diag::err_statically_allocated_object, |
| 1432 | FD->getName()); |
| 1433 | FD->setInvalidDecl(); |
| 1434 | EnclosingDecl->setInvalidDecl(); |
| 1435 | continue; |
| 1436 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1437 | // Keep track of the number of named members. |
| 1438 | if (IdentifierInfo *II = FD->getIdentifier()) { |
| 1439 | // Detect duplicate member names. |
| 1440 | if (!FieldIDs.insert(II)) { |
| 1441 | Diag(FD->getLocation(), diag::err_duplicate_member, II->getName()); |
| 1442 | // Find the previous decl. |
| 1443 | SourceLocation PrevLoc; |
| 1444 | for (unsigned i = 0, e = RecFields.size(); ; ++i) { |
| 1445 | assert(i != e && "Didn't find previous def!"); |
| 1446 | if (RecFields[i]->getIdentifier() == II) { |
| 1447 | PrevLoc = RecFields[i]->getLocation(); |
| 1448 | break; |
| 1449 | } |
| 1450 | } |
| 1451 | Diag(PrevLoc, diag::err_previous_definition); |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 1452 | FD->setInvalidDecl(); |
| 1453 | EnclosingDecl->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1454 | continue; |
| 1455 | } |
| 1456 | ++NumNamedMembers; |
| 1457 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1458 | } |
| 1459 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1460 | // Okay, we successfully defined 'Record'. |
Chris Lattner | e1e7985 | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 1461 | if (Record) { |
Fariborz Jahanian | e267ab6 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 1462 | Record->defineBody(&RecFields[0], RecFields.size()); |
Chris Lattner | e1e7985 | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 1463 | Consumer.HandleTagDeclDefinition(Record); |
| 1464 | } else { |
Chris Lattner | a91d381 | 2008-02-05 22:40:55 +0000 | [diff] [blame] | 1465 | ObjCIvarDecl **ClsFields = reinterpret_cast<ObjCIvarDecl**>(&RecFields[0]); |
| 1466 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(EnclosingDecl)) |
| 1467 | ID->addInstanceVariablesToClass(ClsFields, RecFields.size(), RBrac); |
| 1468 | else if (ObjCImplementationDecl *IMPDecl = |
| 1469 | dyn_cast<ObjCImplementationDecl>(EnclosingDecl)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1470 | assert(IMPDecl && "ActOnFields - missing ObjCImplementationDecl"); |
| 1471 | IMPDecl->ObjCAddInstanceVariablesToClassImpl(ClsFields, RecFields.size()); |
Fariborz Jahanian | 3a3ca1b | 2007-10-31 18:48:14 +0000 | [diff] [blame] | 1472 | CheckImplementationIvars(IMPDecl, ClsFields, RecFields.size(), RBrac); |
Fariborz Jahanian | d0b90bf | 2007-09-26 18:27:25 +0000 | [diff] [blame] | 1473 | } |
Fariborz Jahanian | b04a021 | 2007-09-14 21:08:27 +0000 | [diff] [blame] | 1474 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1475 | } |
| 1476 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1477 | Sema::DeclTy *Sema::ActOnEnumConstant(Scope *S, DeclTy *theEnumDecl, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1478 | DeclTy *lastEnumConst, |
| 1479 | SourceLocation IdLoc, IdentifierInfo *Id, |
| 1480 | SourceLocation EqualLoc, ExprTy *val) { |
| 1481 | theEnumDecl = theEnumDecl; // silence unused warning. |
| 1482 | EnumConstantDecl *LastEnumConst = |
| 1483 | cast_or_null<EnumConstantDecl>(static_cast<Decl*>(lastEnumConst)); |
| 1484 | Expr *Val = static_cast<Expr*>(val); |
| 1485 | |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 1486 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 1487 | // we find one that is. |
| 1488 | while ((S->getFlags() & Scope::DeclScope) == 0) |
| 1489 | S = S->getParent(); |
| 1490 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1491 | // Verify that there isn't already something declared with this name in this |
| 1492 | // scope. |
Steve Naroff | 8e74c93 | 2007-09-13 21:41:19 +0000 | [diff] [blame] | 1493 | if (ScopedDecl *PrevDecl = LookupScopedDecl(Id, Decl::IDNS_Ordinary, |
| 1494 | IdLoc, S)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1495 | if (S->isDeclScope(PrevDecl)) { |
| 1496 | if (isa<EnumConstantDecl>(PrevDecl)) |
| 1497 | Diag(IdLoc, diag::err_redefinition_of_enumerator, Id->getName()); |
| 1498 | else |
| 1499 | Diag(IdLoc, diag::err_redefinition, Id->getName()); |
| 1500 | Diag(PrevDecl->getLocation(), diag::err_previous_definition); |
Chris Lattner | a73349d | 2008-02-26 00:33:57 +0000 | [diff] [blame] | 1501 | delete Val; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1502 | return 0; |
| 1503 | } |
| 1504 | } |
| 1505 | |
| 1506 | llvm::APSInt EnumVal(32); |
| 1507 | QualType EltTy; |
| 1508 | if (Val) { |
Chris Lattner | 421a23d | 2007-08-27 21:16:18 +0000 | [diff] [blame] | 1509 | // Make sure to promote the operand type to int. |
| 1510 | UsualUnaryConversions(Val); |
| 1511 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1512 | // C99 6.7.2.2p2: Make sure we have an integer constant expression. |
| 1513 | SourceLocation ExpLoc; |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 1514 | if (!Val->isIntegerConstantExpr(EnumVal, Context, &ExpLoc)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1515 | Diag(ExpLoc, diag::err_enum_value_not_integer_constant_expr, |
| 1516 | Id->getName()); |
Chris Lattner | a73349d | 2008-02-26 00:33:57 +0000 | [diff] [blame] | 1517 | delete Val; |
Chris Lattner | b7416f9 | 2007-08-27 17:37:24 +0000 | [diff] [blame] | 1518 | Val = 0; // Just forget about it. |
Chris Lattner | e9ca851 | 2007-08-29 16:03:41 +0000 | [diff] [blame] | 1519 | } else { |
| 1520 | EltTy = Val->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1521 | } |
Chris Lattner | b7416f9 | 2007-08-27 17:37:24 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
| 1524 | if (!Val) { |
| 1525 | if (LastEnumConst) { |
| 1526 | // Assign the last value + 1. |
| 1527 | EnumVal = LastEnumConst->getInitVal(); |
| 1528 | ++EnumVal; |
Chris Lattner | 421a23d | 2007-08-27 21:16:18 +0000 | [diff] [blame] | 1529 | |
| 1530 | // Check for overflow on increment. |
| 1531 | if (EnumVal < LastEnumConst->getInitVal()) |
| 1532 | Diag(IdLoc, diag::warn_enum_value_overflow); |
| 1533 | |
Chris Lattner | b7416f9 | 2007-08-27 17:37:24 +0000 | [diff] [blame] | 1534 | EltTy = LastEnumConst->getType(); |
| 1535 | } else { |
| 1536 | // First value, set to zero. |
| 1537 | EltTy = Context.IntTy; |
Chris Lattner | 701e5eb | 2007-09-04 02:45:27 +0000 | [diff] [blame] | 1538 | EnumVal.zextOrTrunc( |
| 1539 | static_cast<uint32_t>(Context.getTypeSize(EltTy, IdLoc))); |
Chris Lattner | b7416f9 | 2007-08-27 17:37:24 +0000 | [diff] [blame] | 1540 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1541 | } |
| 1542 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1543 | EnumConstantDecl *New = new EnumConstantDecl(IdLoc, Id, EltTy, Val, EnumVal, |
| 1544 | LastEnumConst); |
| 1545 | |
| 1546 | // Register this decl in the current scope stack. |
Steve Naroff | c752d04 | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 1547 | New->setNext(Id->getFETokenInfo<ScopedDecl>()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1548 | Id->setFETokenInfo(New); |
| 1549 | S->AddDecl(New); |
| 1550 | return New; |
| 1551 | } |
| 1552 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1553 | void Sema::ActOnEnumBody(SourceLocation EnumLoc, DeclTy *EnumDeclX, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1554 | DeclTy **Elements, unsigned NumElements) { |
| 1555 | EnumDecl *Enum = cast<EnumDecl>(static_cast<Decl*>(EnumDeclX)); |
| 1556 | assert(!Enum->isDefinition() && "Enum redefinitions can't reach here"); |
| 1557 | |
Chris Lattner | e37f0be | 2007-08-28 05:10:31 +0000 | [diff] [blame] | 1558 | // TODO: If the result value doesn't fit in an int, it must be a long or long |
| 1559 | // long value. ISO C does not support this, but GCC does as an extension, |
| 1560 | // emit a warning. |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 1561 | unsigned IntWidth = |
| 1562 | Context.Target.getIntWidth(Context.getFullLoc(Enum->getLocation())); |
Chris Lattner | e37f0be | 2007-08-28 05:10:31 +0000 | [diff] [blame] | 1563 | |
| 1564 | |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1565 | // Verify that all the values are okay, compute the size of the values, and |
| 1566 | // reverse the list. |
| 1567 | unsigned NumNegativeBits = 0; |
| 1568 | unsigned NumPositiveBits = 0; |
| 1569 | |
| 1570 | // Keep track of whether all elements have type int. |
| 1571 | bool AllElementsInt = true; |
| 1572 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1573 | EnumConstantDecl *EltList = 0; |
| 1574 | for (unsigned i = 0; i != NumElements; ++i) { |
| 1575 | EnumConstantDecl *ECD = |
| 1576 | cast_or_null<EnumConstantDecl>(static_cast<Decl*>(Elements[i])); |
| 1577 | if (!ECD) continue; // Already issued a diagnostic. |
Chris Lattner | 211a30e | 2007-08-28 05:27:00 +0000 | [diff] [blame] | 1578 | |
| 1579 | // If the enum value doesn't fit in an int, emit an extension warning. |
Chris Lattner | 211a30e | 2007-08-28 05:27:00 +0000 | [diff] [blame] | 1580 | const llvm::APSInt &InitVal = ECD->getInitVal(); |
Chris Lattner | a73349d | 2008-02-26 00:33:57 +0000 | [diff] [blame] | 1581 | assert(InitVal.getBitWidth() >= IntWidth && |
| 1582 | "Should have promoted value to int"); |
Chris Lattner | 211a30e | 2007-08-28 05:27:00 +0000 | [diff] [blame] | 1583 | if (InitVal.getBitWidth() > IntWidth) { |
| 1584 | llvm::APSInt V(InitVal); |
| 1585 | V.trunc(IntWidth); |
| 1586 | V.extend(InitVal.getBitWidth()); |
| 1587 | if (V != InitVal) |
| 1588 | Diag(ECD->getLocation(), diag::ext_enum_value_not_int, |
| 1589 | InitVal.toString()); |
| 1590 | } |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1591 | |
| 1592 | // Keep track of the size of positive and negative values. |
Chris Lattner | a73349d | 2008-02-26 00:33:57 +0000 | [diff] [blame] | 1593 | if (InitVal.isUnsigned() || InitVal.isNonNegative()) |
Chris Lattner | 21dd821 | 2008-01-14 21:47:29 +0000 | [diff] [blame] | 1594 | NumPositiveBits = std::max(NumPositiveBits, |
| 1595 | (unsigned)InitVal.getActiveBits()); |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1596 | else |
Chris Lattner | 21dd821 | 2008-01-14 21:47:29 +0000 | [diff] [blame] | 1597 | NumNegativeBits = std::max(NumNegativeBits, |
| 1598 | (unsigned)InitVal.getMinSignedBits()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1599 | |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1600 | // Keep track of whether every enum element has type int (very commmon). |
| 1601 | if (AllElementsInt) |
| 1602 | AllElementsInt = ECD->getType() == Context.IntTy; |
| 1603 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1604 | ECD->setNextDeclarator(EltList); |
| 1605 | EltList = ECD; |
| 1606 | } |
| 1607 | |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1608 | // Figure out the type that should be used for this enum. |
| 1609 | // FIXME: Support attribute(packed) on enums and -fshort-enums. |
| 1610 | QualType BestType; |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 1611 | unsigned BestWidth; |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1612 | |
| 1613 | if (NumNegativeBits) { |
| 1614 | // If there is a negative value, figure out the smallest integer type (of |
| 1615 | // int/long/longlong) that fits. |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 1616 | if (NumNegativeBits <= IntWidth && NumPositiveBits < IntWidth) { |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1617 | BestType = Context.IntTy; |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 1618 | BestWidth = IntWidth; |
| 1619 | } else { |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 1620 | BestWidth = |
| 1621 | Context.Target.getLongWidth(Context.getFullLoc(Enum->getLocation())); |
| 1622 | |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 1623 | if (NumNegativeBits <= BestWidth && NumPositiveBits < BestWidth) |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1624 | BestType = Context.LongTy; |
| 1625 | else { |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 1626 | BestWidth = Context.Target.getLongLongWidth( |
| 1627 | Context.getFullLoc(Enum->getLocation())); |
| 1628 | |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 1629 | if (NumNegativeBits > BestWidth || NumPositiveBits >= BestWidth) |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1630 | Diag(Enum->getLocation(), diag::warn_enum_too_large); |
| 1631 | BestType = Context.LongLongTy; |
| 1632 | } |
| 1633 | } |
| 1634 | } else { |
| 1635 | // If there is no negative value, figure out which of uint, ulong, ulonglong |
| 1636 | // fits. |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 1637 | if (NumPositiveBits <= IntWidth) { |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1638 | BestType = Context.UnsignedIntTy; |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 1639 | BestWidth = IntWidth; |
| 1640 | } else if (NumPositiveBits <= |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 1641 | (BestWidth = Context.Target.getLongWidth( |
| 1642 | Context.getFullLoc(Enum->getLocation())))) |
| 1643 | |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1644 | BestType = Context.UnsignedLongTy; |
| 1645 | else { |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 1646 | BestWidth = |
| 1647 | Context.Target.getLongLongWidth(Context.getFullLoc(Enum->getLocation())); |
| 1648 | |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 1649 | assert(NumPositiveBits <= BestWidth && |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1650 | "How could an initializer get larger than ULL?"); |
| 1651 | BestType = Context.UnsignedLongLongTy; |
| 1652 | } |
| 1653 | } |
| 1654 | |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 1655 | // Loop over all of the enumerator constants, changing their types to match |
| 1656 | // the type of the enum if needed. |
| 1657 | for (unsigned i = 0; i != NumElements; ++i) { |
| 1658 | EnumConstantDecl *ECD = |
| 1659 | cast_or_null<EnumConstantDecl>(static_cast<Decl*>(Elements[i])); |
| 1660 | if (!ECD) continue; // Already issued a diagnostic. |
| 1661 | |
| 1662 | // Standard C says the enumerators have int type, but we allow, as an |
| 1663 | // extension, the enumerators to be larger than int size. If each |
| 1664 | // enumerator value fits in an int, type it as an int, otherwise type it the |
| 1665 | // same as the enumerator decl itself. This means that in "enum { X = 1U }" |
| 1666 | // that X has type 'int', not 'unsigned'. |
Chris Lattner | a73349d | 2008-02-26 00:33:57 +0000 | [diff] [blame] | 1667 | if (ECD->getType() == Context.IntTy) { |
| 1668 | // Make sure the init value is signed. |
| 1669 | llvm::APSInt IV = ECD->getInitVal(); |
| 1670 | IV.setIsSigned(true); |
| 1671 | ECD->setInitVal(IV); |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 1672 | continue; // Already int type. |
Chris Lattner | a73349d | 2008-02-26 00:33:57 +0000 | [diff] [blame] | 1673 | } |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 1674 | |
| 1675 | // Determine whether the value fits into an int. |
| 1676 | llvm::APSInt InitVal = ECD->getInitVal(); |
| 1677 | bool FitsInInt; |
| 1678 | if (InitVal.isUnsigned() || !InitVal.isNegative()) |
| 1679 | FitsInInt = InitVal.getActiveBits() < IntWidth; |
| 1680 | else |
| 1681 | FitsInInt = InitVal.getMinSignedBits() <= IntWidth; |
| 1682 | |
| 1683 | // If it fits into an integer type, force it. Otherwise force it to match |
| 1684 | // the enum decl type. |
| 1685 | QualType NewTy; |
| 1686 | unsigned NewWidth; |
| 1687 | bool NewSign; |
| 1688 | if (FitsInInt) { |
| 1689 | NewTy = Context.IntTy; |
| 1690 | NewWidth = IntWidth; |
| 1691 | NewSign = true; |
| 1692 | } else if (ECD->getType() == BestType) { |
| 1693 | // Already the right type! |
| 1694 | continue; |
| 1695 | } else { |
| 1696 | NewTy = BestType; |
| 1697 | NewWidth = BestWidth; |
| 1698 | NewSign = BestType->isSignedIntegerType(); |
| 1699 | } |
| 1700 | |
| 1701 | // Adjust the APSInt value. |
| 1702 | InitVal.extOrTrunc(NewWidth); |
| 1703 | InitVal.setIsSigned(NewSign); |
| 1704 | ECD->setInitVal(InitVal); |
| 1705 | |
| 1706 | // Adjust the Expr initializer and type. |
| 1707 | ECD->setInitExpr(new ImplicitCastExpr(NewTy, ECD->getInitExpr())); |
| 1708 | ECD->setType(NewTy); |
| 1709 | } |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 1710 | |
Chris Lattner | e00b18c | 2007-08-28 18:24:31 +0000 | [diff] [blame] | 1711 | Enum->defineElements(EltList, BestType); |
Chris Lattner | e1e7985 | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 1712 | Consumer.HandleTagDeclDefinition(Enum); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1713 | } |
| 1714 | |
Anders Carlsson | dfab6cb | 2008-02-08 00:33:21 +0000 | [diff] [blame] | 1715 | Sema::DeclTy *Sema::ActOnFileScopeAsmDecl(SourceLocation Loc, |
| 1716 | ExprTy *expr) { |
| 1717 | StringLiteral *AsmString = cast<StringLiteral>((Expr*)expr); |
| 1718 | |
| 1719 | return new FileScopeAsmDecl(Loc, AsmString); |
| 1720 | } |
| 1721 | |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 1722 | Sema::DeclTy* Sema::ActOnLinkageSpec(SourceLocation Loc, |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 1723 | SourceLocation LBrace, |
| 1724 | SourceLocation RBrace, |
| 1725 | const char *Lang, |
| 1726 | unsigned StrSize, |
| 1727 | DeclTy *D) { |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 1728 | LinkageSpecDecl::LanguageIDs Language; |
| 1729 | Decl *dcl = static_cast<Decl *>(D); |
| 1730 | if (strncmp(Lang, "\"C\"", StrSize) == 0) |
| 1731 | Language = LinkageSpecDecl::lang_c; |
| 1732 | else if (strncmp(Lang, "\"C++\"", StrSize) == 0) |
| 1733 | Language = LinkageSpecDecl::lang_cxx; |
| 1734 | else { |
| 1735 | Diag(Loc, diag::err_bad_language); |
| 1736 | return 0; |
| 1737 | } |
| 1738 | |
| 1739 | // FIXME: Add all the various semantics of linkage specifications |
| 1740 | return new LinkageSpecDecl(Loc, Language, dcl); |
| 1741 | } |
| 1742 | |
Chris Lattner | 74788ba | 2008-02-21 00:48:22 +0000 | [diff] [blame] | 1743 | void Sema::HandleDeclAttribute(Decl *New, AttributeList *Attr) { |
Anders Carlsson | 6ede0ff | 2007-12-19 06:16:30 +0000 | [diff] [blame] | 1744 | |
Chris Lattner | 74788ba | 2008-02-21 00:48:22 +0000 | [diff] [blame] | 1745 | switch (Attr->getKind()) { |
Chris Lattner | 212839c | 2008-02-20 23:17:35 +0000 | [diff] [blame] | 1746 | case AttributeList::AT_vector_size: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1747 | if (ValueDecl *vDecl = dyn_cast<ValueDecl>(New)) { |
Chris Lattner | 74788ba | 2008-02-21 00:48:22 +0000 | [diff] [blame] | 1748 | QualType newType = HandleVectorTypeAttribute(vDecl->getType(), Attr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1749 | if (!newType.isNull()) // install the new vector type into the decl |
| 1750 | vDecl->setType(newType); |
| 1751 | } |
| 1752 | if (TypedefDecl *tDecl = dyn_cast<TypedefDecl>(New)) { |
| 1753 | QualType newType = HandleVectorTypeAttribute(tDecl->getUnderlyingType(), |
Chris Lattner | 74788ba | 2008-02-21 00:48:22 +0000 | [diff] [blame] | 1754 | Attr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1755 | if (!newType.isNull()) // install the new vector type into the decl |
| 1756 | tDecl->setUnderlyingType(newType); |
| 1757 | } |
Chris Lattner | 212839c | 2008-02-20 23:17:35 +0000 | [diff] [blame] | 1758 | break; |
| 1759 | case AttributeList::AT_ocu_vector_type: |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1760 | if (TypedefDecl *tDecl = dyn_cast<TypedefDecl>(New)) |
Chris Lattner | 74788ba | 2008-02-21 00:48:22 +0000 | [diff] [blame] | 1761 | HandleOCUVectorTypeAttribute(tDecl, Attr); |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1762 | else |
Chris Lattner | 74788ba | 2008-02-21 00:48:22 +0000 | [diff] [blame] | 1763 | Diag(Attr->getLoc(), |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1764 | diag::err_typecheck_ocu_vector_not_typedef); |
Chris Lattner | 212839c | 2008-02-20 23:17:35 +0000 | [diff] [blame] | 1765 | break; |
| 1766 | case AttributeList::AT_address_space: |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1767 | if (TypedefDecl *tDecl = dyn_cast<TypedefDecl>(New)) { |
| 1768 | QualType newType = HandleAddressSpaceTypeAttribute( |
| 1769 | tDecl->getUnderlyingType(), |
Chris Lattner | 74788ba | 2008-02-21 00:48:22 +0000 | [diff] [blame] | 1770 | Attr); |
| 1771 | tDecl->setUnderlyingType(newType); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1772 | } else if (ValueDecl *vDecl = dyn_cast<ValueDecl>(New)) { |
| 1773 | QualType newType = HandleAddressSpaceTypeAttribute(vDecl->getType(), |
Chris Lattner | 74788ba | 2008-02-21 00:48:22 +0000 | [diff] [blame] | 1774 | Attr); |
| 1775 | // install the new addr spaced type into the decl |
| 1776 | vDecl->setType(newType); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1777 | } |
Chris Lattner | 212839c | 2008-02-20 23:17:35 +0000 | [diff] [blame] | 1778 | break; |
| 1779 | case AttributeList::AT_aligned: |
Chris Lattner | 74788ba | 2008-02-21 00:48:22 +0000 | [diff] [blame] | 1780 | HandleAlignedAttribute(New, Attr); |
Chris Lattner | 212839c | 2008-02-20 23:17:35 +0000 | [diff] [blame] | 1781 | break; |
| 1782 | case AttributeList::AT_packed: |
Chris Lattner | 74788ba | 2008-02-21 00:48:22 +0000 | [diff] [blame] | 1783 | HandlePackedAttribute(New, Attr); |
Chris Lattner | 212839c | 2008-02-20 23:17:35 +0000 | [diff] [blame] | 1784 | break; |
Nate Begeman | c398f0b | 2008-02-21 19:30:49 +0000 | [diff] [blame] | 1785 | case AttributeList::AT_annotate: |
| 1786 | HandleAnnotateAttribute(New, Attr); |
| 1787 | break; |
Ted Kremenek | aecb383 | 2008-02-27 20:43:06 +0000 | [diff] [blame^] | 1788 | case AttributeList::AT_noreturn: |
| 1789 | HandleNoReturnAttribute(New, Attr); |
| 1790 | break; |
Chris Lattner | 212839c | 2008-02-20 23:17:35 +0000 | [diff] [blame] | 1791 | default: |
| 1792 | // FIXME: add other attributes... |
| 1793 | break; |
| 1794 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1795 | } |
| 1796 | |
| 1797 | void Sema::HandleDeclAttributes(Decl *New, AttributeList *declspec_prefix, |
| 1798 | AttributeList *declarator_postfix) { |
| 1799 | while (declspec_prefix) { |
| 1800 | HandleDeclAttribute(New, declspec_prefix); |
| 1801 | declspec_prefix = declspec_prefix->getNext(); |
| 1802 | } |
| 1803 | while (declarator_postfix) { |
| 1804 | HandleDeclAttribute(New, declarator_postfix); |
| 1805 | declarator_postfix = declarator_postfix->getNext(); |
| 1806 | } |
| 1807 | } |
| 1808 | |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1809 | void Sema::HandleOCUVectorTypeAttribute(TypedefDecl *tDecl, |
| 1810 | AttributeList *rawAttr) { |
| 1811 | QualType curType = tDecl->getUnderlyingType(); |
Anders Carlsson | 78aaae9 | 2007-12-19 07:19:40 +0000 | [diff] [blame] | 1812 | // check the attribute arguments. |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1813 | if (rawAttr->getNumArgs() != 1) { |
Chris Lattner | 2070d80 | 2008-02-20 23:25:22 +0000 | [diff] [blame] | 1814 | Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments, |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1815 | std::string("1")); |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1816 | return; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1817 | } |
| 1818 | Expr *sizeExpr = static_cast<Expr *>(rawAttr->getArg(0)); |
| 1819 | llvm::APSInt vecSize(32); |
| 1820 | if (!sizeExpr->isIntegerConstantExpr(vecSize, Context)) { |
Chris Lattner | 2070d80 | 2008-02-20 23:25:22 +0000 | [diff] [blame] | 1821 | Diag(rawAttr->getLoc(), diag::err_attribute_argument_not_int, |
Anders Carlsson | 042c4e7 | 2008-02-16 19:51:27 +0000 | [diff] [blame] | 1822 | "ocu_vector_type", sizeExpr->getSourceRange()); |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1823 | return; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1824 | } |
| 1825 | // unlike gcc's vector_size attribute, we do not allow vectors to be defined |
| 1826 | // in conjunction with complex types (pointers, arrays, functions, etc.). |
| 1827 | Type *canonType = curType.getCanonicalType().getTypePtr(); |
| 1828 | if (!(canonType->isIntegerType() || canonType->isRealFloatingType())) { |
Chris Lattner | 2070d80 | 2008-02-20 23:25:22 +0000 | [diff] [blame] | 1829 | Diag(rawAttr->getLoc(), diag::err_attribute_invalid_vector_type, |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1830 | curType.getCanonicalType().getAsString()); |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1831 | return; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1832 | } |
| 1833 | // unlike gcc's vector_size attribute, the size is specified as the |
| 1834 | // number of elements, not the number of bytes. |
Chris Lattner | 701e5eb | 2007-09-04 02:45:27 +0000 | [diff] [blame] | 1835 | unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue()); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1836 | |
| 1837 | if (vectorSize == 0) { |
Chris Lattner | 2070d80 | 2008-02-20 23:25:22 +0000 | [diff] [blame] | 1838 | Diag(rawAttr->getLoc(), diag::err_attribute_zero_size, |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1839 | sizeExpr->getSourceRange()); |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1840 | return; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1841 | } |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1842 | // Instantiate/Install the vector type, the number of elements is > 0. |
| 1843 | tDecl->setUnderlyingType(Context.getOCUVectorType(curType, vectorSize)); |
| 1844 | // Remember this typedef decl, we will need it later for diagnostics. |
| 1845 | OCUVectorDecls.push_back(tDecl); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1846 | } |
| 1847 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1848 | QualType Sema::HandleVectorTypeAttribute(QualType curType, |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1849 | AttributeList *rawAttr) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1850 | // check the attribute arugments. |
| 1851 | if (rawAttr->getNumArgs() != 1) { |
Chris Lattner | 2070d80 | 2008-02-20 23:25:22 +0000 | [diff] [blame] | 1852 | Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1853 | std::string("1")); |
| 1854 | return QualType(); |
| 1855 | } |
| 1856 | Expr *sizeExpr = static_cast<Expr *>(rawAttr->getArg(0)); |
| 1857 | llvm::APSInt vecSize(32); |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 1858 | if (!sizeExpr->isIntegerConstantExpr(vecSize, Context)) { |
Chris Lattner | 2070d80 | 2008-02-20 23:25:22 +0000 | [diff] [blame] | 1859 | Diag(rawAttr->getLoc(), diag::err_attribute_argument_not_int, |
Anders Carlsson | 042c4e7 | 2008-02-16 19:51:27 +0000 | [diff] [blame] | 1860 | "vector_size", sizeExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1861 | return QualType(); |
| 1862 | } |
| 1863 | // navigate to the base type - we need to provide for vector pointers, |
| 1864 | // vector arrays, and functions returning vectors. |
| 1865 | Type *canonType = curType.getCanonicalType().getTypePtr(); |
| 1866 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1867 | if (canonType->isPointerType() || canonType->isArrayType() || |
| 1868 | canonType->isFunctionType()) { |
Chris Lattner | 54b263b | 2007-12-19 05:38:06 +0000 | [diff] [blame] | 1869 | assert(0 && "HandleVector(): Complex type construction unimplemented"); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1870 | /* FIXME: rebuild the type from the inside out, vectorizing the inner type. |
| 1871 | do { |
| 1872 | if (PointerType *PT = dyn_cast<PointerType>(canonType)) |
| 1873 | canonType = PT->getPointeeType().getTypePtr(); |
| 1874 | else if (ArrayType *AT = dyn_cast<ArrayType>(canonType)) |
| 1875 | canonType = AT->getElementType().getTypePtr(); |
| 1876 | else if (FunctionType *FT = dyn_cast<FunctionType>(canonType)) |
| 1877 | canonType = FT->getResultType().getTypePtr(); |
| 1878 | } while (canonType->isPointerType() || canonType->isArrayType() || |
| 1879 | canonType->isFunctionType()); |
| 1880 | */ |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1881 | } |
| 1882 | // the base type must be integer or float. |
| 1883 | if (!(canonType->isIntegerType() || canonType->isRealFloatingType())) { |
Chris Lattner | 2070d80 | 2008-02-20 23:25:22 +0000 | [diff] [blame] | 1884 | Diag(rawAttr->getLoc(), diag::err_attribute_invalid_vector_type, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1885 | curType.getCanonicalType().getAsString()); |
| 1886 | return QualType(); |
| 1887 | } |
Chris Lattner | 701e5eb | 2007-09-04 02:45:27 +0000 | [diff] [blame] | 1888 | unsigned typeSize = static_cast<unsigned>( |
Chris Lattner | 2070d80 | 2008-02-20 23:25:22 +0000 | [diff] [blame] | 1889 | Context.getTypeSize(curType, rawAttr->getLoc())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1890 | // vecSize is specified in bytes - convert to bits. |
Chris Lattner | 701e5eb | 2007-09-04 02:45:27 +0000 | [diff] [blame] | 1891 | unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1892 | |
| 1893 | // the vector size needs to be an integral multiple of the type size. |
| 1894 | if (vectorSize % typeSize) { |
Chris Lattner | 2070d80 | 2008-02-20 23:25:22 +0000 | [diff] [blame] | 1895 | Diag(rawAttr->getLoc(), diag::err_attribute_invalid_size, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1896 | sizeExpr->getSourceRange()); |
| 1897 | return QualType(); |
| 1898 | } |
| 1899 | if (vectorSize == 0) { |
Chris Lattner | 2070d80 | 2008-02-20 23:25:22 +0000 | [diff] [blame] | 1900 | Diag(rawAttr->getLoc(), diag::err_attribute_zero_size, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1901 | sizeExpr->getSourceRange()); |
| 1902 | return QualType(); |
| 1903 | } |
Nate Begeman | c398f0b | 2008-02-21 19:30:49 +0000 | [diff] [blame] | 1904 | // Instantiate the vector type, the number of elements is > 0, and not |
| 1905 | // required to be a power of 2, unlike GCC. |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1906 | return Context.getVectorType(curType, vectorSize/typeSize); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1907 | } |
| 1908 | |
Chris Lattner | 2070d80 | 2008-02-20 23:25:22 +0000 | [diff] [blame] | 1909 | void Sema::HandlePackedAttribute(Decl *d, AttributeList *rawAttr) { |
Anders Carlsson | ad14806 | 2008-02-16 00:29:18 +0000 | [diff] [blame] | 1910 | // check the attribute arguments. |
| 1911 | if (rawAttr->getNumArgs() > 0) { |
Chris Lattner | 2070d80 | 2008-02-20 23:25:22 +0000 | [diff] [blame] | 1912 | Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments, |
Anders Carlsson | ad14806 | 2008-02-16 00:29:18 +0000 | [diff] [blame] | 1913 | std::string("0")); |
| 1914 | return; |
| 1915 | } |
| 1916 | |
| 1917 | if (TagDecl *TD = dyn_cast<TagDecl>(d)) |
| 1918 | TD->addAttr(new PackedAttr); |
| 1919 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) { |
| 1920 | // If the alignment is less than or equal to 8 bits, the packed attribute |
| 1921 | // has no effect. |
| 1922 | if (Context.getTypeAlign(FD->getType(), SourceLocation()) <= 8) |
Chris Lattner | 2070d80 | 2008-02-20 23:25:22 +0000 | [diff] [blame] | 1923 | Diag(rawAttr->getLoc(), |
Anders Carlsson | ad14806 | 2008-02-16 00:29:18 +0000 | [diff] [blame] | 1924 | diag::warn_attribute_ignored_for_field_of_type, |
Chris Lattner | 2070d80 | 2008-02-20 23:25:22 +0000 | [diff] [blame] | 1925 | rawAttr->getName()->getName(), FD->getType().getAsString()); |
Anders Carlsson | ad14806 | 2008-02-16 00:29:18 +0000 | [diff] [blame] | 1926 | else |
Anders Carlsson | 425a609 | 2008-02-16 00:39:40 +0000 | [diff] [blame] | 1927 | FD->addAttr(new PackedAttr); |
Anders Carlsson | ad14806 | 2008-02-16 00:29:18 +0000 | [diff] [blame] | 1928 | } else |
Chris Lattner | 2070d80 | 2008-02-20 23:25:22 +0000 | [diff] [blame] | 1929 | Diag(rawAttr->getLoc(), diag::warn_attribute_ignored, |
| 1930 | rawAttr->getName()->getName()); |
Anders Carlsson | ad14806 | 2008-02-16 00:29:18 +0000 | [diff] [blame] | 1931 | } |
Nate Begeman | c398f0b | 2008-02-21 19:30:49 +0000 | [diff] [blame] | 1932 | |
Ted Kremenek | aecb383 | 2008-02-27 20:43:06 +0000 | [diff] [blame^] | 1933 | void Sema::HandleNoReturnAttribute(Decl *d, AttributeList *rawAttr) { |
| 1934 | // check the attribute arguments. |
| 1935 | if (rawAttr->getNumArgs() != 0) { |
| 1936 | Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments, |
| 1937 | std::string("0")); |
| 1938 | return; |
| 1939 | } |
| 1940 | |
| 1941 | d->addAttr(new NoReturnAttr()); |
| 1942 | } |
| 1943 | |
Nate Begeman | c398f0b | 2008-02-21 19:30:49 +0000 | [diff] [blame] | 1944 | void Sema::HandleAnnotateAttribute(Decl *d, AttributeList *rawAttr) { |
| 1945 | // check the attribute arguments. |
| 1946 | if (rawAttr->getNumArgs() != 1) { |
| 1947 | Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments, |
| 1948 | std::string("1")); |
| 1949 | return; |
| 1950 | } |
| 1951 | Expr *argExpr = static_cast<Expr *>(rawAttr->getArg(0)); |
| 1952 | StringLiteral *SE = dyn_cast<StringLiteral>(argExpr); |
Anders Carlsson | ad14806 | 2008-02-16 00:29:18 +0000 | [diff] [blame] | 1953 | |
Nate Begeman | c398f0b | 2008-02-21 19:30:49 +0000 | [diff] [blame] | 1954 | // Make sure that there is a string literal as the annotation's single |
| 1955 | // argument. |
| 1956 | if (!SE) { |
| 1957 | Diag(rawAttr->getLoc(), diag::err_attribute_annotate_no_string); |
| 1958 | return; |
| 1959 | } |
| 1960 | d->addAttr(new AnnotateAttr(std::string(SE->getStrData(), |
| 1961 | SE->getByteLength()))); |
| 1962 | } |
| 1963 | |
Anders Carlsson | 78aaae9 | 2007-12-19 07:19:40 +0000 | [diff] [blame] | 1964 | void Sema::HandleAlignedAttribute(Decl *d, AttributeList *rawAttr) |
| 1965 | { |
| 1966 | // check the attribute arguments. |
Eli Friedman | 4ca0867 | 2008-01-30 17:38:42 +0000 | [diff] [blame] | 1967 | if (rawAttr->getNumArgs() > 1) { |
Chris Lattner | 2070d80 | 2008-02-20 23:25:22 +0000 | [diff] [blame] | 1968 | Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments, |
Anders Carlsson | 78aaae9 | 2007-12-19 07:19:40 +0000 | [diff] [blame] | 1969 | std::string("1")); |
| 1970 | return; |
| 1971 | } |
Eli Friedman | 4ca0867 | 2008-01-30 17:38:42 +0000 | [diff] [blame] | 1972 | |
Anders Carlsson | 042c4e7 | 2008-02-16 19:51:27 +0000 | [diff] [blame] | 1973 | unsigned Align = 0; |
| 1974 | |
| 1975 | if (rawAttr->getNumArgs() == 0) { |
| 1976 | // FIXME: This should be the target specific maximum alignment. |
| 1977 | // (For now we just use 128 bits which is the maximum on X86. |
| 1978 | Align = 128; |
Eli Friedman | 4ca0867 | 2008-01-30 17:38:42 +0000 | [diff] [blame] | 1979 | return; |
Anders Carlsson | 042c4e7 | 2008-02-16 19:51:27 +0000 | [diff] [blame] | 1980 | } else { |
| 1981 | Expr *alignmentExpr = static_cast<Expr *>(rawAttr->getArg(0)); |
| 1982 | llvm::APSInt alignment(32); |
| 1983 | if (!alignmentExpr->isIntegerConstantExpr(alignment, Context)) { |
Chris Lattner | 2070d80 | 2008-02-20 23:25:22 +0000 | [diff] [blame] | 1984 | Diag(rawAttr->getLoc(), diag::err_attribute_argument_not_int, |
Anders Carlsson | 042c4e7 | 2008-02-16 19:51:27 +0000 | [diff] [blame] | 1985 | "aligned", alignmentExpr->getSourceRange()); |
| 1986 | return; |
| 1987 | } |
| 1988 | |
| 1989 | Align = alignment.getZExtValue() * 8; |
| 1990 | } |
Eli Friedman | 4ca0867 | 2008-01-30 17:38:42 +0000 | [diff] [blame] | 1991 | |
Anders Carlsson | 042c4e7 | 2008-02-16 19:51:27 +0000 | [diff] [blame] | 1992 | d->addAttr(new AlignedAttr(Align)); |
Anders Carlsson | 78aaae9 | 2007-12-19 07:19:40 +0000 | [diff] [blame] | 1993 | } |