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