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