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