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