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" |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 15 | #include "clang/AST/APValue.h" |
Chris Lattner | e1e7985 | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTConsumer.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 19 | #include "clang/AST/ExprCXX.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | #include "clang/Parse/DeclSpec.h" |
Daniel Dunbar | e4858a6 | 2008-08-11 03:45:03 +0000 | [diff] [blame] | 21 | #include "clang/Basic/Diagnostic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | #include "clang/Basic/TargetInfo.h" |
Steve Naroff | 4c49a6c | 2008-01-30 23:46:05 +0000 | [diff] [blame] | 23 | #include "clang/Basic/SourceManager.h" |
| 24 | // FIXME: layering (ideally, Sema shouldn't be dependent on Lex API's) |
Chris Lattner | e1e7985 | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 25 | #include "clang/Lex/Preprocessor.h" |
Steve Naroff | 4c49a6c | 2008-01-30 23:46:05 +0000 | [diff] [blame] | 26 | #include "clang/Lex/HeaderSearch.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/SmallSet.h" |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/STLExtras.h" |
| 29 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 30 | using namespace clang; |
| 31 | |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 32 | Sema::TypeTy *Sema::isTypeName(IdentifierInfo &II, Scope *S, |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 33 | const CXXScopeSpec *SS) { |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 34 | DeclContext *DC = 0; |
| 35 | if (SS) { |
| 36 | if (SS->isInvalid()) |
| 37 | return 0; |
| 38 | DC = static_cast<DeclContext*>(SS->getScopeRep()); |
| 39 | } |
| 40 | Decl *IIDecl = LookupDecl(&II, Decl::IDNS_Ordinary, S, DC, false); |
Steve Naroff | b327ce0 | 2008-04-02 14:35:35 +0000 | [diff] [blame] | 41 | |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 42 | if (IIDecl && (isa<TypedefDecl>(IIDecl) || |
| 43 | isa<ObjCInterfaceDecl>(IIDecl) || |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 44 | isa<TagDecl>(IIDecl) || |
| 45 | isa<TemplateTypeParmDecl>(IIDecl))) |
Fariborz Jahanian | bece4ac | 2007-10-12 16:34:10 +0000 | [diff] [blame] | 46 | return IIDecl; |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 47 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 50 | DeclContext *Sema::getContainingDC(DeclContext *DC) { |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 51 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(DC)) { |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 52 | // A C++ out-of-line method will return to the file declaration context. |
Argyrios Kyrtzidis | 5239304 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 53 | if (MD->isOutOfLineDefinition()) |
| 54 | return MD->getLexicalDeclContext(); |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 55 | |
| 56 | // A C++ inline method is parsed *after* the topmost class it was declared in |
| 57 | // is fully parsed (it's "complete"). |
| 58 | // The parsing of a C++ inline method happens at the declaration context of |
Argyrios Kyrtzidis | 77407b8 | 2008-11-19 18:01:13 +0000 | [diff] [blame] | 59 | // the topmost (non-nested) class it is lexically declared in. |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 60 | assert(isa<CXXRecordDecl>(MD->getParent()) && "C++ method not in Record."); |
| 61 | DC = MD->getParent(); |
Argyrios Kyrtzidis | 77407b8 | 2008-11-19 18:01:13 +0000 | [diff] [blame] | 62 | while (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC->getLexicalParent())) |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 63 | DC = RD; |
| 64 | |
| 65 | // Return the declaration context of the topmost class the inline method is |
| 66 | // declared in. |
| 67 | return DC; |
| 68 | } |
| 69 | |
Argyrios Kyrtzidis | 5239304 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 70 | if (isa<ObjCMethodDecl>(DC)) |
| 71 | return Context.getTranslationUnitDecl(); |
| 72 | |
| 73 | if (ScopedDecl *SD = dyn_cast<ScopedDecl>(DC)) |
| 74 | return SD->getLexicalDeclContext(); |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 75 | |
Argyrios Kyrtzidis | 77407b8 | 2008-11-19 18:01:13 +0000 | [diff] [blame] | 76 | return DC->getLexicalParent(); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 79 | void Sema::PushDeclContext(Scope *S, DeclContext *DC) { |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 80 | assert(getContainingDC(DC) == CurContext && |
Zhongxing Xu | e50897a | 2008-12-08 07:14:51 +0000 | [diff] [blame] | 81 | "The next DeclContext should be lexically contained in the current one."); |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 82 | CurContext = DC; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 83 | S->setEntity(DC); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 86 | void Sema::PopDeclContext() { |
| 87 | assert(CurContext && "DeclContext imbalance!"); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 88 | |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 89 | CurContext = getContainingDC(CurContext); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 92 | /// Add this decl to the scope shadowed decl chains. |
| 93 | void Sema::PushOnScopeChains(NamedDecl *D, Scope *S) { |
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 94 | S->AddDecl(D); |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 95 | |
| 96 | // C++ [basic.scope]p4: |
| 97 | // -- exactly one declaration shall declare a class name or |
| 98 | // enumeration name that is not a typedef name and the other |
| 99 | // declarations shall all refer to the same object or |
| 100 | // enumerator, or all refer to functions and function templates; |
| 101 | // in this case the class name or enumeration name is hidden. |
| 102 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) { |
| 103 | // We are pushing the name of a tag (enum or class). |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 104 | if (CurContext == TD->getDeclContext()) { |
| 105 | // We're pushing the tag into the current context, which might |
| 106 | // require some reshuffling in the identifier resolver. |
| 107 | IdentifierResolver::iterator |
| 108 | I = IdResolver.begin(TD->getIdentifier(), CurContext, |
| 109 | false/*LookInParentCtx*/); |
| 110 | if (I != IdResolver.end()) { |
| 111 | // There is already a declaration with the same name in the same |
| 112 | // scope. It must be found before we find the new declaration, |
| 113 | // so swap the order on the shadowed declaration chain. |
| 114 | IdResolver.AddShadowedDecl(TD, *I); |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 115 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 116 | // Add this declaration to the current context. |
| 117 | CurContext->addDecl(Context, TD); |
| 118 | |
| 119 | return; |
| 120 | } |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 121 | } |
Argyrios Kyrtzidis | f1af6a7 | 2008-10-22 23:08:24 +0000 | [diff] [blame] | 122 | } else if (getLangOptions().CPlusPlus && isa<FunctionDecl>(D)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 123 | // We are pushing the name of a function, which might be an |
| 124 | // overloaded name. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 125 | FunctionDecl *FD = cast<FunctionDecl>(D); |
| 126 | Decl *Prev = LookupDecl(FD->getDeclName(), Decl::IDNS_Ordinary, S, |
| 127 | FD->getDeclContext(), false, false); |
| 128 | if (Prev && (isa<OverloadedFunctionDecl>(Prev) || isa<FunctionDecl>(Prev))) { |
| 129 | // There is already a declaration with the same name in |
| 130 | // the same scope. It must be a function or an overloaded |
| 131 | // function. |
| 132 | OverloadedFunctionDecl* Ovl = dyn_cast<OverloadedFunctionDecl>(Prev); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 133 | if (!Ovl) { |
| 134 | // We haven't yet overloaded this function. Take the existing |
| 135 | // FunctionDecl and put it into an OverloadedFunctionDecl. |
| 136 | Ovl = OverloadedFunctionDecl::Create(Context, |
| 137 | FD->getDeclContext(), |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 138 | FD->getDeclName()); |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 139 | Ovl->addOverload(cast<FunctionDecl>(Prev)); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 140 | |
Douglas Gregor | 9e7d9de | 2008-12-15 21:24:18 +0000 | [diff] [blame] | 141 | // If there is a name binding for the existing FunctionDecl, |
| 142 | // remove it. |
| 143 | for (IdentifierResolver::iterator I |
| 144 | = IdResolver.begin(FD->getDeclName(), FD->getDeclContext(), |
| 145 | false/*LookInParentCtx*/), |
| 146 | E = IdResolver.end(); I != E; ++I) { |
| 147 | if (*I == Prev) { |
| 148 | IdResolver.RemoveDecl(*I); |
| 149 | S->RemoveDecl(*I); |
| 150 | break; |
| 151 | } |
| 152 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 153 | |
Douglas Gregor | 9e7d9de | 2008-12-15 21:24:18 +0000 | [diff] [blame] | 154 | // Add the name binding for the OverloadedFunctionDecl. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 155 | IdResolver.AddDecl(Ovl); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 156 | |
| 157 | // Update the context with the newly-created overloaded |
| 158 | // function set. |
| 159 | FD->getDeclContext()->insert(Context, Ovl); |
| 160 | |
| 161 | S->AddDecl(Ovl); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 164 | // We added this function declaration to the scope earlier, but |
| 165 | // we don't want it there because it is part of the overloaded |
| 166 | // function declaration. |
| 167 | S->RemoveDecl(FD); |
| 168 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 169 | // We have an OverloadedFunctionDecl. Add the new FunctionDecl |
| 170 | // to its list of overloads. |
| 171 | Ovl->addOverload(FD); |
| 172 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 173 | // Add this new function declaration to the declaration context. |
| 174 | CurContext->addDecl(Context, FD, false); |
| 175 | |
| 176 | return; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 177 | } |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 178 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 179 | |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 180 | // Add scoped declarations into their context, so that they can be |
| 181 | // found later. Declarations without a context won't be inserted |
| 182 | // into any context. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 183 | if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) |
| 184 | CurContext->addDecl(Context, SD); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 185 | |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 186 | IdResolver.AddDecl(D); |
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Steve Naroff | b216c88 | 2007-10-09 22:01:59 +0000 | [diff] [blame] | 189 | void Sema::ActOnPopScope(SourceLocation Loc, Scope *S) { |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 190 | if (S->decl_empty()) return; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 191 | assert((S->getFlags() & (Scope::DeclScope | Scope::TemplateParamScope)) && |
| 192 | "Scope shouldn't contain decls!"); |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 193 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 194 | for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end(); |
| 195 | I != E; ++I) { |
Steve Naroff | c752d04 | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 196 | Decl *TmpD = static_cast<Decl*>(*I); |
| 197 | assert(TmpD && "This decl didn't get pushed??"); |
Argyrios Kyrtzidis | 7643536 | 2008-06-10 01:32:09 +0000 | [diff] [blame] | 198 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 199 | assert(isa<NamedDecl>(TmpD) && "Decl isn't NamedDecl?"); |
| 200 | NamedDecl *D = cast<NamedDecl>(TmpD); |
Argyrios Kyrtzidis | 7643536 | 2008-06-10 01:32:09 +0000 | [diff] [blame] | 201 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 202 | if (!D->getDeclName()) continue; |
Chris Lattner | 7f925cc | 2008-04-11 07:00:53 +0000 | [diff] [blame] | 203 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 204 | // Remove this name from our lexical scope. |
| 205 | IdResolver.RemoveDecl(D); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
Steve Naroff | e8043c3 | 2008-04-01 23:04:06 +0000 | [diff] [blame] | 209 | /// getObjCInterfaceDecl - Look up a for a class declaration in the scope. |
| 210 | /// return 0 if one not found. |
Steve Naroff | e8043c3 | 2008-04-01 23:04:06 +0000 | [diff] [blame] | 211 | ObjCInterfaceDecl *Sema::getObjCInterfaceDecl(IdentifierInfo *Id) { |
Steve Naroff | 3110251 | 2008-04-02 18:30:49 +0000 | [diff] [blame] | 212 | // The third "scope" argument is 0 since we aren't enabling lazy built-in |
| 213 | // creation from this context. |
| 214 | Decl *IDecl = LookupDecl(Id, Decl::IDNS_Ordinary, 0, false); |
Fariborz Jahanian | 4cabdfc | 2007-10-12 19:38:20 +0000 | [diff] [blame] | 215 | |
Steve Naroff | b327ce0 | 2008-04-02 14:35:35 +0000 | [diff] [blame] | 216 | return dyn_cast_or_null<ObjCInterfaceDecl>(IDecl); |
Fariborz Jahanian | 4cabdfc | 2007-10-12 19:38:20 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Steve Naroff | e8043c3 | 2008-04-01 23:04:06 +0000 | [diff] [blame] | 219 | /// LookupDecl - Look up the inner-most declaration in the specified |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 220 | /// namespace. |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 221 | Decl *Sema::LookupDecl(DeclarationName Name, unsigned NSI, Scope *S, |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 222 | const DeclContext *LookupCtx, |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 223 | bool enableLazyBuiltinCreation, |
Douglas Gregor | 0a59acb | 2008-12-16 00:38:16 +0000 | [diff] [blame] | 224 | bool LookInParent) { |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 225 | if (!Name) return 0; |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 226 | unsigned NS = NSI; |
| 227 | if (getLangOptions().CPlusPlus && (NS & Decl::IDNS_Ordinary)) |
| 228 | NS |= Decl::IDNS_Tag; |
Chris Lattner | 7f925cc | 2008-04-11 07:00:53 +0000 | [diff] [blame] | 229 | |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 230 | if (LookupCtx == 0 && |
| 231 | (!getLangOptions().CPlusPlus || (NS == Decl::IDNS_Label))) { |
| 232 | // Unqualified name lookup in C/Objective-C and name lookup for |
| 233 | // labels in C++ is purely lexical, so search in the |
| 234 | // declarations attached to the name. |
| 235 | assert(!LookupCtx && "Can't perform qualified name lookup here"); |
| 236 | IdentifierResolver::iterator I |
| 237 | = IdResolver.begin(Name, CurContext, LookInParent); |
| 238 | |
| 239 | // Scan up the scope chain looking for a decl that matches this |
| 240 | // identifier that is in the appropriate namespace. This search |
| 241 | // should not take long, as shadowing of names is uncommon, and |
| 242 | // deep shadowing is extremely uncommon. |
| 243 | for (; I != IdResolver.end(); ++I) |
| 244 | if ((*I)->getIdentifierNamespace() & NS) |
| 245 | return *I; |
| 246 | } else if (LookupCtx) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 247 | assert(getLangOptions().CPlusPlus && "No qualified name lookup in C"); |
| 248 | |
| 249 | // Perform qualified name lookup into the LookupCtx. |
| 250 | // FIXME: Will need to look into base classes and such. |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 251 | DeclContext::lookup_const_iterator I, E; |
| 252 | for (llvm::tie(I, E) = LookupCtx->lookup(Context, Name); I != E; ++I) |
| 253 | if ((*I)->getIdentifierNamespace() & NS) |
| 254 | return *I; |
| 255 | } else { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 256 | // Name lookup for ordinary names and tag names in C++ requires |
| 257 | // looking into scopes that aren't strictly lexical, and |
| 258 | // therefore we walk through the context as well as walking |
| 259 | // through the scopes. |
| 260 | IdentifierResolver::iterator |
| 261 | I = IdResolver.begin(Name, CurContext, true/*LookInParentCtx*/), |
| 262 | IEnd = IdResolver.end(); |
| 263 | for (; S; S = S->getParent()) { |
| 264 | // Check whether the IdResolver has anything in this scope. |
| 265 | // FIXME: The isDeclScope check could be expensive. Can we do better? |
| 266 | for (; I != IEnd && S->isDeclScope(*I); ++I) |
| 267 | if ((*I)->getIdentifierNamespace() & NS) |
| 268 | return *I; |
| 269 | |
| 270 | // If there is an entity associated with this scope, it's a |
| 271 | // DeclContext. We might need to perform qualified lookup into |
| 272 | // it. |
| 273 | DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); |
| 274 | while (Ctx && Ctx->isFunctionOrMethod()) |
| 275 | Ctx = Ctx->getParent(); |
| 276 | while (Ctx && (Ctx->isNamespace() || Ctx->isCXXRecord())) { |
| 277 | // Look for declarations of this name in this scope. |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 278 | DeclContext::lookup_const_iterator I, E; |
| 279 | for (llvm::tie(I, E) = Ctx->lookup(Context, Name); I != E; ++I) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 280 | // FIXME: Cache this result in the IdResolver |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 281 | if ((*I)->getIdentifierNamespace() & NS) |
| 282 | return *I; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | Ctx = Ctx->getParent(); |
| 286 | } |
| 287 | |
| 288 | if (!LookInParent) |
| 289 | return 0; |
| 290 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 291 | } |
Chris Lattner | 7f925cc | 2008-04-11 07:00:53 +0000 | [diff] [blame] | 292 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 293 | // If we didn't find a use of this identifier, and if the identifier |
| 294 | // corresponds to a compiler builtin, create the decl object for the builtin |
| 295 | // now, injecting it into translation unit scope, and return it. |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 296 | if (NS & Decl::IDNS_Ordinary) { |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 297 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
| 298 | if (enableLazyBuiltinCreation && II && |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 299 | (LookupCtx == 0 || isa<TranslationUnitDecl>(LookupCtx))) { |
Steve Naroff | b327ce0 | 2008-04-02 14:35:35 +0000 | [diff] [blame] | 300 | // If this is a builtin on this (or all) targets, create the decl. |
| 301 | if (unsigned BuiltinID = II->getBuiltinID()) |
| 302 | return LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID, S); |
| 303 | } |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 304 | if (getLangOptions().ObjC1 && II) { |
Steve Naroff | e8043c3 | 2008-04-01 23:04:06 +0000 | [diff] [blame] | 305 | // @interface and @compatibility_alias introduce typedef-like names. |
| 306 | // 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] | 307 | // therefore not scoped decls). They can, however, be shadowed by |
Steve Naroff | e8043c3 | 2008-04-01 23:04:06 +0000 | [diff] [blame] | 308 | // other names in IDNS_Ordinary. |
Steve Naroff | 3110251 | 2008-04-02 18:30:49 +0000 | [diff] [blame] | 309 | ObjCInterfaceDeclsTy::iterator IDI = ObjCInterfaceDecls.find(II); |
| 310 | if (IDI != ObjCInterfaceDecls.end()) |
| 311 | return IDI->second; |
Steve Naroff | e8043c3 | 2008-04-01 23:04:06 +0000 | [diff] [blame] | 312 | ObjCAliasTy::iterator I = ObjCAliasDecls.find(II); |
| 313 | if (I != ObjCAliasDecls.end()) |
| 314 | return I->second->getClassInterface(); |
| 315 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 316 | } |
| 317 | return 0; |
| 318 | } |
| 319 | |
Chris Lattner | 95e2c71 | 2008-05-05 22:18:14 +0000 | [diff] [blame] | 320 | void Sema::InitBuiltinVaListType() { |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 321 | if (!Context.getBuiltinVaListType().isNull()) |
| 322 | return; |
| 323 | |
| 324 | IdentifierInfo *VaIdent = &Context.Idents.get("__builtin_va_list"); |
Steve Naroff | b327ce0 | 2008-04-02 14:35:35 +0000 | [diff] [blame] | 325 | Decl *VaDecl = LookupDecl(VaIdent, Decl::IDNS_Ordinary, TUScope); |
Steve Naroff | 733002f | 2007-10-18 22:17:45 +0000 | [diff] [blame] | 326 | TypedefDecl *VaTypedef = cast<TypedefDecl>(VaDecl); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 327 | Context.setBuiltinVaListType(Context.getTypedefType(VaTypedef)); |
| 328 | } |
| 329 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 330 | /// LazilyCreateBuiltin - The specified Builtin-ID was first used at file scope. |
| 331 | /// lazily create a decl for it. |
Chris Lattner | 22b73ba | 2007-10-10 23:42:28 +0000 | [diff] [blame] | 332 | ScopedDecl *Sema::LazilyCreateBuiltin(IdentifierInfo *II, unsigned bid, |
| 333 | Scope *S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 334 | Builtin::ID BID = (Builtin::ID)bid; |
| 335 | |
Chris Lattner | bd7eb1c | 2008-09-28 05:54:29 +0000 | [diff] [blame] | 336 | if (Context.BuiltinInfo.hasVAListUse(BID)) |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 337 | InitBuiltinVaListType(); |
| 338 | |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 339 | QualType R = Context.BuiltinInfo.GetBuiltinType(BID, Context); |
Argyrios Kyrtzidis | ff898cd | 2008-04-17 14:47:13 +0000 | [diff] [blame] | 340 | FunctionDecl *New = FunctionDecl::Create(Context, |
| 341 | Context.getTranslationUnitDecl(), |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 342 | SourceLocation(), II, R, |
Chris Lattner | a98e58d | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 343 | FunctionDecl::Extern, false, 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 344 | |
Chris Lattner | 95e2c71 | 2008-05-05 22:18:14 +0000 | [diff] [blame] | 345 | // Create Decl objects for each parameter, adding them to the |
| 346 | // FunctionDecl. |
| 347 | if (FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(R)) { |
| 348 | llvm::SmallVector<ParmVarDecl*, 16> Params; |
| 349 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) |
| 350 | Params.push_back(ParmVarDecl::Create(Context, New, SourceLocation(), 0, |
| 351 | FT->getArgType(i), VarDecl::None, 0, |
| 352 | 0)); |
| 353 | New->setParams(&Params[0], Params.size()); |
| 354 | } |
| 355 | |
| 356 | |
| 357 | |
Chris Lattner | 7f925cc | 2008-04-11 07:00:53 +0000 | [diff] [blame] | 358 | // TUScope is the translation-unit scope to insert this function into. |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 359 | PushOnScopeChains(New, TUScope); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 360 | return New; |
| 361 | } |
| 362 | |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 363 | /// GetStdNamespace - This method gets the C++ "std" namespace. This is where |
| 364 | /// everything from the standard library is defined. |
| 365 | NamespaceDecl *Sema::GetStdNamespace() { |
| 366 | if (!StdNamespace) { |
Chris Lattner | 8edea83 | 2008-11-20 05:45:14 +0000 | [diff] [blame] | 367 | IdentifierInfo *StdIdent = &PP.getIdentifierTable().get("std"); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 368 | DeclContext *Global = Context.getTranslationUnitDecl(); |
Chris Lattner | 8edea83 | 2008-11-20 05:45:14 +0000 | [diff] [blame] | 369 | Decl *Std = LookupDecl(StdIdent, Decl::IDNS_Tag | Decl::IDNS_Ordinary, |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 370 | 0, Global, /*enableLazyBuiltinCreation=*/false); |
| 371 | StdNamespace = dyn_cast_or_null<NamespaceDecl>(Std); |
| 372 | } |
| 373 | return StdNamespace; |
| 374 | } |
| 375 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 376 | /// MergeTypeDefDecl - We just parsed a typedef 'New' which has the same name |
| 377 | /// and scope as a previous declaration 'Old'. Figure out how to resolve this |
| 378 | /// situation, merging decls or emitting diagnostics as appropriate. |
| 379 | /// |
Steve Naroff | e8043c3 | 2008-04-01 23:04:06 +0000 | [diff] [blame] | 380 | TypedefDecl *Sema::MergeTypeDefDecl(TypedefDecl *New, Decl *OldD) { |
Steve Naroff | 2b255c4 | 2008-09-09 14:32:20 +0000 | [diff] [blame] | 381 | // Allow multiple definitions for ObjC built-in typedefs. |
| 382 | // FIXME: Verify the underlying types are equivalent! |
| 383 | if (getLangOptions().ObjC1) { |
Chris Lattner | 2bac0f6 | 2008-11-20 05:41:43 +0000 | [diff] [blame] | 384 | const IdentifierInfo *TypeID = New->getIdentifier(); |
| 385 | switch (TypeID->getLength()) { |
| 386 | default: break; |
| 387 | case 2: |
| 388 | if (!TypeID->isStr("id")) |
| 389 | break; |
Steve Naroff | 2b255c4 | 2008-09-09 14:32:20 +0000 | [diff] [blame] | 390 | Context.setObjCIdType(New); |
| 391 | return New; |
Chris Lattner | 2bac0f6 | 2008-11-20 05:41:43 +0000 | [diff] [blame] | 392 | case 5: |
| 393 | if (!TypeID->isStr("Class")) |
| 394 | break; |
Steve Naroff | 2b255c4 | 2008-09-09 14:32:20 +0000 | [diff] [blame] | 395 | Context.setObjCClassType(New); |
| 396 | return New; |
Chris Lattner | 2bac0f6 | 2008-11-20 05:41:43 +0000 | [diff] [blame] | 397 | case 3: |
| 398 | if (!TypeID->isStr("SEL")) |
| 399 | break; |
Steve Naroff | 2b255c4 | 2008-09-09 14:32:20 +0000 | [diff] [blame] | 400 | Context.setObjCSelType(New); |
| 401 | return New; |
Chris Lattner | 2bac0f6 | 2008-11-20 05:41:43 +0000 | [diff] [blame] | 402 | case 8: |
| 403 | if (!TypeID->isStr("Protocol")) |
| 404 | break; |
Steve Naroff | 2b255c4 | 2008-09-09 14:32:20 +0000 | [diff] [blame] | 405 | Context.setObjCProtoType(New->getUnderlyingType()); |
| 406 | return New; |
| 407 | } |
| 408 | // Fall through - the typedef name was not a builtin type. |
| 409 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 410 | // Verify the old decl was also a typedef. |
| 411 | TypedefDecl *Old = dyn_cast<TypedefDecl>(OldD); |
| 412 | if (!Old) { |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 413 | Diag(New->getLocation(), diag::err_redefinition_different_kind) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 414 | << New->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 415 | Diag(OldD->getLocation(), diag::note_previous_definition); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 416 | return New; |
| 417 | } |
| 418 | |
Chris Lattner | 99cb997 | 2008-07-25 18:44:27 +0000 | [diff] [blame] | 419 | // If the typedef types are not identical, reject them in all languages and |
| 420 | // with any extensions enabled. |
| 421 | if (Old->getUnderlyingType() != New->getUnderlyingType() && |
| 422 | Context.getCanonicalType(Old->getUnderlyingType()) != |
| 423 | Context.getCanonicalType(New->getUnderlyingType())) { |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 424 | Diag(New->getLocation(), diag::err_redefinition_different_typedef) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 425 | << New->getUnderlyingType() << Old->getUnderlyingType(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 426 | Diag(Old->getLocation(), diag::note_previous_definition); |
Chris Lattner | 99cb997 | 2008-07-25 18:44:27 +0000 | [diff] [blame] | 427 | return Old; |
| 428 | } |
| 429 | |
Eli Friedman | 54ecfce | 2008-06-11 06:20:39 +0000 | [diff] [blame] | 430 | if (getLangOptions().Microsoft) return New; |
| 431 | |
Douglas Gregor | bbe2743 | 2008-11-21 16:29:06 +0000 | [diff] [blame] | 432 | // C++ [dcl.typedef]p2: |
| 433 | // In a given non-class scope, a typedef specifier can be used to |
| 434 | // redefine the name of any type declared in that scope to refer |
| 435 | // to the type to which it already refers. |
| 436 | if (getLangOptions().CPlusPlus && !isa<CXXRecordDecl>(CurContext)) |
| 437 | return New; |
| 438 | |
| 439 | // In C, redeclaration of a type is a constraint violation (6.7.2.3p1). |
Steve Naroff | 4c49a6c | 2008-01-30 23:46:05 +0000 | [diff] [blame] | 440 | // Apparently GCC, Intel, and Sun all silently ignore the redeclaration if |
| 441 | // *either* declaration is in a system header. The code below implements |
| 442 | // this adhoc compatibility rule. FIXME: The following code will not |
| 443 | // work properly when compiling ".i" files (containing preprocessed output). |
Daniel Dunbar | 2fe0997 | 2008-09-12 18:10:20 +0000 | [diff] [blame] | 444 | if (PP.getDiagnostics().getSuppressSystemWarnings()) { |
| 445 | SourceManager &SrcMgr = Context.getSourceManager(); |
| 446 | if (SrcMgr.isInSystemHeader(Old->getLocation())) |
| 447 | return New; |
| 448 | if (SrcMgr.isInSystemHeader(New->getLocation())) |
| 449 | return New; |
| 450 | } |
Eli Friedman | 54ecfce | 2008-06-11 06:20:39 +0000 | [diff] [blame] | 451 | |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 452 | Diag(New->getLocation(), diag::err_redefinition) << New->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 453 | Diag(Old->getLocation(), diag::note_previous_definition); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 454 | return New; |
| 455 | } |
| 456 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 457 | /// DeclhasAttr - returns true if decl Declaration already has the target |
| 458 | /// attribute. |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 459 | static bool DeclHasAttr(const Decl *decl, const Attr *target) { |
| 460 | for (const Attr *attr = decl->getAttrs(); attr; attr = attr->getNext()) |
| 461 | if (attr->getKind() == target->getKind()) |
| 462 | return true; |
| 463 | |
| 464 | return false; |
| 465 | } |
| 466 | |
| 467 | /// MergeAttributes - append attributes from the Old decl to the New one. |
| 468 | static void MergeAttributes(Decl *New, Decl *Old) { |
| 469 | Attr *attr = const_cast<Attr*>(Old->getAttrs()), *tmp; |
| 470 | |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 471 | while (attr) { |
| 472 | tmp = attr; |
| 473 | attr = attr->getNext(); |
| 474 | |
| 475 | if (!DeclHasAttr(New, tmp)) { |
| 476 | New->addAttr(tmp); |
| 477 | } else { |
| 478 | tmp->setNext(0); |
| 479 | delete(tmp); |
| 480 | } |
| 481 | } |
Nuno Lopes | 9141bee | 2008-06-01 22:53:53 +0000 | [diff] [blame] | 482 | |
| 483 | Old->invalidateAttrs(); |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 484 | } |
| 485 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 486 | /// MergeFunctionDecl - We just parsed a function 'New' from |
| 487 | /// declarator D which has the same name and scope as a previous |
| 488 | /// declaration 'Old'. Figure out how to resolve this situation, |
| 489 | /// merging decls or emitting diagnostics as appropriate. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 490 | /// Redeclaration will be set true if this New is a redeclaration OldD. |
| 491 | /// |
| 492 | /// In C++, New and Old must be declarations that are not |
| 493 | /// overloaded. Use IsOverload to determine whether New and Old are |
| 494 | /// overloaded, and to select the Old declaration that New should be |
| 495 | /// merged with. |
Douglas Gregor | f009795 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 496 | FunctionDecl * |
| 497 | Sema::MergeFunctionDecl(FunctionDecl *New, Decl *OldD, bool &Redeclaration) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 498 | assert(!isa<OverloadedFunctionDecl>(OldD) && |
| 499 | "Cannot merge with an overloaded function declaration"); |
| 500 | |
Douglas Gregor | f009795 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 501 | Redeclaration = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 502 | // Verify the old decl was also a function. |
| 503 | FunctionDecl *Old = dyn_cast<FunctionDecl>(OldD); |
| 504 | if (!Old) { |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 505 | Diag(New->getLocation(), diag::err_redefinition_different_kind) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 506 | << New->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 507 | Diag(OldD->getLocation(), diag::note_previous_definition); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 508 | return New; |
| 509 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 510 | |
| 511 | // Determine whether the previous declaration was a definition, |
| 512 | // implicit declaration, or a declaration. |
| 513 | diag::kind PrevDiag; |
| 514 | if (Old->isThisDeclarationADefinition()) |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 515 | PrevDiag = diag::note_previous_definition; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 516 | else if (Old->isImplicit()) |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 517 | PrevDiag = diag::note_previous_implicit_declaration; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 518 | else |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 519 | PrevDiag = diag::note_previous_declaration; |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 520 | |
Chris Lattner | 8bcfc5b | 2008-04-06 23:10:54 +0000 | [diff] [blame] | 521 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
| 522 | QualType NewQType = Context.getCanonicalType(New->getType()); |
Chris Lattner | 5519644 | 2007-11-20 19:04:50 +0000 | [diff] [blame] | 523 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 524 | if (getLangOptions().CPlusPlus) { |
| 525 | // (C++98 13.1p2): |
| 526 | // Certain function declarations cannot be overloaded: |
| 527 | // -- Function declarations that differ only in the return type |
| 528 | // cannot be overloaded. |
| 529 | QualType OldReturnType |
| 530 | = cast<FunctionType>(OldQType.getTypePtr())->getResultType(); |
| 531 | QualType NewReturnType |
| 532 | = cast<FunctionType>(NewQType.getTypePtr())->getResultType(); |
| 533 | if (OldReturnType != NewReturnType) { |
| 534 | Diag(New->getLocation(), diag::err_ovl_diff_return_type); |
| 535 | Diag(Old->getLocation(), PrevDiag); |
| 536 | return New; |
| 537 | } |
| 538 | |
| 539 | const CXXMethodDecl* OldMethod = dyn_cast<CXXMethodDecl>(Old); |
| 540 | const CXXMethodDecl* NewMethod = dyn_cast<CXXMethodDecl>(New); |
| 541 | if (OldMethod && NewMethod) { |
| 542 | // -- Member function declarations with the same name and the |
| 543 | // same parameter types cannot be overloaded if any of them |
| 544 | // is a static member function declaration. |
| 545 | if (OldMethod->isStatic() || NewMethod->isStatic()) { |
| 546 | Diag(New->getLocation(), diag::err_ovl_static_nonstatic_member); |
| 547 | Diag(Old->getLocation(), PrevDiag); |
| 548 | return New; |
| 549 | } |
Douglas Gregor | 9e7d9de | 2008-12-15 21:24:18 +0000 | [diff] [blame] | 550 | |
| 551 | // C++ [class.mem]p1: |
| 552 | // [...] A member shall not be declared twice in the |
| 553 | // member-specification, except that a nested class or member |
| 554 | // class template can be declared and then later defined. |
| 555 | if (OldMethod->getLexicalDeclContext() == |
| 556 | NewMethod->getLexicalDeclContext()) { |
| 557 | unsigned NewDiag; |
| 558 | if (isa<CXXConstructorDecl>(OldMethod)) |
| 559 | NewDiag = diag::err_constructor_redeclared; |
| 560 | else if (isa<CXXDestructorDecl>(NewMethod)) |
| 561 | NewDiag = diag::err_destructor_redeclared; |
| 562 | else if (isa<CXXConversionDecl>(NewMethod)) |
| 563 | NewDiag = diag::err_conv_function_redeclared; |
| 564 | else |
| 565 | NewDiag = diag::err_member_redeclared; |
| 566 | |
| 567 | Diag(New->getLocation(), NewDiag); |
| 568 | Diag(Old->getLocation(), PrevDiag); |
| 569 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | // (C++98 8.3.5p3): |
| 573 | // All declarations for a function shall agree exactly in both the |
| 574 | // return type and the parameter-type-list. |
| 575 | if (OldQType == NewQType) { |
| 576 | // We have a redeclaration. |
| 577 | MergeAttributes(New, Old); |
| 578 | Redeclaration = true; |
| 579 | return MergeCXXFunctionDecl(New, Old); |
| 580 | } |
| 581 | |
| 582 | // Fall through for conflicting redeclarations and redefinitions. |
Douglas Gregor | f009795 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 583 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 584 | |
| 585 | // C: Function types need to be compatible, not identical. This handles |
Steve Naroff | adbbd0c | 2008-01-14 20:51:29 +0000 | [diff] [blame] | 586 | // duplicate function decls like "void f(int); void f(enum X);" properly. |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 587 | if (!getLangOptions().CPlusPlus && |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 588 | Context.typesAreCompatible(OldQType, NewQType)) { |
Douglas Gregor | f009795 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 589 | MergeAttributes(New, Old); |
| 590 | Redeclaration = true; |
Steve Naroff | adbbd0c | 2008-01-14 20:51:29 +0000 | [diff] [blame] | 591 | return New; |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 592 | } |
Chris Lattner | e3995fe | 2007-11-06 06:07:26 +0000 | [diff] [blame] | 593 | |
Steve Naroff | 837618c | 2008-01-16 15:01:34 +0000 | [diff] [blame] | 594 | // A function that has already been declared has been redeclared or defined |
| 595 | // with a different type- show appropriate diagnostic |
Steve Naroff | 837618c | 2008-01-16 15:01:34 +0000 | [diff] [blame] | 596 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 597 | // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope. |
| 598 | // TODO: This is totally simplistic. It should handle merging functions |
| 599 | // together etc, merging extern int X; int X; ... |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 600 | Diag(New->getLocation(), diag::err_conflicting_types) << New->getDeclName(); |
Steve Naroff | 837618c | 2008-01-16 15:01:34 +0000 | [diff] [blame] | 601 | Diag(Old->getLocation(), PrevDiag); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 602 | return New; |
| 603 | } |
| 604 | |
Steve Naroff | ff9eb1f | 2008-08-08 17:50:35 +0000 | [diff] [blame] | 605 | /// Predicate for C "tentative" external object definitions (C99 6.9.2). |
Steve Naroff | d4d46cd | 2008-08-10 15:28:06 +0000 | [diff] [blame] | 606 | static bool isTentativeDefinition(VarDecl *VD) { |
Steve Naroff | ff9eb1f | 2008-08-08 17:50:35 +0000 | [diff] [blame] | 607 | if (VD->isFileVarDecl()) |
| 608 | return (!VD->getInit() && |
| 609 | (VD->getStorageClass() == VarDecl::None || |
| 610 | VD->getStorageClass() == VarDecl::Static)); |
| 611 | return false; |
| 612 | } |
| 613 | |
| 614 | /// CheckForFileScopedRedefinitions - Make sure we forgo redefinition errors |
| 615 | /// when dealing with C "tentative" external object definitions (C99 6.9.2). |
| 616 | void Sema::CheckForFileScopedRedefinitions(Scope *S, VarDecl *VD) { |
| 617 | bool VDIsTentative = isTentativeDefinition(VD); |
Steve Naroff | f855e6f | 2008-08-10 15:20:13 +0000 | [diff] [blame] | 618 | bool VDIsIncompleteArray = VD->getType()->isIncompleteArrayType(); |
Steve Naroff | ff9eb1f | 2008-08-08 17:50:35 +0000 | [diff] [blame] | 619 | |
| 620 | for (IdentifierResolver::iterator |
| 621 | I = IdResolver.begin(VD->getIdentifier(), |
| 622 | VD->getDeclContext(), false/*LookInParentCtx*/), |
| 623 | E = IdResolver.end(); I != E; ++I) { |
Argyrios Kyrtzidis | 15a12d0 | 2008-09-09 21:18:04 +0000 | [diff] [blame] | 624 | if (*I != VD && isDeclInScope(*I, VD->getDeclContext(), S)) { |
Steve Naroff | ff9eb1f | 2008-08-08 17:50:35 +0000 | [diff] [blame] | 625 | VarDecl *OldDecl = dyn_cast<VarDecl>(*I); |
| 626 | |
Steve Naroff | f855e6f | 2008-08-10 15:20:13 +0000 | [diff] [blame] | 627 | // Handle the following case: |
| 628 | // int a[10]; |
| 629 | // int a[]; - the code below makes sure we set the correct type. |
| 630 | // int a[11]; - this is an error, size isn't 10. |
| 631 | if (OldDecl && VDIsTentative && VDIsIncompleteArray && |
| 632 | OldDecl->getType()->isConstantArrayType()) |
| 633 | VD->setType(OldDecl->getType()); |
| 634 | |
Steve Naroff | ff9eb1f | 2008-08-08 17:50:35 +0000 | [diff] [blame] | 635 | // Check for "tentative" definitions. We can't accomplish this in |
| 636 | // MergeVarDecl since the initializer hasn't been attached. |
| 637 | if (!OldDecl || isTentativeDefinition(OldDecl) || VDIsTentative) |
| 638 | continue; |
| 639 | |
| 640 | // Handle __private_extern__ just like extern. |
| 641 | if (OldDecl->getStorageClass() != VarDecl::Extern && |
| 642 | OldDecl->getStorageClass() != VarDecl::PrivateExtern && |
| 643 | VD->getStorageClass() != VarDecl::Extern && |
| 644 | VD->getStorageClass() != VarDecl::PrivateExtern) { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 645 | Diag(VD->getLocation(), diag::err_redefinition) << VD->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 646 | Diag(OldDecl->getLocation(), diag::note_previous_definition); |
Steve Naroff | ff9eb1f | 2008-08-08 17:50:35 +0000 | [diff] [blame] | 647 | } |
| 648 | } |
| 649 | } |
| 650 | } |
| 651 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 652 | /// MergeVarDecl - We just parsed a variable 'New' which has the same name |
| 653 | /// and scope as a previous declaration 'Old'. Figure out how to resolve this |
| 654 | /// situation, merging decls or emitting diagnostics as appropriate. |
| 655 | /// |
Steve Naroff | ff9eb1f | 2008-08-08 17:50:35 +0000 | [diff] [blame] | 656 | /// Tentative definition rules (C99 6.9.2p2) are checked by |
| 657 | /// FinalizeDeclaratorGroup. Unfortunately, we can't analyze tentative |
| 658 | /// definitions here, since the initializer hasn't been attached. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 659 | /// |
Steve Naroff | e8043c3 | 2008-04-01 23:04:06 +0000 | [diff] [blame] | 660 | VarDecl *Sema::MergeVarDecl(VarDecl *New, Decl *OldD) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 661 | // Verify the old decl was also a variable. |
| 662 | VarDecl *Old = dyn_cast<VarDecl>(OldD); |
| 663 | if (!Old) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 664 | Diag(New->getLocation(), diag::err_redefinition_different_kind) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 665 | << New->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 666 | Diag(OldD->getLocation(), diag::note_previous_definition); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 667 | return New; |
| 668 | } |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 669 | |
| 670 | MergeAttributes(New, Old); |
| 671 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 672 | // Verify the types match. |
Chris Lattner | 8bcfc5b | 2008-04-06 23:10:54 +0000 | [diff] [blame] | 673 | QualType OldCType = Context.getCanonicalType(Old->getType()); |
| 674 | QualType NewCType = Context.getCanonicalType(New->getType()); |
Steve Naroff | 907747b | 2008-08-09 16:04:40 +0000 | [diff] [blame] | 675 | if (OldCType != NewCType && !Context.typesAreCompatible(OldCType, NewCType)) { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 676 | Diag(New->getLocation(), diag::err_redefinition) << New->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 677 | Diag(Old->getLocation(), diag::note_previous_definition); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 678 | return New; |
| 679 | } |
Steve Naroff | b7b032e | 2008-01-30 00:44:01 +0000 | [diff] [blame] | 680 | // C99 6.2.2p4: Check if we have a static decl followed by a non-static. |
| 681 | if (New->getStorageClass() == VarDecl::Static && |
| 682 | (Old->getStorageClass() == VarDecl::None || |
| 683 | Old->getStorageClass() == VarDecl::Extern)) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 684 | Diag(New->getLocation(), diag::err_static_non_static) << New->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 685 | Diag(Old->getLocation(), diag::note_previous_definition); |
Steve Naroff | b7b032e | 2008-01-30 00:44:01 +0000 | [diff] [blame] | 686 | return New; |
| 687 | } |
| 688 | // C99 6.2.2p4: Check if we have a non-static decl followed by a static. |
| 689 | if (New->getStorageClass() != VarDecl::Static && |
| 690 | Old->getStorageClass() == VarDecl::Static) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 691 | Diag(New->getLocation(), diag::err_non_static_static) << New->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 692 | Diag(Old->getLocation(), diag::note_previous_definition); |
Steve Naroff | b7b032e | 2008-01-30 00:44:01 +0000 | [diff] [blame] | 693 | return New; |
| 694 | } |
Steve Naroff | 094cefb | 2008-09-17 14:05:40 +0000 | [diff] [blame] | 695 | // Variables with external linkage are analyzed in FinalizeDeclaratorGroup. |
| 696 | if (New->getStorageClass() != VarDecl::Extern && !New->isFileVarDecl()) { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 697 | Diag(New->getLocation(), diag::err_redefinition) << New->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 698 | Diag(Old->getLocation(), diag::note_previous_definition); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 699 | } |
| 700 | return New; |
| 701 | } |
| 702 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 703 | /// CheckParmsForFunctionDef - Check that the parameters of the given |
| 704 | /// function are appropriate for the definition of a function. This |
| 705 | /// takes care of any checks that cannot be performed on the |
| 706 | /// declaration itself, e.g., that the types of each of the function |
| 707 | /// parameters are complete. |
| 708 | bool Sema::CheckParmsForFunctionDef(FunctionDecl *FD) { |
| 709 | bool HasInvalidParm = false; |
| 710 | for (unsigned p = 0, NumParams = FD->getNumParams(); p < NumParams; ++p) { |
| 711 | ParmVarDecl *Param = FD->getParamDecl(p); |
| 712 | |
| 713 | // C99 6.7.5.3p4: the parameters in a parameter type list in a |
| 714 | // function declarator that is part of a function definition of |
| 715 | // that function shall not have incomplete type. |
| 716 | if (Param->getType()->isIncompleteType() && |
| 717 | !Param->isInvalidDecl()) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 718 | Diag(Param->getLocation(), diag::err_typecheck_decl_incomplete_type) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 719 | << Param->getType(); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 720 | Param->setInvalidDecl(); |
| 721 | HasInvalidParm = true; |
| 722 | } |
Chris Lattner | 777f07b | 2008-12-17 07:32:46 +0000 | [diff] [blame] | 723 | |
| 724 | // C99 6.9.1p5: If the declarator includes a parameter type list, the |
| 725 | // declaration of each parameter shall include an identifier. |
| 726 | if (Param->getIdentifier() == 0 && !getLangOptions().CPlusPlus) |
| 727 | Diag(Param->getLocation(), diag::err_parameter_name_omitted); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | return HasInvalidParm; |
| 731 | } |
| 732 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 733 | /// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with |
| 734 | /// no declarator (e.g. "struct foo;") is parsed. |
| 735 | Sema::DeclTy *Sema::ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS) { |
| 736 | // TODO: emit error on 'int;' or 'const enum foo;'. |
| 737 | // TODO: emit error on 'typedef int;' |
| 738 | // if (!DS.isMissingDeclaratorOk()) Diag(...); |
| 739 | |
Steve Naroff | 9219928 | 2007-11-17 21:37:36 +0000 | [diff] [blame] | 740 | return dyn_cast_or_null<TagDecl>(static_cast<Decl *>(DS.getTypeRep())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 741 | } |
| 742 | |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 743 | bool Sema::CheckSingleInitializer(Expr *&Init, QualType DeclType) { |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 744 | // Get the type before calling CheckSingleAssignmentConstraints(), since |
| 745 | // it can promote the expression. |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 746 | QualType InitType = Init->getType(); |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 747 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 748 | AssignConvertType ConvTy = CheckSingleAssignmentConstraints(DeclType, Init); |
| 749 | return DiagnoseAssignmentResult(ConvTy, Init->getLocStart(), DeclType, |
| 750 | InitType, Init, "initializing"); |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 751 | } |
| 752 | |
Steve Naroff | a49e1fa | 2008-01-22 00:55:40 +0000 | [diff] [blame] | 753 | bool Sema::CheckStringLiteralInit(StringLiteral *strLiteral, QualType &DeclT) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 754 | const ArrayType *AT = Context.getAsArrayType(DeclT); |
| 755 | |
| 756 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
Steve Naroff | a49e1fa | 2008-01-22 00:55:40 +0000 | [diff] [blame] | 757 | // C99 6.7.8p14. We have an array of character type with unknown size |
| 758 | // being initialized to a string literal. |
| 759 | llvm::APSInt ConstVal(32); |
| 760 | ConstVal = strLiteral->getByteLength() + 1; |
| 761 | // Return a new array type (C99 6.7.8p22). |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 762 | DeclT = Context.getConstantArrayType(IAT->getElementType(), ConstVal, |
Steve Naroff | a49e1fa | 2008-01-22 00:55:40 +0000 | [diff] [blame] | 763 | ArrayType::Normal, 0); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 764 | } else { |
| 765 | const ConstantArrayType *CAT = cast<ConstantArrayType>(AT); |
Steve Naroff | a49e1fa | 2008-01-22 00:55:40 +0000 | [diff] [blame] | 766 | // C99 6.7.8p14. We have an array of character type with known size. |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 767 | // FIXME: Avoid truncation for 64-bit length strings. |
| 768 | if (strLiteral->getByteLength() > (unsigned)CAT->getSize().getZExtValue()) |
Steve Naroff | a49e1fa | 2008-01-22 00:55:40 +0000 | [diff] [blame] | 769 | Diag(strLiteral->getSourceRange().getBegin(), |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 770 | diag::warn_initializer_string_for_char_array_too_long) |
| 771 | << strLiteral->getSourceRange(); |
Steve Naroff | a49e1fa | 2008-01-22 00:55:40 +0000 | [diff] [blame] | 772 | } |
| 773 | // Set type from "char *" to "constant array of char". |
| 774 | strLiteral->setType(DeclT); |
| 775 | // For now, we always return false (meaning success). |
| 776 | return false; |
| 777 | } |
| 778 | |
| 779 | StringLiteral *Sema::IsStringLiteralInit(Expr *Init, QualType DeclType) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 780 | const ArrayType *AT = Context.getAsArrayType(DeclType); |
Steve Naroff | a996033 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 781 | if (AT && AT->getElementType()->isCharType()) { |
| 782 | return dyn_cast<StringLiteral>(Init); |
| 783 | } |
Steve Naroff | a49e1fa | 2008-01-22 00:55:40 +0000 | [diff] [blame] | 784 | return 0; |
| 785 | } |
| 786 | |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 787 | bool Sema::CheckInitializerTypes(Expr *&Init, QualType &DeclType, |
| 788 | SourceLocation InitLoc, |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 789 | DeclarationName InitEntity) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 790 | // C++ [dcl.init.ref]p1: |
Sebastian Redl | d14094d | 2008-11-24 20:06:50 +0000 | [diff] [blame] | 791 | // A variable declared to be a T&, that is "reference to type T" |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 792 | // (8.3.2), shall be initialized by an object, or function, of |
| 793 | // type T or by an object that can be converted into a T. |
| 794 | if (DeclType->isReferenceType()) |
| 795 | return CheckReferenceInit(Init, DeclType); |
| 796 | |
Steve Naroff | ca10730 | 2008-01-21 23:53:58 +0000 | [diff] [blame] | 797 | // C99 6.7.8p3: The type of the entity to be initialized shall be an array |
| 798 | // of unknown size ("[]") or an object type that is not a variable array type. |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 799 | if (const VariableArrayType *VAT = Context.getAsVariableArrayType(DeclType)) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 800 | return Diag(InitLoc, diag::err_variable_object_no_init) |
| 801 | << VAT->getSizeExpr()->getSourceRange(); |
Steve Naroff | ca10730 | 2008-01-21 23:53:58 +0000 | [diff] [blame] | 802 | |
Steve Naroff | 2fdc374 | 2007-12-10 22:44:33 +0000 | [diff] [blame] | 803 | InitListExpr *InitList = dyn_cast<InitListExpr>(Init); |
| 804 | if (!InitList) { |
Steve Naroff | a49e1fa | 2008-01-22 00:55:40 +0000 | [diff] [blame] | 805 | // FIXME: Handle wide strings |
| 806 | if (StringLiteral *strLiteral = IsStringLiteralInit(Init, DeclType)) |
| 807 | return CheckStringLiteralInit(strLiteral, DeclType); |
Eli Friedman | a312ce2 | 2008-02-08 00:48:24 +0000 | [diff] [blame] | 808 | |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 809 | // C++ [dcl.init]p14: |
| 810 | // -- If the destination type is a (possibly cv-qualified) class |
| 811 | // type: |
| 812 | if (getLangOptions().CPlusPlus && DeclType->isRecordType()) { |
| 813 | QualType DeclTypeC = Context.getCanonicalType(DeclType); |
| 814 | QualType InitTypeC = Context.getCanonicalType(Init->getType()); |
| 815 | |
| 816 | // -- If the initialization is direct-initialization, or if it is |
| 817 | // copy-initialization where the cv-unqualified version of the |
| 818 | // source type is the same class as, or a derived class of, the |
| 819 | // class of the destination, constructors are considered. |
| 820 | if ((DeclTypeC.getUnqualifiedType() == InitTypeC.getUnqualifiedType()) || |
| 821 | IsDerivedFrom(InitTypeC, DeclTypeC)) { |
| 822 | CXXConstructorDecl *Constructor |
| 823 | = PerformInitializationByConstructor(DeclType, &Init, 1, |
| 824 | InitLoc, Init->getSourceRange(), |
| 825 | InitEntity, IK_Copy); |
| 826 | return Constructor == 0; |
| 827 | } |
| 828 | |
| 829 | // -- Otherwise (i.e., for the remaining copy-initialization |
| 830 | // cases), user-defined conversion sequences that can |
| 831 | // convert from the source type to the destination type or |
| 832 | // (when a conversion function is used) to a derived class |
| 833 | // thereof are enumerated as described in 13.3.1.4, and the |
| 834 | // best one is chosen through overload resolution |
| 835 | // (13.3). If the conversion cannot be done or is |
| 836 | // ambiguous, the initialization is ill-formed. The |
| 837 | // function selected is called with the initializer |
| 838 | // expression as its argument; if the function is a |
| 839 | // constructor, the call initializes a temporary of the |
| 840 | // destination type. |
| 841 | // FIXME: We're pretending to do copy elision here; return to |
| 842 | // this when we have ASTs for such things. |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 843 | if (!PerformImplicitConversion(Init, DeclType)) |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 844 | return false; |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 845 | |
| 846 | return Diag(InitLoc, diag::err_typecheck_convert_incompatible) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 847 | << DeclType << InitEntity << "initializing" |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 848 | << Init->getSourceRange(); |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 849 | } |
| 850 | |
Steve Naroff | 1ac6fdd | 2008-09-29 20:07:05 +0000 | [diff] [blame] | 851 | // C99 6.7.8p16. |
Eli Friedman | a312ce2 | 2008-02-08 00:48:24 +0000 | [diff] [blame] | 852 | if (DeclType->isArrayType()) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 853 | return Diag(Init->getLocStart(), diag::err_array_init_list_required) |
| 854 | << Init->getSourceRange(); |
Eli Friedman | a312ce2 | 2008-02-08 00:48:24 +0000 | [diff] [blame] | 855 | |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 856 | return CheckSingleInitializer(Init, DeclType); |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 857 | } else if (getLangOptions().CPlusPlus) { |
| 858 | // C++ [dcl.init]p14: |
| 859 | // [...] If the class is an aggregate (8.5.1), and the initializer |
| 860 | // is a brace-enclosed list, see 8.5.1. |
| 861 | // |
| 862 | // Note: 8.5.1 is handled below; here, we diagnose the case where |
| 863 | // we have an initializer list and a destination type that is not |
| 864 | // an aggregate. |
| 865 | // FIXME: In C++0x, this is yet another form of initialization. |
| 866 | if (const RecordType *ClassRec = DeclType->getAsRecordType()) { |
| 867 | const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(ClassRec->getDecl()); |
| 868 | if (!ClassDecl->isAggregate()) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 869 | return Diag(InitLoc, diag::err_init_non_aggr_init_list) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 870 | << DeclType << Init->getSourceRange(); |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 871 | } |
Steve Naroff | 2fdc374 | 2007-12-10 22:44:33 +0000 | [diff] [blame] | 872 | } |
Eli Friedman | e6f058f | 2008-06-06 19:40:52 +0000 | [diff] [blame] | 873 | |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 874 | InitListChecker CheckInitList(this, InitList, DeclType); |
| 875 | return CheckInitList.HadError(); |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 876 | } |
| 877 | |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 878 | /// GetNameForDeclarator - Determine the full declaration name for the |
| 879 | /// given Declarator. |
| 880 | DeclarationName Sema::GetNameForDeclarator(Declarator &D) { |
| 881 | switch (D.getKind()) { |
| 882 | case Declarator::DK_Abstract: |
| 883 | assert(D.getIdentifier() == 0 && "abstract declarators have no name"); |
| 884 | return DeclarationName(); |
| 885 | |
| 886 | case Declarator::DK_Normal: |
| 887 | assert (D.getIdentifier() != 0 && "normal declarators have an identifier"); |
| 888 | return DeclarationName(D.getIdentifier()); |
| 889 | |
| 890 | case Declarator::DK_Constructor: { |
| 891 | QualType Ty = Context.getTypeDeclType((TypeDecl *)D.getDeclaratorIdType()); |
| 892 | Ty = Context.getCanonicalType(Ty); |
| 893 | return Context.DeclarationNames.getCXXConstructorName(Ty); |
| 894 | } |
| 895 | |
| 896 | case Declarator::DK_Destructor: { |
| 897 | QualType Ty = Context.getTypeDeclType((TypeDecl *)D.getDeclaratorIdType()); |
| 898 | Ty = Context.getCanonicalType(Ty); |
| 899 | return Context.DeclarationNames.getCXXDestructorName(Ty); |
| 900 | } |
| 901 | |
| 902 | case Declarator::DK_Conversion: { |
| 903 | QualType Ty = QualType::getFromOpaquePtr(D.getDeclaratorIdType()); |
| 904 | Ty = Context.getCanonicalType(Ty); |
| 905 | return Context.DeclarationNames.getCXXConversionFunctionName(Ty); |
| 906 | } |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 907 | |
| 908 | case Declarator::DK_Operator: |
| 909 | assert(D.getIdentifier() == 0 && "operator names have no identifier"); |
| 910 | return Context.DeclarationNames.getCXXOperatorName( |
| 911 | D.getOverloadedOperator()); |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | assert(false && "Unknown name kind"); |
| 915 | return DeclarationName(); |
| 916 | } |
| 917 | |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 918 | /// isNearlyMatchingMemberFunction - Determine whether the C++ member |
| 919 | /// functions Declaration and Definition are "nearly" matching. This |
| 920 | /// heuristic is used to improve diagnostics in the case where an |
| 921 | /// out-of-line member function definition doesn't match any |
| 922 | /// declaration within the class. |
| 923 | static bool isNearlyMatchingMemberFunction(ASTContext &Context, |
| 924 | FunctionDecl *Declaration, |
| 925 | FunctionDecl *Definition) { |
| 926 | if (Declaration->param_size() != Definition->param_size()) |
| 927 | return false; |
| 928 | for (unsigned Idx = 0; Idx < Declaration->param_size(); ++Idx) { |
| 929 | QualType DeclParamTy = Declaration->getParamDecl(Idx)->getType(); |
| 930 | QualType DefParamTy = Definition->getParamDecl(Idx)->getType(); |
| 931 | |
| 932 | DeclParamTy = Context.getCanonicalType(DeclParamTy.getNonReferenceType()); |
| 933 | DefParamTy = Context.getCanonicalType(DefParamTy.getNonReferenceType()); |
| 934 | if (DeclParamTy.getUnqualifiedType() != DefParamTy.getUnqualifiedType()) |
| 935 | return false; |
| 936 | } |
| 937 | |
| 938 | return true; |
| 939 | } |
| 940 | |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 941 | Sema::DeclTy * |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 942 | Sema::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *lastDecl, |
| 943 | bool IsFunctionDefinition) { |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 944 | ScopedDecl *LastDeclarator = dyn_cast_or_null<ScopedDecl>((Decl *)lastDecl); |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 945 | DeclarationName Name = GetNameForDeclarator(D); |
| 946 | |
Chris Lattner | e80a59c | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 947 | // All of these full declarators require an identifier. If it doesn't have |
| 948 | // one, the ParsedFreeStandingDeclSpec action should be used. |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 949 | if (!Name) { |
Chris Lattner | 1f6f54b | 2008-11-11 06:13:16 +0000 | [diff] [blame] | 950 | if (!D.getInvalidType()) // Reject this if we think it is valid. |
| 951 | Diag(D.getDeclSpec().getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 952 | diag::err_declarator_need_ident) |
| 953 | << D.getDeclSpec().getSourceRange() << D.getSourceRange(); |
Chris Lattner | e80a59c | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 954 | return 0; |
| 955 | } |
| 956 | |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 957 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 958 | // we find one that is. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 959 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 960 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 961 | S = S->getParent(); |
| 962 | |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 963 | DeclContext *DC; |
| 964 | Decl *PrevDecl; |
Steve Naroff | c752d04 | 2007-09-13 18:10:37 +0000 | [diff] [blame] | 965 | ScopedDecl *New; |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 966 | bool InvalidDecl = false; |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 967 | |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 968 | // See if this is a redefinition of a variable in the same scope. |
| 969 | if (!D.getCXXScopeSpec().isSet()) { |
| 970 | DC = CurContext; |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 971 | PrevDecl = LookupDecl(Name, Decl::IDNS_Ordinary, S); |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 972 | } else { // Something like "int foo::x;" |
| 973 | DC = static_cast<DeclContext*>(D.getCXXScopeSpec().getScopeRep()); |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 974 | PrevDecl = LookupDecl(Name, Decl::IDNS_Ordinary, S, DC); |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 975 | |
| 976 | // C++ 7.3.1.2p2: |
| 977 | // Members (including explicit specializations of templates) of a named |
| 978 | // namespace can also be defined outside that namespace by explicit |
| 979 | // qualification of the name being defined, provided that the entity being |
| 980 | // defined was already declared in the namespace and the definition appears |
| 981 | // after the point of declaration in a namespace that encloses the |
| 982 | // declarations namespace. |
| 983 | // |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 984 | // Note that we only check the context at this point. We don't yet |
| 985 | // have enough information to make sure that PrevDecl is actually |
| 986 | // the declaration we want to match. For example, given: |
| 987 | // |
Douglas Gregor | 9d35097 | 2008-12-12 08:25:50 +0000 | [diff] [blame] | 988 | // class X { |
| 989 | // void f(); |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 990 | // void f(float); |
Douglas Gregor | 9d35097 | 2008-12-12 08:25:50 +0000 | [diff] [blame] | 991 | // }; |
| 992 | // |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 993 | // void X::f(int) { } // ill-formed |
| 994 | // |
| 995 | // In this case, PrevDecl will point to the overload set |
| 996 | // containing the two f's declared in X, but neither of them |
| 997 | // matches. |
| 998 | if (!CurContext->Encloses(DC)) { |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 999 | // The qualifying scope doesn't enclose the original declaration. |
| 1000 | // Emit diagnostic based on current scope. |
| 1001 | SourceLocation L = D.getIdentifierLoc(); |
| 1002 | SourceRange R = D.getCXXScopeSpec().getRange(); |
| 1003 | if (isa<FunctionDecl>(CurContext)) { |
Chris Lattner | 011bb4e | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 1004 | Diag(L, diag::err_invalid_declarator_in_function) << Name << R; |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1005 | } else { |
Chris Lattner | 011bb4e | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 1006 | Diag(L, diag::err_invalid_declarator_scope) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1007 | << Name << cast<NamedDecl>(DC)->getDeclName() << R; |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1008 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1009 | InvalidDecl = true; |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1010 | } |
| 1011 | } |
| 1012 | |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 1013 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1014 | // Maybe we will complain about the shadowed template parameter. |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1015 | InvalidDecl = InvalidDecl |
| 1016 | || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1017 | // Just pretend that we didn't see the previous declaration. |
| 1018 | PrevDecl = 0; |
| 1019 | } |
| 1020 | |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1021 | // In C++, the previous declaration we find might be a tag type |
| 1022 | // (class or enum). In this case, the new declaration will hide the |
| 1023 | // tag type. |
| 1024 | if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag) |
| 1025 | PrevDecl = 0; |
| 1026 | |
Chris Lattner | 41af093 | 2007-11-14 06:34:38 +0000 | [diff] [blame] | 1027 | QualType R = GetTypeForDeclarator(D, S); |
| 1028 | assert(!R.isNull() && "GetTypeForDeclarator() returned null type"); |
| 1029 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1030 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 1031 | // Typedef declarators cannot be qualified (C++ [dcl.meaning]p1). |
| 1032 | if (D.getCXXScopeSpec().isSet()) { |
| 1033 | Diag(D.getIdentifierLoc(), diag::err_qualified_typedef_declarator) |
| 1034 | << D.getCXXScopeSpec().getRange(); |
| 1035 | InvalidDecl = true; |
| 1036 | // Pretend we didn't see the scope specifier. |
| 1037 | DC = 0; |
| 1038 | } |
| 1039 | |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 1040 | // Check that there are no default arguments (C++ only). |
| 1041 | if (getLangOptions().CPlusPlus) |
| 1042 | CheckExtraCXXDefaultArguments(D); |
| 1043 | |
Chris Lattner | 41af093 | 2007-11-14 06:34:38 +0000 | [diff] [blame] | 1044 | TypedefDecl *NewTD = ParseTypedefDecl(S, D, R, LastDeclarator); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1045 | if (!NewTD) return 0; |
| 1046 | |
| 1047 | // Handle attributes prior to checking for duplicates in MergeVarDecl |
Chris Lattner | 3ff30c8 | 2008-06-29 00:02:00 +0000 | [diff] [blame] | 1048 | ProcessDeclAttributes(NewTD, D); |
Steve Naroff | ffce4d5 | 2008-01-09 23:34:55 +0000 | [diff] [blame] | 1049 | // Merge the decl with the existing one if appropriate. If the decl is |
| 1050 | // in an outer scope, it isn't the same thing. |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1051 | if (PrevDecl && isDeclInScope(PrevDecl, DC, S)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1052 | NewTD = MergeTypeDefDecl(NewTD, PrevDecl); |
| 1053 | if (NewTD == 0) return 0; |
| 1054 | } |
| 1055 | New = NewTD; |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 1056 | if (S->getFnParent() == 0) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1057 | // C99 6.7.7p2: If a typedef name specifies a variably modified type |
| 1058 | // then it shall have block scope. |
Eli Friedman | 9db1397 | 2008-02-15 12:53:51 +0000 | [diff] [blame] | 1059 | if (NewTD->getUnderlyingType()->isVariablyModifiedType()) { |
Anders Carlsson | 96e05bc | 2008-12-07 00:20:55 +0000 | [diff] [blame] | 1060 | if (NewTD->getUnderlyingType()->isVariableArrayType()) |
| 1061 | Diag(D.getIdentifierLoc(), diag::err_vla_decl_in_file_scope); |
| 1062 | else |
| 1063 | Diag(D.getIdentifierLoc(), diag::err_vm_decl_in_file_scope); |
| 1064 | |
Steve Naroff | d7444aa | 2007-08-31 17:20:07 +0000 | [diff] [blame] | 1065 | InvalidDecl = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1066 | } |
| 1067 | } |
Chris Lattner | 41af093 | 2007-11-14 06:34:38 +0000 | [diff] [blame] | 1068 | } else if (R.getTypePtr()->isFunctionType()) { |
Chris Lattner | 271f1a6 | 2007-09-27 15:15:46 +0000 | [diff] [blame] | 1069 | FunctionDecl::StorageClass SC = FunctionDecl::None; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1070 | switch (D.getDeclSpec().getStorageClassSpec()) { |
| 1071 | default: assert(0 && "Unknown storage class!"); |
| 1072 | case DeclSpec::SCS_auto: |
| 1073 | case DeclSpec::SCS_register: |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1074 | case DeclSpec::SCS_mutable: |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 1075 | Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_func); |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 1076 | InvalidDecl = true; |
| 1077 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1078 | case DeclSpec::SCS_unspecified: SC = FunctionDecl::None; break; |
| 1079 | case DeclSpec::SCS_extern: SC = FunctionDecl::Extern; break; |
| 1080 | case DeclSpec::SCS_static: SC = FunctionDecl::Static; break; |
Steve Naroff | 7dd0bd4 | 2008-01-28 21:57:15 +0000 | [diff] [blame] | 1081 | case DeclSpec::SCS_private_extern: SC = FunctionDecl::PrivateExtern;break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
Chris Lattner | a98e58d | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 1084 | bool isInline = D.getDeclSpec().isInlineSpecified(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1085 | // bool isVirtual = D.getDeclSpec().isVirtualSpecified(); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1086 | bool isExplicit = D.getDeclSpec().isExplicitSpecified(); |
| 1087 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1088 | FunctionDecl *NewFD; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1089 | if (D.getKind() == Declarator::DK_Constructor) { |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1090 | // This is a C++ constructor declaration. |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1091 | assert(DC->isCXXRecord() && |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1092 | "Constructors can only be declared in a member context"); |
| 1093 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1094 | bool isInvalidDecl = CheckConstructorDeclarator(D, R, SC); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1095 | |
| 1096 | // Create the new declaration |
| 1097 | NewFD = CXXConstructorDecl::Create(Context, |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1098 | cast<CXXRecordDecl>(DC), |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 1099 | D.getIdentifierLoc(), Name, R, |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1100 | isExplicit, isInline, |
| 1101 | /*isImplicitlyDeclared=*/false); |
| 1102 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1103 | if (isInvalidDecl) |
| 1104 | NewFD->setInvalidDecl(); |
| 1105 | } else if (D.getKind() == Declarator::DK_Destructor) { |
| 1106 | // This is a C++ destructor declaration. |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1107 | if (DC->isCXXRecord()) { |
Argyrios Kyrtzidis | c7ed9c6 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 1108 | bool isInvalidDecl = CheckDestructorDeclarator(D, R, SC); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1109 | |
Argyrios Kyrtzidis | c7ed9c6 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 1110 | NewFD = CXXDestructorDecl::Create(Context, |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1111 | cast<CXXRecordDecl>(DC), |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 1112 | D.getIdentifierLoc(), Name, R, |
Argyrios Kyrtzidis | c7ed9c6 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 1113 | isInline, |
| 1114 | /*isImplicitlyDeclared=*/false); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1115 | |
Argyrios Kyrtzidis | c7ed9c6 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 1116 | if (isInvalidDecl) |
| 1117 | NewFD->setInvalidDecl(); |
| 1118 | } else { |
| 1119 | Diag(D.getIdentifierLoc(), diag::err_destructor_not_member); |
| 1120 | // Create a FunctionDecl to satisfy the function definition parsing |
| 1121 | // code path. |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1122 | NewFD = FunctionDecl::Create(Context, DC, D.getIdentifierLoc(), |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 1123 | Name, R, SC, isInline, LastDeclarator, |
Argyrios Kyrtzidis | c7ed9c6 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 1124 | // FIXME: Move to DeclGroup... |
| 1125 | D.getDeclSpec().getSourceRange().getBegin()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1126 | NewFD->setInvalidDecl(); |
Argyrios Kyrtzidis | c7ed9c6 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 1127 | } |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1128 | } else if (D.getKind() == Declarator::DK_Conversion) { |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1129 | if (!DC->isCXXRecord()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1130 | Diag(D.getIdentifierLoc(), |
| 1131 | diag::err_conv_function_not_member); |
| 1132 | return 0; |
| 1133 | } else { |
| 1134 | bool isInvalidDecl = CheckConversionDeclarator(D, R, SC); |
| 1135 | |
| 1136 | NewFD = CXXConversionDecl::Create(Context, |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1137 | cast<CXXRecordDecl>(DC), |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 1138 | D.getIdentifierLoc(), Name, R, |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1139 | isInline, isExplicit); |
| 1140 | |
| 1141 | if (isInvalidDecl) |
| 1142 | NewFD->setInvalidDecl(); |
| 1143 | } |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1144 | } else if (DC->isCXXRecord()) { |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1145 | // This is a C++ method declaration. |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1146 | NewFD = CXXMethodDecl::Create(Context, cast<CXXRecordDecl>(DC), |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 1147 | D.getIdentifierLoc(), Name, R, |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1148 | (SC == FunctionDecl::Static), isInline, |
| 1149 | LastDeclarator); |
| 1150 | } else { |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1151 | NewFD = FunctionDecl::Create(Context, DC, |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1152 | D.getIdentifierLoc(), |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 1153 | Name, R, SC, isInline, LastDeclarator, |
Steve Naroff | 0eb07bf | 2008-10-03 00:02:03 +0000 | [diff] [blame] | 1154 | // FIXME: Move to DeclGroup... |
| 1155 | D.getDeclSpec().getSourceRange().getBegin()); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1156 | } |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 1157 | |
Ted Kremenek | f5c93c1 | 2008-02-27 22:18:07 +0000 | [diff] [blame] | 1158 | // Handle attributes. |
Chris Lattner | 3ff30c8 | 2008-06-29 00:02:00 +0000 | [diff] [blame] | 1159 | ProcessDeclAttributes(NewFD, D); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1160 | |
Douglas Gregor | 9e7d9de | 2008-12-15 21:24:18 +0000 | [diff] [blame] | 1161 | // Set the lexical context. If the declarator has a C++ |
| 1162 | // scope specifier, the lexical context will be different |
| 1163 | // from the semantic context. |
| 1164 | NewFD->setLexicalDeclContext(CurContext); |
| 1165 | |
Daniel Dunbar | a80f874 | 2008-08-05 01:35:17 +0000 | [diff] [blame] | 1166 | // Handle GNU asm-label extension (encoded as an attribute). |
Daniel Dunbar | 914701e | 2008-08-05 16:28:08 +0000 | [diff] [blame] | 1167 | if (Expr *E = (Expr*) D.getAsmLabel()) { |
Daniel Dunbar | a80f874 | 2008-08-05 01:35:17 +0000 | [diff] [blame] | 1168 | // The parser guarantees this is a string. |
| 1169 | StringLiteral *SE = cast<StringLiteral>(E); |
| 1170 | NewFD->addAttr(new AsmLabelAttr(std::string(SE->getStrData(), |
| 1171 | SE->getByteLength()))); |
| 1172 | } |
| 1173 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1174 | // Copy the parameter declarations from the declarator D to |
| 1175 | // the function declaration NewFD, if they are available. |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 1176 | if (D.getNumTypeObjects() > 0) { |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1177 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 1178 | |
| 1179 | // Create Decl objects for each parameter, adding them to the |
| 1180 | // FunctionDecl. |
| 1181 | llvm::SmallVector<ParmVarDecl*, 16> Params; |
| 1182 | |
| 1183 | // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs |
| 1184 | // function that takes no arguments, not a function that takes a |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1185 | // single void argument. |
Eli Friedman | 6d1e4b5 | 2008-05-22 08:54:03 +0000 | [diff] [blame] | 1186 | // We let through "const void" here because Sema::GetTypeForDeclarator |
| 1187 | // already checks for that case. |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1188 | if (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
| 1189 | FTI.ArgInfo[0].Param && |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1190 | ((ParmVarDecl*)FTI.ArgInfo[0].Param)->getType()->isVoidType()) { |
| 1191 | // empty arg list, don't push any params. |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1192 | ParmVarDecl *Param = (ParmVarDecl*)FTI.ArgInfo[0].Param; |
| 1193 | |
Chris Lattner | def026a | 2008-04-10 02:26:16 +0000 | [diff] [blame] | 1194 | // In C++, the empty parameter-type-list must be spelled "void"; a |
| 1195 | // typedef of void is not permitted. |
| 1196 | if (getLangOptions().CPlusPlus && |
Eli Friedman | 6d1e4b5 | 2008-05-22 08:54:03 +0000 | [diff] [blame] | 1197 | Param->getType().getUnqualifiedType() != Context.VoidTy) { |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1198 | Diag(Param->getLocation(), diag::ext_param_typedef_of_void); |
| 1199 | } |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 1200 | } else if (FTI.NumArgs > 0 && FTI.ArgInfo[0].Param != 0) { |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1201 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) |
| 1202 | Params.push_back((ParmVarDecl *)FTI.ArgInfo[i].Param); |
| 1203 | } |
| 1204 | |
| 1205 | NewFD->setParams(&Params[0], Params.size()); |
Douglas Gregor | 6cbd3df | 2008-10-24 18:09:54 +0000 | [diff] [blame] | 1206 | } else if (R->getAsTypedefType()) { |
| 1207 | // When we're declaring a function with a typedef, as in the |
| 1208 | // following example, we'll need to synthesize (unnamed) |
| 1209 | // parameters for use in the declaration. |
| 1210 | // |
| 1211 | // @code |
| 1212 | // typedef void fn(int); |
| 1213 | // fn f; |
| 1214 | // @endcode |
| 1215 | const FunctionTypeProto *FT = R->getAsFunctionTypeProto(); |
| 1216 | if (!FT) { |
| 1217 | // This is a typedef of a function with no prototype, so we |
| 1218 | // don't need to do anything. |
| 1219 | } else if ((FT->getNumArgs() == 0) || |
| 1220 | (FT->getNumArgs() == 1 && !FT->isVariadic() && |
| 1221 | FT->getArgType(0)->isVoidType())) { |
| 1222 | // This is a zero-argument function. We don't need to do anything. |
| 1223 | } else { |
| 1224 | // Synthesize a parameter for each argument type. |
| 1225 | llvm::SmallVector<ParmVarDecl*, 16> Params; |
| 1226 | for (FunctionTypeProto::arg_type_iterator ArgType = FT->arg_type_begin(); |
| 1227 | ArgType != FT->arg_type_end(); ++ArgType) { |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1228 | Params.push_back(ParmVarDecl::Create(Context, DC, |
Douglas Gregor | 6cbd3df | 2008-10-24 18:09:54 +0000 | [diff] [blame] | 1229 | SourceLocation(), 0, |
| 1230 | *ArgType, VarDecl::None, |
| 1231 | 0, 0)); |
| 1232 | } |
| 1233 | |
| 1234 | NewFD->setParams(&Params[0], Params.size()); |
| 1235 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1238 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(NewFD)) |
| 1239 | InvalidDecl = InvalidDecl || CheckConstructor(Constructor); |
Douglas Gregor | 9e7d9de | 2008-12-15 21:24:18 +0000 | [diff] [blame] | 1240 | else if (isa<CXXDestructorDecl>(NewFD)) |
| 1241 | cast<CXXRecordDecl>(NewFD->getParent())->setUserDeclaredDestructor(true); |
| 1242 | else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(NewFD)) |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 1243 | ActOnConversionDeclarator(Conversion); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1244 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 1245 | // Extra checking for C++ overloaded operators (C++ [over.oper]). |
| 1246 | if (NewFD->isOverloadedOperator() && |
| 1247 | CheckOverloadedOperatorDeclaration(NewFD)) |
| 1248 | NewFD->setInvalidDecl(); |
| 1249 | |
Steve Naroff | ffce4d5 | 2008-01-09 23:34:55 +0000 | [diff] [blame] | 1250 | // Merge the decl with the existing one if appropriate. Since C functions |
| 1251 | // 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] | 1252 | if (PrevDecl && |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1253 | (!getLangOptions().CPlusPlus||isDeclInScope(PrevDecl, DC, S))) { |
Douglas Gregor | f009795 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 1254 | bool Redeclaration = false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1255 | |
| 1256 | // If C++, determine whether NewFD is an overload of PrevDecl or |
| 1257 | // a declaration that requires merging. If it's an overload, |
| 1258 | // there's no more work to do here; we'll just add the new |
| 1259 | // function to the scope. |
| 1260 | OverloadedFunctionDecl::function_iterator MatchedDecl; |
| 1261 | if (!getLangOptions().CPlusPlus || |
| 1262 | !IsOverload(NewFD, PrevDecl, MatchedDecl)) { |
| 1263 | Decl *OldDecl = PrevDecl; |
| 1264 | |
| 1265 | // If PrevDecl was an overloaded function, extract the |
| 1266 | // FunctionDecl that matched. |
| 1267 | if (isa<OverloadedFunctionDecl>(PrevDecl)) |
| 1268 | OldDecl = *MatchedDecl; |
| 1269 | |
| 1270 | // NewFD and PrevDecl represent declarations that need to be |
| 1271 | // merged. |
| 1272 | NewFD = MergeFunctionDecl(NewFD, OldDecl, Redeclaration); |
| 1273 | |
| 1274 | if (NewFD == 0) return 0; |
| 1275 | if (Redeclaration) { |
| 1276 | NewFD->setPreviousDeclaration(cast<FunctionDecl>(OldDecl)); |
| 1277 | |
| 1278 | if (OldDecl == PrevDecl) { |
| 1279 | // Remove the name binding for the previous |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1280 | // declaration. |
| 1281 | if (S->isDeclScope(PrevDecl)) { |
| 1282 | IdResolver.RemoveDecl(cast<NamedDecl>(PrevDecl)); |
| 1283 | S->RemoveDecl(PrevDecl); |
| 1284 | } |
| 1285 | |
| 1286 | // Introduce the new binding for this declaration. |
| 1287 | IdResolver.AddDecl(NewFD); |
| 1288 | if (getLangOptions().CPlusPlus && NewFD->getParent()) |
| 1289 | NewFD->getParent()->insert(Context, NewFD); |
| 1290 | |
| 1291 | // Add the redeclaration to the current scope, since we'll |
| 1292 | // be skipping PushOnScopeChains. |
| 1293 | S->AddDecl(NewFD); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1294 | } else { |
| 1295 | // We need to update the OverloadedFunctionDecl with the |
| 1296 | // latest declaration of this function, so that name |
| 1297 | // lookup will always refer to the latest declaration of |
| 1298 | // this function. |
| 1299 | *MatchedDecl = NewFD; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1300 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1301 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1302 | if (getLangOptions().CPlusPlus) { |
| 1303 | // Add this declaration to the current context. |
| 1304 | CurContext->addDecl(Context, NewFD, false); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1305 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1306 | // Check default arguments now that we have merged decls. |
| 1307 | CheckCXXDefaultArguments(NewFD); |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 1308 | |
| 1309 | // An out-of-line member function declaration must also be a |
| 1310 | // definition (C++ [dcl.meaning]p1). |
| 1311 | if (!IsFunctionDefinition && D.getCXXScopeSpec().isSet() && |
| 1312 | !InvalidDecl) { |
| 1313 | Diag(NewFD->getLocation(), diag::err_out_of_line_declaration) |
| 1314 | << D.getCXXScopeSpec().getRange(); |
| 1315 | NewFD->setInvalidDecl(); |
| 1316 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1317 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1318 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1319 | return NewFD; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1320 | } |
Douglas Gregor | f009795 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 1321 | } |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 1322 | |
| 1323 | if (!Redeclaration && D.getCXXScopeSpec().isSet()) { |
| 1324 | // The user tried to provide an out-of-line definition for a |
| 1325 | // member function, but there was no such member function |
| 1326 | // declared (C++ [class.mfct]p2). For example: |
| 1327 | // |
| 1328 | // class X { |
| 1329 | // void f() const; |
| 1330 | // }; |
| 1331 | // |
| 1332 | // void X::f() { } // ill-formed |
| 1333 | // |
| 1334 | // Complain about this problem, and attempt to suggest close |
| 1335 | // matches (e.g., those that differ only in cv-qualifiers and |
| 1336 | // whether the parameter types are references). |
| 1337 | Diag(D.getIdentifierLoc(), diag::err_member_def_does_not_match) |
| 1338 | << cast<CXXRecordDecl>(DC)->getDeclName() |
| 1339 | << D.getCXXScopeSpec().getRange(); |
| 1340 | InvalidDecl = true; |
| 1341 | |
| 1342 | PrevDecl = LookupDecl(Name, Decl::IDNS_Ordinary, S, DC); |
| 1343 | if (!PrevDecl) { |
| 1344 | // Nothing to suggest. |
| 1345 | } else if (OverloadedFunctionDecl *Ovl |
| 1346 | = dyn_cast<OverloadedFunctionDecl>(PrevDecl)) { |
| 1347 | for (OverloadedFunctionDecl::function_iterator |
| 1348 | Func = Ovl->function_begin(), |
| 1349 | FuncEnd = Ovl->function_end(); |
| 1350 | Func != FuncEnd; ++Func) { |
| 1351 | if (isNearlyMatchingMemberFunction(Context, *Func, NewFD)) |
| 1352 | Diag((*Func)->getLocation(), diag::note_member_def_close_match); |
| 1353 | |
| 1354 | } |
| 1355 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(PrevDecl)) { |
| 1356 | // Suggest this no matter how mismatched it is; it's the only |
| 1357 | // thing we have. |
| 1358 | unsigned diag; |
| 1359 | if (isNearlyMatchingMemberFunction(Context, Method, NewFD)) |
| 1360 | diag = diag::note_member_def_close_match; |
| 1361 | else if (Method->getBody()) |
| 1362 | diag = diag::note_previous_definition; |
| 1363 | else |
| 1364 | diag = diag::note_previous_declaration; |
| 1365 | Diag(Method->getLocation(), diag); |
| 1366 | } |
| 1367 | |
| 1368 | PrevDecl = 0; |
| 1369 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1370 | } |
| 1371 | New = NewFD; |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1372 | |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 1373 | if (getLangOptions().CPlusPlus) { |
| 1374 | // In C++, check default arguments now that we have merged decls. |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1375 | CheckCXXDefaultArguments(NewFD); |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 1376 | |
| 1377 | // An out-of-line member function declaration must also be a |
| 1378 | // definition (C++ [dcl.meaning]p1). |
| 1379 | if (!IsFunctionDefinition && D.getCXXScopeSpec().isSet()) { |
| 1380 | Diag(NewFD->getLocation(), diag::err_out_of_line_declaration) |
| 1381 | << D.getCXXScopeSpec().getRange(); |
| 1382 | InvalidDecl = true; |
| 1383 | } |
| 1384 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1385 | } else { |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 1386 | // Check that there are no default arguments (C++ only). |
| 1387 | if (getLangOptions().CPlusPlus) |
| 1388 | CheckExtraCXXDefaultArguments(D); |
| 1389 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1390 | if (R.getTypePtr()->isObjCInterfaceType()) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1391 | Diag(D.getIdentifierLoc(), diag::err_statically_allocated_object) |
| 1392 | << D.getIdentifier(); |
Fariborz Jahanian | e7f64cc | 2007-10-12 22:10:42 +0000 | [diff] [blame] | 1393 | InvalidDecl = true; |
| 1394 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1395 | |
| 1396 | VarDecl *NewVD; |
| 1397 | VarDecl::StorageClass SC; |
| 1398 | switch (D.getDeclSpec().getStorageClassSpec()) { |
Chris Lattner | 9e151e1 | 2008-03-15 21:10:16 +0000 | [diff] [blame] | 1399 | default: assert(0 && "Unknown storage class!"); |
| 1400 | case DeclSpec::SCS_unspecified: SC = VarDecl::None; break; |
| 1401 | case DeclSpec::SCS_extern: SC = VarDecl::Extern; break; |
| 1402 | case DeclSpec::SCS_static: SC = VarDecl::Static; break; |
| 1403 | case DeclSpec::SCS_auto: SC = VarDecl::Auto; break; |
| 1404 | case DeclSpec::SCS_register: SC = VarDecl::Register; break; |
| 1405 | case DeclSpec::SCS_private_extern: SC = VarDecl::PrivateExtern; break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1406 | case DeclSpec::SCS_mutable: |
| 1407 | // mutable can only appear on non-static class members, so it's always |
| 1408 | // an error here |
| 1409 | Diag(D.getIdentifierLoc(), diag::err_mutable_nonmember); |
| 1410 | InvalidDecl = true; |
Douglas Gregor | e89b028 | 2008-12-01 22:46:22 +0000 | [diff] [blame] | 1411 | SC = VarDecl::None; |
Sebastian Redl | a11f42f | 2008-11-17 23:24:37 +0000 | [diff] [blame] | 1412 | break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1413 | } |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 1414 | |
| 1415 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
| 1416 | if (!II) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 1417 | Diag(D.getIdentifierLoc(), diag::err_bad_variable_name) |
| 1418 | << Name.getAsString(); |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 1419 | return 0; |
| 1420 | } |
| 1421 | |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1422 | if (DC->isCXXRecord()) { |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1423 | // This is a static data member for a C++ class. |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1424 | NewVD = CXXClassVarDecl::Create(Context, cast<CXXRecordDecl>(DC), |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1425 | D.getIdentifierLoc(), II, |
| 1426 | R, LastDeclarator); |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 1427 | } else { |
Daniel Dunbar | 6f0200e | 2008-09-08 20:05:47 +0000 | [diff] [blame] | 1428 | bool ThreadSpecified = D.getDeclSpec().isThreadSpecified(); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1429 | if (S->getFnParent() == 0) { |
| 1430 | // C99 6.9p2: The storage-class specifiers auto and register shall not |
| 1431 | // appear in the declaration specifiers in an external declaration. |
| 1432 | if (SC == VarDecl::Auto || SC == VarDecl::Register) { |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 1433 | Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_fscope); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1434 | InvalidDecl = true; |
| 1435 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1436 | } |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1437 | NewVD = VarDecl::Create(Context, DC, D.getIdentifierLoc(), |
| 1438 | II, R, SC, LastDeclarator, |
| 1439 | // FIXME: Move to DeclGroup... |
| 1440 | D.getDeclSpec().getSourceRange().getBegin()); |
| 1441 | NewVD->setThreadSpecified(ThreadSpecified); |
Steve Naroff | 53a3234 | 2007-08-28 18:45:29 +0000 | [diff] [blame] | 1442 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1443 | // Handle attributes prior to checking for duplicates in MergeVarDecl |
Chris Lattner | 3ff30c8 | 2008-06-29 00:02:00 +0000 | [diff] [blame] | 1444 | ProcessDeclAttributes(NewVD, D); |
Nate Begeman | c8e89a8 | 2008-03-14 18:07:10 +0000 | [diff] [blame] | 1445 | |
Daniel Dunbar | a735ad8 | 2008-08-06 00:03:29 +0000 | [diff] [blame] | 1446 | // Handle GNU asm-label extension (encoded as an attribute). |
| 1447 | if (Expr *E = (Expr*) D.getAsmLabel()) { |
| 1448 | // The parser guarantees this is a string. |
| 1449 | StringLiteral *SE = cast<StringLiteral>(E); |
| 1450 | NewVD->addAttr(new AsmLabelAttr(std::string(SE->getStrData(), |
| 1451 | SE->getByteLength()))); |
| 1452 | } |
| 1453 | |
Nate Begeman | c8e89a8 | 2008-03-14 18:07:10 +0000 | [diff] [blame] | 1454 | // Emit an error if an address space was applied to decl with local storage. |
| 1455 | // This includes arrays of objects with address space qualifiers, but not |
| 1456 | // automatic variables that point to other address spaces. |
| 1457 | // ISO/IEC TR 18037 S5.1.2 |
Nate Begeman | 8e7dafe | 2008-03-25 18:36:32 +0000 | [diff] [blame] | 1458 | if (NewVD->hasLocalStorage() && (NewVD->getType().getAddressSpace() != 0)) { |
| 1459 | Diag(D.getIdentifierLoc(), diag::err_as_qualified_auto_decl); |
| 1460 | InvalidDecl = true; |
Nate Begeman | 5af27e0 | 2008-03-14 00:22:18 +0000 | [diff] [blame] | 1461 | } |
Steve Naroff | ffce4d5 | 2008-01-09 23:34:55 +0000 | [diff] [blame] | 1462 | // Merge the decl with the existing one if appropriate. If the decl is |
| 1463 | // in an outer scope, it isn't the same thing. |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1464 | if (PrevDecl && isDeclInScope(PrevDecl, DC, S)) { |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 1465 | if (isa<FieldDecl>(PrevDecl) && D.getCXXScopeSpec().isSet()) { |
| 1466 | // The user tried to define a non-static data member |
| 1467 | // out-of-line (C++ [dcl.meaning]p1). |
| 1468 | Diag(NewVD->getLocation(), diag::err_nonstatic_member_out_of_line) |
| 1469 | << D.getCXXScopeSpec().getRange(); |
| 1470 | NewVD->Destroy(Context); |
| 1471 | return 0; |
| 1472 | } |
| 1473 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1474 | NewVD = MergeVarDecl(NewVD, PrevDecl); |
| 1475 | if (NewVD == 0) return 0; |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 1476 | |
| 1477 | if (D.getCXXScopeSpec().isSet()) { |
| 1478 | // No previous declaration in the qualifying scope. |
| 1479 | Diag(D.getIdentifierLoc(), diag::err_typecheck_no_member) |
| 1480 | << Name << D.getCXXScopeSpec().getRange(); |
| 1481 | InvalidDecl = true; |
| 1482 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1483 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1484 | New = NewVD; |
| 1485 | } |
| 1486 | |
Argyrios Kyrtzidis | 5239304 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 1487 | // Set the lexical context. If the declarator has a C++ scope specifier, the |
| 1488 | // lexical context will be different from the semantic context. |
| 1489 | New->setLexicalDeclContext(CurContext); |
| 1490 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1491 | // If this has an identifier, add it to the scope stack. |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 1492 | if (Name) |
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 1493 | PushOnScopeChains(New, S); |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 1494 | // If any semantic error occurred, mark the decl as invalid. |
| 1495 | if (D.getInvalidType() || InvalidDecl) |
| 1496 | New->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1497 | |
| 1498 | return New; |
| 1499 | } |
| 1500 | |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1501 | void Sema::InitializerElementNotConstant(const Expr *Init) { |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1502 | Diag(Init->getExprLoc(), diag::err_init_element_not_constant) |
| 1503 | << Init->getSourceRange(); |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1504 | } |
| 1505 | |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1506 | bool Sema::CheckAddressConstantExpressionLValue(const Expr* Init) { |
| 1507 | switch (Init->getStmtClass()) { |
| 1508 | default: |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1509 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1510 | return true; |
| 1511 | case Expr::ParenExprClass: { |
| 1512 | const ParenExpr* PE = cast<ParenExpr>(Init); |
| 1513 | return CheckAddressConstantExpressionLValue(PE->getSubExpr()); |
| 1514 | } |
| 1515 | case Expr::CompoundLiteralExprClass: |
| 1516 | return cast<CompoundLiteralExpr>(Init)->isFileScope(); |
| 1517 | case Expr::DeclRefExprClass: { |
| 1518 | const Decl *D = cast<DeclRefExpr>(Init)->getDecl(); |
Eli Friedman | 97c0a39 | 2008-05-21 03:39:11 +0000 | [diff] [blame] | 1519 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 1520 | if (VD->hasGlobalStorage()) |
| 1521 | return false; |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1522 | InitializerElementNotConstant(Init); |
Eli Friedman | 97c0a39 | 2008-05-21 03:39:11 +0000 | [diff] [blame] | 1523 | return true; |
| 1524 | } |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1525 | if (isa<FunctionDecl>(D)) |
| 1526 | return false; |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1527 | InitializerElementNotConstant(Init); |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 1528 | return true; |
| 1529 | } |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1530 | case Expr::MemberExprClass: { |
| 1531 | const MemberExpr *M = cast<MemberExpr>(Init); |
| 1532 | if (M->isArrow()) |
| 1533 | return CheckAddressConstantExpression(M->getBase()); |
| 1534 | return CheckAddressConstantExpressionLValue(M->getBase()); |
| 1535 | } |
| 1536 | case Expr::ArraySubscriptExprClass: { |
| 1537 | // FIXME: Should we pedwarn for "x[0+0]" (where x is a pointer)? |
| 1538 | const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(Init); |
| 1539 | return CheckAddressConstantExpression(ASE->getBase()) || |
| 1540 | CheckArithmeticConstantExpression(ASE->getIdx()); |
| 1541 | } |
| 1542 | case Expr::StringLiteralClass: |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 1543 | case Expr::PredefinedExprClass: |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1544 | return false; |
| 1545 | case Expr::UnaryOperatorClass: { |
| 1546 | const UnaryOperator *Exp = cast<UnaryOperator>(Init); |
| 1547 | |
| 1548 | // C99 6.6p9 |
| 1549 | if (Exp->getOpcode() == UnaryOperator::Deref) |
Eli Friedman | 97c0a39 | 2008-05-21 03:39:11 +0000 | [diff] [blame] | 1550 | return CheckAddressConstantExpression(Exp->getSubExpr()); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1551 | |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1552 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1553 | return true; |
| 1554 | } |
| 1555 | } |
| 1556 | } |
| 1557 | |
| 1558 | bool Sema::CheckAddressConstantExpression(const Expr* Init) { |
| 1559 | switch (Init->getStmtClass()) { |
| 1560 | default: |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1561 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1562 | return true; |
Chris Lattner | 506ff88 | 2008-10-06 07:26:43 +0000 | [diff] [blame] | 1563 | case Expr::ParenExprClass: |
| 1564 | return CheckAddressConstantExpression(cast<ParenExpr>(Init)->getSubExpr()); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1565 | case Expr::StringLiteralClass: |
| 1566 | case Expr::ObjCStringLiteralClass: |
| 1567 | return false; |
Chris Lattner | 506ff88 | 2008-10-06 07:26:43 +0000 | [diff] [blame] | 1568 | case Expr::CallExprClass: |
Douglas Gregor | b460980 | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 1569 | case Expr::CXXOperatorCallExprClass: |
Chris Lattner | 506ff88 | 2008-10-06 07:26:43 +0000 | [diff] [blame] | 1570 | // __builtin___CFStringMakeConstantString is a valid constant l-value. |
| 1571 | if (cast<CallExpr>(Init)->isBuiltinCall() == |
| 1572 | Builtin::BI__builtin___CFStringMakeConstantString) |
| 1573 | return false; |
| 1574 | |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1575 | InitializerElementNotConstant(Init); |
Chris Lattner | 506ff88 | 2008-10-06 07:26:43 +0000 | [diff] [blame] | 1576 | return true; |
| 1577 | |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1578 | case Expr::UnaryOperatorClass: { |
| 1579 | const UnaryOperator *Exp = cast<UnaryOperator>(Init); |
| 1580 | |
| 1581 | // C99 6.6p9 |
| 1582 | if (Exp->getOpcode() == UnaryOperator::AddrOf) |
| 1583 | return CheckAddressConstantExpressionLValue(Exp->getSubExpr()); |
| 1584 | |
| 1585 | if (Exp->getOpcode() == UnaryOperator::Extension) |
| 1586 | return CheckAddressConstantExpression(Exp->getSubExpr()); |
| 1587 | |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1588 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1589 | return true; |
| 1590 | } |
| 1591 | case Expr::BinaryOperatorClass: { |
| 1592 | // FIXME: Should we pedwarn for expressions like "a + 1 + 2"? |
| 1593 | const BinaryOperator *Exp = cast<BinaryOperator>(Init); |
| 1594 | |
| 1595 | Expr *PExp = Exp->getLHS(); |
| 1596 | Expr *IExp = Exp->getRHS(); |
| 1597 | if (IExp->getType()->isPointerType()) |
| 1598 | std::swap(PExp, IExp); |
| 1599 | |
| 1600 | // FIXME: Should we pedwarn if IExp isn't an integer constant expression? |
| 1601 | return CheckAddressConstantExpression(PExp) || |
| 1602 | CheckArithmeticConstantExpression(IExp); |
| 1603 | } |
Eli Friedman | c3f0764 | 2008-08-25 20:46:57 +0000 | [diff] [blame] | 1604 | case Expr::ImplicitCastExprClass: |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 1605 | case Expr::CStyleCastExprClass: { |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1606 | const Expr* SubExpr = cast<CastExpr>(Init)->getSubExpr(); |
Eli Friedman | c3f0764 | 2008-08-25 20:46:57 +0000 | [diff] [blame] | 1607 | if (Init->getStmtClass() == Expr::ImplicitCastExprClass) { |
| 1608 | // Check for implicit promotion |
| 1609 | if (SubExpr->getType()->isFunctionType() || |
| 1610 | SubExpr->getType()->isArrayType()) |
| 1611 | return CheckAddressConstantExpressionLValue(SubExpr); |
| 1612 | } |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1613 | |
| 1614 | // Check for pointer->pointer cast |
| 1615 | if (SubExpr->getType()->isPointerType()) |
| 1616 | return CheckAddressConstantExpression(SubExpr); |
| 1617 | |
Eli Friedman | c3f0764 | 2008-08-25 20:46:57 +0000 | [diff] [blame] | 1618 | if (SubExpr->getType()->isIntegralType()) { |
| 1619 | // Check for the special-case of a pointer->int->pointer cast; |
| 1620 | // this isn't standard, but some code requires it. See |
| 1621 | // PR2720 for an example. |
| 1622 | if (const CastExpr* SubCast = dyn_cast<CastExpr>(SubExpr)) { |
| 1623 | if (SubCast->getSubExpr()->getType()->isPointerType()) { |
| 1624 | unsigned IntWidth = Context.getIntWidth(SubCast->getType()); |
| 1625 | unsigned PointerWidth = Context.getTypeSize(Context.VoidPtrTy); |
| 1626 | if (IntWidth >= PointerWidth) { |
| 1627 | return CheckAddressConstantExpression(SubCast->getSubExpr()); |
| 1628 | } |
| 1629 | } |
| 1630 | } |
| 1631 | } |
| 1632 | if (SubExpr->getType()->isArithmeticType()) { |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1633 | return CheckArithmeticConstantExpression(SubExpr); |
Eli Friedman | c3f0764 | 2008-08-25 20:46:57 +0000 | [diff] [blame] | 1634 | } |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1635 | |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1636 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1637 | return true; |
| 1638 | } |
| 1639 | case Expr::ConditionalOperatorClass: { |
| 1640 | // FIXME: Should we pedwarn here? |
| 1641 | const ConditionalOperator *Exp = cast<ConditionalOperator>(Init); |
| 1642 | if (!Exp->getCond()->getType()->isArithmeticType()) { |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1643 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1644 | return true; |
| 1645 | } |
| 1646 | if (CheckArithmeticConstantExpression(Exp->getCond())) |
| 1647 | return true; |
| 1648 | if (Exp->getLHS() && |
| 1649 | CheckAddressConstantExpression(Exp->getLHS())) |
| 1650 | return true; |
| 1651 | return CheckAddressConstantExpression(Exp->getRHS()); |
| 1652 | } |
| 1653 | case Expr::AddrLabelExprClass: |
| 1654 | return false; |
| 1655 | } |
| 1656 | } |
| 1657 | |
Eli Friedman | 4caf055 | 2008-06-09 05:05:07 +0000 | [diff] [blame] | 1658 | static const Expr* FindExpressionBaseAddress(const Expr* E); |
| 1659 | |
| 1660 | static const Expr* FindExpressionBaseAddressLValue(const Expr* E) { |
| 1661 | switch (E->getStmtClass()) { |
| 1662 | default: |
| 1663 | return E; |
| 1664 | case Expr::ParenExprClass: { |
| 1665 | const ParenExpr* PE = cast<ParenExpr>(E); |
| 1666 | return FindExpressionBaseAddressLValue(PE->getSubExpr()); |
| 1667 | } |
| 1668 | case Expr::MemberExprClass: { |
| 1669 | const MemberExpr *M = cast<MemberExpr>(E); |
| 1670 | if (M->isArrow()) |
| 1671 | return FindExpressionBaseAddress(M->getBase()); |
| 1672 | return FindExpressionBaseAddressLValue(M->getBase()); |
| 1673 | } |
| 1674 | case Expr::ArraySubscriptExprClass: { |
| 1675 | const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(E); |
| 1676 | return FindExpressionBaseAddress(ASE->getBase()); |
| 1677 | } |
| 1678 | case Expr::UnaryOperatorClass: { |
| 1679 | const UnaryOperator *Exp = cast<UnaryOperator>(E); |
| 1680 | |
| 1681 | if (Exp->getOpcode() == UnaryOperator::Deref) |
| 1682 | return FindExpressionBaseAddress(Exp->getSubExpr()); |
| 1683 | |
| 1684 | return E; |
| 1685 | } |
| 1686 | } |
| 1687 | } |
| 1688 | |
| 1689 | static const Expr* FindExpressionBaseAddress(const Expr* E) { |
| 1690 | switch (E->getStmtClass()) { |
| 1691 | default: |
| 1692 | return E; |
| 1693 | case Expr::ParenExprClass: { |
| 1694 | const ParenExpr* PE = cast<ParenExpr>(E); |
| 1695 | return FindExpressionBaseAddress(PE->getSubExpr()); |
| 1696 | } |
| 1697 | case Expr::UnaryOperatorClass: { |
| 1698 | const UnaryOperator *Exp = cast<UnaryOperator>(E); |
| 1699 | |
| 1700 | // C99 6.6p9 |
| 1701 | if (Exp->getOpcode() == UnaryOperator::AddrOf) |
| 1702 | return FindExpressionBaseAddressLValue(Exp->getSubExpr()); |
| 1703 | |
| 1704 | if (Exp->getOpcode() == UnaryOperator::Extension) |
| 1705 | return FindExpressionBaseAddress(Exp->getSubExpr()); |
| 1706 | |
| 1707 | return E; |
| 1708 | } |
| 1709 | case Expr::BinaryOperatorClass: { |
| 1710 | const BinaryOperator *Exp = cast<BinaryOperator>(E); |
| 1711 | |
| 1712 | Expr *PExp = Exp->getLHS(); |
| 1713 | Expr *IExp = Exp->getRHS(); |
| 1714 | if (IExp->getType()->isPointerType()) |
| 1715 | std::swap(PExp, IExp); |
| 1716 | |
| 1717 | return FindExpressionBaseAddress(PExp); |
| 1718 | } |
| 1719 | case Expr::ImplicitCastExprClass: { |
| 1720 | const Expr* SubExpr = cast<ImplicitCastExpr>(E)->getSubExpr(); |
| 1721 | |
| 1722 | // Check for implicit promotion |
| 1723 | if (SubExpr->getType()->isFunctionType() || |
| 1724 | SubExpr->getType()->isArrayType()) |
| 1725 | return FindExpressionBaseAddressLValue(SubExpr); |
| 1726 | |
| 1727 | // Check for pointer->pointer cast |
| 1728 | if (SubExpr->getType()->isPointerType()) |
| 1729 | return FindExpressionBaseAddress(SubExpr); |
| 1730 | |
| 1731 | // We assume that we have an arithmetic expression here; |
| 1732 | // if we don't, we'll figure it out later |
| 1733 | return 0; |
| 1734 | } |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 1735 | case Expr::CStyleCastExprClass: { |
Eli Friedman | 4caf055 | 2008-06-09 05:05:07 +0000 | [diff] [blame] | 1736 | const Expr* SubExpr = cast<CastExpr>(E)->getSubExpr(); |
| 1737 | |
| 1738 | // Check for pointer->pointer cast |
| 1739 | if (SubExpr->getType()->isPointerType()) |
| 1740 | return FindExpressionBaseAddress(SubExpr); |
| 1741 | |
| 1742 | // We assume that we have an arithmetic expression here; |
| 1743 | // if we don't, we'll figure it out later |
| 1744 | return 0; |
| 1745 | } |
| 1746 | } |
| 1747 | } |
| 1748 | |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 1749 | bool Sema::CheckArithmeticConstantExpression(const Expr* Init) { |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1750 | switch (Init->getStmtClass()) { |
| 1751 | default: |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1752 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1753 | return true; |
| 1754 | case Expr::ParenExprClass: { |
| 1755 | const ParenExpr* PE = cast<ParenExpr>(Init); |
| 1756 | return CheckArithmeticConstantExpression(PE->getSubExpr()); |
| 1757 | } |
| 1758 | case Expr::FloatingLiteralClass: |
| 1759 | case Expr::IntegerLiteralClass: |
| 1760 | case Expr::CharacterLiteralClass: |
| 1761 | case Expr::ImaginaryLiteralClass: |
| 1762 | case Expr::TypesCompatibleExprClass: |
| 1763 | case Expr::CXXBoolLiteralExprClass: |
| 1764 | return false; |
Douglas Gregor | b460980 | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 1765 | case Expr::CallExprClass: |
| 1766 | case Expr::CXXOperatorCallExprClass: { |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1767 | const CallExpr *CE = cast<CallExpr>(Init); |
Chris Lattner | 45b6b9d | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 1768 | |
| 1769 | // Allow any constant foldable calls to builtins. |
| 1770 | if (CE->isBuiltinCall() && CE->isEvaluatable(Context)) |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1771 | return false; |
Chris Lattner | 45b6b9d | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 1772 | |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1773 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1774 | return true; |
| 1775 | } |
| 1776 | case Expr::DeclRefExprClass: { |
| 1777 | const Decl *D = cast<DeclRefExpr>(Init)->getDecl(); |
| 1778 | if (isa<EnumConstantDecl>(D)) |
| 1779 | return false; |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1780 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1781 | return true; |
| 1782 | } |
| 1783 | case Expr::CompoundLiteralExprClass: |
| 1784 | // Allow "(vector type){2,4}"; normal C constraints don't allow this, |
| 1785 | // but vectors are allowed to be magic. |
| 1786 | if (Init->getType()->isVectorType()) |
| 1787 | return false; |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1788 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1789 | return true; |
| 1790 | case Expr::UnaryOperatorClass: { |
| 1791 | const UnaryOperator *Exp = cast<UnaryOperator>(Init); |
| 1792 | |
| 1793 | switch (Exp->getOpcode()) { |
| 1794 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 1795 | // See C99 6.6p3. |
| 1796 | default: |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1797 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1798 | return true; |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1799 | case UnaryOperator::OffsetOf: |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1800 | if (Exp->getSubExpr()->getType()->isConstantSizeType()) |
| 1801 | return false; |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1802 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1803 | return true; |
| 1804 | case UnaryOperator::Extension: |
| 1805 | case UnaryOperator::LNot: |
| 1806 | case UnaryOperator::Plus: |
| 1807 | case UnaryOperator::Minus: |
| 1808 | case UnaryOperator::Not: |
| 1809 | return CheckArithmeticConstantExpression(Exp->getSubExpr()); |
| 1810 | } |
| 1811 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1812 | case Expr::SizeOfAlignOfExprClass: { |
| 1813 | const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1814 | // Special check for void types, which are allowed as an extension |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1815 | if (Exp->getTypeOfArgument()->isVoidType()) |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1816 | return false; |
| 1817 | // alignof always evaluates to a constant. |
| 1818 | // FIXME: is sizeof(int[3.0]) a constant expression? |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1819 | if (Exp->isSizeOf() && !Exp->getTypeOfArgument()->isConstantSizeType()) { |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1820 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1821 | return true; |
| 1822 | } |
| 1823 | return false; |
| 1824 | } |
| 1825 | case Expr::BinaryOperatorClass: { |
| 1826 | const BinaryOperator *Exp = cast<BinaryOperator>(Init); |
| 1827 | |
| 1828 | if (Exp->getLHS()->getType()->isArithmeticType() && |
| 1829 | Exp->getRHS()->getType()->isArithmeticType()) { |
| 1830 | return CheckArithmeticConstantExpression(Exp->getLHS()) || |
| 1831 | CheckArithmeticConstantExpression(Exp->getRHS()); |
| 1832 | } |
| 1833 | |
Eli Friedman | 4caf055 | 2008-06-09 05:05:07 +0000 | [diff] [blame] | 1834 | if (Exp->getLHS()->getType()->isPointerType() && |
| 1835 | Exp->getRHS()->getType()->isPointerType()) { |
| 1836 | const Expr* LHSBase = FindExpressionBaseAddress(Exp->getLHS()); |
| 1837 | const Expr* RHSBase = FindExpressionBaseAddress(Exp->getRHS()); |
| 1838 | |
| 1839 | // Only allow a null (constant integer) base; we could |
| 1840 | // allow some additional cases if necessary, but this |
| 1841 | // is sufficient to cover offsetof-like constructs. |
| 1842 | if (!LHSBase && !RHSBase) { |
| 1843 | return CheckAddressConstantExpression(Exp->getLHS()) || |
| 1844 | CheckAddressConstantExpression(Exp->getRHS()); |
| 1845 | } |
| 1846 | } |
| 1847 | |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1848 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1849 | return true; |
| 1850 | } |
| 1851 | case Expr::ImplicitCastExprClass: |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 1852 | case Expr::CStyleCastExprClass: { |
Argyrios Kyrtzidis | 0835a3c | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 1853 | const Expr *SubExpr = cast<CastExpr>(Init)->getSubExpr(); |
Eli Friedman | 6d4abe1 | 2008-09-01 22:08:17 +0000 | [diff] [blame] | 1854 | if (SubExpr->getType()->isArithmeticType()) |
| 1855 | return CheckArithmeticConstantExpression(SubExpr); |
| 1856 | |
Eli Friedman | b529d83 | 2008-09-02 09:37:00 +0000 | [diff] [blame] | 1857 | if (SubExpr->getType()->isPointerType()) { |
| 1858 | const Expr* Base = FindExpressionBaseAddress(SubExpr); |
| 1859 | // If the pointer has a null base, this is an offsetof-like construct |
| 1860 | if (!Base) |
| 1861 | return CheckAddressConstantExpression(SubExpr); |
| 1862 | } |
| 1863 | |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1864 | InitializerElementNotConstant(Init); |
Eli Friedman | 6d4abe1 | 2008-09-01 22:08:17 +0000 | [diff] [blame] | 1865 | return true; |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1866 | } |
| 1867 | case Expr::ConditionalOperatorClass: { |
| 1868 | const ConditionalOperator *Exp = cast<ConditionalOperator>(Init); |
Chris Lattner | 46cfefa | 2008-10-06 05:42:39 +0000 | [diff] [blame] | 1869 | |
| 1870 | // If GNU extensions are disabled, we require all operands to be arithmetic |
| 1871 | // constant expressions. |
| 1872 | if (getLangOptions().NoExtensions) { |
| 1873 | return CheckArithmeticConstantExpression(Exp->getCond()) || |
| 1874 | (Exp->getLHS() && CheckArithmeticConstantExpression(Exp->getLHS())) || |
| 1875 | CheckArithmeticConstantExpression(Exp->getRHS()); |
| 1876 | } |
| 1877 | |
| 1878 | // Otherwise, we have to emulate some of the behavior of fold here. |
| 1879 | // Basically GCC treats things like "4 ? 1 : somefunc()" as a constant |
| 1880 | // because it can constant fold things away. To retain compatibility with |
| 1881 | // GCC code, we see if we can fold the condition to a constant (which we |
| 1882 | // should always be able to do in theory). If so, we only require the |
| 1883 | // specified arm of the conditional to be a constant. This is a horrible |
| 1884 | // hack, but is require by real world code that uses __builtin_constant_p. |
| 1885 | APValue Val; |
Chris Lattner | 6ee7aa1 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 1886 | if (!Exp->getCond()->Evaluate(Val, Context)) { |
| 1887 | // If Evaluate couldn't fold it, CheckArithmeticConstantExpression |
Chris Lattner | 46cfefa | 2008-10-06 05:42:39 +0000 | [diff] [blame] | 1888 | // won't be able to either. Use it to emit the diagnostic though. |
| 1889 | bool Res = CheckArithmeticConstantExpression(Exp->getCond()); |
Chris Lattner | 6ee7aa1 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 1890 | assert(Res && "Evaluate couldn't evaluate this constant?"); |
Chris Lattner | 46cfefa | 2008-10-06 05:42:39 +0000 | [diff] [blame] | 1891 | return Res; |
| 1892 | } |
| 1893 | |
| 1894 | // Verify that the side following the condition is also a constant. |
| 1895 | const Expr *TrueSide = Exp->getLHS(), *FalseSide = Exp->getRHS(); |
| 1896 | if (Val.getInt() == 0) |
| 1897 | std::swap(TrueSide, FalseSide); |
| 1898 | |
| 1899 | if (TrueSide && CheckArithmeticConstantExpression(TrueSide)) |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1900 | return true; |
Chris Lattner | 46cfefa | 2008-10-06 05:42:39 +0000 | [diff] [blame] | 1901 | |
| 1902 | // Okay, the evaluated side evaluates to a constant, so we accept this. |
| 1903 | // Check to see if the other side is obviously not a constant. If so, |
| 1904 | // emit a warning that this is a GNU extension. |
Chris Lattner | 45b6b9d | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 1905 | if (FalseSide && !FalseSide->isEvaluatable(Context)) |
Chris Lattner | 46cfefa | 2008-10-06 05:42:39 +0000 | [diff] [blame] | 1906 | Diag(Init->getExprLoc(), |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1907 | diag::ext_typecheck_expression_not_constant_but_accepted) |
| 1908 | << FalseSide->getSourceRange(); |
Chris Lattner | 46cfefa | 2008-10-06 05:42:39 +0000 | [diff] [blame] | 1909 | return false; |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1910 | } |
| 1911 | } |
| 1912 | } |
| 1913 | |
| 1914 | bool Sema::CheckForConstantInitializer(Expr *Init, QualType DclT) { |
Anders Carlsson | 9e09f5d | 2008-12-05 05:09:56 +0000 | [diff] [blame] | 1915 | Expr::EvalResult Result; |
| 1916 | |
Nuno Lopes | 9a979c3 | 2008-07-07 16:46:50 +0000 | [diff] [blame] | 1917 | Init = Init->IgnoreParens(); |
| 1918 | |
Anders Carlsson | 9e09f5d | 2008-12-05 05:09:56 +0000 | [diff] [blame] | 1919 | if (Init->Evaluate(Result, Context) && !Result.HasSideEffects) |
| 1920 | return false; |
| 1921 | |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1922 | // Look through CXXDefaultArgExprs; they have no meaning in this context. |
| 1923 | if (CXXDefaultArgExpr* DAE = dyn_cast<CXXDefaultArgExpr>(Init)) |
| 1924 | return CheckForConstantInitializer(DAE->getExpr(), DclT); |
| 1925 | |
Nuno Lopes | 9a979c3 | 2008-07-07 16:46:50 +0000 | [diff] [blame] | 1926 | if (CompoundLiteralExpr *e = dyn_cast<CompoundLiteralExpr>(Init)) |
| 1927 | return CheckForConstantInitializer(e->getInitializer(), DclT); |
| 1928 | |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1929 | if (InitListExpr *Exp = dyn_cast<InitListExpr>(Init)) { |
| 1930 | unsigned numInits = Exp->getNumInits(); |
| 1931 | for (unsigned i = 0; i < numInits; i++) { |
| 1932 | // FIXME: Need to get the type of the declaration for C++, |
| 1933 | // because it could be a reference? |
| 1934 | if (CheckForConstantInitializer(Exp->getInit(i), |
| 1935 | Exp->getInit(i)->getType())) |
| 1936 | return true; |
| 1937 | } |
| 1938 | return false; |
| 1939 | } |
| 1940 | |
Anders Carlsson | 9e09f5d | 2008-12-05 05:09:56 +0000 | [diff] [blame] | 1941 | // FIXME: We can probably remove some of this code below, now that |
| 1942 | // Expr::Evaluate is doing the heavy lifting for scalars. |
| 1943 | |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1944 | if (Init->isNullPointerConstant(Context)) |
| 1945 | return false; |
| 1946 | if (Init->getType()->isArithmeticType()) { |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 1947 | QualType InitTy = Context.getCanonicalType(Init->getType()) |
| 1948 | .getUnqualifiedType(); |
Eli Friedman | c1cc6dc | 2008-05-30 18:14:48 +0000 | [diff] [blame] | 1949 | if (InitTy == Context.BoolTy) { |
| 1950 | // Special handling for pointers implicitly cast to bool; |
| 1951 | // (e.g. "_Bool rr = &rr;"). This is only legal at the top level. |
| 1952 | if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Init)) { |
| 1953 | Expr* SubE = ICE->getSubExpr(); |
| 1954 | if (SubE->getType()->isPointerType() || |
| 1955 | SubE->getType()->isArrayType() || |
| 1956 | SubE->getType()->isFunctionType()) { |
| 1957 | return CheckAddressConstantExpression(Init); |
| 1958 | } |
| 1959 | } |
| 1960 | } else if (InitTy->isIntegralType()) { |
| 1961 | Expr* SubE = 0; |
Argyrios Kyrtzidis | 0835a3c | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 1962 | if (CastExpr* CE = dyn_cast<CastExpr>(Init)) |
Eli Friedman | c1cc6dc | 2008-05-30 18:14:48 +0000 | [diff] [blame] | 1963 | SubE = CE->getSubExpr(); |
| 1964 | // Special check for pointer cast to int; we allow as an extension |
| 1965 | // an address constant cast to an integer if the integer |
| 1966 | // is of an appropriate width (this sort of code is apparently used |
| 1967 | // in some places). |
| 1968 | // FIXME: Add pedwarn? |
| 1969 | // FIXME: Don't allow bitfields here! Need the FieldDecl for that. |
| 1970 | if (SubE && (SubE->getType()->isPointerType() || |
| 1971 | SubE->getType()->isArrayType() || |
| 1972 | SubE->getType()->isFunctionType())) { |
| 1973 | unsigned IntWidth = Context.getTypeSize(Init->getType()); |
| 1974 | unsigned PointerWidth = Context.getTypeSize(Context.VoidPtrTy); |
| 1975 | if (IntWidth >= PointerWidth) |
| 1976 | return CheckAddressConstantExpression(Init); |
| 1977 | } |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1978 | } |
| 1979 | |
| 1980 | return CheckArithmeticConstantExpression(Init); |
| 1981 | } |
| 1982 | |
| 1983 | if (Init->getType()->isPointerType()) |
| 1984 | return CheckAddressConstantExpression(Init); |
| 1985 | |
Eli Friedman | c1cc6dc | 2008-05-30 18:14:48 +0000 | [diff] [blame] | 1986 | // An array type at the top level that isn't an init-list must |
| 1987 | // be a string literal |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1988 | if (Init->getType()->isArrayType()) |
| 1989 | return false; |
| 1990 | |
Nuno Lopes | 73419bf | 2008-09-01 18:42:41 +0000 | [diff] [blame] | 1991 | if (Init->getType()->isFunctionType()) |
| 1992 | return false; |
| 1993 | |
Steve Naroff | 8af6a45 | 2008-10-02 17:12:56 +0000 | [diff] [blame] | 1994 | // Allow block exprs at top level. |
| 1995 | if (Init->getType()->isBlockPointerType()) |
| 1996 | return false; |
| 1997 | |
Steve Naroff | 6594a70 | 2008-10-27 11:34:16 +0000 | [diff] [blame] | 1998 | InitializerElementNotConstant(Init); |
Eli Friedman | c594b32 | 2008-05-20 13:48:25 +0000 | [diff] [blame] | 1999 | return true; |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 2000 | } |
| 2001 | |
Sebastian Redl | 798d119 | 2008-12-13 16:23:55 +0000 | [diff] [blame] | 2002 | void Sema::AddInitializerToDecl(DeclTy *dcl, ExprArg init) { |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 2003 | Decl *RealDecl = static_cast<Decl *>(dcl); |
Sebastian Redl | 798d119 | 2008-12-13 16:23:55 +0000 | [diff] [blame] | 2004 | Expr *Init = static_cast<Expr *>(init.release()); |
Chris Lattner | 9a11b9a | 2007-10-19 20:10:30 +0000 | [diff] [blame] | 2005 | assert(Init && "missing initializer"); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 2006 | |
Chris Lattner | 9a11b9a | 2007-10-19 20:10:30 +0000 | [diff] [blame] | 2007 | // If there is no declaration, there was an error parsing it. Just ignore |
| 2008 | // the initializer. |
| 2009 | if (RealDecl == 0) { |
| 2010 | delete Init; |
| 2011 | return; |
| 2012 | } |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 2013 | |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 2014 | VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl); |
| 2015 | if (!VDecl) { |
Steve Naroff | 8e74c93 | 2007-09-13 21:41:19 +0000 | [diff] [blame] | 2016 | Diag(dyn_cast<ScopedDecl>(RealDecl)->getLocation(), |
| 2017 | diag::err_illegal_initializer); |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 2018 | RealDecl->setInvalidDecl(); |
| 2019 | return; |
| 2020 | } |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 2021 | // Get the decls type and save a reference for later, since |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 2022 | // CheckInitializerTypes may change it. |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 2023 | QualType DclT = VDecl->getType(), SavT = DclT; |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 2024 | if (VDecl->isBlockVarDecl()) { |
| 2025 | VarDecl::StorageClass SC = VDecl->getStorageClass(); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 2026 | if (SC == VarDecl::Extern) { // C99 6.7.8p5 |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 2027 | Diag(VDecl->getLocation(), diag::err_block_extern_cant_init); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 2028 | VDecl->setInvalidDecl(); |
| 2029 | } else if (!VDecl->isInvalidDecl()) { |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 2030 | if (CheckInitializerTypes(Init, DclT, VDecl->getLocation(), |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2031 | VDecl->getDeclName())) |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 2032 | VDecl->setInvalidDecl(); |
Anders Carlsson | c5eb731 | 2008-08-22 05:00:02 +0000 | [diff] [blame] | 2033 | |
| 2034 | // C++ 3.6.2p2, allow dynamic initialization of static initializers. |
| 2035 | if (!getLangOptions().CPlusPlus) { |
| 2036 | if (SC == VarDecl::Static) // C99 6.7.8p4. |
| 2037 | CheckForConstantInitializer(Init, DclT); |
| 2038 | } |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 2039 | } |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 2040 | } else if (VDecl->isFileVarDecl()) { |
| 2041 | if (VDecl->getStorageClass() == VarDecl::Extern) |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 2042 | Diag(VDecl->getLocation(), diag::warn_extern_init); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 2043 | if (!VDecl->isInvalidDecl()) |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 2044 | if (CheckInitializerTypes(Init, DclT, VDecl->getLocation(), |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2045 | VDecl->getDeclName())) |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 2046 | VDecl->setInvalidDecl(); |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 2047 | |
Anders Carlsson | c5eb731 | 2008-08-22 05:00:02 +0000 | [diff] [blame] | 2048 | // C++ 3.6.2p2, allow dynamic initialization of static initializers. |
| 2049 | if (!getLangOptions().CPlusPlus) { |
| 2050 | // C99 6.7.8p4. All file scoped initializers need to be constant. |
| 2051 | CheckForConstantInitializer(Init, DclT); |
| 2052 | } |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 2053 | } |
| 2054 | // If the type changed, it means we had an incomplete type that was |
| 2055 | // completed by the initializer. For example: |
| 2056 | // int ary[] = { 1, 3, 5 }; |
| 2057 | // "ary" transitions from a VariableArrayType to a ConstantArrayType. |
Christopher Lamb | 48b1239 | 2007-11-29 19:09:19 +0000 | [diff] [blame] | 2058 | if (!VDecl->isInvalidDecl() && (DclT != SavT)) { |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 2059 | VDecl->setType(DclT); |
Christopher Lamb | 48b1239 | 2007-11-29 19:09:19 +0000 | [diff] [blame] | 2060 | Init->setType(DclT); |
| 2061 | } |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 2062 | |
| 2063 | // Attach the initializer to the decl. |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 2064 | VDecl->setInit(Init); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 2065 | return; |
| 2066 | } |
| 2067 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2068 | void Sema::ActOnUninitializedDecl(DeclTy *dcl) { |
| 2069 | Decl *RealDecl = static_cast<Decl *>(dcl); |
| 2070 | |
Argyrios Kyrtzidis | 48c2e90 | 2008-11-07 13:01:22 +0000 | [diff] [blame] | 2071 | // If there is no declaration, there was an error parsing it. Just ignore it. |
| 2072 | if (RealDecl == 0) |
| 2073 | return; |
| 2074 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2075 | if (VarDecl *Var = dyn_cast<VarDecl>(RealDecl)) { |
| 2076 | QualType Type = Var->getType(); |
| 2077 | // C++ [dcl.init.ref]p3: |
| 2078 | // The initializer can be omitted for a reference only in a |
| 2079 | // parameter declaration (8.3.5), in the declaration of a |
| 2080 | // function return type, in the declaration of a class member |
| 2081 | // within its class declaration (9.2), and where the extern |
| 2082 | // specifier is explicitly used. |
Douglas Gregor | 9e7d9de | 2008-12-15 21:24:18 +0000 | [diff] [blame] | 2083 | if (Type->isReferenceType() && |
| 2084 | Var->getStorageClass() != VarDecl::Extern && |
| 2085 | Var->getStorageClass() != VarDecl::PrivateExtern) { |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 2086 | Diag(Var->getLocation(), diag::err_reference_var_requires_init) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2087 | << Var->getDeclName() |
| 2088 | << SourceRange(Var->getLocation(), Var->getLocation()); |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 2089 | Var->setInvalidDecl(); |
| 2090 | return; |
| 2091 | } |
| 2092 | |
| 2093 | // C++ [dcl.init]p9: |
| 2094 | // |
| 2095 | // If no initializer is specified for an object, and the object |
| 2096 | // is of (possibly cv-qualified) non-POD class type (or array |
| 2097 | // thereof), the object shall be default-initialized; if the |
| 2098 | // object is of const-qualified type, the underlying class type |
| 2099 | // shall have a user-declared default constructor. |
| 2100 | if (getLangOptions().CPlusPlus) { |
| 2101 | QualType InitType = Type; |
| 2102 | if (const ArrayType *Array = Context.getAsArrayType(Type)) |
| 2103 | InitType = Array->getElementType(); |
Douglas Gregor | 9e7d9de | 2008-12-15 21:24:18 +0000 | [diff] [blame] | 2104 | if (Var->getStorageClass() != VarDecl::Extern && |
| 2105 | Var->getStorageClass() != VarDecl::PrivateExtern && |
| 2106 | InitType->isRecordType()) { |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 2107 | const CXXConstructorDecl *Constructor |
| 2108 | = PerformInitializationByConstructor(InitType, 0, 0, |
| 2109 | Var->getLocation(), |
| 2110 | SourceRange(Var->getLocation(), |
| 2111 | Var->getLocation()), |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2112 | Var->getDeclName(), |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 2113 | IK_Default); |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 2114 | if (!Constructor) |
| 2115 | Var->setInvalidDecl(); |
| 2116 | } |
| 2117 | } |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2118 | |
Douglas Gregor | 818ce48 | 2008-10-29 13:50:18 +0000 | [diff] [blame] | 2119 | #if 0 |
| 2120 | // FIXME: Temporarily disabled because we are not properly parsing |
| 2121 | // linkage specifications on declarations, e.g., |
| 2122 | // |
| 2123 | // extern "C" const CGPoint CGPointerZero; |
| 2124 | // |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2125 | // C++ [dcl.init]p9: |
| 2126 | // |
| 2127 | // If no initializer is specified for an object, and the |
| 2128 | // object is of (possibly cv-qualified) non-POD class type (or |
| 2129 | // array thereof), the object shall be default-initialized; if |
| 2130 | // the object is of const-qualified type, the underlying class |
| 2131 | // type shall have a user-declared default |
| 2132 | // constructor. Otherwise, if no initializer is specified for |
| 2133 | // an object, the object and its subobjects, if any, have an |
| 2134 | // indeterminate initial value; if the object or any of its |
| 2135 | // subobjects are of const-qualified type, the program is |
| 2136 | // ill-formed. |
| 2137 | // |
| 2138 | // This isn't technically an error in C, so we don't diagnose it. |
| 2139 | // |
| 2140 | // FIXME: Actually perform the POD/user-defined default |
| 2141 | // constructor check. |
| 2142 | if (getLangOptions().CPlusPlus && |
Douglas Gregor | 818ce48 | 2008-10-29 13:50:18 +0000 | [diff] [blame] | 2143 | Context.getCanonicalType(Type).isConstQualified() && |
| 2144 | Var->getStorageClass() != VarDecl::Extern) |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 2145 | Diag(Var->getLocation(), diag::err_const_var_requires_init) |
| 2146 | << Var->getName() |
| 2147 | << SourceRange(Var->getLocation(), Var->getLocation()); |
Douglas Gregor | 818ce48 | 2008-10-29 13:50:18 +0000 | [diff] [blame] | 2148 | #endif |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2149 | } |
| 2150 | } |
| 2151 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2152 | /// The declarators are chained together backwards, reverse the list. |
| 2153 | Sema::DeclTy *Sema::FinalizeDeclaratorGroup(Scope *S, DeclTy *group) { |
| 2154 | // Often we have single declarators, handle them quickly. |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 2155 | Decl *GroupDecl = static_cast<Decl*>(group); |
| 2156 | if (GroupDecl == 0) |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 2157 | return 0; |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 2158 | |
| 2159 | ScopedDecl *Group = dyn_cast<ScopedDecl>(GroupDecl); |
| 2160 | ScopedDecl *NewGroup = 0; |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 2161 | if (Group->getNextDeclarator() == 0) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2162 | NewGroup = Group; |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 2163 | else { // reverse the list. |
| 2164 | while (Group) { |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 2165 | ScopedDecl *Next = Group->getNextDeclarator(); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 2166 | Group->setNextDeclarator(NewGroup); |
| 2167 | NewGroup = Group; |
| 2168 | Group = Next; |
| 2169 | } |
| 2170 | } |
| 2171 | // Perform semantic analysis that depends on having fully processed both |
| 2172 | // the declarator and initializer. |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 2173 | for (ScopedDecl *ID = NewGroup; ID; ID = ID->getNextDeclarator()) { |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 2174 | VarDecl *IDecl = dyn_cast<VarDecl>(ID); |
| 2175 | if (!IDecl) |
| 2176 | continue; |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 2177 | QualType T = IDecl->getType(); |
| 2178 | |
Anders Carlsson | 96e05bc | 2008-12-07 00:20:55 +0000 | [diff] [blame] | 2179 | if (T->isVariableArrayType()) { |
Anders Carlsson | 7fd1df2 | 2008-12-07 00:49:48 +0000 | [diff] [blame] | 2180 | const VariableArrayType *VAT = |
| 2181 | cast<VariableArrayType>(T.getUnqualifiedType()); |
| 2182 | |
| 2183 | // FIXME: This won't give the correct result for |
| 2184 | // int a[10][n]; |
| 2185 | SourceRange SizeRange = VAT->getSizeExpr()->getSourceRange(); |
Anders Carlsson | 96e05bc | 2008-12-07 00:20:55 +0000 | [diff] [blame] | 2186 | if (IDecl->isFileVarDecl()) { |
Anders Carlsson | 7fd1df2 | 2008-12-07 00:49:48 +0000 | [diff] [blame] | 2187 | Diag(IDecl->getLocation(), diag::err_vla_decl_in_file_scope) << |
| 2188 | SizeRange; |
| 2189 | |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2190 | IDecl->setInvalidDecl(); |
Anders Carlsson | 96e05bc | 2008-12-07 00:20:55 +0000 | [diff] [blame] | 2191 | } else { |
| 2192 | // C99 6.7.5.2p2: If an identifier is declared to be an object with |
| 2193 | // static storage duration, it shall not have a variable length array. |
| 2194 | if (IDecl->getStorageClass() == VarDecl::Static) { |
Anders Carlsson | 7fd1df2 | 2008-12-07 00:49:48 +0000 | [diff] [blame] | 2195 | Diag(IDecl->getLocation(), diag::err_vla_decl_has_static_storage) |
| 2196 | << SizeRange; |
Anders Carlsson | 96e05bc | 2008-12-07 00:20:55 +0000 | [diff] [blame] | 2197 | IDecl->setInvalidDecl(); |
| 2198 | } else if (IDecl->getStorageClass() == VarDecl::Extern) { |
Anders Carlsson | 7fd1df2 | 2008-12-07 00:49:48 +0000 | [diff] [blame] | 2199 | Diag(IDecl->getLocation(), diag::err_vla_decl_has_extern_linkage) |
| 2200 | << SizeRange; |
Anders Carlsson | 96e05bc | 2008-12-07 00:20:55 +0000 | [diff] [blame] | 2201 | IDecl->setInvalidDecl(); |
| 2202 | } |
| 2203 | } |
| 2204 | } else if (T->isVariablyModifiedType()) { |
| 2205 | if (IDecl->isFileVarDecl()) { |
| 2206 | Diag(IDecl->getLocation(), diag::err_vm_decl_in_file_scope); |
| 2207 | IDecl->setInvalidDecl(); |
| 2208 | } else { |
| 2209 | if (IDecl->getStorageClass() == VarDecl::Extern) { |
| 2210 | Diag(IDecl->getLocation(), diag::err_vm_decl_has_extern_linkage); |
| 2211 | IDecl->setInvalidDecl(); |
| 2212 | } |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 2213 | } |
| 2214 | } |
Anders Carlsson | 96e05bc | 2008-12-07 00:20:55 +0000 | [diff] [blame] | 2215 | |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 2216 | // Block scope. C99 6.7p7: If an identifier for an object is declared with |
| 2217 | // 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] | 2218 | if (IDecl->isBlockVarDecl() && |
| 2219 | IDecl->getStorageClass() != VarDecl::Extern) { |
Chris Lattner | fd89bc8 | 2008-04-02 01:05:10 +0000 | [diff] [blame] | 2220 | if (T->isIncompleteType() && !IDecl->isInvalidDecl()) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2221 | Diag(IDecl->getLocation(), diag::err_typecheck_decl_incomplete_type)<<T; |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 2222 | IDecl->setInvalidDecl(); |
| 2223 | } |
| 2224 | } |
| 2225 | // File scope. C99 6.9.2p2: A declaration of an identifier for and |
| 2226 | // object that has file scope without an initializer, and without a |
| 2227 | // storage-class specifier or with the storage-class specifier "static", |
| 2228 | // constitutes a tentative definition. Note: A tentative definition with |
| 2229 | // external linkage is valid (C99 6.2.2p5). |
Steve Naroff | ff9eb1f | 2008-08-08 17:50:35 +0000 | [diff] [blame] | 2230 | if (isTentativeDefinition(IDecl)) { |
Eli Friedman | 9db1397 | 2008-02-15 12:53:51 +0000 | [diff] [blame] | 2231 | if (T->isIncompleteArrayType()) { |
Steve Naroff | 9a75f8a | 2008-01-18 20:40:52 +0000 | [diff] [blame] | 2232 | // C99 6.9.2 (p2, p5): Implicit initialization causes an incomplete |
| 2233 | // array to be completed. Don't issue a diagnostic. |
Chris Lattner | fd89bc8 | 2008-04-02 01:05:10 +0000 | [diff] [blame] | 2234 | } else if (T->isIncompleteType() && !IDecl->isInvalidDecl()) { |
Steve Naroff | 9a75f8a | 2008-01-18 20:40:52 +0000 | [diff] [blame] | 2235 | // C99 6.9.2p3: If the declaration of an identifier for an object is |
| 2236 | // a tentative definition and has internal linkage (C99 6.2.2p3), the |
| 2237 | // declared type shall not be an incomplete type. |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2238 | Diag(IDecl->getLocation(), diag::err_typecheck_decl_incomplete_type)<<T; |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 2239 | IDecl->setInvalidDecl(); |
| 2240 | } |
| 2241 | } |
Steve Naroff | ff9eb1f | 2008-08-08 17:50:35 +0000 | [diff] [blame] | 2242 | if (IDecl->isFileVarDecl()) |
| 2243 | CheckForFileScopedRedefinitions(S, IDecl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2244 | } |
| 2245 | return NewGroup; |
| 2246 | } |
Steve Naroff | e1223f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 2247 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2248 | /// ActOnParamDeclarator - Called from Parser::ParseFunctionDeclarator() |
| 2249 | /// to introduce parameters into function prototype scope. |
| 2250 | Sema::DeclTy * |
| 2251 | Sema::ActOnParamDeclarator(Scope *S, Declarator &D) { |
Chris Lattner | 985abd9 | 2008-06-26 06:49:43 +0000 | [diff] [blame] | 2252 | const DeclSpec &DS = D.getDeclSpec(); |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 2253 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2254 | // Verify C99 6.7.5.3p2: The only SCS allowed is 'register'. |
Daniel Dunbar | 33ad012 | 2008-09-03 21:54:21 +0000 | [diff] [blame] | 2255 | VarDecl::StorageClass StorageClass = VarDecl::None; |
| 2256 | if (DS.getStorageClassSpec() == DeclSpec::SCS_register) { |
| 2257 | StorageClass = VarDecl::Register; |
| 2258 | } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) { |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2259 | Diag(DS.getStorageClassSpecLoc(), |
| 2260 | diag::err_invalid_storage_class_in_func_decl); |
Chris Lattner | 985abd9 | 2008-06-26 06:49:43 +0000 | [diff] [blame] | 2261 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2262 | } |
| 2263 | if (DS.isThreadSpecified()) { |
| 2264 | Diag(DS.getThreadSpecLoc(), |
| 2265 | diag::err_invalid_storage_class_in_func_decl); |
Chris Lattner | 985abd9 | 2008-06-26 06:49:43 +0000 | [diff] [blame] | 2266 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2267 | } |
| 2268 | |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 2269 | // Check that there are no default arguments inside the type of this |
| 2270 | // parameter (C++ only). |
| 2271 | if (getLangOptions().CPlusPlus) |
| 2272 | CheckExtraCXXDefaultArguments(D); |
| 2273 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2274 | // In this context, we *do not* check D.getInvalidType(). If the declarator |
| 2275 | // type was invalid, GetTypeForDeclarator() still returns a "valid" type, |
| 2276 | // though it will not reflect the user specified type. |
| 2277 | QualType parmDeclType = GetTypeForDeclarator(D, S); |
| 2278 | |
| 2279 | assert(!parmDeclType.isNull() && "GetTypeForDeclarator() returned null type"); |
| 2280 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2281 | // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope. |
| 2282 | // Can this happen for params? We already checked that they don't conflict |
| 2283 | // among each other. Here they can only shadow globals, which is ok. |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2284 | IdentifierInfo *II = D.getIdentifier(); |
| 2285 | if (Decl *PrevDecl = LookupDecl(II, Decl::IDNS_Ordinary, S)) { |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 2286 | if (PrevDecl->isTemplateParameter()) { |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 2287 | // Maybe we will complain about the shadowed template parameter. |
| 2288 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
| 2289 | // Just pretend that we didn't see the previous declaration. |
| 2290 | PrevDecl = 0; |
| 2291 | } else if (S->isDeclScope(PrevDecl)) { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 2292 | Diag(D.getIdentifierLoc(), diag::err_param_redefinition) << II; |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2293 | |
| 2294 | // Recover by removing the name |
| 2295 | II = 0; |
| 2296 | D.SetIdentifier(0, D.getIdentifierLoc()); |
| 2297 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2298 | } |
Steve Naroff | 6a9f3e3 | 2007-08-07 22:44:21 +0000 | [diff] [blame] | 2299 | |
| 2300 | // Perform the default function/array conversion (C99 6.7.5.3p[7,8]). |
| 2301 | // Doing the promotion here has a win and a loss. The win is the type for |
| 2302 | // both Decl's and DeclRefExpr's will match (a convenient invariant for the |
| 2303 | // code generator). The loss is the orginal type isn't preserved. For example: |
| 2304 | // |
| 2305 | // void func(int parmvardecl[5]) { // convert "int [5]" to "int *" |
| 2306 | // int blockvardecl[5]; |
| 2307 | // sizeof(parmvardecl); // size == 4 |
| 2308 | // sizeof(blockvardecl); // size == 20 |
| 2309 | // } |
| 2310 | // |
| 2311 | // For expressions, all implicit conversions are captured using the |
| 2312 | // ImplicitCastExpr AST node (we have no such mechanism for Decl's). |
| 2313 | // |
| 2314 | // FIXME: If a source translation tool needs to see the original type, then |
| 2315 | // we need to consider storing both types (in ParmVarDecl)... |
| 2316 | // |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2317 | if (parmDeclType->isArrayType()) { |
Chris Lattner | 529bd02 | 2008-01-02 22:50:48 +0000 | [diff] [blame] | 2318 | // int x[restrict 4] -> int *restrict |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2319 | parmDeclType = Context.getArrayDecayedType(parmDeclType); |
Chris Lattner | 529bd02 | 2008-01-02 22:50:48 +0000 | [diff] [blame] | 2320 | } else if (parmDeclType->isFunctionType()) |
Steve Naroff | 6a9f3e3 | 2007-08-07 22:44:21 +0000 | [diff] [blame] | 2321 | parmDeclType = Context.getPointerType(parmDeclType); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2322 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2323 | ParmVarDecl *New = ParmVarDecl::Create(Context, CurContext, |
| 2324 | D.getIdentifierLoc(), II, |
Daniel Dunbar | 33ad012 | 2008-09-03 21:54:21 +0000 | [diff] [blame] | 2325 | parmDeclType, StorageClass, |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2326 | 0, 0); |
Anders Carlsson | f78915f | 2008-02-15 07:04:12 +0000 | [diff] [blame] | 2327 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2328 | if (D.getInvalidType()) |
Steve Naroff | 53a3234 | 2007-08-28 18:45:29 +0000 | [diff] [blame] | 2329 | New->setInvalidDecl(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2330 | |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 2331 | // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1). |
| 2332 | if (D.getCXXScopeSpec().isSet()) { |
| 2333 | Diag(D.getIdentifierLoc(), diag::err_qualified_param_declarator) |
| 2334 | << D.getCXXScopeSpec().getRange(); |
| 2335 | New->setInvalidDecl(); |
| 2336 | } |
| 2337 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2338 | // Add the parameter declaration into this scope. |
| 2339 | S->AddDecl(New); |
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 2340 | if (II) |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2341 | IdResolver.AddDecl(New); |
Nate Begeman | b7894b5 | 2008-02-17 21:20:31 +0000 | [diff] [blame] | 2342 | |
Chris Lattner | 3ff30c8 | 2008-06-29 00:02:00 +0000 | [diff] [blame] | 2343 | ProcessDeclAttributes(New, D); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2344 | return New; |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2345 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2346 | } |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 2347 | |
Chris Lattner | b652cea | 2007-10-09 17:14:05 +0000 | [diff] [blame] | 2348 | Sema::DeclTy *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Declarator &D) { |
Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 2349 | assert(getCurFunctionDecl() == 0 && "Function parsing confused"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2350 | assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function && |
| 2351 | "Not a function declarator!"); |
| 2352 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2353 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2354 | // Verify 6.9.1p6: 'every identifier in the identifier list shall be declared' |
| 2355 | // for a K&R function. |
| 2356 | if (!FTI.hasPrototype) { |
| 2357 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) { |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2358 | if (FTI.ArgInfo[i].Param == 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2359 | Diag(FTI.ArgInfo[i].IdentLoc, diag::ext_param_not_declared) |
| 2360 | << FTI.ArgInfo[i].Ident; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2361 | // Implicitly declare the argument as type 'int' for lack of a better |
| 2362 | // type. |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2363 | DeclSpec DS; |
| 2364 | const char* PrevSpec; // unused |
| 2365 | DS.SetTypeSpecType(DeclSpec::TST_int, FTI.ArgInfo[i].IdentLoc, |
| 2366 | PrevSpec); |
| 2367 | Declarator ParamD(DS, Declarator::KNRTypeListContext); |
| 2368 | ParamD.SetIdentifier(FTI.ArgInfo[i].Ident, FTI.ArgInfo[i].IdentLoc); |
| 2369 | FTI.ArgInfo[i].Param = ActOnParamDeclarator(FnBodyScope, ParamD); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2370 | } |
| 2371 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2372 | } else { |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2373 | // FIXME: Diagnose arguments without names in C. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2374 | } |
| 2375 | |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 2376 | Scope *ParentScope = FnBodyScope->getParent(); |
Steve Naroff | adbbd0c | 2008-01-14 20:51:29 +0000 | [diff] [blame] | 2377 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2378 | return ActOnStartOfFunctionDef(FnBodyScope, |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 2379 | ActOnDeclarator(ParentScope, D, 0, |
| 2380 | /*IsFunctionDefinition=*/true)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2381 | } |
| 2382 | |
| 2383 | Sema::DeclTy *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, DeclTy *D) { |
| 2384 | Decl *decl = static_cast<Decl*>(D); |
Chris Lattner | e9ba323 | 2008-02-16 01:20:36 +0000 | [diff] [blame] | 2385 | FunctionDecl *FD = cast<FunctionDecl>(decl); |
Douglas Gregor | 6fc17ff | 2008-10-29 15:10:40 +0000 | [diff] [blame] | 2386 | |
| 2387 | // See if this is a redefinition. |
| 2388 | const FunctionDecl *Definition; |
| 2389 | if (FD->getBody(Definition)) { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 2390 | Diag(FD->getLocation(), diag::err_redefinition) << FD->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2391 | Diag(Definition->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 6fc17ff | 2008-10-29 15:10:40 +0000 | [diff] [blame] | 2392 | } |
| 2393 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2394 | PushDeclContext(FnBodyScope, FD); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2395 | |
| 2396 | // Check the validity of our function parameters |
| 2397 | CheckParmsForFunctionDef(FD); |
| 2398 | |
| 2399 | // Introduce our parameters into the function scope |
| 2400 | for (unsigned p = 0, NumParams = FD->getNumParams(); p < NumParams; ++p) { |
| 2401 | ParmVarDecl *Param = FD->getParamDecl(p); |
| 2402 | // If this has an identifier, add it to the scope stack. |
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 2403 | if (Param->getIdentifier()) |
| 2404 | PushOnScopeChains(Param, FnBodyScope); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2405 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2406 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2407 | return FD; |
| 2408 | } |
| 2409 | |
Sebastian Redl | 798d119 | 2008-12-13 16:23:55 +0000 | [diff] [blame] | 2410 | Sema::DeclTy *Sema::ActOnFinishFunctionBody(DeclTy *D, StmtArg BodyArg) { |
Steve Naroff | d6d054d | 2007-11-11 23:20:51 +0000 | [diff] [blame] | 2411 | Decl *dcl = static_cast<Decl *>(D); |
Sebastian Redl | 798d119 | 2008-12-13 16:23:55 +0000 | [diff] [blame] | 2412 | Stmt *Body = static_cast<Stmt*>(BodyArg.release()); |
Steve Naroff | 394f3f4 | 2008-07-25 17:57:26 +0000 | [diff] [blame] | 2413 | if (FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(dcl)) { |
Sebastian Redl | 798d119 | 2008-12-13 16:23:55 +0000 | [diff] [blame] | 2414 | FD->setBody(Body); |
Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 2415 | assert(FD == getCurFunctionDecl() && "Function parsing confused"); |
Steve Naroff | 394f3f4 | 2008-07-25 17:57:26 +0000 | [diff] [blame] | 2416 | } else if (ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(dcl)) { |
Steve Naroff | d6d054d | 2007-11-11 23:20:51 +0000 | [diff] [blame] | 2417 | MD->setBody((Stmt*)Body); |
Steve Naroff | 394f3f4 | 2008-07-25 17:57:26 +0000 | [diff] [blame] | 2418 | } else |
| 2419 | return 0; |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 2420 | PopDeclContext(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2421 | // Verify and clean out per-function state. |
| 2422 | |
| 2423 | // Check goto/label use. |
| 2424 | for (llvm::DenseMap<IdentifierInfo*, LabelStmt*>::iterator |
| 2425 | I = LabelMap.begin(), E = LabelMap.end(); I != E; ++I) { |
| 2426 | // Verify that we have no forward references left. If so, there was a goto |
| 2427 | // or address of a label taken, but no definition of it. Label fwd |
| 2428 | // definitions are indicated with a null substmt. |
| 2429 | if (I->second->getSubStmt() == 0) { |
| 2430 | LabelStmt *L = I->second; |
| 2431 | // Emit error. |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 2432 | Diag(L->getIdentLoc(), diag::err_undeclared_label_use) << L->getName(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2433 | |
| 2434 | // At this point, we have gotos that use the bogus label. Stitch it into |
| 2435 | // the function body so that they aren't leaked and that the AST is well |
| 2436 | // formed. |
Chris Lattner | 0cbc215 | 2008-01-25 00:01:10 +0000 | [diff] [blame] | 2437 | if (Body) { |
| 2438 | L->setSubStmt(new NullStmt(L->getIdentLoc())); |
Sebastian Redl | 798d119 | 2008-12-13 16:23:55 +0000 | [diff] [blame] | 2439 | cast<CompoundStmt>(Body)->push_back(L); |
Chris Lattner | 0cbc215 | 2008-01-25 00:01:10 +0000 | [diff] [blame] | 2440 | } else { |
| 2441 | // The whole function wasn't parsed correctly, just delete this. |
| 2442 | delete L; |
| 2443 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2444 | } |
| 2445 | } |
| 2446 | LabelMap.clear(); |
| 2447 | |
Steve Naroff | d6d054d | 2007-11-11 23:20:51 +0000 | [diff] [blame] | 2448 | return D; |
Fariborz Jahanian | 60fbca0 | 2007-11-10 16:31:34 +0000 | [diff] [blame] | 2449 | } |
| 2450 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2451 | /// ImplicitlyDefineFunction - An undeclared identifier was used in a function |
| 2452 | /// 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] | 2453 | ScopedDecl *Sema::ImplicitlyDefineFunction(SourceLocation Loc, |
| 2454 | IdentifierInfo &II, Scope *S) { |
Chris Lattner | 37d1084 | 2008-05-05 21:18:06 +0000 | [diff] [blame] | 2455 | // Extension in C99. Legal in C90, but warn about it. |
| 2456 | if (getLangOptions().C99) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2457 | Diag(Loc, diag::ext_implicit_function_decl) << &II; |
Chris Lattner | 37d1084 | 2008-05-05 21:18:06 +0000 | [diff] [blame] | 2458 | else |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2459 | Diag(Loc, diag::warn_implicit_function_decl) << &II; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2460 | |
| 2461 | // FIXME: handle stuff like: |
| 2462 | // void foo() { extern float X(); } |
| 2463 | // void bar() { X(); } <-- implicit decl for X in another scope. |
| 2464 | |
| 2465 | // Set a Declarator for the implicit definition: int foo(); |
| 2466 | const char *Dummy; |
| 2467 | DeclSpec DS; |
| 2468 | bool Error = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, Dummy); |
| 2469 | Error = Error; // Silence warning. |
| 2470 | assert(!Error && "Error setting up implicit decl!"); |
| 2471 | Declarator D(DS, Declarator::BlockContext); |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2472 | D.AddTypeInfo(DeclaratorChunk::getFunction(false, false, 0, 0, 0, Loc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2473 | D.SetIdentifier(&II, Loc); |
| 2474 | |
Argyrios Kyrtzidis | 93213bb | 2008-05-01 21:04:16 +0000 | [diff] [blame] | 2475 | // Insert this function into translation-unit scope. |
| 2476 | |
| 2477 | DeclContext *PrevDC = CurContext; |
| 2478 | CurContext = Context.getTranslationUnitDecl(); |
| 2479 | |
Steve Naroff | e2ef815 | 2008-04-04 14:32:09 +0000 | [diff] [blame] | 2480 | FunctionDecl *FD = |
Daniel Dunbar | 914701e | 2008-08-05 16:28:08 +0000 | [diff] [blame] | 2481 | dyn_cast<FunctionDecl>(static_cast<Decl*>(ActOnDeclarator(TUScope, D, 0))); |
Steve Naroff | e2ef815 | 2008-04-04 14:32:09 +0000 | [diff] [blame] | 2482 | FD->setImplicit(); |
Argyrios Kyrtzidis | 93213bb | 2008-05-01 21:04:16 +0000 | [diff] [blame] | 2483 | |
| 2484 | CurContext = PrevDC; |
| 2485 | |
Steve Naroff | e2ef815 | 2008-04-04 14:32:09 +0000 | [diff] [blame] | 2486 | return FD; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2487 | } |
| 2488 | |
| 2489 | |
Chris Lattner | 41af093 | 2007-11-14 06:34:38 +0000 | [diff] [blame] | 2490 | TypedefDecl *Sema::ParseTypedefDecl(Scope *S, Declarator &D, QualType T, |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 2491 | ScopedDecl *LastDeclarator) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2492 | assert(D.getIdentifier() && "Wrong callback for declspec without declarator"); |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 2493 | assert(!T.isNull() && "GetTypeForDeclarator() returned null type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2494 | |
| 2495 | // Scope manipulation handled by caller. |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2496 | TypedefDecl *NewTD = TypedefDecl::Create(Context, CurContext, |
| 2497 | D.getIdentifierLoc(), |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 2498 | D.getIdentifier(), |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 2499 | T, LastDeclarator); |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 2500 | if (D.getInvalidType()) |
| 2501 | NewTD->setInvalidDecl(); |
| 2502 | return NewTD; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2503 | } |
| 2504 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 2505 | /// 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] | 2506 | /// former case, Name will be non-null. In the later case, Name will be null. |
| 2507 | /// TagType indicates what kind of tag this is. TK indicates whether this is a |
| 2508 | /// reference/declaration/definition of a tag. |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 2509 | Sema::DeclTy *Sema::ActOnTag(Scope *S, unsigned TagType, TagKind TK, |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2510 | SourceLocation KWLoc, const CXXScopeSpec &SS, |
| 2511 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 2512 | AttributeList *Attr) { |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 2513 | // If this is not a definition, it must have a name. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2514 | assert((Name != 0 || TK == TK_Definition) && |
| 2515 | "Nameless record must be a definition!"); |
| 2516 | |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2517 | TagDecl::TagKind Kind; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2518 | switch (TagType) { |
| 2519 | default: assert(0 && "Unknown tag type!"); |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2520 | case DeclSpec::TST_struct: Kind = TagDecl::TK_struct; break; |
| 2521 | case DeclSpec::TST_union: Kind = TagDecl::TK_union; break; |
| 2522 | case DeclSpec::TST_class: Kind = TagDecl::TK_class; break; |
| 2523 | case DeclSpec::TST_enum: Kind = TagDecl::TK_enum; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2524 | } |
| 2525 | |
Argyrios Kyrtzidis | 0f84a23 | 2008-11-09 22:53:32 +0000 | [diff] [blame] | 2526 | DeclContext *DC = CurContext; |
| 2527 | ScopedDecl *PrevDecl = 0; |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 2528 | |
Argyrios Kyrtzidis | 630c81b | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 2529 | if (Name && SS.isNotEmpty()) { |
| 2530 | // We have a nested-name tag ('struct foo::bar'). |
| 2531 | |
| 2532 | // Check for invalid 'foo::'. |
Argyrios Kyrtzidis | 0f84a23 | 2008-11-09 22:53:32 +0000 | [diff] [blame] | 2533 | if (SS.isInvalid()) { |
Argyrios Kyrtzidis | 630c81b | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 2534 | Name = 0; |
| 2535 | goto CreateNewDecl; |
| 2536 | } |
| 2537 | |
Argyrios Kyrtzidis | 0f84a23 | 2008-11-09 22:53:32 +0000 | [diff] [blame] | 2538 | DC = static_cast<DeclContext*>(SS.getScopeRep()); |
| 2539 | // Look-up name inside 'foo::'. |
Argyrios Kyrtzidis | 630c81b | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 2540 | PrevDecl = dyn_cast_or_null<TagDecl>(LookupDecl(Name, Decl::IDNS_Tag,S,DC)); |
| 2541 | |
| 2542 | // A tag 'foo::bar' must already exist. |
| 2543 | if (PrevDecl == 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2544 | Diag(NameLoc, diag::err_not_tag_in_scope) << Name << SS.getRange(); |
Argyrios Kyrtzidis | 630c81b | 2008-11-09 22:09:58 +0000 | [diff] [blame] | 2545 | Name = 0; |
| 2546 | goto CreateNewDecl; |
| 2547 | } |
| 2548 | } else { |
| 2549 | // If this is a named struct, check to see if there was a previous forward |
| 2550 | // declaration or definition. |
| 2551 | // Use ScopedDecl instead of TagDecl, because a NamespaceDecl may come up. |
| 2552 | PrevDecl = dyn_cast_or_null<ScopedDecl>(LookupDecl(Name, Decl::IDNS_Tag,S)); |
| 2553 | } |
| 2554 | |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 2555 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 2556 | // Maybe we will complain about the shadowed template parameter. |
| 2557 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 2558 | // Just pretend that we didn't see the previous declaration. |
| 2559 | PrevDecl = 0; |
| 2560 | } |
| 2561 | |
Ted Kremenek | 7e8cc57 | 2008-09-02 21:26:19 +0000 | [diff] [blame] | 2562 | if (PrevDecl) { |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2563 | assert((isa<TagDecl>(PrevDecl) || isa<NamespaceDecl>(PrevDecl)) && |
| 2564 | "unexpected Decl type"); |
| 2565 | if (TagDecl *PrevTagDecl = dyn_cast<TagDecl>(PrevDecl)) { |
Chris Lattner | 14943b9 | 2008-07-03 03:30:58 +0000 | [diff] [blame] | 2566 | // If this is a use of a previous tag, or if the tag is already declared |
| 2567 | // in the same scope (so that the definition/declaration completes or |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2568 | // rementions the tag), reuse the decl. |
Argyrios Kyrtzidis | 0f84a23 | 2008-11-09 22:53:32 +0000 | [diff] [blame] | 2569 | if (TK == TK_Reference || isDeclInScope(PrevDecl, DC, S)) { |
Chris Lattner | 14943b9 | 2008-07-03 03:30:58 +0000 | [diff] [blame] | 2570 | // Make sure that this wasn't declared as an enum and now used as a |
| 2571 | // struct or something similar. |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2572 | if (PrevTagDecl->getTagKind() != Kind) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2573 | Diag(KWLoc, diag::err_use_with_wrong_tag) << Name; |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2574 | Diag(PrevDecl->getLocation(), diag::note_previous_use); |
Chris Lattner | 14943b9 | 2008-07-03 03:30:58 +0000 | [diff] [blame] | 2575 | // Recover by making this an anonymous redefinition. |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2576 | Name = 0; |
Chris Lattner | 14943b9 | 2008-07-03 03:30:58 +0000 | [diff] [blame] | 2577 | PrevDecl = 0; |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2578 | } else { |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 2579 | // If this is a use, just return the declaration we found. |
Chris Lattner | 14943b9 | 2008-07-03 03:30:58 +0000 | [diff] [blame] | 2580 | |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 2581 | // FIXME: In the future, return a variant or some other clue |
| 2582 | // for the consumer of this Decl to know it doesn't own it. |
| 2583 | // For our current ASTs this shouldn't be a problem, but will |
| 2584 | // need to be changed with DeclGroups. |
| 2585 | if (TK == TK_Reference) |
Chris Lattner | 14943b9 | 2008-07-03 03:30:58 +0000 | [diff] [blame] | 2586 | return PrevDecl; |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 2587 | |
| 2588 | // Diagnose attempts to redefine a tag. |
| 2589 | if (TK == TK_Definition) { |
| 2590 | if (TagDecl *Def = PrevTagDecl->getDefinition(Context)) { |
| 2591 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 2592 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 2593 | // If this is a redefinition, recover by making this struct be |
| 2594 | // anonymous, which will make any later references get the previous |
| 2595 | // definition. |
| 2596 | Name = 0; |
| 2597 | PrevDecl = 0; |
| 2598 | } |
| 2599 | // Okay, this is definition of a previously declared or referenced |
| 2600 | // tag PrevDecl. We're going to create a new Decl for it. |
| 2601 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2602 | } |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 2603 | // If we get here we have (another) forward declaration or we |
| 2604 | // have a definition. Just create a new decl. |
| 2605 | } else { |
| 2606 | // If we get here, this is a definition of a new tag type in a nested |
| 2607 | // scope, e.g. "struct foo; void bar() { struct foo; }", just create a |
| 2608 | // new decl/type. We set PrevDecl to NULL so that the entities |
| 2609 | // have distinct types. |
| 2610 | PrevDecl = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2611 | } |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 2612 | // If we get here, we're going to create a new Decl. If PrevDecl |
| 2613 | // is non-NULL, it's a definition of the tag declared by |
| 2614 | // PrevDecl. If it's NULL, we have a new definition. |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2615 | } else { |
Argyrios Kyrtzidis | b02ef24 | 2008-07-16 07:45:46 +0000 | [diff] [blame] | 2616 | // PrevDecl is a namespace. |
Argyrios Kyrtzidis | 0f84a23 | 2008-11-09 22:53:32 +0000 | [diff] [blame] | 2617 | if (isDeclInScope(PrevDecl, DC, S)) { |
Ted Kremenek | a89d197 | 2008-09-03 18:03:35 +0000 | [diff] [blame] | 2618 | // The tag name clashes with a namespace name, issue an error and |
| 2619 | // recover by making this tag be anonymous. |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2620 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2621 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Argyrios Kyrtzidis | b02ef24 | 2008-07-16 07:45:46 +0000 | [diff] [blame] | 2622 | Name = 0; |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 2623 | PrevDecl = 0; |
| 2624 | } else { |
| 2625 | // The existing declaration isn't relevant to us; we're in a |
| 2626 | // new scope, so clear out the previous declaration. |
| 2627 | PrevDecl = 0; |
Argyrios Kyrtzidis | b02ef24 | 2008-07-16 07:45:46 +0000 | [diff] [blame] | 2628 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2629 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2630 | } |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 2631 | |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 2632 | CreateNewDecl: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2633 | |
| 2634 | // If there is an identifier, use the location of the identifier as the |
| 2635 | // location of the decl, otherwise use the location of the struct/union |
| 2636 | // keyword. |
| 2637 | SourceLocation Loc = NameLoc.isValid() ? NameLoc : KWLoc; |
| 2638 | |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 2639 | // Otherwise, create a new declaration. If there is a previous |
| 2640 | // declaration of the same entity, the two will be linked via |
| 2641 | // PrevDecl. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2642 | TagDecl *New; |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2643 | if (Kind == TagDecl::TK_enum) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2644 | // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.: |
| 2645 | // enum X { A, B, C } D; D should chain to X. |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 2646 | New = EnumDecl::Create(Context, DC, Loc, Name, |
| 2647 | cast_or_null<EnumDecl>(PrevDecl)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2648 | // If this is an undefined enum, warn. |
| 2649 | if (TK != TK_Definition) Diag(Loc, diag::ext_forward_ref_enum); |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2650 | } else { |
| 2651 | // struct/union/class |
| 2652 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2653 | // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.: |
| 2654 | // struct X { int A; } D; D should chain to X. |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2655 | if (getLangOptions().CPlusPlus) |
Ted Kremenek | 2b345eb | 2008-09-05 17:39:33 +0000 | [diff] [blame] | 2656 | // FIXME: Look for a way to use RecordDecl for simple structs. |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 2657 | New = CXXRecordDecl::Create(Context, Kind, DC, Loc, Name, |
| 2658 | cast_or_null<CXXRecordDecl>(PrevDecl)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2659 | else |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 2660 | New = RecordDecl::Create(Context, Kind, DC, Loc, Name, |
| 2661 | cast_or_null<RecordDecl>(PrevDecl)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2662 | } |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 2663 | |
| 2664 | if (Kind != TagDecl::TK_enum) { |
| 2665 | // Handle #pragma pack: if the #pragma pack stack has non-default |
| 2666 | // alignment, make up a packed attribute for this decl. These |
| 2667 | // attributes are checked when the ASTContext lays out the |
| 2668 | // structure. |
| 2669 | // |
| 2670 | // It is important for implementing the correct semantics that this |
| 2671 | // happen here (in act on tag decl). The #pragma pack stack is |
| 2672 | // maintained as a result of parser callbacks which can occur at |
| 2673 | // many points during the parsing of a struct declaration (because |
| 2674 | // the #pragma tokens are effectively skipped over during the |
| 2675 | // parsing of the struct). |
| 2676 | if (unsigned Alignment = PackContext.getAlignment()) |
| 2677 | New->addAttr(new PackedAttr(Alignment * 8)); |
| 2678 | } |
| 2679 | |
| 2680 | if (Attr) |
| 2681 | ProcessDeclAttributeList(New, Attr); |
| 2682 | |
| 2683 | // Set the lexical context. If the tag has a C++ scope specifier, the |
| 2684 | // lexical context will be different from the semantic context. |
| 2685 | New->setLexicalDeclContext(CurContext); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2686 | |
| 2687 | // If this has an identifier, add it to the scope stack. |
| 2688 | if (Name) { |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 2689 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 2690 | // we find one that is. |
| 2691 | while ((S->getFlags() & Scope::DeclScope) == 0) |
| 2692 | S = S->getParent(); |
| 2693 | |
| 2694 | // Add it to the decl chain. |
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 2695 | PushOnScopeChains(New, S); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2696 | } |
Argyrios Kyrtzidis | 5239304 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 2697 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2698 | return New; |
| 2699 | } |
| 2700 | |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2701 | |
Chris Lattner | 1d353ba | 2008-11-12 21:17:48 +0000 | [diff] [blame] | 2702 | /// TryToFixInvalidVariablyModifiedType - Helper method to turn variable array |
| 2703 | /// types into constant array types in certain situations which would otherwise |
| 2704 | /// be errors (for GCC compatibility). |
| 2705 | static QualType TryToFixInvalidVariablyModifiedType(QualType T, |
| 2706 | ASTContext &Context) { |
Eli Friedman | 1b76ada | 2008-06-03 21:01:11 +0000 | [diff] [blame] | 2707 | // This method tries to turn a variable array into a constant |
| 2708 | // array even when the size isn't an ICE. This is necessary |
| 2709 | // for compatibility with code that depends on gcc's buggy |
| 2710 | // constant expression folding, like struct {char x[(int)(char*)2];} |
Chris Lattner | 57d5788 | 2008-11-12 19:48:13 +0000 | [diff] [blame] | 2711 | const VariableArrayType* VLATy = dyn_cast<VariableArrayType>(T); |
| 2712 | if (!VLATy) return QualType(); |
| 2713 | |
| 2714 | APValue Result; |
| 2715 | if (!VLATy->getSizeExpr() || |
Chris Lattner | 6ee7aa1 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 2716 | !VLATy->getSizeExpr()->Evaluate(Result, Context)) |
Chris Lattner | 57d5788 | 2008-11-12 19:48:13 +0000 | [diff] [blame] | 2717 | return QualType(); |
| 2718 | |
| 2719 | assert(Result.isInt() && "Size expressions must be integers!"); |
| 2720 | llvm::APSInt &Res = Result.getInt(); |
| 2721 | if (Res > llvm::APSInt(Res.getBitWidth(), Res.isUnsigned())) |
| 2722 | return Context.getConstantArrayType(VLATy->getElementType(), |
| 2723 | Res, ArrayType::Normal, 0); |
Eli Friedman | 1b76ada | 2008-06-03 21:01:11 +0000 | [diff] [blame] | 2724 | return QualType(); |
| 2725 | } |
| 2726 | |
Anders Carlsson | 9f1e572 | 2008-12-06 20:33:04 +0000 | [diff] [blame] | 2727 | bool Sema::VerifyBitField(SourceLocation FieldLoc, IdentifierInfo *FieldName, |
Chris Lattner | cd08707 | 2008-12-12 04:56:04 +0000 | [diff] [blame] | 2728 | QualType FieldTy, const Expr *BitWidth) { |
Anders Carlsson | 9f1e572 | 2008-12-06 20:33:04 +0000 | [diff] [blame] | 2729 | // FIXME: 6.7.2.1p4 - verify the field type. |
| 2730 | |
| 2731 | llvm::APSInt Value; |
| 2732 | if (VerifyIntegerConstantExpression(BitWidth, &Value)) |
| 2733 | return true; |
| 2734 | |
Chris Lattner | cd08707 | 2008-12-12 04:56:04 +0000 | [diff] [blame] | 2735 | // Zero-width bitfield is ok for anonymous field. |
| 2736 | if (Value == 0 && FieldName) |
| 2737 | return Diag(FieldLoc, diag::err_bitfield_has_zero_width) << FieldName; |
| 2738 | |
| 2739 | if (Value.isNegative()) |
| 2740 | return Diag(FieldLoc, diag::err_bitfield_has_negative_width) << FieldName; |
Anders Carlsson | 9f1e572 | 2008-12-06 20:33:04 +0000 | [diff] [blame] | 2741 | |
| 2742 | uint64_t TypeSize = Context.getTypeSize(FieldTy); |
| 2743 | // FIXME: We won't need the 0 size once we check that the field type is valid. |
Chris Lattner | cd08707 | 2008-12-12 04:56:04 +0000 | [diff] [blame] | 2744 | if (TypeSize && Value.getZExtValue() > TypeSize) |
| 2745 | return Diag(FieldLoc, diag::err_bitfield_width_exceeds_type_size) |
| 2746 | << FieldName << (unsigned)TypeSize; |
Anders Carlsson | 9f1e572 | 2008-12-06 20:33:04 +0000 | [diff] [blame] | 2747 | |
| 2748 | return false; |
| 2749 | } |
| 2750 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 2751 | /// 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] | 2752 | /// to create a FieldDecl object for it. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2753 | Sema::DeclTy *Sema::ActOnField(Scope *S, DeclTy *TagD, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2754 | SourceLocation DeclStart, |
| 2755 | Declarator &D, ExprTy *BitfieldWidth) { |
| 2756 | IdentifierInfo *II = D.getIdentifier(); |
| 2757 | Expr *BitWidth = (Expr*)BitfieldWidth; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2758 | SourceLocation Loc = DeclStart; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2759 | RecordDecl *Record = (RecordDecl *)TagD; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2760 | if (II) Loc = D.getIdentifierLoc(); |
| 2761 | |
| 2762 | // FIXME: Unnamed fields can be handled in various different ways, for |
| 2763 | // example, unnamed unions inject all members into the struct namespace! |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2764 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2765 | QualType T = GetTypeForDeclarator(D, S); |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 2766 | assert(!T.isNull() && "GetTypeForDeclarator() returned null type"); |
| 2767 | bool InvalidDecl = false; |
Anders Carlsson | 9f1e572 | 2008-12-06 20:33:04 +0000 | [diff] [blame] | 2768 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2769 | // C99 6.7.2.1p8: A member of a structure or union may have any type other |
| 2770 | // than a variably modified type. |
Eli Friedman | 9db1397 | 2008-02-15 12:53:51 +0000 | [diff] [blame] | 2771 | if (T->isVariablyModifiedType()) { |
Chris Lattner | 1d353ba | 2008-11-12 21:17:48 +0000 | [diff] [blame] | 2772 | QualType FixedTy = TryToFixInvalidVariablyModifiedType(T, Context); |
Eli Friedman | 1b76ada | 2008-06-03 21:01:11 +0000 | [diff] [blame] | 2773 | if (!FixedTy.isNull()) { |
Chris Lattner | 23cd0d9 | 2008-11-13 18:49:38 +0000 | [diff] [blame] | 2774 | Diag(Loc, diag::warn_illegal_constant_array_size); |
Eli Friedman | 1b76ada | 2008-06-03 21:01:11 +0000 | [diff] [blame] | 2775 | T = FixedTy; |
| 2776 | } else { |
Chris Lattner | 23cd0d9 | 2008-11-13 18:49:38 +0000 | [diff] [blame] | 2777 | Diag(Loc, diag::err_typecheck_field_variable_size); |
Chris Lattner | 3ab5543 | 2008-11-12 19:45:49 +0000 | [diff] [blame] | 2778 | T = Context.IntTy; |
Eli Friedman | 1b76ada | 2008-06-03 21:01:11 +0000 | [diff] [blame] | 2779 | InvalidDecl = true; |
| 2780 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2781 | } |
Anders Carlsson | 9f1e572 | 2008-12-06 20:33:04 +0000 | [diff] [blame] | 2782 | |
| 2783 | if (BitWidth) { |
| 2784 | if (VerifyBitField(Loc, II, T, BitWidth)) |
| 2785 | InvalidDecl = true; |
| 2786 | } else { |
| 2787 | // Not a bitfield. |
| 2788 | |
| 2789 | // validate II. |
| 2790 | |
| 2791 | } |
| 2792 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2793 | // FIXME: Chain fielddecls together. |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2794 | FieldDecl *NewFD; |
| 2795 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2796 | // FIXME: We don't want CurContext for C, do we? No, we'll need some |
| 2797 | // other way to determine the current RecordDecl. |
| 2798 | NewFD = FieldDecl::Create(Context, Record, |
| 2799 | Loc, II, T, BitWidth, |
| 2800 | D.getDeclSpec().getStorageClassSpec() == |
| 2801 | DeclSpec::SCS_mutable, |
| 2802 | /*PrevDecl=*/0); |
| 2803 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2804 | if (getLangOptions().CPlusPlus) |
| 2805 | CheckExtraCXXDefaultArguments(D); |
| 2806 | |
Chris Lattner | 3ff30c8 | 2008-06-29 00:02:00 +0000 | [diff] [blame] | 2807 | ProcessDeclAttributes(NewFD, D); |
Anders Carlsson | ad14806 | 2008-02-16 00:29:18 +0000 | [diff] [blame] | 2808 | |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 2809 | if (D.getInvalidType() || InvalidDecl) |
| 2810 | NewFD->setInvalidDecl(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2811 | |
| 2812 | if (II && getLangOptions().CPlusPlus) |
| 2813 | PushOnScopeChains(NewFD, S); |
| 2814 | else |
| 2815 | Record->addDecl(Context, NewFD); |
| 2816 | |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 2817 | return NewFD; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2818 | } |
| 2819 | |
Fariborz Jahanian | 89204a1 | 2007-10-01 16:53:59 +0000 | [diff] [blame] | 2820 | /// TranslateIvarVisibility - Translate visibility from a token ID to an |
| 2821 | /// AST enum value. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2822 | static ObjCIvarDecl::AccessControl |
Fariborz Jahanian | 89204a1 | 2007-10-01 16:53:59 +0000 | [diff] [blame] | 2823 | TranslateIvarVisibility(tok::ObjCKeywordKind ivarVisibility) { |
Steve Naroff | f13271f | 2007-09-14 23:09:53 +0000 | [diff] [blame] | 2824 | switch (ivarVisibility) { |
Chris Lattner | 33d34a6 | 2008-10-12 00:28:42 +0000 | [diff] [blame] | 2825 | default: assert(0 && "Unknown visitibility kind"); |
| 2826 | case tok::objc_private: return ObjCIvarDecl::Private; |
| 2827 | case tok::objc_public: return ObjCIvarDecl::Public; |
| 2828 | case tok::objc_protected: return ObjCIvarDecl::Protected; |
| 2829 | case tok::objc_package: return ObjCIvarDecl::Package; |
Steve Naroff | f13271f | 2007-09-14 23:09:53 +0000 | [diff] [blame] | 2830 | } |
| 2831 | } |
| 2832 | |
Fariborz Jahanian | 45bc03f | 2008-04-11 16:55:42 +0000 | [diff] [blame] | 2833 | /// ActOnIvar - Each ivar field of an objective-c class is passed into this |
| 2834 | /// in order to create an IvarDecl object for it. |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 2835 | Sema::DeclTy *Sema::ActOnIvar(Scope *S, |
Fariborz Jahanian | 45bc03f | 2008-04-11 16:55:42 +0000 | [diff] [blame] | 2836 | SourceLocation DeclStart, |
| 2837 | Declarator &D, ExprTy *BitfieldWidth, |
| 2838 | tok::ObjCKeywordKind Visibility) { |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 2839 | IdentifierInfo *II = D.getIdentifier(); |
| 2840 | Expr *BitWidth = (Expr*)BitfieldWidth; |
| 2841 | SourceLocation Loc = DeclStart; |
| 2842 | if (II) Loc = D.getIdentifierLoc(); |
| 2843 | |
| 2844 | // FIXME: Unnamed fields can be handled in various different ways, for |
| 2845 | // example, unnamed unions inject all members into the struct namespace! |
| 2846 | |
Anders Carlsson | 9f1e572 | 2008-12-06 20:33:04 +0000 | [diff] [blame] | 2847 | QualType T = GetTypeForDeclarator(D, S); |
| 2848 | assert(!T.isNull() && "GetTypeForDeclarator() returned null type"); |
| 2849 | bool InvalidDecl = false; |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 2850 | |
| 2851 | if (BitWidth) { |
| 2852 | // TODO: Validate. |
| 2853 | //printf("WARNING: BITFIELDS IGNORED!\n"); |
| 2854 | |
| 2855 | // 6.7.2.1p3 |
| 2856 | // 6.7.2.1p4 |
| 2857 | |
| 2858 | } else { |
| 2859 | // Not a bitfield. |
| 2860 | |
| 2861 | // validate II. |
| 2862 | |
| 2863 | } |
| 2864 | |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 2865 | // C99 6.7.2.1p8: A member of a structure or union may have any type other |
| 2866 | // than a variably modified type. |
| 2867 | if (T->isVariablyModifiedType()) { |
Anders Carlsson | 96e05bc | 2008-12-07 00:20:55 +0000 | [diff] [blame] | 2868 | Diag(Loc, diag::err_typecheck_ivar_variable_size); |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 2869 | InvalidDecl = true; |
| 2870 | } |
| 2871 | |
Ted Kremenek | b8db21d | 2008-07-23 18:04:17 +0000 | [diff] [blame] | 2872 | // Get the visibility (access control) for this ivar. |
| 2873 | ObjCIvarDecl::AccessControl ac = |
| 2874 | Visibility != tok::objc_not_keyword ? TranslateIvarVisibility(Visibility) |
| 2875 | : ObjCIvarDecl::None; |
| 2876 | |
| 2877 | // Construct the decl. |
| 2878 | ObjCIvarDecl *NewID = ObjCIvarDecl::Create(Context, Loc, II, T, ac, |
Steve Naroff | 8f3b265 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 2879 | (Expr *)BitfieldWidth); |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 2880 | |
Ted Kremenek | b8db21d | 2008-07-23 18:04:17 +0000 | [diff] [blame] | 2881 | // Process attributes attached to the ivar. |
Chris Lattner | 3ff30c8 | 2008-06-29 00:02:00 +0000 | [diff] [blame] | 2882 | ProcessDeclAttributes(NewID, D); |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 2883 | |
| 2884 | if (D.getInvalidType() || InvalidDecl) |
| 2885 | NewID->setInvalidDecl(); |
Ted Kremenek | b8db21d | 2008-07-23 18:04:17 +0000 | [diff] [blame] | 2886 | |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 2887 | return NewID; |
| 2888 | } |
| 2889 | |
Fariborz Jahanian | 9d048ff | 2007-09-29 00:54:24 +0000 | [diff] [blame] | 2890 | void Sema::ActOnFields(Scope* S, |
Fariborz Jahanian | 3f5faf7 | 2007-10-04 00:45:27 +0000 | [diff] [blame] | 2891 | SourceLocation RecLoc, DeclTy *RecDecl, |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 2892 | DeclTy **Fields, unsigned NumFields, |
Daniel Dunbar | 1bfe1c2 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 2893 | SourceLocation LBrac, SourceLocation RBrac, |
Daniel Dunbar | 7d07664 | 2008-10-03 17:33:35 +0000 | [diff] [blame] | 2894 | AttributeList *Attr) { |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 2895 | Decl *EnclosingDecl = static_cast<Decl*>(RecDecl); |
| 2896 | assert(EnclosingDecl && "missing record or interface decl"); |
| 2897 | RecordDecl *Record = dyn_cast<RecordDecl>(EnclosingDecl); |
| 2898 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2899 | if (Record) |
| 2900 | if (RecordDecl* DefRecord = Record->getDefinition(Context)) { |
| 2901 | // Diagnose code like: |
| 2902 | // struct S { struct S {} X; }; |
| 2903 | // We discover this when we complete the outer S. Reject and ignore the |
| 2904 | // outer S. |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 2905 | Diag(DefRecord->getLocation(), diag::err_nested_redefinition) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2906 | << DefRecord->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2907 | Diag(RecLoc, diag::note_previous_definition); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2908 | Record->setInvalidDecl(); |
| 2909 | return; |
| 2910 | } |
| 2911 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2912 | // Verify that all the fields are okay. |
| 2913 | unsigned NumNamedMembers = 0; |
| 2914 | llvm::SmallVector<FieldDecl*, 32> RecFields; |
| 2915 | llvm::SmallSet<const IdentifierInfo*, 32> FieldIDs; |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 2916 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2917 | for (unsigned i = 0; i != NumFields; ++i) { |
Fariborz Jahanian | e267ab6 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 2918 | |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 2919 | FieldDecl *FD = cast_or_null<FieldDecl>(static_cast<Decl*>(Fields[i])); |
| 2920 | assert(FD && "missing field decl"); |
| 2921 | |
| 2922 | // Remember all fields. |
| 2923 | RecFields.push_back(FD); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2924 | |
| 2925 | // Get the type for the field. |
Chris Lattner | 02c642e | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 2926 | Type *FDTy = FD->getType().getTypePtr(); |
Steve Naroff | f13271f | 2007-09-14 23:09:53 +0000 | [diff] [blame] | 2927 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2928 | // 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] | 2929 | if (FDTy->isFunctionType()) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 2930 | Diag(FD->getLocation(), diag::err_field_declared_as_function) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2931 | << FD->getDeclName(); |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 2932 | FD->setInvalidDecl(); |
| 2933 | EnclosingDecl->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2934 | continue; |
| 2935 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2936 | // C99 6.7.2.1p2 - A field may not be an incomplete type except... |
| 2937 | if (FDTy->isIncompleteType()) { |
Fariborz Jahanian | e267ab6 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 2938 | if (!Record) { // Incomplete ivar type is always an error. |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2939 | Diag(FD->getLocation(), diag::err_field_incomplete) <<FD->getDeclName(); |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 2940 | FD->setInvalidDecl(); |
| 2941 | EnclosingDecl->setInvalidDecl(); |
Fariborz Jahanian | 3f5faf7 | 2007-10-04 00:45:27 +0000 | [diff] [blame] | 2942 | continue; |
Fariborz Jahanian | e267ab6 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 2943 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2944 | if (i != NumFields-1 || // ... that the last member ... |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2945 | !Record->isStruct() || // ... of a structure ... |
Chris Lattner | 02c642e | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 2946 | !FDTy->isArrayType()) { //... may have incomplete array type. |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2947 | Diag(FD->getLocation(), diag::err_field_incomplete) <<FD->getDeclName(); |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 2948 | FD->setInvalidDecl(); |
| 2949 | EnclosingDecl->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2950 | continue; |
| 2951 | } |
Fariborz Jahanian | e267ab6 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 2952 | if (NumNamedMembers < 1) { //... must have more than named member ... |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 2953 | Diag(FD->getLocation(), diag::err_flexible_array_empty_struct) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2954 | << FD->getDeclName(); |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 2955 | FD->setInvalidDecl(); |
| 2956 | EnclosingDecl->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2957 | continue; |
| 2958 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2959 | // 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] | 2960 | if (Record) |
| 2961 | Record->setHasFlexibleArrayMember(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2962 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2963 | /// C99 6.7.2.1p2 - a struct ending in a flexible array member cannot be the |
| 2964 | /// field of another structure or the element of an array. |
Chris Lattner | 02c642e | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 2965 | if (const RecordType *FDTTy = FDTy->getAsRecordType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2966 | if (FDTTy->getDecl()->hasFlexibleArrayMember()) { |
| 2967 | // 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] | 2968 | if (Record && Record->isUnion()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2969 | Record->setHasFlexibleArrayMember(true); |
| 2970 | } else { |
| 2971 | // If this is a struct/class and this is not the last element, reject |
| 2972 | // it. Note that GCC supports variable sized arrays in the middle of |
| 2973 | // structures. |
| 2974 | if (i != NumFields-1) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 2975 | Diag(FD->getLocation(), diag::err_variable_sized_type_in_struct) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2976 | << FD->getDeclName(); |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 2977 | FD->setInvalidDecl(); |
| 2978 | EnclosingDecl->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2979 | continue; |
| 2980 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2981 | // We support flexible arrays at the end of structs in other structs |
| 2982 | // as an extension. |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 2983 | Diag(FD->getLocation(), diag::ext_flexible_array_in_struct) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2984 | << FD->getDeclName(); |
Fariborz Jahanian | 3f5faf7 | 2007-10-04 00:45:27 +0000 | [diff] [blame] | 2985 | if (Record) |
Fariborz Jahanian | e267ab6 | 2007-09-14 16:27:55 +0000 | [diff] [blame] | 2986 | Record->setHasFlexibleArrayMember(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2987 | } |
| 2988 | } |
| 2989 | } |
Fariborz Jahanian | e7f64cc | 2007-10-12 22:10:42 +0000 | [diff] [blame] | 2990 | /// A field cannot be an Objective-c object |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2991 | if (FDTy->isObjCInterfaceType()) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 2992 | Diag(FD->getLocation(), diag::err_statically_allocated_object) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 2993 | << FD->getDeclName(); |
Fariborz Jahanian | e7f64cc | 2007-10-12 22:10:42 +0000 | [diff] [blame] | 2994 | FD->setInvalidDecl(); |
| 2995 | EnclosingDecl->setInvalidDecl(); |
| 2996 | continue; |
| 2997 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2998 | // Keep track of the number of named members. |
| 2999 | if (IdentifierInfo *II = FD->getIdentifier()) { |
| 3000 | // Detect duplicate member names. |
| 3001 | if (!FieldIDs.insert(II)) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3002 | Diag(FD->getLocation(), diag::err_duplicate_member) << II; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3003 | // Find the previous decl. |
| 3004 | SourceLocation PrevLoc; |
Chris Lattner | 33d34a6 | 2008-10-12 00:28:42 +0000 | [diff] [blame] | 3005 | for (unsigned i = 0; ; ++i) { |
| 3006 | assert(i != RecFields.size() && "Didn't find previous def!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3007 | if (RecFields[i]->getIdentifier() == II) { |
| 3008 | PrevLoc = RecFields[i]->getLocation(); |
| 3009 | break; |
| 3010 | } |
| 3011 | } |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 3012 | Diag(PrevLoc, diag::note_previous_definition); |
Steve Naroff | 7421664 | 2007-09-14 22:20:54 +0000 | [diff] [blame] | 3013 | FD->setInvalidDecl(); |
| 3014 | EnclosingDecl->setInvalidDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3015 | continue; |
| 3016 | } |
| 3017 | ++NumNamedMembers; |
| 3018 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3019 | } |
| 3020 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3021 | // Okay, we successfully defined 'Record'. |
Chris Lattner | e1e7985 | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 3022 | if (Record) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3023 | Record->completeDefinition(Context); |
Argyrios Kyrtzidis | a4755c6 | 2008-08-09 00:58:37 +0000 | [diff] [blame] | 3024 | // If this is a C++ record, HandleTagDeclDefinition will be invoked in |
| 3025 | // Sema::ActOnFinishCXXClassDef. |
| 3026 | if (!isa<CXXRecordDecl>(Record)) |
| 3027 | Consumer.HandleTagDeclDefinition(Record); |
Chris Lattner | e1e7985 | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 3028 | } else { |
Chris Lattner | a91d381 | 2008-02-05 22:40:55 +0000 | [diff] [blame] | 3029 | ObjCIvarDecl **ClsFields = reinterpret_cast<ObjCIvarDecl**>(&RecFields[0]); |
Fariborz Jahanian | 60f8c86 | 2008-12-13 20:28:25 +0000 | [diff] [blame] | 3030 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(EnclosingDecl)) { |
Chris Lattner | a91d381 | 2008-02-05 22:40:55 +0000 | [diff] [blame] | 3031 | ID->addInstanceVariablesToClass(ClsFields, RecFields.size(), RBrac); |
Fariborz Jahanian | a769c00 | 2008-12-17 21:40:49 +0000 | [diff] [blame] | 3032 | #if 0 |
Fariborz Jahanian | 3281eff | 2008-12-16 01:08:35 +0000 | [diff] [blame] | 3033 | // Must enforce the rule that ivars in the base classes may not be |
| 3034 | // duplicates. |
| 3035 | FieldIDs.clear(); |
| 3036 | RecordDecl *RD = ID->getRecordForDecl(); |
| 3037 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 3038 | e = RD->field_end(); i != e; ++i) { |
| 3039 | FieldDecl *FD = *i; |
| 3040 | if (IdentifierInfo *II = FD->getIdentifier()) |
| 3041 | if (!FieldIDs.insert(II)) { |
| 3042 | Diag(FD->getLocation(), diag::err_duplicate_member) << II; |
| 3043 | FD->setInvalidDecl(); |
| 3044 | for (RecordDecl::field_iterator j = RD->field_begin(), |
| 3045 | e1 = RD->field_end(); j != e1; ++j) |
| 3046 | if (II == (*j)->getIdentifier()) { |
| 3047 | Diag((*j)->getLocation(), diag::note_previous_definition); |
| 3048 | break; |
| 3049 | } |
| 3050 | } |
| 3051 | } |
Fariborz Jahanian | a769c00 | 2008-12-17 21:40:49 +0000 | [diff] [blame] | 3052 | #endif |
Fariborz Jahanian | 60f8c86 | 2008-12-13 20:28:25 +0000 | [diff] [blame] | 3053 | } |
Chris Lattner | a91d381 | 2008-02-05 22:40:55 +0000 | [diff] [blame] | 3054 | else if (ObjCImplementationDecl *IMPDecl = |
| 3055 | dyn_cast<ObjCImplementationDecl>(EnclosingDecl)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3056 | assert(IMPDecl && "ActOnFields - missing ObjCImplementationDecl"); |
| 3057 | IMPDecl->ObjCAddInstanceVariablesToClassImpl(ClsFields, RecFields.size()); |
Fariborz Jahanian | 3a3ca1b | 2007-10-31 18:48:14 +0000 | [diff] [blame] | 3058 | CheckImplementationIvars(IMPDecl, ClsFields, RecFields.size(), RBrac); |
Fariborz Jahanian | d0b90bf | 2007-09-26 18:27:25 +0000 | [diff] [blame] | 3059 | } |
Fariborz Jahanian | b04a021 | 2007-09-14 21:08:27 +0000 | [diff] [blame] | 3060 | } |
Daniel Dunbar | 7d07664 | 2008-10-03 17:33:35 +0000 | [diff] [blame] | 3061 | |
| 3062 | if (Attr) |
| 3063 | ProcessDeclAttributeList(Record, Attr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3064 | } |
| 3065 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 3066 | Sema::DeclTy *Sema::ActOnEnumConstant(Scope *S, DeclTy *theEnumDecl, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3067 | DeclTy *lastEnumConst, |
| 3068 | SourceLocation IdLoc, IdentifierInfo *Id, |
| 3069 | SourceLocation EqualLoc, ExprTy *val) { |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 3070 | EnumDecl *TheEnumDecl = cast<EnumDecl>(static_cast<Decl*>(theEnumDecl)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3071 | EnumConstantDecl *LastEnumConst = |
| 3072 | cast_or_null<EnumConstantDecl>(static_cast<Decl*>(lastEnumConst)); |
| 3073 | Expr *Val = static_cast<Expr*>(val); |
| 3074 | |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 3075 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 3076 | // we find one that is. |
| 3077 | while ((S->getFlags() & Scope::DeclScope) == 0) |
| 3078 | S = S->getParent(); |
| 3079 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3080 | // Verify that there isn't already something declared with this name in this |
| 3081 | // scope. |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 3082 | Decl *PrevDecl = LookupDecl(Id, Decl::IDNS_Ordinary, S); |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 3083 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 3084 | // Maybe we will complain about the shadowed template parameter. |
| 3085 | DiagnoseTemplateParameterShadow(IdLoc, PrevDecl); |
| 3086 | // Just pretend that we didn't see the previous declaration. |
| 3087 | PrevDecl = 0; |
| 3088 | } |
| 3089 | |
| 3090 | if (PrevDecl) { |
Argyrios Kyrtzidis | 0ff12f0 | 2008-07-16 21:01:53 +0000 | [diff] [blame] | 3091 | // When in C++, we may get a TagDecl with the same name; in this case the |
| 3092 | // enum constant will 'hide' the tag. |
| 3093 | assert((getLangOptions().CPlusPlus || !isa<TagDecl>(PrevDecl)) && |
| 3094 | "Received TagDecl when not in C++!"); |
Argyrios Kyrtzidis | 15a12d0 | 2008-09-09 21:18:04 +0000 | [diff] [blame] | 3095 | if (!isa<TagDecl>(PrevDecl) && isDeclInScope(PrevDecl, CurContext, S)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3096 | if (isa<EnumConstantDecl>(PrevDecl)) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3097 | Diag(IdLoc, diag::err_redefinition_of_enumerator) << Id; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3098 | else |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3099 | Diag(IdLoc, diag::err_redefinition) << Id; |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 3100 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | a73349d | 2008-02-26 00:33:57 +0000 | [diff] [blame] | 3101 | delete Val; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3102 | return 0; |
| 3103 | } |
| 3104 | } |
| 3105 | |
| 3106 | llvm::APSInt EnumVal(32); |
| 3107 | QualType EltTy; |
| 3108 | if (Val) { |
Chris Lattner | 421a23d | 2007-08-27 21:16:18 +0000 | [diff] [blame] | 3109 | // Make sure to promote the operand type to int. |
| 3110 | UsualUnaryConversions(Val); |
| 3111 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3112 | // C99 6.7.2.2p2: Make sure we have an integer constant expression. |
| 3113 | SourceLocation ExpLoc; |
Anders Carlsson | 49184b2 | 2008-12-05 16:33:57 +0000 | [diff] [blame] | 3114 | if (VerifyIntegerConstantExpression(Val, &EnumVal)) { |
Chris Lattner | a73349d | 2008-02-26 00:33:57 +0000 | [diff] [blame] | 3115 | delete Val; |
Chris Lattner | b7416f9 | 2007-08-27 17:37:24 +0000 | [diff] [blame] | 3116 | Val = 0; // Just forget about it. |
Chris Lattner | e9ca851 | 2007-08-29 16:03:41 +0000 | [diff] [blame] | 3117 | } else { |
| 3118 | EltTy = Val->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3119 | } |
Chris Lattner | b7416f9 | 2007-08-27 17:37:24 +0000 | [diff] [blame] | 3120 | } |
| 3121 | |
| 3122 | if (!Val) { |
| 3123 | if (LastEnumConst) { |
| 3124 | // Assign the last value + 1. |
| 3125 | EnumVal = LastEnumConst->getInitVal(); |
| 3126 | ++EnumVal; |
Chris Lattner | 421a23d | 2007-08-27 21:16:18 +0000 | [diff] [blame] | 3127 | |
| 3128 | // Check for overflow on increment. |
| 3129 | if (EnumVal < LastEnumConst->getInitVal()) |
| 3130 | Diag(IdLoc, diag::warn_enum_value_overflow); |
| 3131 | |
Chris Lattner | b7416f9 | 2007-08-27 17:37:24 +0000 | [diff] [blame] | 3132 | EltTy = LastEnumConst->getType(); |
| 3133 | } else { |
| 3134 | // First value, set to zero. |
| 3135 | EltTy = Context.IntTy; |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3136 | EnumVal.zextOrTrunc(static_cast<uint32_t>(Context.getTypeSize(EltTy))); |
Chris Lattner | b7416f9 | 2007-08-27 17:37:24 +0000 | [diff] [blame] | 3137 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3138 | } |
| 3139 | |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 3140 | EnumConstantDecl *New = |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 3141 | EnumConstantDecl::Create(Context, TheEnumDecl, IdLoc, Id, EltTy, |
| 3142 | Val, EnumVal, |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 3143 | LastEnumConst); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3144 | |
| 3145 | // Register this decl in the current scope stack. |
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 3146 | PushOnScopeChains(New, S); |
Douglas Gregor | 45579f5 | 2008-12-17 02:04:30 +0000 | [diff] [blame] | 3147 | |
| 3148 | // Add this enumerator into the enum itself. |
| 3149 | // FIXME: This means that the enumerator is stored in two |
| 3150 | // DeclContexts. This is not a long-term solution. |
| 3151 | New->setLexicalDeclContext(TheEnumDecl); |
| 3152 | TheEnumDecl->addDecl(Context, New, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3153 | return New; |
| 3154 | } |
| 3155 | |
Steve Naroff | 02408c6 | 2008-08-07 14:08:16 +0000 | [diff] [blame] | 3156 | // FIXME: For consistency with ActOnFields(), we should have the parser |
| 3157 | // pass in the source location for the left/right braces. |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 3158 | void Sema::ActOnEnumBody(SourceLocation EnumLoc, DeclTy *EnumDeclX, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3159 | DeclTy **Elements, unsigned NumElements) { |
| 3160 | EnumDecl *Enum = cast<EnumDecl>(static_cast<Decl*>(EnumDeclX)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3161 | |
Douglas Gregor | 7df7b6b | 2008-12-15 16:32:14 +0000 | [diff] [blame] | 3162 | if (Enum) { |
| 3163 | if (EnumDecl *Def = cast_or_null<EnumDecl>(Enum->getDefinition(Context))) { |
| 3164 | // Diagnose code like: |
| 3165 | // enum e0 { |
| 3166 | // E0 = sizeof(enum e0 { E1 }) |
| 3167 | // }; |
| 3168 | Diag(Def->getLocation(), diag::err_nested_redefinition) |
| 3169 | << Enum->getDeclName(); |
| 3170 | Diag(Enum->getLocation(), diag::note_previous_definition); |
| 3171 | Enum->setInvalidDecl(); |
| 3172 | return; |
| 3173 | } |
Steve Naroff | 02408c6 | 2008-08-07 14:08:16 +0000 | [diff] [blame] | 3174 | } |
Chris Lattner | e37f0be | 2007-08-28 05:10:31 +0000 | [diff] [blame] | 3175 | // TODO: If the result value doesn't fit in an int, it must be a long or long |
| 3176 | // long value. ISO C does not support this, but GCC does as an extension, |
| 3177 | // emit a warning. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3178 | unsigned IntWidth = Context.Target.getIntWidth(); |
Chris Lattner | e37f0be | 2007-08-28 05:10:31 +0000 | [diff] [blame] | 3179 | |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 3180 | // Verify that all the values are okay, compute the size of the values, and |
| 3181 | // reverse the list. |
| 3182 | unsigned NumNegativeBits = 0; |
| 3183 | unsigned NumPositiveBits = 0; |
| 3184 | |
| 3185 | // Keep track of whether all elements have type int. |
| 3186 | bool AllElementsInt = true; |
| 3187 | |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 3188 | QualType EnumType = Context.getTypeDeclType(Enum); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3189 | EnumConstantDecl *EltList = 0; |
| 3190 | for (unsigned i = 0; i != NumElements; ++i) { |
| 3191 | EnumConstantDecl *ECD = |
| 3192 | cast_or_null<EnumConstantDecl>(static_cast<Decl*>(Elements[i])); |
| 3193 | if (!ECD) continue; // Already issued a diagnostic. |
Chris Lattner | 211a30e | 2007-08-28 05:27:00 +0000 | [diff] [blame] | 3194 | |
| 3195 | // 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] | 3196 | const llvm::APSInt &InitVal = ECD->getInitVal(); |
Chris Lattner | a73349d | 2008-02-26 00:33:57 +0000 | [diff] [blame] | 3197 | assert(InitVal.getBitWidth() >= IntWidth && |
| 3198 | "Should have promoted value to int"); |
Chris Lattner | 211a30e | 2007-08-28 05:27:00 +0000 | [diff] [blame] | 3199 | if (InitVal.getBitWidth() > IntWidth) { |
| 3200 | llvm::APSInt V(InitVal); |
| 3201 | V.trunc(IntWidth); |
| 3202 | V.extend(InitVal.getBitWidth()); |
| 3203 | if (V != InitVal) |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 3204 | Diag(ECD->getLocation(), diag::ext_enum_value_not_int) |
| 3205 | << InitVal.toString(10); |
Chris Lattner | 211a30e | 2007-08-28 05:27:00 +0000 | [diff] [blame] | 3206 | } |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 3207 | |
| 3208 | // Keep track of the size of positive and negative values. |
Chris Lattner | a73349d | 2008-02-26 00:33:57 +0000 | [diff] [blame] | 3209 | if (InitVal.isUnsigned() || InitVal.isNonNegative()) |
Chris Lattner | 21dd821 | 2008-01-14 21:47:29 +0000 | [diff] [blame] | 3210 | NumPositiveBits = std::max(NumPositiveBits, |
| 3211 | (unsigned)InitVal.getActiveBits()); |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 3212 | else |
Chris Lattner | 21dd821 | 2008-01-14 21:47:29 +0000 | [diff] [blame] | 3213 | NumNegativeBits = std::max(NumNegativeBits, |
| 3214 | (unsigned)InitVal.getMinSignedBits()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3215 | |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 3216 | // Keep track of whether every enum element has type int (very commmon). |
| 3217 | if (AllElementsInt) |
| 3218 | AllElementsInt = ECD->getType() == Context.IntTy; |
| 3219 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3220 | ECD->setNextDeclarator(EltList); |
| 3221 | EltList = ECD; |
| 3222 | } |
| 3223 | |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 3224 | // Figure out the type that should be used for this enum. |
| 3225 | // FIXME: Support attribute(packed) on enums and -fshort-enums. |
| 3226 | QualType BestType; |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 3227 | unsigned BestWidth; |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 3228 | |
| 3229 | if (NumNegativeBits) { |
| 3230 | // If there is a negative value, figure out the smallest integer type (of |
| 3231 | // int/long/longlong) that fits. |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 3232 | if (NumNegativeBits <= IntWidth && NumPositiveBits < IntWidth) { |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 3233 | BestType = Context.IntTy; |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 3234 | BestWidth = IntWidth; |
| 3235 | } else { |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3236 | BestWidth = Context.Target.getLongWidth(); |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 3237 | |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 3238 | if (NumNegativeBits <= BestWidth && NumPositiveBits < BestWidth) |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 3239 | BestType = Context.LongTy; |
| 3240 | else { |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3241 | BestWidth = Context.Target.getLongLongWidth(); |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 3242 | |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 3243 | if (NumNegativeBits > BestWidth || NumPositiveBits >= BestWidth) |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 3244 | Diag(Enum->getLocation(), diag::warn_enum_too_large); |
| 3245 | BestType = Context.LongLongTy; |
| 3246 | } |
| 3247 | } |
| 3248 | } else { |
| 3249 | // If there is no negative value, figure out which of uint, ulong, ulonglong |
| 3250 | // fits. |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 3251 | if (NumPositiveBits <= IntWidth) { |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 3252 | BestType = Context.UnsignedIntTy; |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 3253 | BestWidth = IntWidth; |
| 3254 | } else if (NumPositiveBits <= |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3255 | (BestWidth = Context.Target.getLongWidth())) { |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 3256 | BestType = Context.UnsignedLongTy; |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3257 | } else { |
| 3258 | BestWidth = Context.Target.getLongLongWidth(); |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 3259 | assert(NumPositiveBits <= BestWidth && |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 3260 | "How could an initializer get larger than ULL?"); |
| 3261 | BestType = Context.UnsignedLongLongTy; |
| 3262 | } |
| 3263 | } |
| 3264 | |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 3265 | // Loop over all of the enumerator constants, changing their types to match |
| 3266 | // the type of the enum if needed. |
| 3267 | for (unsigned i = 0; i != NumElements; ++i) { |
| 3268 | EnumConstantDecl *ECD = |
| 3269 | cast_or_null<EnumConstantDecl>(static_cast<Decl*>(Elements[i])); |
| 3270 | if (!ECD) continue; // Already issued a diagnostic. |
| 3271 | |
| 3272 | // Standard C says the enumerators have int type, but we allow, as an |
| 3273 | // extension, the enumerators to be larger than int size. If each |
| 3274 | // enumerator value fits in an int, type it as an int, otherwise type it the |
| 3275 | // same as the enumerator decl itself. This means that in "enum { X = 1U }" |
| 3276 | // that X has type 'int', not 'unsigned'. |
Chris Lattner | a73349d | 2008-02-26 00:33:57 +0000 | [diff] [blame] | 3277 | if (ECD->getType() == Context.IntTy) { |
| 3278 | // Make sure the init value is signed. |
| 3279 | llvm::APSInt IV = ECD->getInitVal(); |
| 3280 | IV.setIsSigned(true); |
| 3281 | ECD->setInitVal(IV); |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 3282 | |
| 3283 | if (getLangOptions().CPlusPlus) |
| 3284 | // C++ [dcl.enum]p4: Following the closing brace of an |
| 3285 | // enum-specifier, each enumerator has the type of its |
| 3286 | // enumeration. |
| 3287 | ECD->setType(EnumType); |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 3288 | continue; // Already int type. |
Chris Lattner | a73349d | 2008-02-26 00:33:57 +0000 | [diff] [blame] | 3289 | } |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 3290 | |
| 3291 | // Determine whether the value fits into an int. |
| 3292 | llvm::APSInt InitVal = ECD->getInitVal(); |
| 3293 | bool FitsInInt; |
| 3294 | if (InitVal.isUnsigned() || !InitVal.isNegative()) |
| 3295 | FitsInInt = InitVal.getActiveBits() < IntWidth; |
| 3296 | else |
| 3297 | FitsInInt = InitVal.getMinSignedBits() <= IntWidth; |
| 3298 | |
| 3299 | // If it fits into an integer type, force it. Otherwise force it to match |
| 3300 | // the enum decl type. |
| 3301 | QualType NewTy; |
| 3302 | unsigned NewWidth; |
| 3303 | bool NewSign; |
| 3304 | if (FitsInInt) { |
| 3305 | NewTy = Context.IntTy; |
| 3306 | NewWidth = IntWidth; |
| 3307 | NewSign = true; |
| 3308 | } else if (ECD->getType() == BestType) { |
| 3309 | // Already the right type! |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 3310 | if (getLangOptions().CPlusPlus) |
| 3311 | // C++ [dcl.enum]p4: Following the closing brace of an |
| 3312 | // enum-specifier, each enumerator has the type of its |
| 3313 | // enumeration. |
| 3314 | ECD->setType(EnumType); |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 3315 | continue; |
| 3316 | } else { |
| 3317 | NewTy = BestType; |
| 3318 | NewWidth = BestWidth; |
| 3319 | NewSign = BestType->isSignedIntegerType(); |
| 3320 | } |
| 3321 | |
| 3322 | // Adjust the APSInt value. |
| 3323 | InitVal.extOrTrunc(NewWidth); |
| 3324 | InitVal.setIsSigned(NewSign); |
| 3325 | ECD->setInitVal(InitVal); |
| 3326 | |
| 3327 | // Adjust the Expr initializer and type. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3328 | ECD->setInitExpr(new ImplicitCastExpr(NewTy, ECD->getInitExpr(), |
| 3329 | /*isLvalue=*/false)); |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 3330 | if (getLangOptions().CPlusPlus) |
| 3331 | // C++ [dcl.enum]p4: Following the closing brace of an |
| 3332 | // enum-specifier, each enumerator has the type of its |
| 3333 | // enumeration. |
| 3334 | ECD->setType(EnumType); |
| 3335 | else |
| 3336 | ECD->setType(NewTy); |
Chris Lattner | b7f6e08 | 2007-08-29 17:31:48 +0000 | [diff] [blame] | 3337 | } |
Chris Lattner | ac60968 | 2007-08-28 06:15:15 +0000 | [diff] [blame] | 3338 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3339 | Enum->completeDefinition(Context, BestType); |
Chris Lattner | e1e7985 | 2008-02-06 00:51:33 +0000 | [diff] [blame] | 3340 | Consumer.HandleTagDeclDefinition(Enum); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3341 | } |
| 3342 | |
Anders Carlsson | dfab6cb | 2008-02-08 00:33:21 +0000 | [diff] [blame] | 3343 | Sema::DeclTy *Sema::ActOnFileScopeAsmDecl(SourceLocation Loc, |
Sebastian Redl | 798d119 | 2008-12-13 16:23:55 +0000 | [diff] [blame] | 3344 | ExprArg expr) { |
| 3345 | StringLiteral *AsmString = cast<StringLiteral>((Expr*)expr.release()); |
| 3346 | |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 3347 | return FileScopeAsmDecl::Create(Context, Loc, AsmString); |
Anders Carlsson | dfab6cb | 2008-02-08 00:33:21 +0000 | [diff] [blame] | 3348 | } |
| 3349 | |
Douglas Gregor | f44515a | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 3350 | |
Daniel Dunbar | 4cde927 | 2008-10-14 05:35:18 +0000 | [diff] [blame] | 3351 | void Sema::ActOnPragmaPack(PragmaPackKind Kind, IdentifierInfo *Name, |
| 3352 | ExprTy *alignment, SourceLocation PragmaLoc, |
| 3353 | SourceLocation LParenLoc, SourceLocation RParenLoc) { |
| 3354 | Expr *Alignment = static_cast<Expr *>(alignment); |
| 3355 | |
| 3356 | // If specified then alignment must be a "small" power of two. |
| 3357 | unsigned AlignmentVal = 0; |
| 3358 | if (Alignment) { |
| 3359 | llvm::APSInt Val; |
| 3360 | if (!Alignment->isIntegerConstantExpr(Val, Context) || |
| 3361 | !Val.isPowerOf2() || |
| 3362 | Val.getZExtValue() > 16) { |
| 3363 | Diag(PragmaLoc, diag::warn_pragma_pack_invalid_alignment); |
| 3364 | delete Alignment; |
| 3365 | return; // Ignore |
| 3366 | } |
| 3367 | |
| 3368 | AlignmentVal = (unsigned) Val.getZExtValue(); |
| 3369 | } |
| 3370 | |
| 3371 | switch (Kind) { |
| 3372 | case Action::PPK_Default: // pack([n]) |
| 3373 | PackContext.setAlignment(AlignmentVal); |
| 3374 | break; |
| 3375 | |
| 3376 | case Action::PPK_Show: // pack(show) |
| 3377 | // Show the current alignment, making sure to show the right value |
| 3378 | // for the default. |
| 3379 | AlignmentVal = PackContext.getAlignment(); |
| 3380 | // FIXME: This should come from the target. |
| 3381 | if (AlignmentVal == 0) |
| 3382 | AlignmentVal = 8; |
Chris Lattner | 8365223 | 2008-11-19 07:25:44 +0000 | [diff] [blame] | 3383 | Diag(PragmaLoc, diag::warn_pragma_pack_show) << AlignmentVal; |
Daniel Dunbar | 4cde927 | 2008-10-14 05:35:18 +0000 | [diff] [blame] | 3384 | break; |
| 3385 | |
| 3386 | case Action::PPK_Push: // pack(push [, id] [, [n]) |
| 3387 | PackContext.push(Name); |
| 3388 | // Set the new alignment if specified. |
| 3389 | if (Alignment) |
| 3390 | PackContext.setAlignment(AlignmentVal); |
| 3391 | break; |
| 3392 | |
| 3393 | case Action::PPK_Pop: // pack(pop [, id] [, n]) |
| 3394 | // MSDN, C/C++ Preprocessor Reference > Pragma Directives > pack: |
| 3395 | // "#pragma pack(pop, identifier, n) is undefined" |
| 3396 | if (Alignment && Name) |
| 3397 | Diag(PragmaLoc, diag::warn_pragma_pack_pop_identifer_and_alignment); |
| 3398 | |
| 3399 | // Do the pop. |
| 3400 | if (!PackContext.pop(Name)) { |
| 3401 | // If a name was specified then failure indicates the name |
| 3402 | // wasn't found. Otherwise failure indicates the stack was |
| 3403 | // empty. |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 3404 | Diag(PragmaLoc, diag::warn_pragma_pack_pop_failed) |
| 3405 | << (Name ? "no record matching name" : "stack empty"); |
Daniel Dunbar | 4cde927 | 2008-10-14 05:35:18 +0000 | [diff] [blame] | 3406 | |
| 3407 | // FIXME: Warn about popping named records as MSVC does. |
| 3408 | } else { |
| 3409 | // Pop succeeded, set the new alignment if specified. |
| 3410 | if (Alignment) |
| 3411 | PackContext.setAlignment(AlignmentVal); |
| 3412 | } |
| 3413 | break; |
| 3414 | |
| 3415 | default: |
| 3416 | assert(0 && "Invalid #pragma pack kind."); |
| 3417 | } |
| 3418 | } |
| 3419 | |
| 3420 | bool PragmaPackStack::pop(IdentifierInfo *Name) { |
| 3421 | if (Stack.empty()) |
| 3422 | return false; |
| 3423 | |
| 3424 | // If name is empty just pop top. |
| 3425 | if (!Name) { |
| 3426 | Alignment = Stack.back().first; |
| 3427 | Stack.pop_back(); |
| 3428 | return true; |
| 3429 | } |
| 3430 | |
| 3431 | // Otherwise, find the named record. |
| 3432 | for (unsigned i = Stack.size(); i != 0; ) { |
| 3433 | --i; |
Daniel Dunbar | 0655039 | 2008-11-19 10:32:38 +0000 | [diff] [blame] | 3434 | if (Stack[i].second == Name) { |
Daniel Dunbar | 4cde927 | 2008-10-14 05:35:18 +0000 | [diff] [blame] | 3435 | // Found it, pop up to and including this record. |
| 3436 | Alignment = Stack[i].first; |
| 3437 | Stack.erase(Stack.begin() + i, Stack.end()); |
| 3438 | return true; |
| 3439 | } |
| 3440 | } |
| 3441 | |
| 3442 | return false; |
| 3443 | } |