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