Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 1 | //===------ SemaDeclCXX.cpp - Semantic Analysis for C++ Declarations ------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for C++ declarations. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 15 | #include "SemaInherit.h" |
Argyrios Kyrtzidis | a4755c6 | 2008-08-09 00:58:37 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTConsumer.h" |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclVisitor.h" |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 19 | #include "clang/AST/TypeOrdering.h" |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 20 | #include "clang/AST/StmtVisitor.h" |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 21 | #include "clang/Basic/PartialDiagnostic.h" |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 22 | #include "clang/Lex/Preprocessor.h" |
Daniel Dunbar | 12bc692 | 2008-08-11 03:27:53 +0000 | [diff] [blame] | 23 | #include "clang/Parse/DeclSpec.h" |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Compiler.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 26 | #include <algorithm> // for std::equal |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 27 | #include <map> |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 28 | |
| 29 | using namespace clang; |
| 30 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
| 32 | // CheckDefaultArgumentVisitor |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 35 | namespace { |
| 36 | /// CheckDefaultArgumentVisitor - C++ [dcl.fct.default] Traverses |
| 37 | /// the default argument of a parameter to determine whether it |
| 38 | /// contains any ill-formed subexpressions. For example, this will |
| 39 | /// diagnose the use of local variables or parameters within the |
| 40 | /// default argument expression. |
| 41 | class VISIBILITY_HIDDEN CheckDefaultArgumentVisitor |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 42 | : public StmtVisitor<CheckDefaultArgumentVisitor, bool> { |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 43 | Expr *DefaultArg; |
| 44 | Sema *S; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 45 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 46 | public: |
| 47 | CheckDefaultArgumentVisitor(Expr *defarg, Sema *s) |
| 48 | : DefaultArg(defarg), S(s) {} |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 49 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 50 | bool VisitExpr(Expr *Node); |
| 51 | bool VisitDeclRefExpr(DeclRefExpr *DRE); |
Douglas Gregor | 796da18 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 52 | bool VisitCXXThisExpr(CXXThisExpr *ThisE); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 53 | }; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 54 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 55 | /// VisitExpr - Visit all of the children of this expression. |
| 56 | bool CheckDefaultArgumentVisitor::VisitExpr(Expr *Node) { |
| 57 | bool IsInvalid = false; |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 58 | for (Stmt::child_iterator I = Node->child_begin(), |
| 59 | E = Node->child_end(); I != E; ++I) |
| 60 | IsInvalid |= Visit(*I); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 61 | return IsInvalid; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 64 | /// VisitDeclRefExpr - Visit a reference to a declaration, to |
| 65 | /// determine whether this declaration can be used in the default |
| 66 | /// argument expression. |
| 67 | bool CheckDefaultArgumentVisitor::VisitDeclRefExpr(DeclRefExpr *DRE) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 68 | NamedDecl *Decl = DRE->getDecl(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 69 | if (ParmVarDecl *Param = dyn_cast<ParmVarDecl>(Decl)) { |
| 70 | // C++ [dcl.fct.default]p9 |
| 71 | // Default arguments are evaluated each time the function is |
| 72 | // called. The order of evaluation of function arguments is |
| 73 | // unspecified. Consequently, parameters of a function shall not |
| 74 | // be used in default argument expressions, even if they are not |
| 75 | // evaluated. Parameters of a function declared before a default |
| 76 | // argument expression are in scope and can hide namespace and |
| 77 | // class member names. |
| 78 | return S->Diag(DRE->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 79 | diag::err_param_default_argument_references_param) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 80 | << Param->getDeclName() << DefaultArg->getSourceRange(); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 81 | } else if (VarDecl *VDecl = dyn_cast<VarDecl>(Decl)) { |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 82 | // C++ [dcl.fct.default]p7 |
| 83 | // Local variables shall not be used in default argument |
| 84 | // expressions. |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 85 | if (VDecl->isBlockVarDecl()) |
| 86 | return S->Diag(DRE->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 87 | diag::err_param_default_argument_references_local) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 88 | << VDecl->getDeclName() << DefaultArg->getSourceRange(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 89 | } |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 90 | |
Douglas Gregor | 3996f23 | 2008-11-04 13:41:56 +0000 | [diff] [blame] | 91 | return false; |
| 92 | } |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 93 | |
Douglas Gregor | 796da18 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 94 | /// VisitCXXThisExpr - Visit a C++ "this" expression. |
| 95 | bool CheckDefaultArgumentVisitor::VisitCXXThisExpr(CXXThisExpr *ThisE) { |
| 96 | // C++ [dcl.fct.default]p8: |
| 97 | // The keyword this shall not be used in a default argument of a |
| 98 | // member function. |
| 99 | return S->Diag(ThisE->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 100 | diag::err_param_default_argument_references_this) |
| 101 | << ThisE->getSourceRange(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 102 | } |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 105 | bool |
| 106 | Sema::SetParamDefaultArgument(ParmVarDecl *Param, ExprArg DefaultArg, |
| 107 | SourceLocation EqualLoc) |
| 108 | { |
| 109 | QualType ParamType = Param->getType(); |
| 110 | |
Anders Carlsson | 5653ca5 | 2009-08-25 13:46:13 +0000 | [diff] [blame] | 111 | if (RequireCompleteType(Param->getLocation(), Param->getType(), |
| 112 | diag::err_typecheck_decl_incomplete_type)) { |
| 113 | Param->setInvalidDecl(); |
| 114 | return true; |
| 115 | } |
| 116 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 117 | Expr *Arg = (Expr *)DefaultArg.get(); |
| 118 | |
| 119 | // C++ [dcl.fct.default]p5 |
| 120 | // A default argument expression is implicitly converted (clause |
| 121 | // 4) to the parameter type. The default argument expression has |
| 122 | // the same semantic constraints as the initializer expression in |
| 123 | // a declaration of a variable of the parameter type, using the |
| 124 | // copy-initialization semantics (8.5). |
| 125 | if (CheckInitializerTypes(Arg, ParamType, EqualLoc, |
| 126 | Param->getDeclName(), /*DirectInit=*/false)) |
Anders Carlsson | 9351c17 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 127 | return true; |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 128 | |
| 129 | Arg = MaybeCreateCXXExprWithTemporaries(Arg, /*DestroyTemps=*/false); |
| 130 | |
| 131 | // Okay: add the default argument to the parameter |
| 132 | Param->setDefaultArg(Arg); |
| 133 | |
| 134 | DefaultArg.release(); |
| 135 | |
Anders Carlsson | 9351c17 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 136 | return false; |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 139 | /// ActOnParamDefaultArgument - Check whether the default argument |
| 140 | /// provided for a function parameter is well-formed. If so, attach it |
| 141 | /// to the parameter declaration. |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 142 | void |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 143 | Sema::ActOnParamDefaultArgument(DeclPtrTy param, SourceLocation EqualLoc, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 144 | ExprArg defarg) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 145 | if (!param || !defarg.get()) |
| 146 | return; |
| 147 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 148 | ParmVarDecl *Param = cast<ParmVarDecl>(param.getAs<Decl>()); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 149 | UnparsedDefaultArgLocs.erase(Param); |
| 150 | |
Anders Carlsson | f1b1d59 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 151 | ExprOwningPtr<Expr> DefaultArg(this, defarg.takeAs<Expr>()); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 152 | QualType ParamType = Param->getType(); |
| 153 | |
| 154 | // Default arguments are only permitted in C++ |
| 155 | if (!getLangOptions().CPlusPlus) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 156 | Diag(EqualLoc, diag::err_param_default_argument) |
| 157 | << DefaultArg->getSourceRange(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 158 | Param->setInvalidDecl(); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 159 | return; |
| 160 | } |
| 161 | |
Anders Carlsson | 66e3067 | 2009-08-25 01:02:06 +0000 | [diff] [blame] | 162 | // Check that the default argument is well-formed |
| 163 | CheckDefaultArgumentVisitor DefaultArgChecker(DefaultArg.get(), this); |
| 164 | if (DefaultArgChecker.Visit(DefaultArg.get())) { |
| 165 | Param->setInvalidDecl(); |
| 166 | return; |
| 167 | } |
| 168 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 169 | SetParamDefaultArgument(Param, move(DefaultArg), EqualLoc); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 172 | /// ActOnParamUnparsedDefaultArgument - We've seen a default |
| 173 | /// argument for a function parameter, but we can't parse it yet |
| 174 | /// because we're inside a class definition. Note that this default |
| 175 | /// argument will be parsed later. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 176 | void Sema::ActOnParamUnparsedDefaultArgument(DeclPtrTy param, |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 177 | SourceLocation EqualLoc, |
| 178 | SourceLocation ArgLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 179 | if (!param) |
| 180 | return; |
| 181 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 182 | ParmVarDecl *Param = cast<ParmVarDecl>(param.getAs<Decl>()); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 183 | if (Param) |
| 184 | Param->setUnparsedDefaultArg(); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 185 | |
| 186 | UnparsedDefaultArgLocs[Param] = ArgLoc; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 189 | /// ActOnParamDefaultArgumentError - Parsing or semantic analysis of |
| 190 | /// the default argument for the parameter param failed. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 191 | void Sema::ActOnParamDefaultArgumentError(DeclPtrTy param) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 192 | if (!param) |
| 193 | return; |
| 194 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 195 | ParmVarDecl *Param = cast<ParmVarDecl>(param.getAs<Decl>()); |
| 196 | |
| 197 | Param->setInvalidDecl(); |
| 198 | |
| 199 | UnparsedDefaultArgLocs.erase(Param); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 202 | /// CheckExtraCXXDefaultArguments - Check for any extra default |
| 203 | /// arguments in the declarator, which is not a function declaration |
| 204 | /// or definition and therefore is not permitted to have default |
| 205 | /// arguments. This routine should be invoked for every declarator |
| 206 | /// that is not a function declaration or definition. |
| 207 | void Sema::CheckExtraCXXDefaultArguments(Declarator &D) { |
| 208 | // C++ [dcl.fct.default]p3 |
| 209 | // A default argument expression shall be specified only in the |
| 210 | // parameter-declaration-clause of a function declaration or in a |
| 211 | // template-parameter (14.1). It shall not be specified for a |
| 212 | // parameter pack. If it is specified in a |
| 213 | // parameter-declaration-clause, it shall not occur within a |
| 214 | // declarator or abstract-declarator of a parameter-declaration. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 215 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 216 | DeclaratorChunk &chunk = D.getTypeObject(i); |
| 217 | if (chunk.Kind == DeclaratorChunk::Function) { |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 218 | for (unsigned argIdx = 0, e = chunk.Fun.NumArgs; argIdx != e; ++argIdx) { |
| 219 | ParmVarDecl *Param = |
| 220 | cast<ParmVarDecl>(chunk.Fun.ArgInfo[argIdx].Param.getAs<Decl>()); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 221 | if (Param->hasUnparsedDefaultArg()) { |
| 222 | CachedTokens *Toks = chunk.Fun.ArgInfo[argIdx].DefaultArgTokens; |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 223 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc) |
| 224 | << SourceRange((*Toks)[1].getLocation(), Toks->back().getLocation()); |
| 225 | delete Toks; |
| 226 | chunk.Fun.ArgInfo[argIdx].DefaultArgTokens = 0; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 227 | } else if (Param->getDefaultArg()) { |
| 228 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc) |
| 229 | << Param->getDefaultArg()->getSourceRange(); |
| 230 | Param->setDefaultArg(0); |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 237 | // MergeCXXFunctionDecl - Merge two declarations of the same C++ |
| 238 | // function, once we already know that they have the same |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 239 | // type. Subroutine of MergeFunctionDecl. Returns true if there was an |
| 240 | // error, false otherwise. |
| 241 | bool Sema::MergeCXXFunctionDecl(FunctionDecl *New, FunctionDecl *Old) { |
| 242 | bool Invalid = false; |
| 243 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 244 | // C++ [dcl.fct.default]p4: |
| 245 | // |
| 246 | // For non-template functions, default arguments can be added in |
| 247 | // later declarations of a function in the same |
| 248 | // scope. Declarations in different scopes have completely |
| 249 | // distinct sets of default arguments. That is, declarations in |
| 250 | // inner scopes do not acquire default arguments from |
| 251 | // declarations in outer scopes, and vice versa. In a given |
| 252 | // function declaration, all parameters subsequent to a |
| 253 | // parameter with a default argument shall have default |
| 254 | // arguments supplied in this or previous declarations. A |
| 255 | // default argument shall not be redefined by a later |
| 256 | // declaration (not even to the same value). |
| 257 | for (unsigned p = 0, NumParams = Old->getNumParams(); p < NumParams; ++p) { |
| 258 | ParmVarDecl *OldParam = Old->getParamDecl(p); |
| 259 | ParmVarDecl *NewParam = New->getParamDecl(p); |
| 260 | |
| 261 | if(OldParam->getDefaultArg() && NewParam->getDefaultArg()) { |
| 262 | Diag(NewParam->getLocation(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 263 | diag::err_param_default_argument_redefinition) |
| 264 | << NewParam->getDefaultArg()->getSourceRange(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 265 | Diag(OldParam->getLocation(), diag::note_previous_definition); |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 266 | Invalid = true; |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 267 | } else if (OldParam->getDefaultArg()) { |
| 268 | // Merge the old default argument into the new parameter |
| 269 | NewParam->setDefaultArg(OldParam->getDefaultArg()); |
| 270 | } |
| 271 | } |
| 272 | |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 273 | if (CheckEquivalentExceptionSpec( |
| 274 | Old->getType()->getAsFunctionProtoType(), Old->getLocation(), |
| 275 | New->getType()->getAsFunctionProtoType(), New->getLocation())) { |
| 276 | Invalid = true; |
| 277 | } |
| 278 | |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 279 | return Invalid; |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | /// CheckCXXDefaultArguments - Verify that the default arguments for a |
| 283 | /// function declaration are well-formed according to C++ |
| 284 | /// [dcl.fct.default]. |
| 285 | void Sema::CheckCXXDefaultArguments(FunctionDecl *FD) { |
| 286 | unsigned NumParams = FD->getNumParams(); |
| 287 | unsigned p; |
| 288 | |
| 289 | // Find first parameter with a default argument |
| 290 | for (p = 0; p < NumParams; ++p) { |
| 291 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5f49a0c | 2009-08-25 01:23:32 +0000 | [diff] [blame] | 292 | if (Param->hasDefaultArg()) |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 293 | break; |
| 294 | } |
| 295 | |
| 296 | // C++ [dcl.fct.default]p4: |
| 297 | // In a given function declaration, all parameters |
| 298 | // subsequent to a parameter with a default argument shall |
| 299 | // have default arguments supplied in this or previous |
| 300 | // declarations. A default argument shall not be redefined |
| 301 | // by a later declaration (not even to the same value). |
| 302 | unsigned LastMissingDefaultArg = 0; |
| 303 | for(; p < NumParams; ++p) { |
| 304 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5f49a0c | 2009-08-25 01:23:32 +0000 | [diff] [blame] | 305 | if (!Param->hasDefaultArg()) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 306 | if (Param->isInvalidDecl()) |
| 307 | /* We already complained about this parameter. */; |
| 308 | else if (Param->getIdentifier()) |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 309 | Diag(Param->getLocation(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 310 | diag::err_param_default_argument_missing_name) |
Chris Lattner | 43b628c | 2008-11-19 07:32:16 +0000 | [diff] [blame] | 311 | << Param->getIdentifier(); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 312 | else |
| 313 | Diag(Param->getLocation(), |
| 314 | diag::err_param_default_argument_missing); |
| 315 | |
| 316 | LastMissingDefaultArg = p; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | if (LastMissingDefaultArg > 0) { |
| 321 | // Some default arguments were missing. Clear out all of the |
| 322 | // default arguments up to (and including) the last missing |
| 323 | // default argument, so that we leave the function parameters |
| 324 | // in a semantically valid state. |
| 325 | for (p = 0; p <= LastMissingDefaultArg; ++p) { |
| 326 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 327 | if (Param->hasDefaultArg()) { |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 328 | if (!Param->hasUnparsedDefaultArg()) |
| 329 | Param->getDefaultArg()->Destroy(Context); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 330 | Param->setDefaultArg(0); |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 335 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 336 | /// isCurrentClassName - Determine whether the identifier II is the |
| 337 | /// name of the class type currently being defined. In the case of |
| 338 | /// nested classes, this will only return true if II is the name of |
| 339 | /// the innermost class. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 340 | bool Sema::isCurrentClassName(const IdentifierInfo &II, Scope *, |
| 341 | const CXXScopeSpec *SS) { |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 342 | CXXRecordDecl *CurDecl; |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 343 | if (SS && SS->isSet() && !SS->isInvalid()) { |
Douglas Gregor | ac373c4 | 2009-08-21 22:16:40 +0000 | [diff] [blame] | 344 | DeclContext *DC = computeDeclContext(*SS, true); |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 345 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(DC); |
| 346 | } else |
| 347 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(CurContext); |
| 348 | |
| 349 | if (CurDecl) |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 350 | return &II == CurDecl->getIdentifier(); |
| 351 | else |
| 352 | return false; |
| 353 | } |
| 354 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 355 | /// \brief Check the validity of a C++ base class specifier. |
| 356 | /// |
| 357 | /// \returns a new CXXBaseSpecifier if well-formed, emits diagnostics |
| 358 | /// and returns NULL otherwise. |
| 359 | CXXBaseSpecifier * |
| 360 | Sema::CheckBaseSpecifier(CXXRecordDecl *Class, |
| 361 | SourceRange SpecifierRange, |
| 362 | bool Virtual, AccessSpecifier Access, |
| 363 | QualType BaseType, |
| 364 | SourceLocation BaseLoc) { |
| 365 | // C++ [class.union]p1: |
| 366 | // A union shall not have base classes. |
| 367 | if (Class->isUnion()) { |
| 368 | Diag(Class->getLocation(), diag::err_base_clause_on_union) |
| 369 | << SpecifierRange; |
| 370 | return 0; |
| 371 | } |
| 372 | |
| 373 | if (BaseType->isDependentType()) |
Fariborz Jahanian | 71c6e71 | 2009-07-22 17:41:53 +0000 | [diff] [blame] | 374 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 375 | Class->getTagKind() == RecordDecl::TK_class, |
| 376 | Access, BaseType); |
| 377 | |
| 378 | // Base specifiers must be record types. |
| 379 | if (!BaseType->isRecordType()) { |
| 380 | Diag(BaseLoc, diag::err_base_must_be_class) << SpecifierRange; |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | // C++ [class.union]p1: |
| 385 | // A union shall not be used as a base class. |
| 386 | if (BaseType->isUnionType()) { |
| 387 | Diag(BaseLoc, diag::err_union_as_base_class) << SpecifierRange; |
| 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | // C++ [class.derived]p2: |
| 392 | // The class-name in a base-specifier shall not be an incompletely |
| 393 | // defined class. |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 394 | if (RequireCompleteType(BaseLoc, BaseType, |
| 395 | PDiag(diag::err_incomplete_base_class) |
| 396 | << SpecifierRange)) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 397 | return 0; |
| 398 | |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 399 | // If the base class is polymorphic or isn't empty, the new one is/isn't, too. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 400 | RecordDecl *BaseDecl = BaseType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 401 | assert(BaseDecl && "Record type has no declaration"); |
| 402 | BaseDecl = BaseDecl->getDefinition(Context); |
| 403 | assert(BaseDecl && "Base type is not incomplete, but has no definition"); |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 404 | CXXRecordDecl * CXXBaseDecl = cast<CXXRecordDecl>(BaseDecl); |
| 405 | assert(CXXBaseDecl && "Base type is not a C++ type"); |
| 406 | if (!CXXBaseDecl->isEmpty()) |
| 407 | Class->setEmpty(false); |
| 408 | if (CXXBaseDecl->isPolymorphic()) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 409 | Class->setPolymorphic(true); |
| 410 | |
| 411 | // C++ [dcl.init.aggr]p1: |
| 412 | // An aggregate is [...] a class with [...] no base classes [...]. |
| 413 | Class->setAggregate(false); |
| 414 | Class->setPOD(false); |
| 415 | |
Anders Carlsson | 347ba89 | 2009-04-16 00:08:20 +0000 | [diff] [blame] | 416 | if (Virtual) { |
| 417 | // C++ [class.ctor]p5: |
| 418 | // A constructor is trivial if its class has no virtual base classes. |
| 419 | Class->setHasTrivialConstructor(false); |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 420 | |
| 421 | // C++ [class.copy]p6: |
| 422 | // A copy constructor is trivial if its class has no virtual base classes. |
| 423 | Class->setHasTrivialCopyConstructor(false); |
| 424 | |
| 425 | // C++ [class.copy]p11: |
| 426 | // A copy assignment operator is trivial if its class has no virtual |
| 427 | // base classes. |
| 428 | Class->setHasTrivialCopyAssignment(false); |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 429 | |
| 430 | // C++0x [meta.unary.prop] is_empty: |
| 431 | // T is a class type, but not a union type, with ... no virtual base |
| 432 | // classes |
| 433 | Class->setEmpty(false); |
Anders Carlsson | 347ba89 | 2009-04-16 00:08:20 +0000 | [diff] [blame] | 434 | } else { |
| 435 | // C++ [class.ctor]p5: |
| 436 | // A constructor is trivial if all the direct base classes of its |
| 437 | // class have trivial constructors. |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 438 | if (!cast<CXXRecordDecl>(BaseDecl)->hasTrivialConstructor()) |
| 439 | Class->setHasTrivialConstructor(false); |
| 440 | |
| 441 | // C++ [class.copy]p6: |
| 442 | // A copy constructor is trivial if all the direct base classes of its |
| 443 | // class have trivial copy constructors. |
| 444 | if (!cast<CXXRecordDecl>(BaseDecl)->hasTrivialCopyConstructor()) |
| 445 | Class->setHasTrivialCopyConstructor(false); |
| 446 | |
| 447 | // C++ [class.copy]p11: |
| 448 | // A copy assignment operator is trivial if all the direct base classes |
| 449 | // of its class have trivial copy assignment operators. |
| 450 | if (!cast<CXXRecordDecl>(BaseDecl)->hasTrivialCopyAssignment()) |
| 451 | Class->setHasTrivialCopyAssignment(false); |
Anders Carlsson | 347ba89 | 2009-04-16 00:08:20 +0000 | [diff] [blame] | 452 | } |
Anders Carlsson | 072abef | 2009-04-17 02:34:54 +0000 | [diff] [blame] | 453 | |
| 454 | // C++ [class.ctor]p3: |
| 455 | // A destructor is trivial if all the direct base classes of its class |
| 456 | // have trivial destructors. |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 457 | if (!cast<CXXRecordDecl>(BaseDecl)->hasTrivialDestructor()) |
| 458 | Class->setHasTrivialDestructor(false); |
Anders Carlsson | 347ba89 | 2009-04-16 00:08:20 +0000 | [diff] [blame] | 459 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 460 | // Create the base specifier. |
| 461 | // FIXME: Allocate via ASTContext? |
Fariborz Jahanian | 71c6e71 | 2009-07-22 17:41:53 +0000 | [diff] [blame] | 462 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 463 | Class->getTagKind() == RecordDecl::TK_class, |
| 464 | Access, BaseType); |
| 465 | } |
| 466 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 467 | /// ActOnBaseSpecifier - Parsed a base specifier. A base specifier is |
| 468 | /// one entry in the base class list of a class specifier, for |
| 469 | /// example: |
| 470 | /// class foo : public bar, virtual private baz { |
| 471 | /// 'public bar' and 'virtual private baz' are each base-specifiers. |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 472 | Sema::BaseResult |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 473 | Sema::ActOnBaseSpecifier(DeclPtrTy classdecl, SourceRange SpecifierRange, |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 474 | bool Virtual, AccessSpecifier Access, |
| 475 | TypeTy *basetype, SourceLocation BaseLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 476 | if (!classdecl) |
| 477 | return true; |
| 478 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 479 | AdjustDeclIfTemplate(classdecl); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 480 | CXXRecordDecl *Class = cast<CXXRecordDecl>(classdecl.getAs<Decl>()); |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 481 | QualType BaseType = GetTypeFromParser(basetype); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 482 | if (CXXBaseSpecifier *BaseSpec = CheckBaseSpecifier(Class, SpecifierRange, |
| 483 | Virtual, Access, |
| 484 | BaseType, BaseLoc)) |
| 485 | return BaseSpec; |
| 486 | |
| 487 | return true; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 488 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 489 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 490 | /// \brief Performs the actual work of attaching the given base class |
| 491 | /// specifiers to a C++ class. |
| 492 | bool Sema::AttachBaseSpecifiers(CXXRecordDecl *Class, CXXBaseSpecifier **Bases, |
| 493 | unsigned NumBases) { |
| 494 | if (NumBases == 0) |
| 495 | return false; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 496 | |
| 497 | // Used to keep track of which base types we have already seen, so |
| 498 | // that we can properly diagnose redundant direct base types. Note |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 499 | // that the key is always the unqualified canonical type of the base |
| 500 | // class. |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 501 | std::map<QualType, CXXBaseSpecifier*, QualTypeOrdering> KnownBaseTypes; |
| 502 | |
| 503 | // Copy non-redundant base specifiers into permanent storage. |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 504 | unsigned NumGoodBases = 0; |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 505 | bool Invalid = false; |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 506 | for (unsigned idx = 0; idx < NumBases; ++idx) { |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 507 | QualType NewBaseType |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 508 | = Context.getCanonicalType(Bases[idx]->getType()); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 509 | NewBaseType = NewBaseType.getUnqualifiedType(); |
| 510 | |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 511 | if (KnownBaseTypes[NewBaseType]) { |
| 512 | // C++ [class.mi]p3: |
| 513 | // A class shall not be specified as a direct base class of a |
| 514 | // derived class more than once. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 515 | Diag(Bases[idx]->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 516 | diag::err_duplicate_base_class) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 517 | << KnownBaseTypes[NewBaseType]->getType() |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 518 | << Bases[idx]->getSourceRange(); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 519 | |
| 520 | // Delete the duplicate base class specifier; we're going to |
| 521 | // overwrite its pointer later. |
Douglas Gregor | 2aef06d | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 522 | Context.Deallocate(Bases[idx]); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 523 | |
| 524 | Invalid = true; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 525 | } else { |
| 526 | // Okay, add this new base class. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 527 | KnownBaseTypes[NewBaseType] = Bases[idx]; |
| 528 | Bases[NumGoodBases++] = Bases[idx]; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | |
| 532 | // Attach the remaining base class specifiers to the derived class. |
Fariborz Jahanian | 5ffcd7b | 2009-07-02 18:26:15 +0000 | [diff] [blame] | 533 | Class->setBases(Context, Bases, NumGoodBases); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 534 | |
| 535 | // Delete the remaining (good) base class specifiers, since their |
| 536 | // data has been copied into the CXXRecordDecl. |
| 537 | for (unsigned idx = 0; idx < NumGoodBases; ++idx) |
Douglas Gregor | 2aef06d | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 538 | Context.Deallocate(Bases[idx]); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 539 | |
| 540 | return Invalid; |
| 541 | } |
| 542 | |
| 543 | /// ActOnBaseSpecifiers - Attach the given base specifiers to the |
| 544 | /// class, after checking whether there are any duplicate base |
| 545 | /// classes. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 546 | void Sema::ActOnBaseSpecifiers(DeclPtrTy ClassDecl, BaseTy **Bases, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 547 | unsigned NumBases) { |
| 548 | if (!ClassDecl || !Bases || !NumBases) |
| 549 | return; |
| 550 | |
| 551 | AdjustDeclIfTemplate(ClassDecl); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 552 | AttachBaseSpecifiers(cast<CXXRecordDecl>(ClassDecl.getAs<Decl>()), |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 553 | (CXXBaseSpecifier**)(Bases), NumBases); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 554 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 555 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 556 | //===----------------------------------------------------------------------===// |
| 557 | // C++ class member Handling |
| 558 | //===----------------------------------------------------------------------===// |
| 559 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 560 | /// ActOnCXXMemberDeclarator - This is invoked when a C++ class member |
| 561 | /// declarator is parsed. 'AS' is the access specifier, 'BW' specifies the |
| 562 | /// bitfield width if there is one and 'InitExpr' specifies the initializer if |
Chris Lattner | b6688e0 | 2009-04-12 22:37:57 +0000 | [diff] [blame] | 563 | /// any. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 564 | Sema::DeclPtrTy |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 565 | Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D, |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 566 | MultiTemplateParamsArg TemplateParameterLists, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 567 | ExprTy *BW, ExprTy *InitExpr, bool Deleted) { |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 568 | const DeclSpec &DS = D.getDeclSpec(); |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 569 | DeclarationName Name = GetNameForDeclarator(D); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 570 | Expr *BitWidth = static_cast<Expr*>(BW); |
| 571 | Expr *Init = static_cast<Expr*>(InitExpr); |
| 572 | SourceLocation Loc = D.getIdentifierLoc(); |
| 573 | |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 574 | bool isFunc = D.isFunctionDeclarator(); |
| 575 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 576 | assert(!DS.isFriendSpecified()); |
| 577 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 578 | // C++ 9.2p6: A member shall not be declared to have automatic storage |
| 579 | // duration (auto, register) or with the extern storage-class-specifier. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 580 | // C++ 7.1.1p8: The mutable specifier can be applied only to names of class |
| 581 | // data members and cannot be applied to names declared const or static, |
| 582 | // and cannot be applied to reference members. |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 583 | switch (DS.getStorageClassSpec()) { |
| 584 | case DeclSpec::SCS_unspecified: |
| 585 | case DeclSpec::SCS_typedef: |
| 586 | case DeclSpec::SCS_static: |
| 587 | // FALL THROUGH. |
| 588 | break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 589 | case DeclSpec::SCS_mutable: |
| 590 | if (isFunc) { |
| 591 | if (DS.getStorageClassSpecLoc().isValid()) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 592 | Diag(DS.getStorageClassSpecLoc(), diag::err_mutable_function); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 593 | else |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 594 | Diag(DS.getThreadSpecLoc(), diag::err_mutable_function); |
| 595 | |
Sebastian Redl | a11f42f | 2008-11-17 23:24:37 +0000 | [diff] [blame] | 596 | // FIXME: It would be nicer if the keyword was ignored only for this |
| 597 | // declarator. Otherwise we could get follow-up errors. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 598 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 599 | } else { |
| 600 | QualType T = GetTypeForDeclarator(D, S); |
| 601 | diag::kind err = static_cast<diag::kind>(0); |
| 602 | if (T->isReferenceType()) |
| 603 | err = diag::err_mutable_reference; |
| 604 | else if (T.isConstQualified()) |
| 605 | err = diag::err_mutable_const; |
| 606 | if (err != 0) { |
| 607 | if (DS.getStorageClassSpecLoc().isValid()) |
| 608 | Diag(DS.getStorageClassSpecLoc(), err); |
| 609 | else |
| 610 | Diag(DS.getThreadSpecLoc(), err); |
Sebastian Redl | a11f42f | 2008-11-17 23:24:37 +0000 | [diff] [blame] | 611 | // FIXME: It would be nicer if the keyword was ignored only for this |
| 612 | // declarator. Otherwise we could get follow-up errors. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 613 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 614 | } |
| 615 | } |
| 616 | break; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 617 | default: |
| 618 | if (DS.getStorageClassSpecLoc().isValid()) |
| 619 | Diag(DS.getStorageClassSpecLoc(), |
| 620 | diag::err_storageclass_invalid_for_member); |
| 621 | else |
| 622 | Diag(DS.getThreadSpecLoc(), diag::err_storageclass_invalid_for_member); |
| 623 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 624 | } |
| 625 | |
Argyrios Kyrtzidis | d6caa9e | 2008-10-15 20:23:22 +0000 | [diff] [blame] | 626 | if (!isFunc && |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 627 | D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_typename && |
Argyrios Kyrtzidis | d6caa9e | 2008-10-15 20:23:22 +0000 | [diff] [blame] | 628 | D.getNumTypeObjects() == 0) { |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 629 | // Check also for this case: |
| 630 | // |
| 631 | // typedef int f(); |
| 632 | // f a; |
| 633 | // |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 634 | QualType TDType = GetTypeFromParser(DS.getTypeRep()); |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 635 | isFunc = TDType->isFunctionType(); |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 636 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 637 | |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 638 | bool isInstField = ((DS.getStorageClassSpec() == DeclSpec::SCS_unspecified || |
| 639 | DS.getStorageClassSpec() == DeclSpec::SCS_mutable) && |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 640 | !isFunc); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 641 | |
| 642 | Decl *Member; |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 643 | if (isInstField) { |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 644 | // FIXME: Check for template parameters! |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 645 | Member = HandleField(S, cast<CXXRecordDecl>(CurContext), Loc, D, BitWidth, |
| 646 | AS); |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 647 | assert(Member && "HandleField never returns null"); |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 648 | } else { |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 649 | Member = HandleDeclarator(S, D, move(TemplateParameterLists), false) |
| 650 | .getAs<Decl>(); |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 651 | if (!Member) { |
| 652 | if (BitWidth) DeleteExpr(BitWidth); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 653 | return DeclPtrTy(); |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 654 | } |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 655 | |
| 656 | // Non-instance-fields can't have a bitfield. |
| 657 | if (BitWidth) { |
| 658 | if (Member->isInvalidDecl()) { |
| 659 | // don't emit another diagnostic. |
Douglas Gregor | 2d2e9cf | 2009-03-11 20:22:50 +0000 | [diff] [blame] | 660 | } else if (isa<VarDecl>(Member)) { |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 661 | // C++ 9.6p3: A bit-field shall not be a static member. |
| 662 | // "static member 'A' cannot be a bit-field" |
| 663 | Diag(Loc, diag::err_static_not_bitfield) |
| 664 | << Name << BitWidth->getSourceRange(); |
| 665 | } else if (isa<TypedefDecl>(Member)) { |
| 666 | // "typedef member 'x' cannot be a bit-field" |
| 667 | Diag(Loc, diag::err_typedef_not_bitfield) |
| 668 | << Name << BitWidth->getSourceRange(); |
| 669 | } else { |
| 670 | // A function typedef ("typedef int f(); f a;"). |
| 671 | // C++ 9.6p3: A bit-field shall have integral or enumeration type. |
| 672 | Diag(Loc, diag::err_not_integral_type_bitfield) |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 673 | << Name << cast<ValueDecl>(Member)->getType() |
| 674 | << BitWidth->getSourceRange(); |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | DeleteExpr(BitWidth); |
| 678 | BitWidth = 0; |
| 679 | Member->setInvalidDecl(); |
| 680 | } |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 681 | |
| 682 | Member->setAccess(AS); |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 683 | |
| 684 | // If we have declared a member function template, set the access of the |
| 685 | // templated declaration as well. |
| 686 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Member)) |
| 687 | FunTmpl->getTemplatedDecl()->setAccess(AS); |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 688 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 689 | |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 690 | assert((Name || isInstField) && "No identifier for non-field ?"); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 691 | |
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 692 | if (Init) |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 693 | AddInitializerToDecl(DeclPtrTy::make(Member), ExprArg(*this, Init), false); |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 694 | if (Deleted) // FIXME: Source location is not very good. |
| 695 | SetDeclDeleted(DeclPtrTy::make(Member), D.getSourceRange().getBegin()); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 696 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 697 | if (isInstField) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 698 | FieldCollector->Add(cast<FieldDecl>(Member)); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 699 | return DeclPtrTy(); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 700 | } |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 701 | return DeclPtrTy::make(Member); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 702 | } |
| 703 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 704 | /// ActOnMemInitializer - Handle a C++ member initializer. |
| 705 | Sema::MemInitResult |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 706 | Sema::ActOnMemInitializer(DeclPtrTy ConstructorD, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 707 | Scope *S, |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 708 | const CXXScopeSpec &SS, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 709 | IdentifierInfo *MemberOrBase, |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 710 | TypeTy *TemplateTypeTy, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 711 | SourceLocation IdLoc, |
| 712 | SourceLocation LParenLoc, |
| 713 | ExprTy **Args, unsigned NumArgs, |
| 714 | SourceLocation *CommaLocs, |
| 715 | SourceLocation RParenLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 716 | if (!ConstructorD) |
| 717 | return true; |
| 718 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 719 | AdjustDeclIfTemplate(ConstructorD); |
| 720 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 721 | CXXConstructorDecl *Constructor |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 722 | = dyn_cast<CXXConstructorDecl>(ConstructorD.getAs<Decl>()); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 723 | if (!Constructor) { |
| 724 | // The user wrote a constructor initializer on a function that is |
| 725 | // not a C++ constructor. Ignore the error for now, because we may |
| 726 | // have more member initializers coming; we'll diagnose it just |
| 727 | // once in ActOnMemInitializers. |
| 728 | return true; |
| 729 | } |
| 730 | |
| 731 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 732 | |
| 733 | // C++ [class.base.init]p2: |
| 734 | // Names in a mem-initializer-id are looked up in the scope of the |
| 735 | // constructor’s class and, if not found in that scope, are looked |
| 736 | // up in the scope containing the constructor’s |
| 737 | // definition. [Note: if the constructor’s class contains a member |
| 738 | // with the same name as a direct or virtual base class of the |
| 739 | // class, a mem-initializer-id naming the member or base class and |
| 740 | // composed of a single identifier refers to the class member. A |
| 741 | // mem-initializer-id for the hidden base class may be specified |
| 742 | // using a qualified name. ] |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 743 | if (!SS.getScopeRep() && !TemplateTypeTy) { |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 744 | // Look for a member, first. |
| 745 | FieldDecl *Member = 0; |
| 746 | DeclContext::lookup_result Result |
| 747 | = ClassDecl->lookup(MemberOrBase); |
| 748 | if (Result.first != Result.second) |
| 749 | Member = dyn_cast<FieldDecl>(*Result.first); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 750 | |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 751 | // FIXME: Handle members of an anonymous union. |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 752 | |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 753 | if (Member) |
| 754 | return BuildMemberInitializer(Member, (Expr**)Args, NumArgs, IdLoc, |
| 755 | RParenLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 756 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 757 | // It didn't name a member, so see if it names a class. |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 758 | TypeTy *BaseTy = TemplateTypeTy ? TemplateTypeTy |
| 759 | : getTypeName(*MemberOrBase, IdLoc, S, &SS); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 760 | if (!BaseTy) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 761 | return Diag(IdLoc, diag::err_mem_init_not_member_or_class) |
| 762 | << MemberOrBase << SourceRange(IdLoc, RParenLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 763 | |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 764 | QualType BaseType = GetTypeFromParser(BaseTy); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 765 | |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 766 | return BuildBaseInitializer(BaseType, (Expr **)Args, NumArgs, IdLoc, |
| 767 | RParenLoc, ClassDecl); |
| 768 | } |
| 769 | |
| 770 | Sema::MemInitResult |
| 771 | Sema::BuildMemberInitializer(FieldDecl *Member, Expr **Args, |
| 772 | unsigned NumArgs, SourceLocation IdLoc, |
| 773 | SourceLocation RParenLoc) { |
| 774 | bool HasDependentArg = false; |
| 775 | for (unsigned i = 0; i < NumArgs; i++) |
| 776 | HasDependentArg |= Args[i]->isTypeDependent(); |
| 777 | |
| 778 | CXXConstructorDecl *C = 0; |
| 779 | QualType FieldType = Member->getType(); |
| 780 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 781 | FieldType = Array->getElementType(); |
| 782 | if (FieldType->isDependentType()) { |
| 783 | // Can't check init for dependent type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 784 | } else if (FieldType->getAs<RecordType>()) { |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 785 | if (!HasDependentArg) |
| 786 | C = PerformInitializationByConstructor( |
| 787 | FieldType, (Expr **)Args, NumArgs, IdLoc, |
| 788 | SourceRange(IdLoc, RParenLoc), Member->getDeclName(), IK_Direct); |
| 789 | } else if (NumArgs != 1) { |
| 790 | return Diag(IdLoc, diag::err_mem_initializer_mismatch) |
| 791 | << Member->getDeclName() << SourceRange(IdLoc, RParenLoc); |
| 792 | } else if (!HasDependentArg) { |
| 793 | Expr *NewExp = (Expr*)Args[0]; |
| 794 | if (PerformCopyInitialization(NewExp, FieldType, "passing")) |
| 795 | return true; |
| 796 | Args[0] = NewExp; |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 797 | } |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 798 | // FIXME: Perform direct initialization of the member. |
| 799 | return new (Context) CXXBaseOrMemberInitializer(Member, (Expr **)Args, |
| 800 | NumArgs, C, IdLoc); |
| 801 | } |
| 802 | |
| 803 | Sema::MemInitResult |
| 804 | Sema::BuildBaseInitializer(QualType BaseType, Expr **Args, |
| 805 | unsigned NumArgs, SourceLocation IdLoc, |
| 806 | SourceLocation RParenLoc, CXXRecordDecl *ClassDecl) { |
| 807 | bool HasDependentArg = false; |
| 808 | for (unsigned i = 0; i < NumArgs; i++) |
| 809 | HasDependentArg |= Args[i]->isTypeDependent(); |
| 810 | |
| 811 | if (!BaseType->isDependentType()) { |
| 812 | if (!BaseType->isRecordType()) |
| 813 | return Diag(IdLoc, diag::err_base_init_does_not_name_class) |
| 814 | << BaseType << SourceRange(IdLoc, RParenLoc); |
| 815 | |
| 816 | // C++ [class.base.init]p2: |
| 817 | // [...] Unless the mem-initializer-id names a nonstatic data |
| 818 | // member of the constructor’s class or a direct or virtual base |
| 819 | // of that class, the mem-initializer is ill-formed. A |
| 820 | // mem-initializer-list can initialize a base class using any |
| 821 | // name that denotes that base class type. |
| 822 | |
| 823 | // First, check for a direct base class. |
| 824 | const CXXBaseSpecifier *DirectBaseSpec = 0; |
| 825 | for (CXXRecordDecl::base_class_const_iterator Base = |
| 826 | ClassDecl->bases_begin(); Base != ClassDecl->bases_end(); ++Base) { |
| 827 | if (Context.getCanonicalType(BaseType).getUnqualifiedType() == |
| 828 | Context.getCanonicalType(Base->getType()).getUnqualifiedType()) { |
| 829 | // We found a direct base of this type. That's what we're |
| 830 | // initializing. |
| 831 | DirectBaseSpec = &*Base; |
| 832 | break; |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | // Check for a virtual base class. |
| 837 | // FIXME: We might be able to short-circuit this if we know in advance that |
| 838 | // there are no virtual bases. |
| 839 | const CXXBaseSpecifier *VirtualBaseSpec = 0; |
| 840 | if (!DirectBaseSpec || !DirectBaseSpec->isVirtual()) { |
| 841 | // We haven't found a base yet; search the class hierarchy for a |
| 842 | // virtual base class. |
| 843 | BasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 844 | /*DetectVirtual=*/false); |
| 845 | if (IsDerivedFrom(Context.getTypeDeclType(ClassDecl), BaseType, Paths)) { |
| 846 | for (BasePaths::paths_iterator Path = Paths.begin(); |
| 847 | Path != Paths.end(); ++Path) { |
| 848 | if (Path->back().Base->isVirtual()) { |
| 849 | VirtualBaseSpec = Path->back().Base; |
| 850 | break; |
| 851 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 852 | } |
| 853 | } |
| 854 | } |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 855 | |
| 856 | // C++ [base.class.init]p2: |
| 857 | // If a mem-initializer-id is ambiguous because it designates both |
| 858 | // a direct non-virtual base class and an inherited virtual base |
| 859 | // class, the mem-initializer is ill-formed. |
| 860 | if (DirectBaseSpec && VirtualBaseSpec) |
| 861 | return Diag(IdLoc, diag::err_base_init_direct_and_virtual) |
| 862 | << BaseType << SourceRange(IdLoc, RParenLoc); |
| 863 | // C++ [base.class.init]p2: |
| 864 | // Unless the mem-initializer-id names a nonstatic data membeer of the |
| 865 | // constructor's class ot a direst or virtual base of that class, the |
| 866 | // mem-initializer is ill-formed. |
| 867 | if (!DirectBaseSpec && !VirtualBaseSpec) |
| 868 | return Diag(IdLoc, diag::err_not_direct_base_or_virtual) |
| 869 | << BaseType << ClassDecl->getNameAsCString() |
| 870 | << SourceRange(IdLoc, RParenLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 871 | } |
| 872 | |
Fariborz Jahanian | d7b27e1 | 2009-07-23 00:42:24 +0000 | [diff] [blame] | 873 | CXXConstructorDecl *C = 0; |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 874 | if (!BaseType->isDependentType() && !HasDependentArg) { |
| 875 | DeclarationName Name = Context.DeclarationNames.getCXXConstructorName( |
| 876 | Context.getCanonicalType(BaseType)); |
| 877 | C = PerformInitializationByConstructor(BaseType, (Expr **)Args, NumArgs, |
| 878 | IdLoc, SourceRange(IdLoc, RParenLoc), |
| 879 | Name, IK_Direct); |
| 880 | } |
| 881 | |
| 882 | return new (Context) CXXBaseOrMemberInitializer(BaseType, (Expr **)Args, |
Fariborz Jahanian | d7b27e1 | 2009-07-23 00:42:24 +0000 | [diff] [blame] | 883 | NumArgs, C, IdLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 884 | } |
| 885 | |
Fariborz Jahanian | 87595e4 | 2009-07-23 23:32:59 +0000 | [diff] [blame] | 886 | void |
| 887 | Sema::BuildBaseOrMemberInitializers(ASTContext &C, |
| 888 | CXXConstructorDecl *Constructor, |
| 889 | CXXBaseOrMemberInitializer **Initializers, |
| 890 | unsigned NumInitializers |
| 891 | ) { |
| 892 | llvm::SmallVector<CXXBaseSpecifier *, 4>Bases; |
| 893 | llvm::SmallVector<FieldDecl *, 4>Members; |
| 894 | |
| 895 | Constructor->setBaseOrMemberInitializers(C, |
| 896 | Initializers, NumInitializers, |
| 897 | Bases, Members); |
| 898 | for (unsigned int i = 0; i < Bases.size(); i++) |
| 899 | Diag(Bases[i]->getSourceRange().getBegin(), |
| 900 | diag::err_missing_default_constructor) << 0 << Bases[i]->getType(); |
| 901 | for (unsigned int i = 0; i < Members.size(); i++) |
| 902 | Diag(Members[i]->getLocation(), diag::err_missing_default_constructor) |
| 903 | << 1 << Members[i]->getType(); |
| 904 | } |
| 905 | |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 906 | static void *GetKeyForTopLevelField(FieldDecl *Field) { |
| 907 | // For anonymous unions, use the class declaration as the key. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 908 | if (const RecordType *RT = Field->getType()->getAs<RecordType>()) { |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 909 | if (RT->getDecl()->isAnonymousStructOrUnion()) |
| 910 | return static_cast<void *>(RT->getDecl()); |
| 911 | } |
| 912 | return static_cast<void *>(Field); |
| 913 | } |
| 914 | |
Fariborz Jahanian | e649412 | 2009-08-11 18:49:54 +0000 | [diff] [blame] | 915 | static void *GetKeyForMember(CXXBaseOrMemberInitializer *Member, |
| 916 | bool MemberMaybeAnon=false) { |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 917 | // For fields injected into the class via declaration of an anonymous union, |
| 918 | // use its anonymous union class declaration as the unique key. |
| 919 | if (FieldDecl *Field = Member->getMember()) { |
Fariborz Jahanian | e649412 | 2009-08-11 18:49:54 +0000 | [diff] [blame] | 920 | // After BuildBaseOrMemberInitializers call, Field is the anonymous union |
| 921 | // data member of the class. Data member used in the initializer list is |
| 922 | // in AnonUnionMember field. |
| 923 | if (MemberMaybeAnon && Field->isAnonymousStructOrUnion()) |
| 924 | Field = Member->getAnonUnionMember(); |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 925 | if (Field->getDeclContext()->isRecord()) { |
| 926 | RecordDecl *RD = cast<RecordDecl>(Field->getDeclContext()); |
| 927 | if (RD->isAnonymousStructOrUnion()) |
| 928 | return static_cast<void *>(RD); |
| 929 | } |
| 930 | return static_cast<void *>(Field); |
| 931 | } |
| 932 | return static_cast<RecordType *>(Member->getBaseClass()); |
| 933 | } |
| 934 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 935 | void Sema::ActOnMemInitializers(DeclPtrTy ConstructorDecl, |
Anders Carlsson | a7b3521 | 2009-03-25 02:58:17 +0000 | [diff] [blame] | 936 | SourceLocation ColonLoc, |
| 937 | MemInitTy **MemInits, unsigned NumMemInits) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 938 | if (!ConstructorDecl) |
| 939 | return; |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 940 | |
| 941 | AdjustDeclIfTemplate(ConstructorDecl); |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 942 | |
| 943 | CXXConstructorDecl *Constructor |
| 944 | = dyn_cast<CXXConstructorDecl>(ConstructorDecl.getAs<Decl>()); |
Anders Carlsson | a7b3521 | 2009-03-25 02:58:17 +0000 | [diff] [blame] | 945 | |
| 946 | if (!Constructor) { |
| 947 | Diag(ColonLoc, diag::err_only_constructors_take_base_inits); |
| 948 | return; |
| 949 | } |
Fariborz Jahanian | 16dbdce | 2009-06-30 00:17:27 +0000 | [diff] [blame] | 950 | llvm::DenseMap<void*, CXXBaseOrMemberInitializer *>Members; |
Fariborz Jahanian | 73b85f3 | 2009-07-01 23:35:25 +0000 | [diff] [blame] | 951 | bool err = false; |
Fariborz Jahanian | 7881a05 | 2009-06-29 22:33:26 +0000 | [diff] [blame] | 952 | for (unsigned i = 0; i < NumMemInits; i++) { |
| 953 | CXXBaseOrMemberInitializer *Member = |
| 954 | static_cast<CXXBaseOrMemberInitializer*>(MemInits[i]); |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 955 | void *KeyToMember = GetKeyForMember(Member); |
Fariborz Jahanian | 5ac3dfc | 2009-06-30 21:52:59 +0000 | [diff] [blame] | 956 | CXXBaseOrMemberInitializer *&PrevMember = Members[KeyToMember]; |
Fariborz Jahanian | 16dbdce | 2009-06-30 00:17:27 +0000 | [diff] [blame] | 957 | if (!PrevMember) { |
Fariborz Jahanian | 47deacf | 2009-06-30 00:02:17 +0000 | [diff] [blame] | 958 | PrevMember = Member; |
Fariborz Jahanian | 16dbdce | 2009-06-30 00:17:27 +0000 | [diff] [blame] | 959 | continue; |
Fariborz Jahanian | 7881a05 | 2009-06-29 22:33:26 +0000 | [diff] [blame] | 960 | } |
Fariborz Jahanian | 16dbdce | 2009-06-30 00:17:27 +0000 | [diff] [blame] | 961 | if (FieldDecl *Field = Member->getMember()) |
| 962 | Diag(Member->getSourceLocation(), |
| 963 | diag::error_multiple_mem_initialization) |
| 964 | << Field->getNameAsString(); |
| 965 | else { |
| 966 | Type *BaseClass = Member->getBaseClass(); |
| 967 | assert(BaseClass && "ActOnMemInitializers - neither field or base"); |
| 968 | Diag(Member->getSourceLocation(), |
| 969 | diag::error_multiple_base_initialization) |
| 970 | << BaseClass->getDesugaredType(true); |
| 971 | } |
| 972 | Diag(PrevMember->getSourceLocation(), diag::note_previous_initializer) |
| 973 | << 0; |
Fariborz Jahanian | 73b85f3 | 2009-07-01 23:35:25 +0000 | [diff] [blame] | 974 | err = true; |
Fariborz Jahanian | 7881a05 | 2009-06-29 22:33:26 +0000 | [diff] [blame] | 975 | } |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame^] | 976 | |
| 977 | if (err) |
| 978 | return; |
| 979 | |
| 980 | BuildBaseOrMemberInitializers(Context, Constructor, |
Fariborz Jahanian | 87595e4 | 2009-07-23 23:32:59 +0000 | [diff] [blame] | 981 | reinterpret_cast<CXXBaseOrMemberInitializer **>(MemInits), |
| 982 | NumMemInits); |
| 983 | |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame^] | 984 | if (Diags.getDiagnosticLevel(diag::warn_base_initialized) == |
| 985 | Diagnostic::Ignored && |
| 986 | Diags.getDiagnosticLevel(diag::warn_field_initialized) == |
| 987 | Diagnostic::Ignored) |
| 988 | return; |
| 989 | |
| 990 | // Also issue warning if order of ctor-initializer list does not match order |
| 991 | // of 1) base class declarations and 2) order of non-static data members. |
| 992 | llvm::SmallVector<const void*, 32> AllBaseOrMembers; |
| 993 | |
| 994 | CXXRecordDecl *ClassDecl |
| 995 | = cast<CXXRecordDecl>(Constructor->getDeclContext()); |
| 996 | // Push virtual bases before others. |
| 997 | for (CXXRecordDecl::base_class_iterator VBase = |
| 998 | ClassDecl->vbases_begin(), |
| 999 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) |
| 1000 | AllBaseOrMembers.push_back(VBase->getType()->getAs<RecordType>()); |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 1001 | |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame^] | 1002 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 1003 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 1004 | // Virtuals are alread in the virtual base list and are constructed |
| 1005 | // first. |
| 1006 | if (Base->isVirtual()) |
| 1007 | continue; |
| 1008 | AllBaseOrMembers.push_back(Base->getType()->getAs<RecordType>()); |
| 1009 | } |
| 1010 | |
| 1011 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 1012 | E = ClassDecl->field_end(); Field != E; ++Field) |
| 1013 | AllBaseOrMembers.push_back(GetKeyForTopLevelField(*Field)); |
| 1014 | |
| 1015 | int Last = AllBaseOrMembers.size(); |
| 1016 | int curIndex = 0; |
| 1017 | CXXBaseOrMemberInitializer *PrevMember = 0; |
| 1018 | for (unsigned i = 0; i < NumMemInits; i++) { |
| 1019 | CXXBaseOrMemberInitializer *Member = |
| 1020 | static_cast<CXXBaseOrMemberInitializer*>(MemInits[i]); |
| 1021 | void *MemberInCtorList = GetKeyForMember(Member, true); |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1022 | |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame^] | 1023 | for (; curIndex < Last; curIndex++) |
| 1024 | if (MemberInCtorList == AllBaseOrMembers[curIndex]) |
| 1025 | break; |
| 1026 | if (curIndex == Last) { |
| 1027 | assert(PrevMember && "Member not in member list?!"); |
| 1028 | // Initializer as specified in ctor-initializer list is out of order. |
| 1029 | // Issue a warning diagnostic. |
| 1030 | if (PrevMember->isBaseInitializer()) { |
| 1031 | // Diagnostics is for an initialized base class. |
| 1032 | Type *BaseClass = PrevMember->getBaseClass(); |
| 1033 | Diag(PrevMember->getSourceLocation(), |
| 1034 | diag::warn_base_initialized) |
| 1035 | << BaseClass->getDesugaredType(true); |
| 1036 | } else { |
| 1037 | FieldDecl *Field = PrevMember->getMember(); |
| 1038 | Diag(PrevMember->getSourceLocation(), |
| 1039 | diag::warn_field_initialized) |
| 1040 | << Field->getNameAsString(); |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 1041 | } |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame^] | 1042 | // Also the note! |
| 1043 | if (FieldDecl *Field = Member->getMember()) |
| 1044 | Diag(Member->getSourceLocation(), |
| 1045 | diag::note_fieldorbase_initialized_here) << 0 |
| 1046 | << Field->getNameAsString(); |
| 1047 | else { |
| 1048 | Type *BaseClass = Member->getBaseClass(); |
| 1049 | Diag(Member->getSourceLocation(), |
| 1050 | diag::note_fieldorbase_initialized_here) << 1 |
| 1051 | << BaseClass->getDesugaredType(true); |
| 1052 | } |
| 1053 | for (curIndex = 0; curIndex < Last; curIndex++) |
| 1054 | if (MemberInCtorList == AllBaseOrMembers[curIndex]) |
| 1055 | break; |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 1056 | } |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame^] | 1057 | PrevMember = Member; |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 1058 | } |
Anders Carlsson | a7b3521 | 2009-03-25 02:58:17 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
Fariborz Jahanian | 393612e | 2009-07-21 22:36:06 +0000 | [diff] [blame] | 1061 | void Sema::ActOnDefaultCtorInitializers(DeclPtrTy CDtorDecl) { |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 1062 | if (!CDtorDecl) |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 1063 | return; |
| 1064 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 1065 | AdjustDeclIfTemplate(CDtorDecl); |
| 1066 | |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 1067 | if (CXXConstructorDecl *Constructor |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 1068 | = dyn_cast<CXXConstructorDecl>(CDtorDecl.getAs<Decl>())) |
Fariborz Jahanian | 87595e4 | 2009-07-23 23:32:59 +0000 | [diff] [blame] | 1069 | BuildBaseOrMemberInitializers(Context, |
| 1070 | Constructor, |
| 1071 | (CXXBaseOrMemberInitializer **)0, 0); |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1074 | namespace { |
| 1075 | /// PureVirtualMethodCollector - traverses a class and its superclasses |
| 1076 | /// and determines if it has any pure virtual methods. |
| 1077 | class VISIBILITY_HIDDEN PureVirtualMethodCollector { |
| 1078 | ASTContext &Context; |
| 1079 | |
Sebastian Redl | dfe292d | 2009-03-22 21:28:55 +0000 | [diff] [blame] | 1080 | public: |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1081 | typedef llvm::SmallVector<const CXXMethodDecl*, 8> MethodList; |
Sebastian Redl | dfe292d | 2009-03-22 21:28:55 +0000 | [diff] [blame] | 1082 | |
| 1083 | private: |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1084 | MethodList Methods; |
| 1085 | |
| 1086 | void Collect(const CXXRecordDecl* RD, MethodList& Methods); |
| 1087 | |
| 1088 | public: |
| 1089 | PureVirtualMethodCollector(ASTContext &Ctx, const CXXRecordDecl* RD) |
| 1090 | : Context(Ctx) { |
| 1091 | |
| 1092 | MethodList List; |
| 1093 | Collect(RD, List); |
| 1094 | |
| 1095 | // Copy the temporary list to methods, and make sure to ignore any |
| 1096 | // null entries. |
| 1097 | for (size_t i = 0, e = List.size(); i != e; ++i) { |
| 1098 | if (List[i]) |
| 1099 | Methods.push_back(List[i]); |
| 1100 | } |
| 1101 | } |
| 1102 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1103 | bool empty() const { return Methods.empty(); } |
| 1104 | |
| 1105 | MethodList::const_iterator methods_begin() { return Methods.begin(); } |
| 1106 | MethodList::const_iterator methods_end() { return Methods.end(); } |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1107 | }; |
| 1108 | |
| 1109 | void PureVirtualMethodCollector::Collect(const CXXRecordDecl* RD, |
| 1110 | MethodList& Methods) { |
| 1111 | // First, collect the pure virtual methods for the base classes. |
| 1112 | for (CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin(), |
| 1113 | BaseEnd = RD->bases_end(); Base != BaseEnd; ++Base) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1114 | if (const RecordType *RT = Base->getType()->getAs<RecordType>()) { |
Chris Lattner | 64540d7 | 2009-03-29 05:01:10 +0000 | [diff] [blame] | 1115 | const CXXRecordDecl *BaseDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1116 | if (BaseDecl && BaseDecl->isAbstract()) |
| 1117 | Collect(BaseDecl, Methods); |
| 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | // Next, zero out any pure virtual methods that this class overrides. |
Anders Carlsson | 8ff8c22 | 2009-05-17 00:00:05 +0000 | [diff] [blame] | 1122 | typedef llvm::SmallPtrSet<const CXXMethodDecl*, 4> MethodSetTy; |
| 1123 | |
| 1124 | MethodSetTy OverriddenMethods; |
| 1125 | size_t MethodsSize = Methods.size(); |
| 1126 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1127 | for (RecordDecl::decl_iterator i = RD->decls_begin(), e = RD->decls_end(); |
Anders Carlsson | 8ff8c22 | 2009-05-17 00:00:05 +0000 | [diff] [blame] | 1128 | i != e; ++i) { |
| 1129 | // Traverse the record, looking for methods. |
| 1130 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(*i)) { |
Sebastian Redl | 23c7d06 | 2009-07-07 20:29:57 +0000 | [diff] [blame] | 1131 | // If the method is pure virtual, add it to the methods vector. |
Anders Carlsson | 8ff8c22 | 2009-05-17 00:00:05 +0000 | [diff] [blame] | 1132 | if (MD->isPure()) { |
| 1133 | Methods.push_back(MD); |
| 1134 | continue; |
| 1135 | } |
| 1136 | |
| 1137 | // Otherwise, record all the overridden methods in our set. |
| 1138 | for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), |
| 1139 | E = MD->end_overridden_methods(); I != E; ++I) { |
| 1140 | // Keep track of the overridden methods. |
| 1141 | OverriddenMethods.insert(*I); |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1142 | } |
| 1143 | } |
| 1144 | } |
| 1145 | |
Anders Carlsson | 8ff8c22 | 2009-05-17 00:00:05 +0000 | [diff] [blame] | 1146 | // Now go through the methods and zero out all the ones we know are |
| 1147 | // overridden. |
| 1148 | for (size_t i = 0, e = MethodsSize; i != e; ++i) { |
| 1149 | if (OverriddenMethods.count(Methods[i])) |
| 1150 | Methods[i] = 0; |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1151 | } |
Anders Carlsson | 8ff8c22 | 2009-05-17 00:00:05 +0000 | [diff] [blame] | 1152 | |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1153 | } |
| 1154 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1155 | |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 1156 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1157 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1158 | unsigned DiagID, AbstractDiagSelID SelID, |
| 1159 | const CXXRecordDecl *CurrentRD) { |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 1160 | if (SelID == -1) |
| 1161 | return RequireNonAbstractType(Loc, T, |
| 1162 | PDiag(DiagID), CurrentRD); |
| 1163 | else |
| 1164 | return RequireNonAbstractType(Loc, T, |
| 1165 | PDiag(DiagID) << SelID, CurrentRD); |
| 1166 | } |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1167 | |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 1168 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
| 1169 | const PartialDiagnostic &PD, |
| 1170 | const CXXRecordDecl *CurrentRD) { |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1171 | if (!getLangOptions().CPlusPlus) |
| 1172 | return false; |
Anders Carlsson | 11f21a0 | 2009-03-23 19:10:31 +0000 | [diff] [blame] | 1173 | |
| 1174 | if (const ArrayType *AT = Context.getAsArrayType(T)) |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 1175 | return RequireNonAbstractType(Loc, AT->getElementType(), PD, |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1176 | CurrentRD); |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 1177 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1178 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 1179 | // Find the innermost pointer type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1180 | while (const PointerType *T = PT->getPointeeType()->getAs<PointerType>()) |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 1181 | PT = T; |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1182 | |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 1183 | if (const ArrayType *AT = Context.getAsArrayType(PT->getPointeeType())) |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 1184 | return RequireNonAbstractType(Loc, AT->getElementType(), PD, CurrentRD); |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1187 | const RecordType *RT = T->getAs<RecordType>(); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1188 | if (!RT) |
| 1189 | return false; |
| 1190 | |
| 1191 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
| 1192 | if (!RD) |
| 1193 | return false; |
| 1194 | |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1195 | if (CurrentRD && CurrentRD != RD) |
| 1196 | return false; |
| 1197 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1198 | if (!RD->isAbstract()) |
| 1199 | return false; |
| 1200 | |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 1201 | Diag(Loc, PD) << RD->getDeclName(); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1202 | |
| 1203 | // Check if we've already emitted the list of pure virtual functions for this |
| 1204 | // class. |
| 1205 | if (PureVirtualClassDiagSet && PureVirtualClassDiagSet->count(RD)) |
| 1206 | return true; |
| 1207 | |
| 1208 | PureVirtualMethodCollector Collector(Context, RD); |
| 1209 | |
| 1210 | for (PureVirtualMethodCollector::MethodList::const_iterator I = |
| 1211 | Collector.methods_begin(), E = Collector.methods_end(); I != E; ++I) { |
| 1212 | const CXXMethodDecl *MD = *I; |
| 1213 | |
| 1214 | Diag(MD->getLocation(), diag::note_pure_virtual_function) << |
| 1215 | MD->getDeclName(); |
| 1216 | } |
| 1217 | |
| 1218 | if (!PureVirtualClassDiagSet) |
| 1219 | PureVirtualClassDiagSet.reset(new RecordDeclSetTy); |
| 1220 | PureVirtualClassDiagSet->insert(RD); |
| 1221 | |
| 1222 | return true; |
| 1223 | } |
| 1224 | |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1225 | namespace { |
| 1226 | class VISIBILITY_HIDDEN AbstractClassUsageDiagnoser |
| 1227 | : public DeclVisitor<AbstractClassUsageDiagnoser, bool> { |
| 1228 | Sema &SemaRef; |
| 1229 | CXXRecordDecl *AbstractClass; |
| 1230 | |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1231 | bool VisitDeclContext(const DeclContext *DC) { |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1232 | bool Invalid = false; |
| 1233 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1234 | for (CXXRecordDecl::decl_iterator I = DC->decls_begin(), |
| 1235 | E = DC->decls_end(); I != E; ++I) |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1236 | Invalid |= Visit(*I); |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1237 | |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1238 | return Invalid; |
| 1239 | } |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1240 | |
| 1241 | public: |
| 1242 | AbstractClassUsageDiagnoser(Sema& SemaRef, CXXRecordDecl *ac) |
| 1243 | : SemaRef(SemaRef), AbstractClass(ac) { |
| 1244 | Visit(SemaRef.Context.getTranslationUnitDecl()); |
| 1245 | } |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1246 | |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1247 | bool VisitFunctionDecl(const FunctionDecl *FD) { |
| 1248 | if (FD->isThisDeclarationADefinition()) { |
| 1249 | // No need to do the check if we're in a definition, because it requires |
| 1250 | // that the return/param types are complete. |
| 1251 | // because that requires |
| 1252 | return VisitDeclContext(FD); |
| 1253 | } |
| 1254 | |
| 1255 | // Check the return type. |
| 1256 | QualType RTy = FD->getType()->getAsFunctionType()->getResultType(); |
| 1257 | bool Invalid = |
| 1258 | SemaRef.RequireNonAbstractType(FD->getLocation(), RTy, |
| 1259 | diag::err_abstract_type_in_decl, |
| 1260 | Sema::AbstractReturnType, |
| 1261 | AbstractClass); |
| 1262 | |
| 1263 | for (FunctionDecl::param_const_iterator I = FD->param_begin(), |
| 1264 | E = FD->param_end(); I != E; ++I) { |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1265 | const ParmVarDecl *VD = *I; |
| 1266 | Invalid |= |
| 1267 | SemaRef.RequireNonAbstractType(VD->getLocation(), |
| 1268 | VD->getOriginalType(), |
| 1269 | diag::err_abstract_type_in_decl, |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1270 | Sema::AbstractParamType, |
| 1271 | AbstractClass); |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
| 1274 | return Invalid; |
| 1275 | } |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1276 | |
| 1277 | bool VisitDecl(const Decl* D) { |
| 1278 | if (const DeclContext *DC = dyn_cast<DeclContext>(D)) |
| 1279 | return VisitDeclContext(DC); |
| 1280 | |
| 1281 | return false; |
| 1282 | } |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1283 | }; |
| 1284 | } |
| 1285 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1286 | void Sema::ActOnFinishCXXMemberSpecification(Scope* S, SourceLocation RLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1287 | DeclPtrTy TagDecl, |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1288 | SourceLocation LBrac, |
| 1289 | SourceLocation RBrac) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 1290 | if (!TagDecl) |
| 1291 | return; |
| 1292 | |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 1293 | AdjustDeclIfTemplate(TagDecl); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1294 | ActOnFields(S, RLoc, TagDecl, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1295 | (DeclPtrTy*)FieldCollector->getCurFields(), |
Daniel Dunbar | 1bfe1c2 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 1296 | FieldCollector->getCurNumFields(), LBrac, RBrac, 0); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1297 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1298 | CXXRecordDecl *RD = cast<CXXRecordDecl>(TagDecl.getAs<Decl>()); |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1299 | if (!RD->isAbstract()) { |
| 1300 | // Collect all the pure virtual methods and see if this is an abstract |
| 1301 | // class after all. |
| 1302 | PureVirtualMethodCollector Collector(Context, RD); |
| 1303 | if (!Collector.empty()) |
| 1304 | RD->setAbstract(true); |
| 1305 | } |
| 1306 | |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1307 | if (RD->isAbstract()) |
| 1308 | AbstractClassUsageDiagnoser(*this, RD); |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1309 | |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 1310 | if (!RD->isDependentType()) |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1311 | AddImplicitlyDeclaredMembersToClass(RD); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1312 | } |
| 1313 | |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1314 | /// AddImplicitlyDeclaredMembersToClass - Adds any implicitly-declared |
| 1315 | /// special functions, such as the default constructor, copy |
| 1316 | /// constructor, or destructor, to the given C++ class (C++ |
| 1317 | /// [special]p1). This routine can only be executed just before the |
| 1318 | /// definition of the class is complete. |
| 1319 | void Sema::AddImplicitlyDeclaredMembersToClass(CXXRecordDecl *ClassDecl) { |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 1320 | CanQualType ClassType |
| 1321 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1322 | |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1323 | // FIXME: Implicit declarations have exception specifications, which are |
| 1324 | // the union of the specifications of the implicitly called functions. |
| 1325 | |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1326 | if (!ClassDecl->hasUserDeclaredConstructor()) { |
| 1327 | // C++ [class.ctor]p5: |
| 1328 | // A default constructor for a class X is a constructor of class X |
| 1329 | // that can be called without an argument. If there is no |
| 1330 | // user-declared constructor for class X, a default constructor is |
| 1331 | // implicitly declared. An implicitly-declared default constructor |
| 1332 | // is an inline public member of its class. |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1333 | DeclarationName Name |
| 1334 | = Context.DeclarationNames.getCXXConstructorName(ClassType); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1335 | CXXConstructorDecl *DefaultCon = |
| 1336 | CXXConstructorDecl::Create(Context, ClassDecl, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1337 | ClassDecl->getLocation(), Name, |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1338 | Context.getFunctionType(Context.VoidTy, |
| 1339 | 0, 0, false, 0), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 1340 | /*DInfo=*/0, |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1341 | /*isExplicit=*/false, |
| 1342 | /*isInline=*/true, |
| 1343 | /*isImplicitlyDeclared=*/true); |
| 1344 | DefaultCon->setAccess(AS_public); |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 1345 | DefaultCon->setImplicit(); |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 1346 | DefaultCon->setTrivial(ClassDecl->hasTrivialConstructor()); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1347 | ClassDecl->addDecl(DefaultCon); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1348 | } |
| 1349 | |
| 1350 | if (!ClassDecl->hasUserDeclaredCopyConstructor()) { |
| 1351 | // C++ [class.copy]p4: |
| 1352 | // If the class definition does not explicitly declare a copy |
| 1353 | // constructor, one is declared implicitly. |
| 1354 | |
| 1355 | // C++ [class.copy]p5: |
| 1356 | // The implicitly-declared copy constructor for a class X will |
| 1357 | // have the form |
| 1358 | // |
| 1359 | // X::X(const X&) |
| 1360 | // |
| 1361 | // if |
| 1362 | bool HasConstCopyConstructor = true; |
| 1363 | |
| 1364 | // -- each direct or virtual base class B of X has a copy |
| 1365 | // constructor whose first parameter is of type const B& or |
| 1366 | // const volatile B&, and |
| 1367 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(); |
| 1368 | HasConstCopyConstructor && Base != ClassDecl->bases_end(); ++Base) { |
| 1369 | const CXXRecordDecl *BaseClassDecl |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1370 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1371 | HasConstCopyConstructor |
| 1372 | = BaseClassDecl->hasConstCopyConstructor(Context); |
| 1373 | } |
| 1374 | |
| 1375 | // -- for all the nonstatic data members of X that are of a |
| 1376 | // class type M (or array thereof), each such class type |
| 1377 | // has a copy constructor whose first parameter is of type |
| 1378 | // const M& or const volatile M&. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1379 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(); |
| 1380 | HasConstCopyConstructor && Field != ClassDecl->field_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1381 | ++Field) { |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1382 | QualType FieldType = (*Field)->getType(); |
| 1383 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 1384 | FieldType = Array->getElementType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1385 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1386 | const CXXRecordDecl *FieldClassDecl |
| 1387 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
| 1388 | HasConstCopyConstructor |
| 1389 | = FieldClassDecl->hasConstCopyConstructor(Context); |
| 1390 | } |
| 1391 | } |
| 1392 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1393 | // Otherwise, the implicitly declared copy constructor will have |
| 1394 | // the form |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1395 | // |
| 1396 | // X::X(X&) |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1397 | QualType ArgType = ClassType; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1398 | if (HasConstCopyConstructor) |
| 1399 | ArgType = ArgType.withConst(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1400 | ArgType = Context.getLValueReferenceType(ArgType); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1401 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1402 | // An implicitly-declared copy constructor is an inline public |
| 1403 | // member of its class. |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1404 | DeclarationName Name |
| 1405 | = Context.DeclarationNames.getCXXConstructorName(ClassType); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1406 | CXXConstructorDecl *CopyConstructor |
| 1407 | = CXXConstructorDecl::Create(Context, ClassDecl, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1408 | ClassDecl->getLocation(), Name, |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1409 | Context.getFunctionType(Context.VoidTy, |
| 1410 | &ArgType, 1, |
| 1411 | false, 0), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 1412 | /*DInfo=*/0, |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1413 | /*isExplicit=*/false, |
| 1414 | /*isInline=*/true, |
| 1415 | /*isImplicitlyDeclared=*/true); |
| 1416 | CopyConstructor->setAccess(AS_public); |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 1417 | CopyConstructor->setImplicit(); |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 1418 | CopyConstructor->setTrivial(ClassDecl->hasTrivialCopyConstructor()); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1419 | |
| 1420 | // Add the parameter to the constructor. |
| 1421 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyConstructor, |
| 1422 | ClassDecl->getLocation(), |
| 1423 | /*IdentifierInfo=*/0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 1424 | ArgType, /*DInfo=*/0, |
| 1425 | VarDecl::None, 0); |
Ted Kremenek | fc76761 | 2009-01-14 00:42:25 +0000 | [diff] [blame] | 1426 | CopyConstructor->setParams(Context, &FromParam, 1); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1427 | ClassDecl->addDecl(CopyConstructor); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1428 | } |
| 1429 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1430 | if (!ClassDecl->hasUserDeclaredCopyAssignment()) { |
| 1431 | // Note: The following rules are largely analoguous to the copy |
| 1432 | // constructor rules. Note that virtual bases are not taken into account |
| 1433 | // for determining the argument type of the operator. Note also that |
| 1434 | // operators taking an object instead of a reference are allowed. |
| 1435 | // |
| 1436 | // C++ [class.copy]p10: |
| 1437 | // If the class definition does not explicitly declare a copy |
| 1438 | // assignment operator, one is declared implicitly. |
| 1439 | // The implicitly-defined copy assignment operator for a class X |
| 1440 | // will have the form |
| 1441 | // |
| 1442 | // X& X::operator=(const X&) |
| 1443 | // |
| 1444 | // if |
| 1445 | bool HasConstCopyAssignment = true; |
| 1446 | |
| 1447 | // -- each direct base class B of X has a copy assignment operator |
| 1448 | // whose parameter is of type const B&, const volatile B& or B, |
| 1449 | // and |
| 1450 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(); |
| 1451 | HasConstCopyAssignment && Base != ClassDecl->bases_end(); ++Base) { |
| 1452 | const CXXRecordDecl *BaseClassDecl |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1453 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 1454 | const CXXMethodDecl *MD = 0; |
| 1455 | HasConstCopyAssignment = BaseClassDecl->hasConstCopyAssignment(Context, |
| 1456 | MD); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
| 1459 | // -- for all the nonstatic data members of X that are of a class |
| 1460 | // type M (or array thereof), each such class type has a copy |
| 1461 | // assignment operator whose parameter is of type const M&, |
| 1462 | // const volatile M& or M. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1463 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(); |
| 1464 | HasConstCopyAssignment && Field != ClassDecl->field_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1465 | ++Field) { |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1466 | QualType FieldType = (*Field)->getType(); |
| 1467 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 1468 | FieldType = Array->getElementType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1469 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1470 | const CXXRecordDecl *FieldClassDecl |
| 1471 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 1472 | const CXXMethodDecl *MD = 0; |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1473 | HasConstCopyAssignment |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 1474 | = FieldClassDecl->hasConstCopyAssignment(Context, MD); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1475 | } |
| 1476 | } |
| 1477 | |
| 1478 | // Otherwise, the implicitly declared copy assignment operator will |
| 1479 | // have the form |
| 1480 | // |
| 1481 | // X& X::operator=(X&) |
| 1482 | QualType ArgType = ClassType; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1483 | QualType RetType = Context.getLValueReferenceType(ArgType); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1484 | if (HasConstCopyAssignment) |
| 1485 | ArgType = ArgType.withConst(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1486 | ArgType = Context.getLValueReferenceType(ArgType); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1487 | |
| 1488 | // An implicitly-declared copy assignment operator is an inline public |
| 1489 | // member of its class. |
| 1490 | DeclarationName Name = |
| 1491 | Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 1492 | CXXMethodDecl *CopyAssignment = |
| 1493 | CXXMethodDecl::Create(Context, ClassDecl, ClassDecl->getLocation(), Name, |
| 1494 | Context.getFunctionType(RetType, &ArgType, 1, |
| 1495 | false, 0), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 1496 | /*DInfo=*/0, /*isStatic=*/false, /*isInline=*/true); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1497 | CopyAssignment->setAccess(AS_public); |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 1498 | CopyAssignment->setImplicit(); |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 1499 | CopyAssignment->setTrivial(ClassDecl->hasTrivialCopyAssignment()); |
Fariborz Jahanian | 2198ba1 | 2009-08-12 21:14:35 +0000 | [diff] [blame] | 1500 | CopyAssignment->setCopyAssignment(true); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1501 | |
| 1502 | // Add the parameter to the operator. |
| 1503 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyAssignment, |
| 1504 | ClassDecl->getLocation(), |
| 1505 | /*IdentifierInfo=*/0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 1506 | ArgType, /*DInfo=*/0, |
| 1507 | VarDecl::None, 0); |
Ted Kremenek | fc76761 | 2009-01-14 00:42:25 +0000 | [diff] [blame] | 1508 | CopyAssignment->setParams(Context, &FromParam, 1); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1509 | |
| 1510 | // Don't call addedAssignmentOperator. There is no way to distinguish an |
| 1511 | // implicit from an explicit assignment operator. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1512 | ClassDecl->addDecl(CopyAssignment); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1513 | } |
| 1514 | |
Douglas Gregor | 9e7d9de | 2008-12-15 21:24:18 +0000 | [diff] [blame] | 1515 | if (!ClassDecl->hasUserDeclaredDestructor()) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1516 | // C++ [class.dtor]p2: |
| 1517 | // If a class has no user-declared destructor, a destructor is |
| 1518 | // declared implicitly. An implicitly-declared destructor is an |
| 1519 | // inline public member of its class. |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1520 | DeclarationName Name |
| 1521 | = Context.DeclarationNames.getCXXDestructorName(ClassType); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1522 | CXXDestructorDecl *Destructor |
| 1523 | = CXXDestructorDecl::Create(Context, ClassDecl, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1524 | ClassDecl->getLocation(), Name, |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1525 | Context.getFunctionType(Context.VoidTy, |
| 1526 | 0, 0, false, 0), |
| 1527 | /*isInline=*/true, |
| 1528 | /*isImplicitlyDeclared=*/true); |
| 1529 | Destructor->setAccess(AS_public); |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 1530 | Destructor->setImplicit(); |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 1531 | Destructor->setTrivial(ClassDecl->hasTrivialDestructor()); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1532 | ClassDecl->addDecl(Destructor); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1533 | } |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1534 | } |
| 1535 | |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 1536 | void Sema::ActOnReenterTemplateScope(Scope *S, DeclPtrTy TemplateD) { |
| 1537 | TemplateDecl *Template = TemplateD.getAs<TemplateDecl>(); |
| 1538 | if (!Template) |
| 1539 | return; |
| 1540 | |
| 1541 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 1542 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 1543 | ParamEnd = Params->end(); |
| 1544 | Param != ParamEnd; ++Param) { |
| 1545 | NamedDecl *Named = cast<NamedDecl>(*Param); |
| 1546 | if (Named->getDeclName()) { |
| 1547 | S->AddDecl(DeclPtrTy::make(Named)); |
| 1548 | IdResolver.AddDecl(Named); |
| 1549 | } |
| 1550 | } |
| 1551 | } |
| 1552 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1553 | /// ActOnStartDelayedCXXMethodDeclaration - We have completed |
| 1554 | /// parsing a top-level (non-nested) C++ class, and we are now |
| 1555 | /// parsing those parts of the given Method declaration that could |
| 1556 | /// not be parsed earlier (C++ [class.mem]p2), such as default |
| 1557 | /// arguments. This action should enter the scope of the given |
| 1558 | /// Method declaration as if we had just parsed the qualified method |
| 1559 | /// name. However, it should not bring the parameters into scope; |
| 1560 | /// that will be performed by ActOnDelayedCXXMethodParameter. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1561 | void Sema::ActOnStartDelayedCXXMethodDeclaration(Scope *S, DeclPtrTy MethodD) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 1562 | if (!MethodD) |
| 1563 | return; |
| 1564 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 1565 | AdjustDeclIfTemplate(MethodD); |
| 1566 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1567 | CXXScopeSpec SS; |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1568 | FunctionDecl *Method = cast<FunctionDecl>(MethodD.getAs<Decl>()); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1569 | QualType ClassTy |
| 1570 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 1571 | SS.setScopeRep( |
| 1572 | NestedNameSpecifier::Create(Context, 0, false, ClassTy.getTypePtr())); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1573 | ActOnCXXEnterDeclaratorScope(S, SS); |
| 1574 | } |
| 1575 | |
| 1576 | /// ActOnDelayedCXXMethodParameter - We've already started a delayed |
| 1577 | /// C++ method declaration. We're (re-)introducing the given |
| 1578 | /// function parameter into scope for use in parsing later parts of |
| 1579 | /// the method declaration. For example, we could see an |
| 1580 | /// ActOnParamDefaultArgument event for this parameter. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1581 | void Sema::ActOnDelayedCXXMethodParameter(Scope *S, DeclPtrTy ParamD) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 1582 | if (!ParamD) |
| 1583 | return; |
| 1584 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1585 | ParmVarDecl *Param = cast<ParmVarDecl>(ParamD.getAs<Decl>()); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 1586 | |
| 1587 | // If this parameter has an unparsed default argument, clear it out |
| 1588 | // to make way for the parsed default argument. |
| 1589 | if (Param->hasUnparsedDefaultArg()) |
| 1590 | Param->setDefaultArg(0); |
| 1591 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1592 | S->AddDecl(DeclPtrTy::make(Param)); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1593 | if (Param->getDeclName()) |
| 1594 | IdResolver.AddDecl(Param); |
| 1595 | } |
| 1596 | |
| 1597 | /// ActOnFinishDelayedCXXMethodDeclaration - We have finished |
| 1598 | /// processing the delayed method declaration for Method. The method |
| 1599 | /// declaration is now considered finished. There may be a separate |
| 1600 | /// ActOnStartOfFunctionDef action later (not necessarily |
| 1601 | /// immediately!) for this method, if it was also defined inside the |
| 1602 | /// class body. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1603 | void Sema::ActOnFinishDelayedCXXMethodDeclaration(Scope *S, DeclPtrTy MethodD) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 1604 | if (!MethodD) |
| 1605 | return; |
| 1606 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 1607 | AdjustDeclIfTemplate(MethodD); |
| 1608 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1609 | FunctionDecl *Method = cast<FunctionDecl>(MethodD.getAs<Decl>()); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1610 | CXXScopeSpec SS; |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1611 | QualType ClassTy |
| 1612 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 1613 | SS.setScopeRep( |
| 1614 | NestedNameSpecifier::Create(Context, 0, false, ClassTy.getTypePtr())); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1615 | ActOnCXXExitDeclaratorScope(S, SS); |
| 1616 | |
| 1617 | // Now that we have our default arguments, check the constructor |
| 1618 | // again. It could produce additional diagnostics or affect whether |
| 1619 | // the class has implicitly-declared destructors, among other |
| 1620 | // things. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 1621 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Method)) |
| 1622 | CheckConstructor(Constructor); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1623 | |
| 1624 | // Check the default arguments, which we may have added. |
| 1625 | if (!Method->isInvalidDecl()) |
| 1626 | CheckCXXDefaultArguments(Method); |
| 1627 | } |
| 1628 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1629 | /// CheckConstructorDeclarator - Called by ActOnDeclarator to check |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1630 | /// the well-formedness of the constructor declarator @p D with type @p |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1631 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 1632 | /// emit diagnostics and set the invalid bit to true. In any case, the type |
| 1633 | /// will be updated to reflect a well-formed type for the constructor and |
| 1634 | /// returned. |
| 1635 | QualType Sema::CheckConstructorDeclarator(Declarator &D, QualType R, |
| 1636 | FunctionDecl::StorageClass &SC) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1637 | bool isVirtual = D.getDeclSpec().isVirtualSpecified(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1638 | |
| 1639 | // C++ [class.ctor]p3: |
| 1640 | // A constructor shall not be virtual (10.3) or static (9.4). A |
| 1641 | // constructor can be invoked for a const, volatile or const |
| 1642 | // volatile object. A constructor shall not be declared const, |
| 1643 | // volatile, or const volatile (9.3.2). |
| 1644 | if (isVirtual) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 1645 | if (!D.isInvalidType()) |
| 1646 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 1647 | << "virtual" << SourceRange(D.getDeclSpec().getVirtualSpecLoc()) |
| 1648 | << SourceRange(D.getIdentifierLoc()); |
| 1649 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1650 | } |
| 1651 | if (SC == FunctionDecl::Static) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 1652 | if (!D.isInvalidType()) |
| 1653 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 1654 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 1655 | << SourceRange(D.getIdentifierLoc()); |
| 1656 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1657 | SC = FunctionDecl::None; |
| 1658 | } |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 1659 | |
| 1660 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 1661 | if (FTI.TypeQuals != 0) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1662 | if (FTI.TypeQuals & QualType::Const) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1663 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 1664 | << "const" << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1665 | if (FTI.TypeQuals & QualType::Volatile) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1666 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 1667 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1668 | if (FTI.TypeQuals & QualType::Restrict) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1669 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 1670 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1671 | } |
| 1672 | |
| 1673 | // Rebuild the function type "R" without any type qualifiers (in |
| 1674 | // case any of the errors above fired) and with "void" as the |
| 1675 | // return type, since constructors don't have return types. We |
| 1676 | // *always* have to do this, because GetTypeForDeclarator will |
| 1677 | // put in a result type of "int" when none was specified. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1678 | const FunctionProtoType *Proto = R->getAsFunctionProtoType(); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 1679 | return Context.getFunctionType(Context.VoidTy, Proto->arg_type_begin(), |
| 1680 | Proto->getNumArgs(), |
| 1681 | Proto->isVariadic(), 0); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1682 | } |
| 1683 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1684 | /// CheckConstructor - Checks a fully-formed constructor for |
| 1685 | /// well-formedness, issuing any diagnostics required. Returns true if |
| 1686 | /// the constructor declarator is invalid. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 1687 | void Sema::CheckConstructor(CXXConstructorDecl *Constructor) { |
Douglas Gregor | 3329756 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 1688 | CXXRecordDecl *ClassDecl |
| 1689 | = dyn_cast<CXXRecordDecl>(Constructor->getDeclContext()); |
| 1690 | if (!ClassDecl) |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 1691 | return Constructor->setInvalidDecl(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1692 | |
| 1693 | // C++ [class.copy]p3: |
| 1694 | // A declaration of a constructor for a class X is ill-formed if |
| 1695 | // its first parameter is of type (optionally cv-qualified) X and |
| 1696 | // either there are no other parameters or else all other |
| 1697 | // parameters have default arguments. |
Douglas Gregor | 3329756 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 1698 | if (!Constructor->isInvalidDecl() && |
| 1699 | ((Constructor->getNumParams() == 1) || |
| 1700 | (Constructor->getNumParams() > 1 && |
Anders Carlsson | ae0b4e7 | 2009-06-06 04:14:07 +0000 | [diff] [blame] | 1701 | Constructor->getParamDecl(1)->hasDefaultArg()))) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1702 | QualType ParamType = Constructor->getParamDecl(0)->getType(); |
| 1703 | QualType ClassTy = Context.getTagDeclType(ClassDecl); |
| 1704 | if (Context.getCanonicalType(ParamType).getUnqualifiedType() == ClassTy) { |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 1705 | SourceLocation ParamLoc = Constructor->getParamDecl(0)->getLocation(); |
| 1706 | Diag(ParamLoc, diag::err_constructor_byvalue_arg) |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 1707 | << CodeModificationHint::CreateInsertion(ParamLoc, " const &"); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 1708 | Constructor->setInvalidDecl(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1709 | } |
| 1710 | } |
| 1711 | |
| 1712 | // Notify the class that we've added a constructor. |
| 1713 | ClassDecl->addedConstructor(Context, Constructor); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 1716 | static inline bool |
| 1717 | FTIHasSingleVoidArgument(DeclaratorChunk::FunctionTypeInfo &FTI) { |
| 1718 | return (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
| 1719 | FTI.ArgInfo[0].Param && |
| 1720 | FTI.ArgInfo[0].Param.getAs<ParmVarDecl>()->getType()->isVoidType()); |
| 1721 | } |
| 1722 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1723 | /// CheckDestructorDeclarator - Called by ActOnDeclarator to check |
| 1724 | /// the well-formednes of the destructor declarator @p D with type @p |
| 1725 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 1726 | /// emit diagnostics and set the declarator to invalid. Even if this happens, |
| 1727 | /// will be updated to reflect a well-formed type for the destructor and |
| 1728 | /// returned. |
| 1729 | QualType Sema::CheckDestructorDeclarator(Declarator &D, |
| 1730 | FunctionDecl::StorageClass& SC) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1731 | // C++ [class.dtor]p1: |
| 1732 | // [...] A typedef-name that names a class is a class-name |
| 1733 | // (7.1.3); however, a typedef-name that names a class shall not |
| 1734 | // be used as the identifier in the declarator for a destructor |
| 1735 | // declaration. |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 1736 | QualType DeclaratorType = GetTypeFromParser(D.getDeclaratorIdType()); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 1737 | if (isa<TypedefType>(DeclaratorType)) { |
| 1738 | Diag(D.getIdentifierLoc(), diag::err_destructor_typedef_name) |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 1739 | << DeclaratorType; |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 1740 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1741 | } |
| 1742 | |
| 1743 | // C++ [class.dtor]p2: |
| 1744 | // A destructor is used to destroy objects of its class type. A |
| 1745 | // destructor takes no parameters, and no return type can be |
| 1746 | // specified for it (not even void). The address of a destructor |
| 1747 | // shall not be taken. A destructor shall not be static. A |
| 1748 | // destructor can be invoked for a const, volatile or const |
| 1749 | // volatile object. A destructor shall not be declared const, |
| 1750 | // volatile or const volatile (9.3.2). |
| 1751 | if (SC == FunctionDecl::Static) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 1752 | if (!D.isInvalidType()) |
| 1753 | Diag(D.getIdentifierLoc(), diag::err_destructor_cannot_be) |
| 1754 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 1755 | << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1756 | SC = FunctionDecl::None; |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 1757 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1758 | } |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 1759 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1760 | // Destructors don't have return types, but the parser will |
| 1761 | // happily parse something like: |
| 1762 | // |
| 1763 | // class X { |
| 1764 | // float ~X(); |
| 1765 | // }; |
| 1766 | // |
| 1767 | // The return type will be eliminated later. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1768 | Diag(D.getIdentifierLoc(), diag::err_destructor_return_type) |
| 1769 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 1770 | << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1771 | } |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 1772 | |
| 1773 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 1774 | if (FTI.TypeQuals != 0 && !D.isInvalidType()) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1775 | if (FTI.TypeQuals & QualType::Const) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1776 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 1777 | << "const" << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1778 | if (FTI.TypeQuals & QualType::Volatile) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1779 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 1780 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1781 | if (FTI.TypeQuals & QualType::Restrict) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1782 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 1783 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 1784 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1785 | } |
| 1786 | |
| 1787 | // Make sure we don't have any parameters. |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 1788 | if (FTI.NumArgs > 0 && !FTIHasSingleVoidArgument(FTI)) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1789 | Diag(D.getIdentifierLoc(), diag::err_destructor_with_params); |
| 1790 | |
| 1791 | // Delete the parameters. |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 1792 | FTI.freeArgs(); |
| 1793 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
| 1796 | // Make sure the destructor isn't variadic. |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 1797 | if (FTI.isVariadic) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1798 | Diag(D.getIdentifierLoc(), diag::err_destructor_variadic); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 1799 | D.setInvalidType(); |
| 1800 | } |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1801 | |
| 1802 | // Rebuild the function type "R" without any type qualifiers or |
| 1803 | // parameters (in case any of the errors above fired) and with |
| 1804 | // "void" as the return type, since destructors don't have return |
| 1805 | // types. We *always* have to do this, because GetTypeForDeclarator |
| 1806 | // will put in a result type of "int" when none was specified. |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 1807 | return Context.getFunctionType(Context.VoidTy, 0, 0, false, 0); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1808 | } |
| 1809 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1810 | /// CheckConversionDeclarator - Called by ActOnDeclarator to check the |
| 1811 | /// well-formednes of the conversion function declarator @p D with |
| 1812 | /// type @p R. If there are any errors in the declarator, this routine |
| 1813 | /// will emit diagnostics and return true. Otherwise, it will return |
| 1814 | /// false. Either way, the type @p R will be updated to reflect a |
| 1815 | /// well-formed type for the conversion operator. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 1816 | void Sema::CheckConversionDeclarator(Declarator &D, QualType &R, |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1817 | FunctionDecl::StorageClass& SC) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1818 | // C++ [class.conv.fct]p1: |
| 1819 | // Neither parameter types nor return type can be specified. The |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 1820 | // type of a conversion function (8.3.5) is "function taking no |
| 1821 | // parameter returning conversion-type-id." |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1822 | if (SC == FunctionDecl::Static) { |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 1823 | if (!D.isInvalidType()) |
| 1824 | Diag(D.getIdentifierLoc(), diag::err_conv_function_not_member) |
| 1825 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 1826 | << SourceRange(D.getIdentifierLoc()); |
| 1827 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1828 | SC = FunctionDecl::None; |
| 1829 | } |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 1830 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1831 | // Conversion functions don't have return types, but the parser will |
| 1832 | // happily parse something like: |
| 1833 | // |
| 1834 | // class X { |
| 1835 | // float operator bool(); |
| 1836 | // }; |
| 1837 | // |
| 1838 | // The return type will be changed later anyway. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1839 | Diag(D.getIdentifierLoc(), diag::err_conv_function_return_type) |
| 1840 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 1841 | << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1842 | } |
| 1843 | |
| 1844 | // Make sure we don't have any parameters. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1845 | if (R->getAsFunctionProtoType()->getNumArgs() > 0) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1846 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_params); |
| 1847 | |
| 1848 | // Delete the parameters. |
Chris Lattner | 1833a83 | 2009-01-20 21:06:38 +0000 | [diff] [blame] | 1849 | D.getTypeObject(0).Fun.freeArgs(); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 1850 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1851 | } |
| 1852 | |
| 1853 | // Make sure the conversion function isn't variadic. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 1854 | if (R->getAsFunctionProtoType()->isVariadic() && !D.isInvalidType()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1855 | Diag(D.getIdentifierLoc(), diag::err_conv_function_variadic); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 1856 | D.setInvalidType(); |
| 1857 | } |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1858 | |
| 1859 | // C++ [class.conv.fct]p4: |
| 1860 | // The conversion-type-id shall not represent a function type nor |
| 1861 | // an array type. |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 1862 | QualType ConvType = GetTypeFromParser(D.getDeclaratorIdType()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1863 | if (ConvType->isArrayType()) { |
| 1864 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_array); |
| 1865 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 1866 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1867 | } else if (ConvType->isFunctionType()) { |
| 1868 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_function); |
| 1869 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 1870 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1871 | } |
| 1872 | |
| 1873 | // Rebuild the function type "R" without any parameters (in case any |
| 1874 | // of the errors above fired) and with the conversion type as the |
| 1875 | // return type. |
| 1876 | R = Context.getFunctionType(ConvType, 0, 0, false, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1877 | R->getAsFunctionProtoType()->getTypeQuals()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1878 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1879 | // C++0x explicit conversion operators. |
| 1880 | if (D.getDeclSpec().isExplicitSpecified() && !getLangOptions().CPlusPlus0x) |
| 1881 | Diag(D.getDeclSpec().getExplicitSpecLoc(), |
| 1882 | diag::warn_explicit_conversion_functions) |
| 1883 | << SourceRange(D.getDeclSpec().getExplicitSpecLoc()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1884 | } |
| 1885 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1886 | /// ActOnConversionDeclarator - Called by ActOnDeclarator to complete |
| 1887 | /// the declaration of the given C++ conversion function. This routine |
| 1888 | /// is responsible for recording the conversion function in the C++ |
| 1889 | /// class, if possible. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1890 | Sema::DeclPtrTy Sema::ActOnConversionDeclarator(CXXConversionDecl *Conversion) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1891 | assert(Conversion && "Expected to receive a conversion function declaration"); |
| 1892 | |
Douglas Gregor | 9d35097 | 2008-12-12 08:25:50 +0000 | [diff] [blame] | 1893 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Conversion->getDeclContext()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1894 | |
| 1895 | // Make sure we aren't redeclaring the conversion function. |
| 1896 | QualType ConvType = Context.getCanonicalType(Conversion->getConversionType()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1897 | |
| 1898 | // C++ [class.conv.fct]p1: |
| 1899 | // [...] A conversion function is never used to convert a |
| 1900 | // (possibly cv-qualified) object to the (possibly cv-qualified) |
| 1901 | // same object type (or a reference to it), to a (possibly |
| 1902 | // cv-qualified) base class of that type (or a reference to it), |
| 1903 | // or to (possibly cv-qualified) void. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1904 | // FIXME: Suppress this warning if the conversion function ends up being a |
| 1905 | // virtual function that overrides a virtual function in a base class. |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1906 | QualType ClassType |
| 1907 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1908 | if (const ReferenceType *ConvTypeRef = ConvType->getAs<ReferenceType>()) |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1909 | ConvType = ConvTypeRef->getPointeeType(); |
| 1910 | if (ConvType->isRecordType()) { |
| 1911 | ConvType = Context.getCanonicalType(ConvType).getUnqualifiedType(); |
| 1912 | if (ConvType == ClassType) |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 1913 | Diag(Conversion->getLocation(), diag::warn_conv_to_self_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 1914 | << ClassType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1915 | else if (IsDerivedFrom(ClassType, ConvType)) |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 1916 | Diag(Conversion->getLocation(), diag::warn_conv_to_base_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 1917 | << ClassType << ConvType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1918 | } else if (ConvType->isVoidType()) { |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 1919 | Diag(Conversion->getLocation(), diag::warn_conv_to_void_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 1920 | << ClassType << ConvType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1921 | } |
| 1922 | |
Douglas Gregor | 70316a0 | 2008-12-26 15:00:45 +0000 | [diff] [blame] | 1923 | if (Conversion->getPreviousDeclaration()) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1924 | const NamedDecl *ExpectedPrevDecl = Conversion->getPreviousDeclaration(); |
| 1925 | if (FunctionTemplateDecl *ConversionTemplate |
| 1926 | = Conversion->getDescribedFunctionTemplate()) |
| 1927 | ExpectedPrevDecl = ConversionTemplate->getPreviousDeclaration(); |
Douglas Gregor | 70316a0 | 2008-12-26 15:00:45 +0000 | [diff] [blame] | 1928 | OverloadedFunctionDecl *Conversions = ClassDecl->getConversionFunctions(); |
| 1929 | for (OverloadedFunctionDecl::function_iterator |
| 1930 | Conv = Conversions->function_begin(), |
| 1931 | ConvEnd = Conversions->function_end(); |
| 1932 | Conv != ConvEnd; ++Conv) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1933 | if (*Conv == ExpectedPrevDecl) { |
Douglas Gregor | 70316a0 | 2008-12-26 15:00:45 +0000 | [diff] [blame] | 1934 | *Conv = Conversion; |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1935 | return DeclPtrTy::make(Conversion); |
Douglas Gregor | 70316a0 | 2008-12-26 15:00:45 +0000 | [diff] [blame] | 1936 | } |
| 1937 | } |
| 1938 | assert(Conversion->isInvalidDecl() && "Conversion should not get here."); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1939 | } else if (FunctionTemplateDecl *ConversionTemplate |
| 1940 | = Conversion->getDescribedFunctionTemplate()) |
| 1941 | ClassDecl->addConversionFunction(Context, ConversionTemplate); |
| 1942 | else if (!Conversion->getPrimaryTemplate()) // ignore specializations |
Douglas Gregor | 70316a0 | 2008-12-26 15:00:45 +0000 | [diff] [blame] | 1943 | ClassDecl->addConversionFunction(Context, Conversion); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1944 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1945 | return DeclPtrTy::make(Conversion); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1946 | } |
| 1947 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 1948 | //===----------------------------------------------------------------------===// |
| 1949 | // Namespace Handling |
| 1950 | //===----------------------------------------------------------------------===// |
| 1951 | |
| 1952 | /// ActOnStartNamespaceDef - This is called at the start of a namespace |
| 1953 | /// definition. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1954 | Sema::DeclPtrTy Sema::ActOnStartNamespaceDef(Scope *NamespcScope, |
| 1955 | SourceLocation IdentLoc, |
| 1956 | IdentifierInfo *II, |
| 1957 | SourceLocation LBrace) { |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 1958 | NamespaceDecl *Namespc = |
| 1959 | NamespaceDecl::Create(Context, CurContext, IdentLoc, II); |
| 1960 | Namespc->setLBracLoc(LBrace); |
| 1961 | |
| 1962 | Scope *DeclRegionScope = NamespcScope->getParent(); |
| 1963 | |
| 1964 | if (II) { |
| 1965 | // C++ [namespace.def]p2: |
| 1966 | // The identifier in an original-namespace-definition shall not have been |
| 1967 | // previously defined in the declarative region in which the |
| 1968 | // original-namespace-definition appears. The identifier in an |
| 1969 | // original-namespace-definition is the name of the namespace. Subsequently |
| 1970 | // in that declarative region, it is treated as an original-namespace-name. |
| 1971 | |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1972 | NamedDecl *PrevDecl = LookupName(DeclRegionScope, II, LookupOrdinaryName, |
| 1973 | true); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1974 | |
| 1975 | if (NamespaceDecl *OrigNS = dyn_cast_or_null<NamespaceDecl>(PrevDecl)) { |
| 1976 | // This is an extended namespace definition. |
| 1977 | // Attach this namespace decl to the chain of extended namespace |
| 1978 | // definitions. |
| 1979 | OrigNS->setNextNamespace(Namespc); |
| 1980 | Namespc->setOriginalNamespace(OrigNS->getOriginalNamespace()); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 1981 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1982 | // Remove the previous declaration from the scope. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1983 | if (DeclRegionScope->isDeclScope(DeclPtrTy::make(OrigNS))) { |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 1984 | IdResolver.RemoveDecl(OrigNS); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1985 | DeclRegionScope->RemoveDecl(DeclPtrTy::make(OrigNS)); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 1986 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1987 | } else if (PrevDecl) { |
| 1988 | // This is an invalid name redefinition. |
| 1989 | Diag(Namespc->getLocation(), diag::err_redefinition_different_kind) |
| 1990 | << Namespc->getDeclName(); |
| 1991 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 1992 | Namespc->setInvalidDecl(); |
| 1993 | // Continue on to push Namespc as current DeclContext and return it. |
| 1994 | } |
| 1995 | |
| 1996 | PushOnScopeChains(Namespc, DeclRegionScope); |
| 1997 | } else { |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 1998 | // FIXME: Handle anonymous namespaces |
| 1999 | } |
| 2000 | |
| 2001 | // Although we could have an invalid decl (i.e. the namespace name is a |
| 2002 | // redefinition), push it as current DeclContext and try to continue parsing. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2003 | // FIXME: We should be able to push Namespc here, so that the each DeclContext |
| 2004 | // for the namespace has the declarations that showed up in that particular |
| 2005 | // namespace definition. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2006 | PushDeclContext(NamespcScope, Namespc); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2007 | return DeclPtrTy::make(Namespc); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2008 | } |
| 2009 | |
| 2010 | /// ActOnFinishNamespaceDef - This callback is called after a namespace is |
| 2011 | /// exited. Decl is the DeclTy returned by ActOnStartNamespaceDef. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2012 | void Sema::ActOnFinishNamespaceDef(DeclPtrTy D, SourceLocation RBrace) { |
| 2013 | Decl *Dcl = D.getAs<Decl>(); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2014 | NamespaceDecl *Namespc = dyn_cast_or_null<NamespaceDecl>(Dcl); |
| 2015 | assert(Namespc && "Invalid parameter, expected NamespaceDecl"); |
| 2016 | Namespc->setRBracLoc(RBrace); |
| 2017 | PopDeclContext(); |
| 2018 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2019 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2020 | Sema::DeclPtrTy Sema::ActOnUsingDirective(Scope *S, |
| 2021 | SourceLocation UsingLoc, |
| 2022 | SourceLocation NamespcLoc, |
| 2023 | const CXXScopeSpec &SS, |
| 2024 | SourceLocation IdentLoc, |
| 2025 | IdentifierInfo *NamespcName, |
| 2026 | AttributeList *AttrList) { |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 2027 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
| 2028 | assert(NamespcName && "Invalid NamespcName."); |
| 2029 | assert(IdentLoc.isValid() && "Invalid NamespceName location."); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2030 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 2031 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2032 | UsingDirectiveDecl *UDir = 0; |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 2033 | |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 2034 | // Lookup namespace name. |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2035 | LookupResult R = LookupParsedName(S, &SS, NamespcName, |
| 2036 | LookupNamespaceName, false); |
| 2037 | if (R.isAmbiguous()) { |
| 2038 | DiagnoseAmbiguousLookup(R, NamespcName, IdentLoc); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2039 | return DeclPtrTy(); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2040 | } |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 2041 | if (NamedDecl *NS = R) { |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 2042 | assert(isa<NamespaceDecl>(NS) && "expected namespace decl"); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2043 | // C++ [namespace.udir]p1: |
| 2044 | // A using-directive specifies that the names in the nominated |
| 2045 | // namespace can be used in the scope in which the |
| 2046 | // using-directive appears after the using-directive. During |
| 2047 | // unqualified name lookup (3.4.1), the names appear as if they |
| 2048 | // were declared in the nearest enclosing namespace which |
| 2049 | // contains both the using-directive and the nominated |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 2050 | // namespace. [Note: in this context, "contains" means "contains |
| 2051 | // directly or indirectly". ] |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2052 | |
| 2053 | // Find enclosing context containing both using-directive and |
| 2054 | // nominated namespace. |
| 2055 | DeclContext *CommonAncestor = cast<DeclContext>(NS); |
| 2056 | while (CommonAncestor && !CommonAncestor->Encloses(CurContext)) |
| 2057 | CommonAncestor = CommonAncestor->getParent(); |
| 2058 | |
Douglas Gregor | 8419fa3 | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 2059 | UDir = UsingDirectiveDecl::Create(Context, |
| 2060 | CurContext, UsingLoc, |
| 2061 | NamespcLoc, |
| 2062 | SS.getRange(), |
| 2063 | (NestedNameSpecifier *)SS.getScopeRep(), |
| 2064 | IdentLoc, |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2065 | cast<NamespaceDecl>(NS), |
| 2066 | CommonAncestor); |
| 2067 | PushUsingDirective(S, UDir); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 2068 | } else { |
Chris Lattner | ead013e | 2009-01-06 07:24:29 +0000 | [diff] [blame] | 2069 | Diag(IdentLoc, diag::err_expected_namespace_name) << SS.getRange(); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 2070 | } |
| 2071 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2072 | // FIXME: We ignore attributes for now. |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 2073 | delete AttrList; |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2074 | return DeclPtrTy::make(UDir); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2075 | } |
| 2076 | |
| 2077 | void Sema::PushUsingDirective(Scope *S, UsingDirectiveDecl *UDir) { |
| 2078 | // If scope has associated entity, then using directive is at namespace |
| 2079 | // or translation unit scope. We add UsingDirectiveDecls, into |
| 2080 | // it's lookup structure. |
| 2081 | if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity())) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2082 | Ctx->addDecl(UDir); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2083 | else |
| 2084 | // Otherwise it is block-sope. using-directives will affect lookup |
| 2085 | // only to the end of scope. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2086 | S->PushUsingDirective(DeclPtrTy::make(UDir)); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 2087 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2088 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2089 | |
| 2090 | Sema::DeclPtrTy Sema::ActOnUsingDeclaration(Scope *S, |
| 2091 | SourceLocation UsingLoc, |
| 2092 | const CXXScopeSpec &SS, |
| 2093 | SourceLocation IdentLoc, |
| 2094 | IdentifierInfo *TargetName, |
Anders Carlsson | 0c6139d | 2009-06-27 00:27:47 +0000 | [diff] [blame] | 2095 | OverloadedOperatorKind Op, |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2096 | AttributeList *AttrList, |
| 2097 | bool IsTypeName) { |
| 2098 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
Eli Friedman | 5d39dee | 2009-06-27 05:59:59 +0000 | [diff] [blame] | 2099 | assert((TargetName || Op) && "Invalid TargetName."); |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2100 | assert(IdentLoc.isValid() && "Invalid TargetName location."); |
| 2101 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
| 2102 | |
| 2103 | UsingDecl *UsingAlias = 0; |
| 2104 | |
Anders Carlsson | 0c6139d | 2009-06-27 00:27:47 +0000 | [diff] [blame] | 2105 | DeclarationName Name; |
| 2106 | if (TargetName) |
| 2107 | Name = TargetName; |
| 2108 | else |
| 2109 | Name = Context.DeclarationNames.getCXXOperatorName(Op); |
Eli Friedman | 2a16a13 | 2009-08-27 05:09:36 +0000 | [diff] [blame] | 2110 | |
| 2111 | // FIXME: Implement this properly! |
| 2112 | if (isUnknownSpecialization(SS)) { |
| 2113 | Diag(IdentLoc, diag::err_using_dependent_unsupported); |
| 2114 | delete AttrList; |
| 2115 | return DeclPtrTy::make((UsingDecl*)0); |
| 2116 | } |
| 2117 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2118 | // Lookup target name. |
Anders Carlsson | 0c6139d | 2009-06-27 00:27:47 +0000 | [diff] [blame] | 2119 | LookupResult R = LookupParsedName(S, &SS, Name, LookupOrdinaryName, false); |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2120 | |
| 2121 | if (NamedDecl *NS = R) { |
| 2122 | if (IsTypeName && !isa<TypeDecl>(NS)) { |
| 2123 | Diag(IdentLoc, diag::err_using_typename_non_type); |
| 2124 | } |
| 2125 | UsingAlias = UsingDecl::Create(Context, CurContext, IdentLoc, SS.getRange(), |
| 2126 | NS->getLocation(), UsingLoc, NS, |
| 2127 | static_cast<NestedNameSpecifier *>(SS.getScopeRep()), |
| 2128 | IsTypeName); |
| 2129 | PushOnScopeChains(UsingAlias, S); |
| 2130 | } else { |
| 2131 | Diag(IdentLoc, diag::err_using_requires_qualname) << SS.getRange(); |
| 2132 | } |
| 2133 | |
| 2134 | // FIXME: We ignore attributes for now. |
| 2135 | delete AttrList; |
| 2136 | return DeclPtrTy::make(UsingAlias); |
| 2137 | } |
| 2138 | |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 2139 | /// getNamespaceDecl - Returns the namespace a decl represents. If the decl |
| 2140 | /// is a namespace alias, returns the namespace it points to. |
| 2141 | static inline NamespaceDecl *getNamespaceDecl(NamedDecl *D) { |
| 2142 | if (NamespaceAliasDecl *AD = dyn_cast_or_null<NamespaceAliasDecl>(D)) |
| 2143 | return AD->getNamespace(); |
| 2144 | return dyn_cast_or_null<NamespaceDecl>(D); |
| 2145 | } |
| 2146 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2147 | Sema::DeclPtrTy Sema::ActOnNamespaceAliasDef(Scope *S, |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 2148 | SourceLocation NamespaceLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2149 | SourceLocation AliasLoc, |
| 2150 | IdentifierInfo *Alias, |
| 2151 | const CXXScopeSpec &SS, |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 2152 | SourceLocation IdentLoc, |
| 2153 | IdentifierInfo *Ident) { |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 2154 | |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 2155 | // Lookup the namespace name. |
| 2156 | LookupResult R = LookupParsedName(S, &SS, Ident, LookupNamespaceName, false); |
| 2157 | |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 2158 | // Check if we have a previous declaration with the same name. |
Anders Carlsson | dd729fc | 2009-03-28 23:49:35 +0000 | [diff] [blame] | 2159 | if (NamedDecl *PrevDecl = LookupName(S, Alias, LookupOrdinaryName, true)) { |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 2160 | if (NamespaceAliasDecl *AD = dyn_cast<NamespaceAliasDecl>(PrevDecl)) { |
| 2161 | // We already have an alias with the same name that points to the same |
| 2162 | // namespace, so don't create a new one. |
| 2163 | if (!R.isAmbiguous() && AD->getNamespace() == getNamespaceDecl(R)) |
| 2164 | return DeclPtrTy(); |
| 2165 | } |
| 2166 | |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 2167 | unsigned DiagID = isa<NamespaceDecl>(PrevDecl) ? diag::err_redefinition : |
| 2168 | diag::err_redefinition_different_kind; |
| 2169 | Diag(AliasLoc, DiagID) << Alias; |
| 2170 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2171 | return DeclPtrTy(); |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 2172 | } |
| 2173 | |
Anders Carlsson | 5721c68 | 2009-03-28 06:42:02 +0000 | [diff] [blame] | 2174 | if (R.isAmbiguous()) { |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 2175 | DiagnoseAmbiguousLookup(R, Ident, IdentLoc); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2176 | return DeclPtrTy(); |
Anders Carlsson | 5721c68 | 2009-03-28 06:42:02 +0000 | [diff] [blame] | 2177 | } |
| 2178 | |
| 2179 | if (!R) { |
| 2180 | Diag(NamespaceLoc, diag::err_expected_namespace_name) << SS.getRange(); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2181 | return DeclPtrTy(); |
Anders Carlsson | 5721c68 | 2009-03-28 06:42:02 +0000 | [diff] [blame] | 2182 | } |
| 2183 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2184 | NamespaceAliasDecl *AliasDecl = |
Douglas Gregor | 6c9c940 | 2009-05-30 06:48:27 +0000 | [diff] [blame] | 2185 | NamespaceAliasDecl::Create(Context, CurContext, NamespaceLoc, AliasLoc, |
| 2186 | Alias, SS.getRange(), |
| 2187 | (NestedNameSpecifier *)SS.getScopeRep(), |
Anders Carlsson | 68771c7 | 2009-03-28 22:58:02 +0000 | [diff] [blame] | 2188 | IdentLoc, R); |
| 2189 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2190 | CurContext->addDecl(AliasDecl); |
Anders Carlsson | 68771c7 | 2009-03-28 22:58:02 +0000 | [diff] [blame] | 2191 | return DeclPtrTy::make(AliasDecl); |
Anders Carlsson | dbb0094 | 2009-03-28 05:27:17 +0000 | [diff] [blame] | 2192 | } |
| 2193 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2194 | void Sema::DefineImplicitDefaultConstructor(SourceLocation CurrentLocation, |
| 2195 | CXXConstructorDecl *Constructor) { |
Fariborz Jahanian | 05a5c45 | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 2196 | assert((Constructor->isImplicit() && Constructor->isDefaultConstructor() && |
| 2197 | !Constructor->isUsed()) && |
| 2198 | "DefineImplicitDefaultConstructor - call it for implicit default ctor"); |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2199 | |
| 2200 | CXXRecordDecl *ClassDecl |
| 2201 | = cast<CXXRecordDecl>(Constructor->getDeclContext()); |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 2202 | assert(ClassDecl && "DefineImplicitDefaultConstructor - invalid constructor"); |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2203 | // Before the implicitly-declared default constructor for a class is |
| 2204 | // implicitly defined, all the implicitly-declared default constructors |
| 2205 | // for its base class and its non-static data members shall have been |
| 2206 | // implicitly defined. |
| 2207 | bool err = false; |
Fariborz Jahanian | 514b7b1 | 2009-06-30 16:36:53 +0000 | [diff] [blame] | 2208 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 2209 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2210 | CXXRecordDecl *BaseClassDecl |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2211 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2212 | if (!BaseClassDecl->hasTrivialConstructor()) { |
| 2213 | if (CXXConstructorDecl *BaseCtor = |
Fariborz Jahanian | 220a0f3 | 2009-06-23 23:42:10 +0000 | [diff] [blame] | 2214 | BaseClassDecl->getDefaultConstructor(Context)) |
| 2215 | MarkDeclarationReferenced(CurrentLocation, BaseCtor); |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2216 | else { |
| 2217 | Diag(CurrentLocation, diag::err_defining_default_ctor) |
Fariborz Jahanian | 3da83eb | 2009-06-20 20:23:38 +0000 | [diff] [blame] | 2218 | << Context.getTagDeclType(ClassDecl) << 1 |
| 2219 | << Context.getTagDeclType(BaseClassDecl); |
| 2220 | Diag(BaseClassDecl->getLocation(), diag::note_previous_class_decl) |
| 2221 | << Context.getTagDeclType(BaseClassDecl); |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2222 | err = true; |
| 2223 | } |
| 2224 | } |
| 2225 | } |
Fariborz Jahanian | 514b7b1 | 2009-06-30 16:36:53 +0000 | [diff] [blame] | 2226 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 2227 | E = ClassDecl->field_end(); Field != E; ++Field) { |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2228 | QualType FieldType = Context.getCanonicalType((*Field)->getType()); |
| 2229 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 2230 | FieldType = Array->getElementType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2231 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2232 | CXXRecordDecl *FieldClassDecl |
| 2233 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Duncan Sands | 6887e63 | 2009-06-25 09:03:06 +0000 | [diff] [blame] | 2234 | if (!FieldClassDecl->hasTrivialConstructor()) { |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2235 | if (CXXConstructorDecl *FieldCtor = |
Fariborz Jahanian | 220a0f3 | 2009-06-23 23:42:10 +0000 | [diff] [blame] | 2236 | FieldClassDecl->getDefaultConstructor(Context)) |
| 2237 | MarkDeclarationReferenced(CurrentLocation, FieldCtor); |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2238 | else { |
| 2239 | Diag(CurrentLocation, diag::err_defining_default_ctor) |
Fariborz Jahanian | 3da83eb | 2009-06-20 20:23:38 +0000 | [diff] [blame] | 2240 | << Context.getTagDeclType(ClassDecl) << 0 << |
| 2241 | Context.getTagDeclType(FieldClassDecl); |
| 2242 | Diag(FieldClassDecl->getLocation(), diag::note_previous_class_decl) |
| 2243 | << Context.getTagDeclType(FieldClassDecl); |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2244 | err = true; |
| 2245 | } |
| 2246 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2247 | } else if (FieldType->isReferenceType()) { |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2248 | Diag(CurrentLocation, diag::err_unintialized_member) |
Anders Carlsson | 5eda816 | 2009-07-09 17:37:12 +0000 | [diff] [blame] | 2249 | << Context.getTagDeclType(ClassDecl) << 0 << Field->getDeclName(); |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2250 | Diag((*Field)->getLocation(), diag::note_declared_at); |
| 2251 | err = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2252 | } else if (FieldType.isConstQualified()) { |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2253 | Diag(CurrentLocation, diag::err_unintialized_member) |
Anders Carlsson | 5eda816 | 2009-07-09 17:37:12 +0000 | [diff] [blame] | 2254 | << Context.getTagDeclType(ClassDecl) << 1 << Field->getDeclName(); |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2255 | Diag((*Field)->getLocation(), diag::note_declared_at); |
| 2256 | err = true; |
| 2257 | } |
| 2258 | } |
| 2259 | if (!err) |
Fariborz Jahanian | c6249b9 | 2009-06-26 16:08:57 +0000 | [diff] [blame] | 2260 | Constructor->setUsed(); |
| 2261 | else |
| 2262 | Constructor->setInvalidDecl(); |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2263 | } |
| 2264 | |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 2265 | void Sema::DefineImplicitDestructor(SourceLocation CurrentLocation, |
| 2266 | CXXDestructorDecl *Destructor) { |
| 2267 | assert((Destructor->isImplicit() && !Destructor->isUsed()) && |
| 2268 | "DefineImplicitDestructor - call it for implicit default dtor"); |
| 2269 | |
| 2270 | CXXRecordDecl *ClassDecl |
| 2271 | = cast<CXXRecordDecl>(Destructor->getDeclContext()); |
| 2272 | assert(ClassDecl && "DefineImplicitDestructor - invalid destructor"); |
| 2273 | // C++ [class.dtor] p5 |
| 2274 | // Before the implicitly-declared default destructor for a class is |
| 2275 | // implicitly defined, all the implicitly-declared default destructors |
| 2276 | // for its base class and its non-static data members shall have been |
| 2277 | // implicitly defined. |
Fariborz Jahanian | 514b7b1 | 2009-06-30 16:36:53 +0000 | [diff] [blame] | 2278 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 2279 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 2280 | CXXRecordDecl *BaseClassDecl |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2281 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 2282 | if (!BaseClassDecl->hasTrivialDestructor()) { |
| 2283 | if (CXXDestructorDecl *BaseDtor = |
| 2284 | const_cast<CXXDestructorDecl*>(BaseClassDecl->getDestructor(Context))) |
| 2285 | MarkDeclarationReferenced(CurrentLocation, BaseDtor); |
| 2286 | else |
| 2287 | assert(false && |
| 2288 | "DefineImplicitDestructor - missing dtor in a base class"); |
| 2289 | } |
| 2290 | } |
| 2291 | |
Fariborz Jahanian | 514b7b1 | 2009-06-30 16:36:53 +0000 | [diff] [blame] | 2292 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 2293 | E = ClassDecl->field_end(); Field != E; ++Field) { |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 2294 | QualType FieldType = Context.getCanonicalType((*Field)->getType()); |
| 2295 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 2296 | FieldType = Array->getElementType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2297 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 2298 | CXXRecordDecl *FieldClassDecl |
| 2299 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
| 2300 | if (!FieldClassDecl->hasTrivialDestructor()) { |
| 2301 | if (CXXDestructorDecl *FieldDtor = |
| 2302 | const_cast<CXXDestructorDecl*>( |
| 2303 | FieldClassDecl->getDestructor(Context))) |
| 2304 | MarkDeclarationReferenced(CurrentLocation, FieldDtor); |
| 2305 | else |
| 2306 | assert(false && |
| 2307 | "DefineImplicitDestructor - missing dtor in class of a data member"); |
| 2308 | } |
| 2309 | } |
| 2310 | } |
| 2311 | Destructor->setUsed(); |
| 2312 | } |
| 2313 | |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 2314 | void Sema::DefineImplicitOverloadedAssign(SourceLocation CurrentLocation, |
| 2315 | CXXMethodDecl *MethodDecl) { |
| 2316 | assert((MethodDecl->isImplicit() && MethodDecl->isOverloadedOperator() && |
| 2317 | MethodDecl->getOverloadedOperator() == OO_Equal && |
| 2318 | !MethodDecl->isUsed()) && |
| 2319 | "DefineImplicitOverloadedAssign - call it for implicit assignment op"); |
| 2320 | |
| 2321 | CXXRecordDecl *ClassDecl |
| 2322 | = cast<CXXRecordDecl>(MethodDecl->getDeclContext()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 2323 | |
Fariborz Jahanian | c6249b9 | 2009-06-26 16:08:57 +0000 | [diff] [blame] | 2324 | // C++[class.copy] p12 |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 2325 | // Before the implicitly-declared copy assignment operator for a class is |
| 2326 | // implicitly defined, all implicitly-declared copy assignment operators |
| 2327 | // for its direct base classes and its nonstatic data members shall have |
| 2328 | // been implicitly defined. |
| 2329 | bool err = false; |
Fariborz Jahanian | 514b7b1 | 2009-06-30 16:36:53 +0000 | [diff] [blame] | 2330 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 2331 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 2332 | CXXRecordDecl *BaseClassDecl |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2333 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 2334 | if (CXXMethodDecl *BaseAssignOpMethod = |
| 2335 | getAssignOperatorMethod(MethodDecl->getParamDecl(0), BaseClassDecl)) |
| 2336 | MarkDeclarationReferenced(CurrentLocation, BaseAssignOpMethod); |
| 2337 | } |
Fariborz Jahanian | 514b7b1 | 2009-06-30 16:36:53 +0000 | [diff] [blame] | 2338 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 2339 | E = ClassDecl->field_end(); Field != E; ++Field) { |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 2340 | QualType FieldType = Context.getCanonicalType((*Field)->getType()); |
| 2341 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 2342 | FieldType = Array->getElementType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2343 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 2344 | CXXRecordDecl *FieldClassDecl |
| 2345 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
| 2346 | if (CXXMethodDecl *FieldAssignOpMethod = |
| 2347 | getAssignOperatorMethod(MethodDecl->getParamDecl(0), FieldClassDecl)) |
| 2348 | MarkDeclarationReferenced(CurrentLocation, FieldAssignOpMethod); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2349 | } else if (FieldType->isReferenceType()) { |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 2350 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
Anders Carlsson | 5e09d4c | 2009-07-09 17:47:25 +0000 | [diff] [blame] | 2351 | << Context.getTagDeclType(ClassDecl) << 0 << Field->getDeclName(); |
| 2352 | Diag(Field->getLocation(), diag::note_declared_at); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 2353 | Diag(CurrentLocation, diag::note_first_required_here); |
| 2354 | err = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2355 | } else if (FieldType.isConstQualified()) { |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 2356 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
Anders Carlsson | 5e09d4c | 2009-07-09 17:47:25 +0000 | [diff] [blame] | 2357 | << Context.getTagDeclType(ClassDecl) << 1 << Field->getDeclName(); |
| 2358 | Diag(Field->getLocation(), diag::note_declared_at); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 2359 | Diag(CurrentLocation, diag::note_first_required_here); |
| 2360 | err = true; |
| 2361 | } |
| 2362 | } |
| 2363 | if (!err) |
| 2364 | MethodDecl->setUsed(); |
| 2365 | } |
| 2366 | |
| 2367 | CXXMethodDecl * |
| 2368 | Sema::getAssignOperatorMethod(ParmVarDecl *ParmDecl, |
| 2369 | CXXRecordDecl *ClassDecl) { |
| 2370 | QualType LHSType = Context.getTypeDeclType(ClassDecl); |
| 2371 | QualType RHSType(LHSType); |
| 2372 | // If class's assignment operator argument is const/volatile qualified, |
| 2373 | // look for operator = (const/volatile B&). Otherwise, look for |
| 2374 | // operator = (B&). |
| 2375 | if (ParmDecl->getType().isConstQualified()) |
| 2376 | RHSType.addConst(); |
| 2377 | if (ParmDecl->getType().isVolatileQualified()) |
| 2378 | RHSType.addVolatile(); |
| 2379 | ExprOwningPtr<Expr> LHS(this, new (Context) DeclRefExpr(ParmDecl, |
| 2380 | LHSType, |
| 2381 | SourceLocation())); |
| 2382 | ExprOwningPtr<Expr> RHS(this, new (Context) DeclRefExpr(ParmDecl, |
| 2383 | RHSType, |
| 2384 | SourceLocation())); |
| 2385 | Expr *Args[2] = { &*LHS, &*RHS }; |
| 2386 | OverloadCandidateSet CandidateSet; |
| 2387 | AddMemberOperatorCandidates(clang::OO_Equal, SourceLocation(), Args, 2, |
| 2388 | CandidateSet); |
| 2389 | OverloadCandidateSet::iterator Best; |
| 2390 | if (BestViableFunction(CandidateSet, |
| 2391 | ClassDecl->getLocation(), Best) == OR_Success) |
| 2392 | return cast<CXXMethodDecl>(Best->Function); |
| 2393 | assert(false && |
| 2394 | "getAssignOperatorMethod - copy assignment operator method not found"); |
| 2395 | return 0; |
| 2396 | } |
| 2397 | |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 2398 | void Sema::DefineImplicitCopyConstructor(SourceLocation CurrentLocation, |
| 2399 | CXXConstructorDecl *CopyConstructor, |
| 2400 | unsigned TypeQuals) { |
| 2401 | assert((CopyConstructor->isImplicit() && |
| 2402 | CopyConstructor->isCopyConstructor(Context, TypeQuals) && |
| 2403 | !CopyConstructor->isUsed()) && |
| 2404 | "DefineImplicitCopyConstructor - call it for implicit copy ctor"); |
| 2405 | |
| 2406 | CXXRecordDecl *ClassDecl |
| 2407 | = cast<CXXRecordDecl>(CopyConstructor->getDeclContext()); |
| 2408 | assert(ClassDecl && "DefineImplicitCopyConstructor - invalid constructor"); |
Fariborz Jahanian | 220a0f3 | 2009-06-23 23:42:10 +0000 | [diff] [blame] | 2409 | // C++ [class.copy] p209 |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 2410 | // Before the implicitly-declared copy constructor for a class is |
| 2411 | // implicitly defined, all the implicitly-declared copy constructors |
| 2412 | // for its base class and its non-static data members shall have been |
| 2413 | // implicitly defined. |
| 2414 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(); |
| 2415 | Base != ClassDecl->bases_end(); ++Base) { |
| 2416 | CXXRecordDecl *BaseClassDecl |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2417 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 2418 | if (CXXConstructorDecl *BaseCopyCtor = |
| 2419 | BaseClassDecl->getCopyConstructor(Context, TypeQuals)) |
Fariborz Jahanian | 220a0f3 | 2009-06-23 23:42:10 +0000 | [diff] [blame] | 2420 | MarkDeclarationReferenced(CurrentLocation, BaseCopyCtor); |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 2421 | } |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2422 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 2423 | FieldEnd = ClassDecl->field_end(); |
| 2424 | Field != FieldEnd; ++Field) { |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 2425 | QualType FieldType = Context.getCanonicalType((*Field)->getType()); |
| 2426 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 2427 | FieldType = Array->getElementType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2428 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 2429 | CXXRecordDecl *FieldClassDecl |
| 2430 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
| 2431 | if (CXXConstructorDecl *FieldCopyCtor = |
| 2432 | FieldClassDecl->getCopyConstructor(Context, TypeQuals)) |
Fariborz Jahanian | 220a0f3 | 2009-06-23 23:42:10 +0000 | [diff] [blame] | 2433 | MarkDeclarationReferenced(CurrentLocation, FieldCopyCtor); |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 2434 | } |
| 2435 | } |
| 2436 | CopyConstructor->setUsed(); |
| 2437 | } |
| 2438 | |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 2439 | Sema::OwningExprResult |
| 2440 | Sema::BuildCXXConstructExpr(QualType DeclInitType, |
| 2441 | CXXConstructorDecl *Constructor, |
| 2442 | Expr **Exprs, unsigned NumExprs) { |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 2443 | bool Elidable = false; |
| 2444 | |
| 2445 | // [class.copy]p15: |
| 2446 | // Whenever a temporary class object is copied using a copy constructor, and |
| 2447 | // this object and the copy have the same cv-unqualified type, an |
| 2448 | // implementation is permitted to treat the original and the copy as two |
| 2449 | // different ways of referring to the same object and not perform a copy at |
| 2450 | //all, even if the class copy constructor or destructor have side effects. |
| 2451 | |
| 2452 | // FIXME: Is this enough? |
| 2453 | if (Constructor->isCopyConstructor(Context) && NumExprs == 1) { |
| 2454 | Expr *E = Exprs[0]; |
| 2455 | while (CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E)) |
| 2456 | E = BE->getSubExpr(); |
| 2457 | |
| 2458 | if (isa<CallExpr>(E) || isa<CXXTemporaryObjectExpr>(E)) |
| 2459 | Elidable = true; |
| 2460 | } |
| 2461 | |
| 2462 | return BuildCXXConstructExpr(DeclInitType, Constructor, Elidable, |
| 2463 | Exprs, NumExprs); |
| 2464 | } |
| 2465 | |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 2466 | /// BuildCXXConstructExpr - Creates a complete call to a constructor, |
| 2467 | /// including handling of its default argument expressions. |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 2468 | Sema::OwningExprResult |
| 2469 | Sema::BuildCXXConstructExpr(QualType DeclInitType, |
| 2470 | CXXConstructorDecl *Constructor, |
| 2471 | bool Elidable, |
| 2472 | Expr **Exprs, |
| 2473 | unsigned NumExprs) { |
Anders Carlsson | 8644aec | 2009-08-25 13:07:08 +0000 | [diff] [blame] | 2474 | ExprOwningPtr<CXXConstructExpr> Temp(this, |
| 2475 | CXXConstructExpr::Create(Context, |
| 2476 | DeclInitType, |
| 2477 | Constructor, |
| 2478 | Elidable, |
| 2479 | Exprs, |
| 2480 | NumExprs)); |
Anders Carlsson | e7624a7 | 2009-08-27 05:08:22 +0000 | [diff] [blame] | 2481 | // Default arguments must be added to constructor call expression. |
Fariborz Jahanian | 2eeed7b | 2009-08-05 00:26:10 +0000 | [diff] [blame] | 2482 | FunctionDecl *FDecl = cast<FunctionDecl>(Constructor); |
| 2483 | unsigned NumArgsInProto = FDecl->param_size(); |
| 2484 | for (unsigned j = NumExprs; j != NumArgsInProto; j++) { |
Anders Carlsson | 8644aec | 2009-08-25 13:07:08 +0000 | [diff] [blame] | 2485 | ParmVarDecl *Param = FDecl->getParamDecl(j); |
| 2486 | |
| 2487 | OwningExprResult ArgExpr = |
| 2488 | BuildCXXDefaultArgExpr(/*FIXME:*/SourceLocation(), |
| 2489 | FDecl, Param); |
| 2490 | if (ArgExpr.isInvalid()) |
| 2491 | return ExprError(); |
| 2492 | |
| 2493 | Temp->setArg(j, ArgExpr.takeAs<Expr>()); |
Fariborz Jahanian | 2eeed7b | 2009-08-05 00:26:10 +0000 | [diff] [blame] | 2494 | } |
Anders Carlsson | 8644aec | 2009-08-25 13:07:08 +0000 | [diff] [blame] | 2495 | return move(Temp); |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 2496 | } |
| 2497 | |
Anders Carlsson | e7624a7 | 2009-08-27 05:08:22 +0000 | [diff] [blame] | 2498 | Sema::OwningExprResult |
| 2499 | Sema::BuildCXXTemporaryObjectExpr(CXXConstructorDecl *Constructor, |
| 2500 | QualType Ty, |
| 2501 | SourceLocation TyBeginLoc, |
| 2502 | MultiExprArg Args, |
| 2503 | SourceLocation RParenLoc) { |
| 2504 | CXXTemporaryObjectExpr *E |
| 2505 | = new (Context) CXXTemporaryObjectExpr(Context, Constructor, Ty, TyBeginLoc, |
| 2506 | (Expr **)Args.get(), |
| 2507 | Args.size(), RParenLoc); |
| 2508 | |
| 2509 | ExprOwningPtr<CXXTemporaryObjectExpr> Temp(this, E); |
| 2510 | |
| 2511 | // Default arguments must be added to constructor call expression. |
| 2512 | FunctionDecl *FDecl = cast<FunctionDecl>(Constructor); |
| 2513 | unsigned NumArgsInProto = FDecl->param_size(); |
| 2514 | for (unsigned j = Args.size(); j != NumArgsInProto; j++) { |
| 2515 | ParmVarDecl *Param = FDecl->getParamDecl(j); |
| 2516 | |
| 2517 | OwningExprResult ArgExpr = BuildCXXDefaultArgExpr(TyBeginLoc, FDecl, Param); |
| 2518 | if (ArgExpr.isInvalid()) |
| 2519 | return ExprError(); |
| 2520 | |
| 2521 | Temp->setArg(j, ArgExpr.takeAs<Expr>()); |
| 2522 | } |
| 2523 | |
| 2524 | Args.release(); |
| 2525 | return move(Temp); |
| 2526 | } |
| 2527 | |
| 2528 | |
Anders Carlsson | fe2de49 | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 2529 | bool Sema::InitializeVarWithConstructor(VarDecl *VD, |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 2530 | CXXConstructorDecl *Constructor, |
| 2531 | QualType DeclInitType, |
| 2532 | Expr **Exprs, unsigned NumExprs) { |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 2533 | OwningExprResult TempResult = BuildCXXConstructExpr(DeclInitType, Constructor, |
| 2534 | Exprs, NumExprs); |
Anders Carlsson | fe2de49 | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 2535 | if (TempResult.isInvalid()) |
| 2536 | return true; |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 2537 | |
| 2538 | Expr *Temp = TempResult.takeAs<Expr>(); |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 2539 | MarkDeclarationReferenced(VD->getLocation(), Constructor); |
Fariborz Jahanian | caa499b | 2009-08-05 18:17:32 +0000 | [diff] [blame] | 2540 | Temp = MaybeCreateCXXExprWithTemporaries(Temp, /*DestroyTemps=*/true); |
Douglas Gregor | 78d1583 | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 2541 | VD->setInit(Context, Temp); |
Anders Carlsson | fe2de49 | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 2542 | |
| 2543 | return false; |
Anders Carlsson | 930e8d0 | 2009-04-16 23:50:50 +0000 | [diff] [blame] | 2544 | } |
| 2545 | |
Fariborz Jahanian | a83f7ed | 2009-08-03 19:13:25 +0000 | [diff] [blame] | 2546 | void Sema::FinalizeVarWithDestructor(VarDecl *VD, QualType DeclInitType) |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 2547 | { |
| 2548 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>( |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2549 | DeclInitType->getAs<RecordType>()->getDecl()); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 2550 | if (!ClassDecl->hasTrivialDestructor()) |
| 2551 | if (CXXDestructorDecl *Destructor = |
| 2552 | const_cast<CXXDestructorDecl*>(ClassDecl->getDestructor(Context))) |
Fariborz Jahanian | a83f7ed | 2009-08-03 19:13:25 +0000 | [diff] [blame] | 2553 | MarkDeclarationReferenced(VD->getLocation(), Destructor); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 2554 | } |
| 2555 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2556 | /// AddCXXDirectInitializerToDecl - This action is called immediately after |
| 2557 | /// ActOnDeclarator, when a C++ direct initializer is present. |
| 2558 | /// e.g: "int x(1);" |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2559 | void Sema::AddCXXDirectInitializerToDecl(DeclPtrTy Dcl, |
| 2560 | SourceLocation LParenLoc, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 2561 | MultiExprArg Exprs, |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2562 | SourceLocation *CommaLocs, |
| 2563 | SourceLocation RParenLoc) { |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 2564 | unsigned NumExprs = Exprs.size(); |
| 2565 | assert(NumExprs != 0 && Exprs.get() && "missing expressions"); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2566 | Decl *RealDecl = Dcl.getAs<Decl>(); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2567 | |
| 2568 | // If there is no declaration, there was an error parsing it. Just ignore |
| 2569 | // the initializer. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2570 | if (RealDecl == 0) |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2571 | return; |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2572 | |
| 2573 | VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl); |
| 2574 | if (!VDecl) { |
| 2575 | Diag(RealDecl->getLocation(), diag::err_illegal_initializer); |
| 2576 | RealDecl->setInvalidDecl(); |
| 2577 | return; |
| 2578 | } |
| 2579 | |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 2580 | // We will represent direct-initialization similarly to copy-initialization: |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 2581 | // int x(1); -as-> int x = 1; |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2582 | // ClassType x(a,b,c); -as-> ClassType x = ClassType(a,b,c); |
| 2583 | // |
| 2584 | // Clients that want to distinguish between the two forms, can check for |
| 2585 | // direct initializer using VarDecl::hasCXXDirectInitializer(). |
| 2586 | // A major benefit is that clients that don't particularly care about which |
| 2587 | // exactly form was it (like the CodeGen) can handle both cases without |
| 2588 | // special case code. |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 2589 | |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 2590 | // If either the declaration has a dependent type or if any of the expressions |
| 2591 | // is type-dependent, we represent the initialization via a ParenListExpr for |
| 2592 | // later use during template instantiation. |
| 2593 | if (VDecl->getType()->isDependentType() || |
| 2594 | Expr::hasAnyTypeDependentArguments((Expr **)Exprs.get(), Exprs.size())) { |
| 2595 | // Let clients know that initialization was done with a direct initializer. |
| 2596 | VDecl->setCXXDirectInitializer(true); |
| 2597 | |
| 2598 | // Store the initialization expressions as a ParenListExpr. |
| 2599 | unsigned NumExprs = Exprs.size(); |
| 2600 | VDecl->setInit(Context, |
| 2601 | new (Context) ParenListExpr(Context, LParenLoc, |
| 2602 | (Expr **)Exprs.release(), |
| 2603 | NumExprs, RParenLoc)); |
| 2604 | return; |
| 2605 | } |
| 2606 | |
| 2607 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2608 | // C++ 8.5p11: |
| 2609 | // The form of initialization (using parentheses or '=') is generally |
| 2610 | // insignificant, but does matter when the entity being initialized has a |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 2611 | // class type. |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 2612 | QualType DeclInitType = VDecl->getType(); |
| 2613 | if (const ArrayType *Array = Context.getAsArrayType(DeclInitType)) |
| 2614 | DeclInitType = Array->getElementType(); |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 2615 | |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 2616 | // FIXME: This isn't the right place to complete the type. |
| 2617 | if (RequireCompleteType(VDecl->getLocation(), VDecl->getType(), |
| 2618 | diag::err_typecheck_decl_incomplete_type)) { |
| 2619 | VDecl->setInvalidDecl(); |
| 2620 | return; |
| 2621 | } |
| 2622 | |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 2623 | if (VDecl->getType()->isRecordType()) { |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 2624 | CXXConstructorDecl *Constructor |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 2625 | = PerformInitializationByConstructor(DeclInitType, |
| 2626 | (Expr **)Exprs.get(), NumExprs, |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 2627 | VDecl->getLocation(), |
| 2628 | SourceRange(VDecl->getLocation(), |
| 2629 | RParenLoc), |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2630 | VDecl->getDeclName(), |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 2631 | IK_Direct); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 2632 | if (!Constructor) |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 2633 | RealDecl->setInvalidDecl(); |
Anders Carlsson | ca29ad9 | 2009-04-15 21:48:18 +0000 | [diff] [blame] | 2634 | else { |
Anders Carlsson | ca29ad9 | 2009-04-15 21:48:18 +0000 | [diff] [blame] | 2635 | VDecl->setCXXDirectInitializer(true); |
Anders Carlsson | fe2de49 | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 2636 | if (InitializeVarWithConstructor(VDecl, Constructor, DeclInitType, |
| 2637 | (Expr**)Exprs.release(), NumExprs)) |
| 2638 | RealDecl->setInvalidDecl(); |
Fariborz Jahanian | a83f7ed | 2009-08-03 19:13:25 +0000 | [diff] [blame] | 2639 | FinalizeVarWithDestructor(VDecl, DeclInitType); |
Anders Carlsson | ca29ad9 | 2009-04-15 21:48:18 +0000 | [diff] [blame] | 2640 | } |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 2641 | return; |
| 2642 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2643 | |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 2644 | if (NumExprs > 1) { |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 2645 | Diag(CommaLocs[0], diag::err_builtin_direct_init_more_than_one_arg) |
| 2646 | << SourceRange(VDecl->getLocation(), RParenLoc); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2647 | RealDecl->setInvalidDecl(); |
| 2648 | return; |
| 2649 | } |
| 2650 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2651 | // Let clients know that initialization was done with a direct initializer. |
| 2652 | VDecl->setCXXDirectInitializer(true); |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 2653 | |
| 2654 | assert(NumExprs == 1 && "Expected 1 expression"); |
| 2655 | // Set the init expression, handles conversions. |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 2656 | AddInitializerToDecl(Dcl, ExprArg(*this, Exprs.release()[0]), |
| 2657 | /*DirectInit=*/true); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2658 | } |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2659 | |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 2660 | /// PerformInitializationByConstructor - Perform initialization by |
| 2661 | /// constructor (C++ [dcl.init]p14), which may occur as part of |
| 2662 | /// direct-initialization or copy-initialization. We are initializing |
| 2663 | /// an object of type @p ClassType with the given arguments @p |
| 2664 | /// Args. @p Loc is the location in the source code where the |
| 2665 | /// initializer occurs (e.g., a declaration, member initializer, |
| 2666 | /// functional cast, etc.) while @p Range covers the whole |
| 2667 | /// initialization. @p InitEntity is the entity being initialized, |
| 2668 | /// which may by the name of a declaration or a type. @p Kind is the |
| 2669 | /// kind of initialization we're performing, which affects whether |
| 2670 | /// explicit constructors will be considered. When successful, returns |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 2671 | /// the constructor that will be used to perform the initialization; |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 2672 | /// when the initialization fails, emits a diagnostic and returns |
| 2673 | /// null. |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 2674 | CXXConstructorDecl * |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 2675 | Sema::PerformInitializationByConstructor(QualType ClassType, |
| 2676 | Expr **Args, unsigned NumArgs, |
| 2677 | SourceLocation Loc, SourceRange Range, |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2678 | DeclarationName InitEntity, |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 2679 | InitializationKind Kind) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2680 | const RecordType *ClassRec = ClassType->getAs<RecordType>(); |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 2681 | assert(ClassRec && "Can only initialize a class type here"); |
| 2682 | |
| 2683 | // C++ [dcl.init]p14: |
| 2684 | // |
| 2685 | // If the initialization is direct-initialization, or if it is |
| 2686 | // copy-initialization where the cv-unqualified version of the |
| 2687 | // source type is the same class as, or a derived class of, the |
| 2688 | // class of the destination, constructors are considered. The |
| 2689 | // applicable constructors are enumerated (13.3.1.3), and the |
| 2690 | // best one is chosen through overload resolution (13.3). The |
| 2691 | // constructor so selected is called to initialize the object, |
| 2692 | // with the initializer expression(s) as its argument(s). If no |
| 2693 | // constructor applies, or the overload resolution is ambiguous, |
| 2694 | // the initialization is ill-formed. |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 2695 | const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(ClassRec->getDecl()); |
| 2696 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 2697 | |
| 2698 | // Add constructors to the overload set. |
Douglas Gregor | 9e7d9de | 2008-12-15 21:24:18 +0000 | [diff] [blame] | 2699 | DeclarationName ConstructorName |
| 2700 | = Context.DeclarationNames.getCXXConstructorName( |
| 2701 | Context.getCanonicalType(ClassType.getUnqualifiedType())); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 2702 | DeclContext::lookup_const_iterator Con, ConEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2703 | for (llvm::tie(Con, ConEnd) = ClassDecl->lookup(ConstructorName); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 2704 | Con != ConEnd; ++Con) { |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2705 | // Find the constructor (which may be a template). |
| 2706 | CXXConstructorDecl *Constructor = 0; |
| 2707 | FunctionTemplateDecl *ConstructorTmpl= dyn_cast<FunctionTemplateDecl>(*Con); |
| 2708 | if (ConstructorTmpl) |
| 2709 | Constructor |
| 2710 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 2711 | else |
| 2712 | Constructor = cast<CXXConstructorDecl>(*Con); |
| 2713 | |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 2714 | if ((Kind == IK_Direct) || |
| 2715 | (Kind == IK_Copy && Constructor->isConvertingConstructor()) || |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2716 | (Kind == IK_Default && Constructor->isDefaultConstructor())) { |
| 2717 | if (ConstructorTmpl) |
| 2718 | AddTemplateOverloadCandidate(ConstructorTmpl, false, 0, 0, |
| 2719 | Args, NumArgs, CandidateSet); |
| 2720 | else |
| 2721 | AddOverloadCandidate(Constructor, Args, NumArgs, CandidateSet); |
| 2722 | } |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 2723 | } |
| 2724 | |
Douglas Gregor | 9e7d9de | 2008-12-15 21:24:18 +0000 | [diff] [blame] | 2725 | // FIXME: When we decide not to synthesize the implicitly-declared |
| 2726 | // constructors, we'll need to make them appear here. |
| 2727 | |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 2728 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 2729 | switch (BestViableFunction(CandidateSet, Loc, Best)) { |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 2730 | case OR_Success: |
| 2731 | // We found a constructor. Return it. |
| 2732 | return cast<CXXConstructorDecl>(Best->Function); |
| 2733 | |
| 2734 | case OR_No_Viable_Function: |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 2735 | if (InitEntity) |
| 2736 | Diag(Loc, diag::err_ovl_no_viable_function_in_init) |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 2737 | << InitEntity << Range; |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 2738 | else |
| 2739 | Diag(Loc, diag::err_ovl_no_viable_function_in_init) |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 2740 | << ClassType << Range; |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 2741 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 2742 | return 0; |
| 2743 | |
| 2744 | case OR_Ambiguous: |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 2745 | if (InitEntity) |
| 2746 | Diag(Loc, diag::err_ovl_ambiguous_init) << InitEntity << Range; |
| 2747 | else |
| 2748 | Diag(Loc, diag::err_ovl_ambiguous_init) << ClassType << Range; |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 2749 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 2750 | return 0; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2751 | |
| 2752 | case OR_Deleted: |
| 2753 | if (InitEntity) |
| 2754 | Diag(Loc, diag::err_ovl_deleted_init) |
| 2755 | << Best->Function->isDeleted() |
| 2756 | << InitEntity << Range; |
| 2757 | else |
| 2758 | Diag(Loc, diag::err_ovl_deleted_init) |
| 2759 | << Best->Function->isDeleted() |
| 2760 | << InitEntity << Range; |
| 2761 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 2762 | return 0; |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 2763 | } |
| 2764 | |
| 2765 | return 0; |
| 2766 | } |
| 2767 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2768 | /// CompareReferenceRelationship - Compare the two types T1 and T2 to |
| 2769 | /// determine whether they are reference-related, |
| 2770 | /// reference-compatible, reference-compatible with added |
| 2771 | /// qualification, or incompatible, for use in C++ initialization by |
| 2772 | /// reference (C++ [dcl.ref.init]p4). Neither type can be a reference |
| 2773 | /// type, and the first type (T1) is the pointee type of the reference |
| 2774 | /// type being initialized. |
| 2775 | Sema::ReferenceCompareResult |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 2776 | Sema::CompareReferenceRelationship(QualType T1, QualType T2, |
| 2777 | bool& DerivedToBase) { |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2778 | assert(!T1->isReferenceType() && |
| 2779 | "T1 must be the pointee type of the reference type"); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2780 | assert(!T2->isReferenceType() && "T2 cannot be a reference type"); |
| 2781 | |
| 2782 | T1 = Context.getCanonicalType(T1); |
| 2783 | T2 = Context.getCanonicalType(T2); |
| 2784 | QualType UnqualT1 = T1.getUnqualifiedType(); |
| 2785 | QualType UnqualT2 = T2.getUnqualifiedType(); |
| 2786 | |
| 2787 | // C++ [dcl.init.ref]p4: |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 2788 | // Given types "cv1 T1" and "cv2 T2," "cv1 T1" is |
| 2789 | // reference-related to "cv2 T2" if T1 is the same type as T2, or |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2790 | // T1 is a base class of T2. |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 2791 | if (UnqualT1 == UnqualT2) |
| 2792 | DerivedToBase = false; |
| 2793 | else if (IsDerivedFrom(UnqualT2, UnqualT1)) |
| 2794 | DerivedToBase = true; |
| 2795 | else |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2796 | return Ref_Incompatible; |
| 2797 | |
| 2798 | // At this point, we know that T1 and T2 are reference-related (at |
| 2799 | // least). |
| 2800 | |
| 2801 | // C++ [dcl.init.ref]p4: |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 2802 | // "cv1 T1" is reference-compatible with "cv2 T2" if T1 is |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2803 | // reference-related to T2 and cv1 is the same cv-qualification |
| 2804 | // as, or greater cv-qualification than, cv2. For purposes of |
| 2805 | // overload resolution, cases for which cv1 is greater |
| 2806 | // cv-qualification than cv2 are identified as |
| 2807 | // reference-compatible with added qualification (see 13.3.3.2). |
| 2808 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 2809 | return Ref_Compatible; |
| 2810 | else if (T1.isMoreQualifiedThan(T2)) |
| 2811 | return Ref_Compatible_With_Added_Qualification; |
| 2812 | else |
| 2813 | return Ref_Related; |
| 2814 | } |
| 2815 | |
| 2816 | /// CheckReferenceInit - Check the initialization of a reference |
| 2817 | /// variable with the given initializer (C++ [dcl.init.ref]). Init is |
| 2818 | /// the initializer (either a simple initializer or an initializer |
Douglas Gregor | 3205a78 | 2008-10-29 23:31:03 +0000 | [diff] [blame] | 2819 | /// list), and DeclType is the type of the declaration. When ICS is |
| 2820 | /// non-null, this routine will compute the implicit conversion |
| 2821 | /// sequence according to C++ [over.ics.ref] and will not produce any |
| 2822 | /// diagnostics; when ICS is null, it will emit diagnostics when any |
| 2823 | /// errors are found. Either way, a return value of true indicates |
| 2824 | /// that there was a failure, a return value of false indicates that |
| 2825 | /// the reference initialization succeeded. |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2826 | /// |
| 2827 | /// When @p SuppressUserConversions, user-defined conversions are |
| 2828 | /// suppressed. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2829 | /// When @p AllowExplicit, we also permit explicit user-defined |
| 2830 | /// conversion functions. |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2831 | /// When @p ForceRValue, we unconditionally treat the initializer as an rvalue. |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 2832 | bool |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2833 | Sema::CheckReferenceInit(Expr *&Init, QualType DeclType, |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2834 | ImplicitConversionSequence *ICS, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2835 | bool SuppressUserConversions, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2836 | bool AllowExplicit, bool ForceRValue) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2837 | assert(DeclType->isReferenceType() && "Reference init needs a reference"); |
| 2838 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2839 | QualType T1 = DeclType->getAs<ReferenceType>()->getPointeeType(); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2840 | QualType T2 = Init->getType(); |
| 2841 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 2842 | // If the initializer is the address of an overloaded function, try |
| 2843 | // to resolve the overloaded function. If all goes well, T2 is the |
| 2844 | // type of the resulting function. |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2845 | if (Context.getCanonicalType(T2) == Context.OverloadTy) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 2846 | FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Init, DeclType, |
| 2847 | ICS != 0); |
| 2848 | if (Fn) { |
| 2849 | // Since we're performing this reference-initialization for |
| 2850 | // real, update the initializer with the resulting function. |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2851 | if (!ICS) { |
| 2852 | if (DiagnoseUseOfDecl(Fn, Init->getSourceRange().getBegin())) |
| 2853 | return true; |
| 2854 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 2855 | FixOverloadedFunctionReference(Init, Fn); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2856 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 2857 | |
| 2858 | T2 = Fn->getType(); |
| 2859 | } |
| 2860 | } |
| 2861 | |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 2862 | // Compute some basic properties of the types and the initializer. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2863 | bool isRValRef = DeclType->isRValueReferenceType(); |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 2864 | bool DerivedToBase = false; |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2865 | Expr::isLvalueResult InitLvalue = ForceRValue ? Expr::LV_InvalidExpression : |
| 2866 | Init->isLvalue(Context); |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 2867 | ReferenceCompareResult RefRelationship |
| 2868 | = CompareReferenceRelationship(T1, T2, DerivedToBase); |
| 2869 | |
| 2870 | // Most paths end in a failed conversion. |
| 2871 | if (ICS) |
| 2872 | ICS->ConversionKind = ImplicitConversionSequence::BadConversion; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2873 | |
| 2874 | // C++ [dcl.init.ref]p5: |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 2875 | // A reference to type "cv1 T1" is initialized by an expression |
| 2876 | // of type "cv2 T2" as follows: |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2877 | |
| 2878 | // -- If the initializer expression |
| 2879 | |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 2880 | // Rvalue references cannot bind to lvalues (N2812). |
| 2881 | // There is absolutely no situation where they can. In particular, note that |
| 2882 | // this is ill-formed, even if B has a user-defined conversion to A&&: |
| 2883 | // B b; |
| 2884 | // A&& r = b; |
| 2885 | if (isRValRef && InitLvalue == Expr::LV_Valid) { |
| 2886 | if (!ICS) |
| 2887 | Diag(Init->getSourceRange().getBegin(), diag::err_lvalue_to_rvalue_ref) |
| 2888 | << Init->getSourceRange(); |
| 2889 | return true; |
| 2890 | } |
| 2891 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2892 | bool BindsDirectly = false; |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 2893 | // -- is an lvalue (but is not a bit-field), and "cv1 T1" is |
| 2894 | // reference-compatible with "cv2 T2," or |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 2895 | // |
| 2896 | // Note that the bit-field check is skipped if we are just computing |
| 2897 | // the implicit conversion sequence (C++ [over.best.ics]p2). |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2898 | if (InitLvalue == Expr::LV_Valid && (ICS || !Init->getBitField()) && |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 2899 | RefRelationship >= Ref_Compatible_With_Added_Qualification) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2900 | BindsDirectly = true; |
| 2901 | |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 2902 | if (ICS) { |
| 2903 | // C++ [over.ics.ref]p1: |
| 2904 | // When a parameter of reference type binds directly (8.5.3) |
| 2905 | // to an argument expression, the implicit conversion sequence |
| 2906 | // is the identity conversion, unless the argument expression |
| 2907 | // has a type that is a derived class of the parameter type, |
| 2908 | // in which case the implicit conversion sequence is a |
| 2909 | // derived-to-base Conversion (13.3.3.1). |
| 2910 | ICS->ConversionKind = ImplicitConversionSequence::StandardConversion; |
| 2911 | ICS->Standard.First = ICK_Identity; |
| 2912 | ICS->Standard.Second = DerivedToBase? ICK_Derived_To_Base : ICK_Identity; |
| 2913 | ICS->Standard.Third = ICK_Identity; |
| 2914 | ICS->Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 2915 | ICS->Standard.ToTypePtr = T1.getAsOpaquePtr(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2916 | ICS->Standard.ReferenceBinding = true; |
| 2917 | ICS->Standard.DirectBinding = true; |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 2918 | ICS->Standard.RRefBinding = false; |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 2919 | ICS->Standard.CopyConstructor = 0; |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 2920 | |
| 2921 | // Nothing more to do: the inaccessibility/ambiguity check for |
| 2922 | // derived-to-base conversions is suppressed when we're |
| 2923 | // computing the implicit conversion sequence (C++ |
| 2924 | // [over.best.ics]p2). |
| 2925 | return false; |
| 2926 | } else { |
| 2927 | // Perform the conversion. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2928 | // FIXME: Binding to a subobject of the lvalue is going to require more |
| 2929 | // AST annotation than this. |
Anders Carlsson | 3503d04 | 2009-07-31 01:23:52 +0000 | [diff] [blame] | 2930 | ImpCastExprToType(Init, T1, CastExpr::CK_Unknown, /*isLvalue=*/true); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2931 | } |
| 2932 | } |
| 2933 | |
| 2934 | // -- has a class type (i.e., T2 is a class type) and can be |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 2935 | // implicitly converted to an lvalue of type "cv3 T3," |
| 2936 | // where "cv1 T1" is reference-compatible with "cv3 T3" |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2937 | // 92) (this conversion is selected by enumerating the |
| 2938 | // applicable conversion functions (13.3.1.6) and choosing |
| 2939 | // the best one through overload resolution (13.3)), |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 2940 | if (!isRValRef && !SuppressUserConversions && T2->isRecordType() && |
| 2941 | !RequireCompleteType(SourceLocation(), T2, 0)) { |
Douglas Gregor | cb9b977 | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 2942 | // FIXME: Look for conversions in base classes! |
| 2943 | CXXRecordDecl *T2RecordDecl |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2944 | = dyn_cast<CXXRecordDecl>(T2->getAs<RecordType>()->getDecl()); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2945 | |
Douglas Gregor | cb9b977 | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 2946 | OverloadCandidateSet CandidateSet; |
| 2947 | OverloadedFunctionDecl *Conversions |
| 2948 | = T2RecordDecl->getConversionFunctions(); |
| 2949 | for (OverloadedFunctionDecl::function_iterator Func |
| 2950 | = Conversions->function_begin(); |
| 2951 | Func != Conversions->function_end(); ++Func) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2952 | FunctionTemplateDecl *ConvTemplate |
| 2953 | = dyn_cast<FunctionTemplateDecl>(*Func); |
| 2954 | CXXConversionDecl *Conv; |
| 2955 | if (ConvTemplate) |
| 2956 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 2957 | else |
| 2958 | Conv = cast<CXXConversionDecl>(*Func); |
Sebastian Redl | dfe292d | 2009-03-22 21:28:55 +0000 | [diff] [blame] | 2959 | |
Douglas Gregor | cb9b977 | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 2960 | // If the conversion function doesn't return a reference type, |
| 2961 | // it can't be considered for this conversion. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2962 | if (Conv->getConversionType()->isLValueReferenceType() && |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2963 | (AllowExplicit || !Conv->isExplicit())) { |
| 2964 | if (ConvTemplate) |
| 2965 | AddTemplateConversionCandidate(ConvTemplate, Init, DeclType, |
| 2966 | CandidateSet); |
| 2967 | else |
| 2968 | AddConversionCandidate(Conv, Init, DeclType, CandidateSet); |
| 2969 | } |
Douglas Gregor | cb9b977 | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 2970 | } |
| 2971 | |
| 2972 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 2973 | switch (BestViableFunction(CandidateSet, Init->getLocStart(), Best)) { |
Douglas Gregor | cb9b977 | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 2974 | case OR_Success: |
| 2975 | // This is a direct binding. |
| 2976 | BindsDirectly = true; |
| 2977 | |
| 2978 | if (ICS) { |
| 2979 | // C++ [over.ics.ref]p1: |
| 2980 | // |
| 2981 | // [...] If the parameter binds directly to the result of |
| 2982 | // applying a conversion function to the argument |
| 2983 | // expression, the implicit conversion sequence is a |
| 2984 | // user-defined conversion sequence (13.3.3.1.2), with the |
| 2985 | // second standard conversion sequence either an identity |
| 2986 | // conversion or, if the conversion function returns an |
| 2987 | // entity of a type that is a derived class of the parameter |
| 2988 | // type, a derived-to-base Conversion. |
| 2989 | ICS->ConversionKind = ImplicitConversionSequence::UserDefinedConversion; |
| 2990 | ICS->UserDefined.Before = Best->Conversions[0].Standard; |
| 2991 | ICS->UserDefined.After = Best->FinalConversion; |
| 2992 | ICS->UserDefined.ConversionFunction = Best->Function; |
| 2993 | assert(ICS->UserDefined.After.ReferenceBinding && |
| 2994 | ICS->UserDefined.After.DirectBinding && |
| 2995 | "Expected a direct reference binding!"); |
| 2996 | return false; |
| 2997 | } else { |
| 2998 | // Perform the conversion. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2999 | // FIXME: Binding to a subobject of the lvalue is going to require more |
| 3000 | // AST annotation than this. |
Anders Carlsson | 3503d04 | 2009-07-31 01:23:52 +0000 | [diff] [blame] | 3001 | ImpCastExprToType(Init, T1, CastExpr::CK_Unknown, /*isLvalue=*/true); |
Douglas Gregor | cb9b977 | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 3002 | } |
| 3003 | break; |
| 3004 | |
| 3005 | case OR_Ambiguous: |
| 3006 | assert(false && "Ambiguous reference binding conversions not implemented."); |
| 3007 | return true; |
| 3008 | |
| 3009 | case OR_No_Viable_Function: |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3010 | case OR_Deleted: |
| 3011 | // There was no suitable conversion, or we found a deleted |
| 3012 | // conversion; continue with other checks. |
Douglas Gregor | cb9b977 | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 3013 | break; |
| 3014 | } |
| 3015 | } |
| 3016 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3017 | if (BindsDirectly) { |
| 3018 | // C++ [dcl.init.ref]p4: |
| 3019 | // [...] In all cases where the reference-related or |
| 3020 | // reference-compatible relationship of two types is used to |
| 3021 | // establish the validity of a reference binding, and T1 is a |
| 3022 | // base class of T2, a program that necessitates such a binding |
| 3023 | // is ill-formed if T1 is an inaccessible (clause 11) or |
| 3024 | // ambiguous (10.2) base class of T2. |
| 3025 | // |
| 3026 | // Note that we only check this condition when we're allowed to |
| 3027 | // complain about errors, because we should not be checking for |
| 3028 | // ambiguity (or inaccessibility) unless the reference binding |
| 3029 | // actually happens. |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3030 | if (DerivedToBase) |
| 3031 | return CheckDerivedToBaseConversion(T2, T1, |
| 3032 | Init->getSourceRange().getBegin(), |
| 3033 | Init->getSourceRange()); |
| 3034 | else |
| 3035 | return false; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3036 | } |
| 3037 | |
| 3038 | // -- Otherwise, the reference shall be to a non-volatile const |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 3039 | // type (i.e., cv1 shall be const), or the reference shall be an |
| 3040 | // rvalue reference and the initializer expression shall be an rvalue. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3041 | if (!isRValRef && T1.getCVRQualifiers() != QualType::Const) { |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3042 | if (!ICS) |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3043 | Diag(Init->getSourceRange().getBegin(), |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3044 | diag::err_not_reference_to_const_init) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3045 | << T1 << (InitLvalue != Expr::LV_Valid? "temporary" : "value") |
| 3046 | << T2 << Init->getSourceRange(); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3047 | return true; |
| 3048 | } |
| 3049 | |
| 3050 | // -- If the initializer expression is an rvalue, with T2 a |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 3051 | // class type, and "cv1 T1" is reference-compatible with |
| 3052 | // "cv2 T2," the reference is bound in one of the |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3053 | // following ways (the choice is implementation-defined): |
| 3054 | // |
| 3055 | // -- The reference is bound to the object represented by |
| 3056 | // the rvalue (see 3.10) or to a sub-object within that |
| 3057 | // object. |
| 3058 | // |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 3059 | // -- A temporary of type "cv1 T2" [sic] is created, and |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3060 | // a constructor is called to copy the entire rvalue |
| 3061 | // object into the temporary. The reference is bound to |
| 3062 | // the temporary or to a sub-object within the |
| 3063 | // temporary. |
| 3064 | // |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3065 | // The constructor that would be used to make the copy |
| 3066 | // shall be callable whether or not the copy is actually |
| 3067 | // done. |
| 3068 | // |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 3069 | // Note that C++0x [dcl.init.ref]p5 takes away this implementation |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3070 | // freedom, so we will always take the first option and never build |
| 3071 | // a temporary in this case. FIXME: We will, however, have to check |
| 3072 | // for the presence of a copy constructor in C++98/03 mode. |
| 3073 | if (InitLvalue != Expr::LV_Valid && T2->isRecordType() && |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3074 | RefRelationship >= Ref_Compatible_With_Added_Qualification) { |
| 3075 | if (ICS) { |
| 3076 | ICS->ConversionKind = ImplicitConversionSequence::StandardConversion; |
| 3077 | ICS->Standard.First = ICK_Identity; |
| 3078 | ICS->Standard.Second = DerivedToBase? ICK_Derived_To_Base : ICK_Identity; |
| 3079 | ICS->Standard.Third = ICK_Identity; |
| 3080 | ICS->Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 3081 | ICS->Standard.ToTypePtr = T1.getAsOpaquePtr(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3082 | ICS->Standard.ReferenceBinding = true; |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 3083 | ICS->Standard.DirectBinding = false; |
| 3084 | ICS->Standard.RRefBinding = isRValRef; |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 3085 | ICS->Standard.CopyConstructor = 0; |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3086 | } else { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3087 | // FIXME: Binding to a subobject of the rvalue is going to require more |
| 3088 | // AST annotation than this. |
Anders Carlsson | 3503d04 | 2009-07-31 01:23:52 +0000 | [diff] [blame] | 3089 | ImpCastExprToType(Init, T1, CastExpr::CK_Unknown, /*isLvalue=*/false); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3090 | } |
| 3091 | return false; |
| 3092 | } |
| 3093 | |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 3094 | // -- Otherwise, a temporary of type "cv1 T1" is created and |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3095 | // initialized from the initializer expression using the |
| 3096 | // rules for a non-reference copy initialization (8.5). The |
| 3097 | // reference is then bound to the temporary. If T1 is |
| 3098 | // reference-related to T2, cv1 must be the same |
| 3099 | // cv-qualification as, or greater cv-qualification than, |
| 3100 | // cv2; otherwise, the program is ill-formed. |
| 3101 | if (RefRelationship == Ref_Related) { |
| 3102 | // If cv1 == cv2 or cv1 is a greater cv-qualified than cv2, then |
| 3103 | // we would be reference-compatible or reference-compatible with |
| 3104 | // added qualification. But that wasn't the case, so the reference |
| 3105 | // initialization fails. |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3106 | if (!ICS) |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3107 | Diag(Init->getSourceRange().getBegin(), |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3108 | diag::err_reference_init_drops_quals) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3109 | << T1 << (InitLvalue != Expr::LV_Valid? "temporary" : "value") |
| 3110 | << T2 << Init->getSourceRange(); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3111 | return true; |
| 3112 | } |
| 3113 | |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 3114 | // If at least one of the types is a class type, the types are not |
| 3115 | // related, and we aren't allowed any user conversions, the |
| 3116 | // reference binding fails. This case is important for breaking |
| 3117 | // recursion, since TryImplicitConversion below will attempt to |
| 3118 | // create a temporary through the use of a copy constructor. |
| 3119 | if (SuppressUserConversions && RefRelationship == Ref_Incompatible && |
| 3120 | (T1->isRecordType() || T2->isRecordType())) { |
| 3121 | if (!ICS) |
| 3122 | Diag(Init->getSourceRange().getBegin(), |
| 3123 | diag::err_typecheck_convert_incompatible) |
| 3124 | << DeclType << Init->getType() << "initializing" << Init->getSourceRange(); |
| 3125 | return true; |
| 3126 | } |
| 3127 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3128 | // Actually try to convert the initializer to T1. |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3129 | if (ICS) { |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 3130 | // C++ [over.ics.ref]p2: |
| 3131 | // |
| 3132 | // When a parameter of reference type is not bound directly to |
| 3133 | // an argument expression, the conversion sequence is the one |
| 3134 | // required to convert the argument expression to the |
| 3135 | // underlying type of the reference according to |
| 3136 | // 13.3.3.1. Conceptually, this conversion sequence corresponds |
| 3137 | // to copy-initializing a temporary of the underlying type with |
| 3138 | // the argument expression. Any difference in top-level |
| 3139 | // cv-qualification is subsumed by the initialization itself |
| 3140 | // and does not constitute a conversion. |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3141 | *ICS = TryImplicitConversion(Init, T1, SuppressUserConversions); |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 3142 | // Of course, that's still a reference binding. |
| 3143 | if (ICS->ConversionKind == ImplicitConversionSequence::StandardConversion) { |
| 3144 | ICS->Standard.ReferenceBinding = true; |
| 3145 | ICS->Standard.RRefBinding = isRValRef; |
| 3146 | } else if(ICS->ConversionKind == |
| 3147 | ImplicitConversionSequence::UserDefinedConversion) { |
| 3148 | ICS->UserDefined.After.ReferenceBinding = true; |
| 3149 | ICS->UserDefined.After.RRefBinding = isRValRef; |
| 3150 | } |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3151 | return ICS->ConversionKind == ImplicitConversionSequence::BadConversion; |
| 3152 | } else { |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 3153 | return PerformImplicitConversion(Init, T1, "initializing"); |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3154 | } |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3155 | } |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3156 | |
| 3157 | /// CheckOverloadedOperatorDeclaration - Check whether the declaration |
| 3158 | /// of this overloaded operator is well-formed. If so, returns false; |
| 3159 | /// otherwise, emits appropriate diagnostics and returns true. |
| 3160 | bool Sema::CheckOverloadedOperatorDeclaration(FunctionDecl *FnDecl) { |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 3161 | assert(FnDecl && FnDecl->isOverloadedOperator() && |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3162 | "Expected an overloaded operator declaration"); |
| 3163 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3164 | OverloadedOperatorKind Op = FnDecl->getOverloadedOperator(); |
| 3165 | |
| 3166 | // C++ [over.oper]p5: |
| 3167 | // The allocation and deallocation functions, operator new, |
| 3168 | // operator new[], operator delete and operator delete[], are |
| 3169 | // described completely in 3.7.3. The attributes and restrictions |
| 3170 | // found in the rest of this subclause do not apply to them unless |
| 3171 | // explicitly stated in 3.7.3. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3172 | // FIXME: Write a separate routine for checking this. For now, just allow it. |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3173 | if (Op == OO_New || Op == OO_Array_New || |
| 3174 | Op == OO_Delete || Op == OO_Array_Delete) |
| 3175 | return false; |
| 3176 | |
| 3177 | // C++ [over.oper]p6: |
| 3178 | // An operator function shall either be a non-static member |
| 3179 | // function or be a non-member function and have at least one |
| 3180 | // parameter whose type is a class, a reference to a class, an |
| 3181 | // enumeration, or a reference to an enumeration. |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 3182 | if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 3183 | if (MethodDecl->isStatic()) |
| 3184 | return Diag(FnDecl->getLocation(), |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3185 | diag::err_operator_overload_static) << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3186 | } else { |
| 3187 | bool ClassOrEnumParam = false; |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 3188 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(), |
| 3189 | ParamEnd = FnDecl->param_end(); |
| 3190 | Param != ParamEnd; ++Param) { |
| 3191 | QualType ParamType = (*Param)->getType().getNonReferenceType(); |
Eli Friedman | 5d39dee | 2009-06-27 05:59:59 +0000 | [diff] [blame] | 3192 | if (ParamType->isDependentType() || ParamType->isRecordType() || |
| 3193 | ParamType->isEnumeralType()) { |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3194 | ClassOrEnumParam = true; |
| 3195 | break; |
| 3196 | } |
| 3197 | } |
| 3198 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 3199 | if (!ClassOrEnumParam) |
| 3200 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 3201 | diag::err_operator_overload_needs_class_or_enum) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3202 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3203 | } |
| 3204 | |
| 3205 | // C++ [over.oper]p8: |
| 3206 | // An operator function cannot have default arguments (8.3.6), |
| 3207 | // except where explicitly stated below. |
| 3208 | // |
| 3209 | // Only the function-call operator allows default arguments |
| 3210 | // (C++ [over.call]p1). |
| 3211 | if (Op != OO_Call) { |
| 3212 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 3213 | Param != FnDecl->param_end(); ++Param) { |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 3214 | if ((*Param)->hasUnparsedDefaultArg()) |
| 3215 | return Diag((*Param)->getLocation(), |
| 3216 | diag::err_operator_overload_default_arg) |
| 3217 | << FnDecl->getDeclName(); |
| 3218 | else if (Expr *DefArg = (*Param)->getDefaultArg()) |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 3219 | return Diag((*Param)->getLocation(), |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 3220 | diag::err_operator_overload_default_arg) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3221 | << FnDecl->getDeclName() << DefArg->getSourceRange(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3222 | } |
| 3223 | } |
| 3224 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 3225 | static const bool OperatorUses[NUM_OVERLOADED_OPERATORS][3] = { |
| 3226 | { false, false, false } |
| 3227 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 3228 | , { Unary, Binary, MemberOnly } |
| 3229 | #include "clang/Basic/OperatorKinds.def" |
| 3230 | }; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3231 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 3232 | bool CanBeUnaryOperator = OperatorUses[Op][0]; |
| 3233 | bool CanBeBinaryOperator = OperatorUses[Op][1]; |
| 3234 | bool MustBeMemberOperator = OperatorUses[Op][2]; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3235 | |
| 3236 | // C++ [over.oper]p8: |
| 3237 | // [...] Operator functions cannot have more or fewer parameters |
| 3238 | // than the number required for the corresponding operator, as |
| 3239 | // described in the rest of this subclause. |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 3240 | unsigned NumParams = FnDecl->getNumParams() |
| 3241 | + (isa<CXXMethodDecl>(FnDecl)? 1 : 0); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3242 | if (Op != OO_Call && |
| 3243 | ((NumParams == 1 && !CanBeUnaryOperator) || |
| 3244 | (NumParams == 2 && !CanBeBinaryOperator) || |
| 3245 | (NumParams < 1) || (NumParams > 2))) { |
| 3246 | // We have the wrong number of parameters. |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 3247 | unsigned ErrorKind; |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 3248 | if (CanBeUnaryOperator && CanBeBinaryOperator) { |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 3249 | ErrorKind = 2; // 2 -> unary or binary. |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 3250 | } else if (CanBeUnaryOperator) { |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 3251 | ErrorKind = 0; // 0 -> unary |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 3252 | } else { |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 3253 | assert(CanBeBinaryOperator && |
| 3254 | "All non-call overloaded operators are unary or binary!"); |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 3255 | ErrorKind = 1; // 1 -> binary |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 3256 | } |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3257 | |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 3258 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_must_be) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3259 | << FnDecl->getDeclName() << NumParams << ErrorKind; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3260 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 3261 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 3262 | // Overloaded operators other than operator() cannot be variadic. |
| 3263 | if (Op != OO_Call && |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3264 | FnDecl->getType()->getAsFunctionProtoType()->isVariadic()) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 3265 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_variadic) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3266 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3267 | } |
| 3268 | |
| 3269 | // Some operators must be non-static member functions. |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 3270 | if (MustBeMemberOperator && !isa<CXXMethodDecl>(FnDecl)) { |
| 3271 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 3272 | diag::err_operator_overload_must_be_member) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3273 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3274 | } |
| 3275 | |
| 3276 | // C++ [over.inc]p1: |
| 3277 | // The user-defined function called operator++ implements the |
| 3278 | // prefix and postfix ++ operator. If this function is a member |
| 3279 | // function with no parameters, or a non-member function with one |
| 3280 | // parameter of class or enumeration type, it defines the prefix |
| 3281 | // increment operator ++ for objects of that type. If the function |
| 3282 | // is a member function with one parameter (which shall be of type |
| 3283 | // int) or a non-member function with two parameters (the second |
| 3284 | // of which shall be of type int), it defines the postfix |
| 3285 | // increment operator ++ for objects of that type. |
| 3286 | if ((Op == OO_PlusPlus || Op == OO_MinusMinus) && NumParams == 2) { |
| 3287 | ParmVarDecl *LastParam = FnDecl->getParamDecl(FnDecl->getNumParams() - 1); |
| 3288 | bool ParamIsInt = false; |
| 3289 | if (const BuiltinType *BT = LastParam->getType()->getAsBuiltinType()) |
| 3290 | ParamIsInt = BT->getKind() == BuiltinType::Int; |
| 3291 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 3292 | if (!ParamIsInt) |
| 3293 | return Diag(LastParam->getLocation(), |
| 3294 | diag::err_operator_overload_post_incdec_must_be_int) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3295 | << LastParam->getType() << (Op == OO_MinusMinus); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3296 | } |
| 3297 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 3298 | // Notify the class if it got an assignment operator. |
| 3299 | if (Op == OO_Equal) { |
| 3300 | // Would have returned earlier otherwise. |
| 3301 | assert(isa<CXXMethodDecl>(FnDecl) && |
| 3302 | "Overloaded = not member, but not filtered."); |
| 3303 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
Fariborz Jahanian | ad25883 | 2009-08-13 21:09:41 +0000 | [diff] [blame] | 3304 | Method->setCopyAssignment(true); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 3305 | Method->getParent()->addedAssignmentOperator(Context, Method); |
| 3306 | } |
| 3307 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 3308 | return false; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3309 | } |
Chris Lattner | 5a003a4 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 3310 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3311 | /// ActOnStartLinkageSpecification - Parsed the beginning of a C++ |
| 3312 | /// linkage specification, including the language and (if present) |
| 3313 | /// the '{'. ExternLoc is the location of the 'extern', LangLoc is |
| 3314 | /// the location of the language string literal, which is provided |
| 3315 | /// by Lang/StrSize. LBraceLoc, if valid, provides the location of |
| 3316 | /// the '{' brace. Otherwise, this linkage specification does not |
| 3317 | /// have any braces. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3318 | Sema::DeclPtrTy Sema::ActOnStartLinkageSpecification(Scope *S, |
| 3319 | SourceLocation ExternLoc, |
| 3320 | SourceLocation LangLoc, |
| 3321 | const char *Lang, |
| 3322 | unsigned StrSize, |
| 3323 | SourceLocation LBraceLoc) { |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 3324 | LinkageSpecDecl::LanguageIDs Language; |
| 3325 | if (strncmp(Lang, "\"C\"", StrSize) == 0) |
| 3326 | Language = LinkageSpecDecl::lang_c; |
| 3327 | else if (strncmp(Lang, "\"C++\"", StrSize) == 0) |
| 3328 | Language = LinkageSpecDecl::lang_cxx; |
| 3329 | else { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3330 | Diag(LangLoc, diag::err_bad_language); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3331 | return DeclPtrTy(); |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 3332 | } |
| 3333 | |
| 3334 | // FIXME: Add all the various semantics of linkage specifications |
| 3335 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3336 | LinkageSpecDecl *D = LinkageSpecDecl::Create(Context, CurContext, |
| 3337 | LangLoc, Language, |
| 3338 | LBraceLoc.isValid()); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3339 | CurContext->addDecl(D); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3340 | PushDeclContext(S, D); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3341 | return DeclPtrTy::make(D); |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 3342 | } |
| 3343 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3344 | /// ActOnFinishLinkageSpecification - Completely the definition of |
| 3345 | /// the C++ linkage specification LinkageSpec. If RBraceLoc is |
| 3346 | /// valid, it's the position of the closing '}' brace in a linkage |
| 3347 | /// specification that uses braces. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3348 | Sema::DeclPtrTy Sema::ActOnFinishLinkageSpecification(Scope *S, |
| 3349 | DeclPtrTy LinkageSpec, |
| 3350 | SourceLocation RBraceLoc) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3351 | if (LinkageSpec) |
| 3352 | PopDeclContext(); |
| 3353 | return LinkageSpec; |
Chris Lattner | 5a003a4 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 3354 | } |
| 3355 | |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 3356 | /// \brief Perform semantic analysis for the variable declaration that |
| 3357 | /// occurs within a C++ catch clause, returning the newly-created |
| 3358 | /// variable. |
| 3359 | VarDecl *Sema::BuildExceptionDeclaration(Scope *S, QualType ExDeclType, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 3360 | DeclaratorInfo *DInfo, |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 3361 | IdentifierInfo *Name, |
| 3362 | SourceLocation Loc, |
| 3363 | SourceRange Range) { |
| 3364 | bool Invalid = false; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 3365 | |
| 3366 | // Arrays and functions decay. |
| 3367 | if (ExDeclType->isArrayType()) |
| 3368 | ExDeclType = Context.getArrayDecayedType(ExDeclType); |
| 3369 | else if (ExDeclType->isFunctionType()) |
| 3370 | ExDeclType = Context.getPointerType(ExDeclType); |
| 3371 | |
| 3372 | // C++ 15.3p1: The exception-declaration shall not denote an incomplete type. |
| 3373 | // The exception-declaration shall not denote a pointer or reference to an |
| 3374 | // incomplete type, other than [cv] void*. |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 3375 | // N2844 forbids rvalue references. |
Douglas Gregor | 2f2433f | 2009-05-18 21:08:14 +0000 | [diff] [blame] | 3376 | if(!ExDeclType->isDependentType() && ExDeclType->isRValueReferenceType()) { |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 3377 | Diag(Loc, diag::err_catch_rvalue_ref) << Range; |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 3378 | Invalid = true; |
| 3379 | } |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 3380 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 3381 | QualType BaseType = ExDeclType; |
| 3382 | int Mode = 0; // 0 for direct type, 1 for pointer, 2 for reference |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 3383 | unsigned DK = diag::err_catch_incomplete; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3384 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 3385 | BaseType = Ptr->getPointeeType(); |
| 3386 | Mode = 1; |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 3387 | DK = diag::err_catch_incomplete_ptr; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3388 | } else if(const ReferenceType *Ref = BaseType->getAs<ReferenceType>()) { |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 3389 | // For the purpose of error recovery, we treat rvalue refs like lvalue refs. |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 3390 | BaseType = Ref->getPointeeType(); |
| 3391 | Mode = 2; |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 3392 | DK = diag::err_catch_incomplete_ref; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 3393 | } |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 3394 | if (!Invalid && (Mode == 0 || !BaseType->isVoidType()) && |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 3395 | !BaseType->isDependentType() && RequireCompleteType(Loc, BaseType, DK)) |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 3396 | Invalid = true; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 3397 | |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 3398 | if (!Invalid && !ExDeclType->isDependentType() && |
| 3399 | RequireNonAbstractType(Loc, ExDeclType, |
| 3400 | diag::err_abstract_type_in_decl, |
| 3401 | AbstractVariableType)) |
Sebastian Redl | fef9f59 | 2009-04-27 21:03:30 +0000 | [diff] [blame] | 3402 | Invalid = true; |
| 3403 | |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 3404 | // FIXME: Need to test for ability to copy-construct and destroy the |
| 3405 | // exception variable. |
| 3406 | |
Sebastian Redl | 8351da0 | 2008-12-22 21:35:02 +0000 | [diff] [blame] | 3407 | // FIXME: Need to check for abstract classes. |
| 3408 | |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 3409 | VarDecl *ExDecl = VarDecl::Create(Context, CurContext, Loc, |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 3410 | Name, ExDeclType, DInfo, VarDecl::None); |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 3411 | |
| 3412 | if (Invalid) |
| 3413 | ExDecl->setInvalidDecl(); |
| 3414 | |
| 3415 | return ExDecl; |
| 3416 | } |
| 3417 | |
| 3418 | /// ActOnExceptionDeclarator - Parsed the exception-declarator in a C++ catch |
| 3419 | /// handler. |
| 3420 | Sema::DeclPtrTy Sema::ActOnExceptionDeclarator(Scope *S, Declarator &D) { |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 3421 | DeclaratorInfo *DInfo = 0; |
| 3422 | QualType ExDeclType = GetTypeForDeclarator(D, S, &DInfo); |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 3423 | |
| 3424 | bool Invalid = D.isInvalidType(); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 3425 | IdentifierInfo *II = D.getIdentifier(); |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 3426 | if (NamedDecl *PrevDecl = LookupName(S, II, LookupOrdinaryName)) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 3427 | // The scope should be freshly made just for us. There is just no way |
| 3428 | // it contains any previous declaration. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3429 | assert(!S->isDeclScope(DeclPtrTy::make(PrevDecl))); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 3430 | if (PrevDecl->isTemplateParameter()) { |
| 3431 | // Maybe we will complain about the shadowed template parameter. |
| 3432 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 3433 | } |
| 3434 | } |
| 3435 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 3436 | if (D.getCXXScopeSpec().isSet() && !Invalid) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 3437 | Diag(D.getIdentifierLoc(), diag::err_qualified_catch_declarator) |
| 3438 | << D.getCXXScopeSpec().getRange(); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 3439 | Invalid = true; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 3440 | } |
| 3441 | |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 3442 | VarDecl *ExDecl = BuildExceptionDeclaration(S, ExDeclType, DInfo, |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 3443 | D.getIdentifier(), |
| 3444 | D.getIdentifierLoc(), |
| 3445 | D.getDeclSpec().getSourceRange()); |
| 3446 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 3447 | if (Invalid) |
| 3448 | ExDecl->setInvalidDecl(); |
| 3449 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 3450 | // Add the exception declaration into this scope. |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 3451 | if (II) |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 3452 | PushOnScopeChains(ExDecl, S); |
| 3453 | else |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3454 | CurContext->addDecl(ExDecl); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 3455 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 3456 | ProcessDeclAttributes(S, ExDecl, D); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3457 | return DeclPtrTy::make(ExDecl); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 3458 | } |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 3459 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3460 | Sema::DeclPtrTy Sema::ActOnStaticAssertDeclaration(SourceLocation AssertLoc, |
| 3461 | ExprArg assertexpr, |
| 3462 | ExprArg assertmessageexpr) { |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 3463 | Expr *AssertExpr = (Expr *)assertexpr.get(); |
| 3464 | StringLiteral *AssertMessage = |
| 3465 | cast<StringLiteral>((Expr *)assertmessageexpr.get()); |
| 3466 | |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 3467 | if (!AssertExpr->isTypeDependent() && !AssertExpr->isValueDependent()) { |
| 3468 | llvm::APSInt Value(32); |
| 3469 | if (!AssertExpr->isIntegerConstantExpr(Value, Context)) { |
| 3470 | Diag(AssertLoc, diag::err_static_assert_expression_is_not_constant) << |
| 3471 | AssertExpr->getSourceRange(); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3472 | return DeclPtrTy(); |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 3473 | } |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 3474 | |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 3475 | if (Value == 0) { |
| 3476 | std::string str(AssertMessage->getStrData(), |
| 3477 | AssertMessage->getByteLength()); |
Anders Carlsson | 94b15fb | 2009-03-15 18:44:04 +0000 | [diff] [blame] | 3478 | Diag(AssertLoc, diag::err_static_assert_failed) |
| 3479 | << str << AssertExpr->getSourceRange(); |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 3480 | } |
| 3481 | } |
| 3482 | |
Anders Carlsson | 77d8142 | 2009-03-15 17:35:16 +0000 | [diff] [blame] | 3483 | assertexpr.release(); |
| 3484 | assertmessageexpr.release(); |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 3485 | Decl *Decl = StaticAssertDecl::Create(Context, CurContext, AssertLoc, |
| 3486 | AssertExpr, AssertMessage); |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 3487 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3488 | CurContext->addDecl(Decl); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3489 | return DeclPtrTy::make(Decl); |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 3490 | } |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 3491 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 3492 | Sema::DeclPtrTy Sema::ActOnFriendDecl(Scope *S, |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 3493 | llvm::PointerUnion<const DeclSpec*,Declarator*> DU, |
| 3494 | bool IsDefinition) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 3495 | Declarator *D = DU.dyn_cast<Declarator*>(); |
| 3496 | const DeclSpec &DS = (D ? D->getDeclSpec() : *DU.get<const DeclSpec*>()); |
| 3497 | |
| 3498 | assert(DS.isFriendSpecified()); |
| 3499 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 3500 | |
| 3501 | // If there's no declarator, then this can only be a friend class |
John McCall | c48fbdf | 2009-08-11 21:13:21 +0000 | [diff] [blame] | 3502 | // declaration (or else it's just syntactically invalid). |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 3503 | if (!D) { |
John McCall | c48fbdf | 2009-08-11 21:13:21 +0000 | [diff] [blame] | 3504 | SourceLocation Loc = DS.getSourceRange().getBegin(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 3505 | |
John McCall | c48fbdf | 2009-08-11 21:13:21 +0000 | [diff] [blame] | 3506 | QualType T; |
| 3507 | DeclContext *DC; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 3508 | |
John McCall | c48fbdf | 2009-08-11 21:13:21 +0000 | [diff] [blame] | 3509 | // In C++0x, we just accept any old type. |
| 3510 | if (getLangOptions().CPlusPlus0x) { |
| 3511 | bool invalid = false; |
| 3512 | QualType T = ConvertDeclSpecToType(DS, Loc, invalid); |
| 3513 | if (invalid) |
| 3514 | return DeclPtrTy(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 3515 | |
John McCall | c48fbdf | 2009-08-11 21:13:21 +0000 | [diff] [blame] | 3516 | // The semantic context in which to create the decl. If it's not |
| 3517 | // a record decl (or we don't yet know if it is), create it in the |
| 3518 | // current context. |
| 3519 | DC = CurContext; |
| 3520 | if (const RecordType *RT = T->getAs<RecordType>()) |
| 3521 | DC = RT->getDecl()->getDeclContext(); |
| 3522 | |
| 3523 | // The C++98 rules are somewhat more complex. |
| 3524 | } else { |
| 3525 | // C++ [class.friend]p2: |
| 3526 | // An elaborated-type-specifier shall be used in a friend declaration |
| 3527 | // for a class.* |
| 3528 | // * The class-key of the elaborated-type-specifier is required. |
| 3529 | CXXRecordDecl *RD = 0; |
| 3530 | |
| 3531 | switch (DS.getTypeSpecType()) { |
| 3532 | case DeclSpec::TST_class: |
| 3533 | case DeclSpec::TST_struct: |
| 3534 | case DeclSpec::TST_union: |
| 3535 | RD = dyn_cast_or_null<CXXRecordDecl>((Decl*) DS.getTypeRep()); |
| 3536 | if (!RD) return DeclPtrTy(); |
| 3537 | break; |
| 3538 | |
| 3539 | case DeclSpec::TST_typename: |
| 3540 | if (const RecordType *RT = |
| 3541 | ((const Type*) DS.getTypeRep())->getAs<RecordType>()) |
| 3542 | RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
| 3543 | // fallthrough |
| 3544 | default: |
| 3545 | if (RD) { |
| 3546 | Diag(DS.getFriendSpecLoc(), diag::err_unelaborated_friend_type) |
| 3547 | << (RD->isUnion()) |
| 3548 | << CodeModificationHint::CreateInsertion(DS.getTypeSpecTypeLoc(), |
| 3549 | RD->isUnion() ? " union" : " class"); |
| 3550 | return DeclPtrTy::make(RD); |
| 3551 | } |
| 3552 | |
| 3553 | Diag(DS.getFriendSpecLoc(), diag::err_unexpected_friend) |
| 3554 | << DS.getSourceRange(); |
| 3555 | return DeclPtrTy(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 3556 | } |
| 3557 | |
John McCall | c48fbdf | 2009-08-11 21:13:21 +0000 | [diff] [blame] | 3558 | // The record declaration we get from friend declarations is not |
| 3559 | // canonicalized; see ActOnTag. |
John McCall | c48fbdf | 2009-08-11 21:13:21 +0000 | [diff] [blame] | 3560 | |
| 3561 | // C++ [class.friend]p2: A class shall not be defined inside |
| 3562 | // a friend declaration. |
| 3563 | if (RD->isDefinition()) |
| 3564 | Diag(DS.getFriendSpecLoc(), diag::err_friend_decl_defines_class) |
| 3565 | << RD->getSourceRange(); |
| 3566 | |
| 3567 | // C++98 [class.friend]p1: A friend of a class is a function |
| 3568 | // or class that is not a member of the class . . . |
| 3569 | // But that's a silly restriction which nobody implements for |
| 3570 | // inner classes, and C++0x removes it anyway, so we only report |
| 3571 | // this (as a warning) if we're being pedantic. |
| 3572 | // |
| 3573 | // Also, definitions currently get treated in a way that causes |
| 3574 | // this error, so only report it if we didn't see a definition. |
| 3575 | else if (RD->getDeclContext() == CurContext && |
| 3576 | !getLangOptions().CPlusPlus0x) |
| 3577 | Diag(DS.getFriendSpecLoc(), diag::ext_friend_inner_class); |
| 3578 | |
| 3579 | T = QualType(RD->getTypeForDecl(), 0); |
| 3580 | DC = RD->getDeclContext(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 3581 | } |
| 3582 | |
John McCall | c48fbdf | 2009-08-11 21:13:21 +0000 | [diff] [blame] | 3583 | FriendClassDecl *FCD = FriendClassDecl::Create(Context, DC, Loc, T, |
| 3584 | DS.getFriendSpecLoc()); |
| 3585 | FCD->setLexicalDeclContext(CurContext); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 3586 | |
John McCall | c48fbdf | 2009-08-11 21:13:21 +0000 | [diff] [blame] | 3587 | if (CurContext->isDependentContext()) |
| 3588 | CurContext->addHiddenDecl(FCD); |
| 3589 | else |
| 3590 | CurContext->addDecl(FCD); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 3591 | |
John McCall | c48fbdf | 2009-08-11 21:13:21 +0000 | [diff] [blame] | 3592 | return DeclPtrTy::make(FCD); |
Anders Carlsson | 0033836 | 2009-05-11 22:55:49 +0000 | [diff] [blame] | 3593 | } |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 3594 | |
| 3595 | // We have a declarator. |
| 3596 | assert(D); |
| 3597 | |
| 3598 | SourceLocation Loc = D->getIdentifierLoc(); |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 3599 | DeclaratorInfo *DInfo = 0; |
| 3600 | QualType T = GetTypeForDeclarator(*D, S, &DInfo); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 3601 | |
| 3602 | // C++ [class.friend]p1 |
| 3603 | // A friend of a class is a function or class.... |
| 3604 | // Note that this sees through typedefs, which is intended. |
| 3605 | if (!T->isFunctionType()) { |
| 3606 | Diag(Loc, diag::err_unexpected_friend); |
| 3607 | |
| 3608 | // It might be worthwhile to try to recover by creating an |
| 3609 | // appropriate declaration. |
| 3610 | return DeclPtrTy(); |
| 3611 | } |
| 3612 | |
| 3613 | // C++ [namespace.memdef]p3 |
| 3614 | // - If a friend declaration in a non-local class first declares a |
| 3615 | // class or function, the friend class or function is a member |
| 3616 | // of the innermost enclosing namespace. |
| 3617 | // - The name of the friend is not found by simple name lookup |
| 3618 | // until a matching declaration is provided in that namespace |
| 3619 | // scope (either before or after the class declaration granting |
| 3620 | // friendship). |
| 3621 | // - If a friend function is called, its name may be found by the |
| 3622 | // name lookup that considers functions from namespaces and |
| 3623 | // classes associated with the types of the function arguments. |
| 3624 | // - When looking for a prior declaration of a class or a function |
| 3625 | // declared as a friend, scopes outside the innermost enclosing |
| 3626 | // namespace scope are not considered. |
| 3627 | |
| 3628 | CXXScopeSpec &ScopeQual = D->getCXXScopeSpec(); |
| 3629 | DeclarationName Name = GetNameForDeclarator(*D); |
| 3630 | assert(Name); |
| 3631 | |
| 3632 | // The existing declaration we found. |
| 3633 | FunctionDecl *FD = NULL; |
| 3634 | |
| 3635 | // The context we found the declaration in, or in which we should |
| 3636 | // create the declaration. |
| 3637 | DeclContext *DC; |
| 3638 | |
| 3639 | // FIXME: handle local classes |
| 3640 | |
| 3641 | // Recover from invalid scope qualifiers as if they just weren't there. |
| 3642 | if (!ScopeQual.isInvalid() && ScopeQual.isSet()) { |
| 3643 | DC = computeDeclContext(ScopeQual); |
| 3644 | |
| 3645 | // FIXME: handle dependent contexts |
| 3646 | if (!DC) return DeclPtrTy(); |
| 3647 | |
| 3648 | Decl *Dec = LookupQualifiedNameWithType(DC, Name, T); |
| 3649 | |
| 3650 | // If searching in that context implicitly found a declaration in |
| 3651 | // a different context, treat it like it wasn't found at all. |
| 3652 | // TODO: better diagnostics for this case. Suggesting the right |
| 3653 | // qualified scope would be nice... |
| 3654 | if (!Dec || Dec->getDeclContext() != DC) { |
| 3655 | D->setInvalidType(); |
| 3656 | Diag(Loc, diag::err_qualified_friend_not_found) << Name << T; |
| 3657 | return DeclPtrTy(); |
| 3658 | } |
| 3659 | |
| 3660 | // C++ [class.friend]p1: A friend of a class is a function or |
| 3661 | // class that is not a member of the class . . . |
| 3662 | if (DC == CurContext) |
| 3663 | Diag(DS.getFriendSpecLoc(), diag::err_friend_is_member); |
| 3664 | |
| 3665 | FD = cast<FunctionDecl>(Dec); |
| 3666 | |
| 3667 | // Otherwise walk out to the nearest namespace scope looking for matches. |
| 3668 | } else { |
| 3669 | // TODO: handle local class contexts. |
| 3670 | |
| 3671 | DC = CurContext; |
| 3672 | while (true) { |
| 3673 | // Skip class contexts. If someone can cite chapter and verse |
| 3674 | // for this behavior, that would be nice --- it's what GCC and |
| 3675 | // EDG do, and it seems like a reasonable intent, but the spec |
| 3676 | // really only says that checks for unqualified existing |
| 3677 | // declarations should stop at the nearest enclosing namespace, |
| 3678 | // not that they should only consider the nearest enclosing |
| 3679 | // namespace. |
| 3680 | while (DC->isRecord()) DC = DC->getParent(); |
| 3681 | |
| 3682 | Decl *Dec = LookupQualifiedNameWithType(DC, Name, T); |
| 3683 | |
| 3684 | // TODO: decide what we think about using declarations. |
| 3685 | if (Dec) { |
| 3686 | FD = cast<FunctionDecl>(Dec); |
| 3687 | break; |
| 3688 | } |
| 3689 | if (DC->isFileContext()) break; |
| 3690 | DC = DC->getParent(); |
| 3691 | } |
| 3692 | |
| 3693 | // C++ [class.friend]p1: A friend of a class is a function or |
| 3694 | // class that is not a member of the class . . . |
John McCall | 7f27d92 | 2009-08-06 20:49:32 +0000 | [diff] [blame] | 3695 | // C++0x changes this for both friend types and functions. |
| 3696 | // Most C++ 98 compilers do seem to give an error here, so |
| 3697 | // we do, too. |
| 3698 | if (FD && DC == CurContext && !getLangOptions().CPlusPlus0x) |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 3699 | Diag(DS.getFriendSpecLoc(), diag::err_friend_is_member); |
| 3700 | } |
| 3701 | |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 3702 | bool Redeclaration = (FD != 0); |
| 3703 | |
| 3704 | // If we found a match, create a friend function declaration with |
| 3705 | // that function as the previous declaration. |
| 3706 | if (Redeclaration) { |
| 3707 | // Create it in the semantic context of the original declaration. |
| 3708 | DC = FD->getDeclContext(); |
| 3709 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 3710 | // If we didn't find something matching the type exactly, create |
| 3711 | // a declaration. This declaration should only be findable via |
| 3712 | // argument-dependent lookup. |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 3713 | } else { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 3714 | assert(DC->isFileContext()); |
| 3715 | |
| 3716 | // This implies that it has to be an operator or function. |
| 3717 | if (D->getKind() == Declarator::DK_Constructor || |
| 3718 | D->getKind() == Declarator::DK_Destructor || |
| 3719 | D->getKind() == Declarator::DK_Conversion) { |
| 3720 | Diag(Loc, diag::err_introducing_special_friend) << |
| 3721 | (D->getKind() == Declarator::DK_Constructor ? 0 : |
| 3722 | D->getKind() == Declarator::DK_Destructor ? 1 : 2); |
| 3723 | return DeclPtrTy(); |
| 3724 | } |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 3725 | } |
| 3726 | |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 3727 | NamedDecl *ND = ActOnFunctionDeclarator(S, *D, DC, T, DInfo, |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 3728 | /* PrevDecl = */ FD, |
| 3729 | MultiTemplateParamsArg(*this), |
| 3730 | IsDefinition, |
| 3731 | Redeclaration); |
| 3732 | FD = cast_or_null<FriendFunctionDecl>(ND); |
| 3733 | |
John McCall | 88232aa | 2009-08-18 00:00:49 +0000 | [diff] [blame] | 3734 | assert(FD->getDeclContext() == DC); |
| 3735 | assert(FD->getLexicalDeclContext() == CurContext); |
| 3736 | |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 3737 | // If this is a dependent context, just add the decl to the |
| 3738 | // class's decl list and don't both with the lookup tables. This |
| 3739 | // doesn't affect lookup because any call that might find this |
| 3740 | // function via ADL necessarily has to involve dependently-typed |
| 3741 | // arguments and hence can't be resolved until |
| 3742 | // template-instantiation anyway. |
| 3743 | if (CurContext->isDependentContext()) |
| 3744 | CurContext->addHiddenDecl(FD); |
| 3745 | else |
| 3746 | CurContext->addDecl(FD); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 3747 | |
| 3748 | return DeclPtrTy::make(FD); |
Anders Carlsson | 0033836 | 2009-05-11 22:55:49 +0000 | [diff] [blame] | 3749 | } |
| 3750 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3751 | void Sema::SetDeclDeleted(DeclPtrTy dcl, SourceLocation DelLoc) { |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 3752 | AdjustDeclIfTemplate(dcl); |
| 3753 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3754 | Decl *Dcl = dcl.getAs<Decl>(); |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 3755 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(Dcl); |
| 3756 | if (!Fn) { |
| 3757 | Diag(DelLoc, diag::err_deleted_non_function); |
| 3758 | return; |
| 3759 | } |
| 3760 | if (const FunctionDecl *Prev = Fn->getPreviousDeclaration()) { |
| 3761 | Diag(DelLoc, diag::err_deleted_decl_not_first); |
| 3762 | Diag(Prev->getLocation(), diag::note_previous_declaration); |
| 3763 | // If the declaration wasn't the first, we delete the function anyway for |
| 3764 | // recovery. |
| 3765 | } |
| 3766 | Fn->setDeleted(); |
| 3767 | } |
Sebastian Redl | 13e8854 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 3768 | |
| 3769 | static void SearchForReturnInStmt(Sema &Self, Stmt *S) { |
| 3770 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); CI != E; |
| 3771 | ++CI) { |
| 3772 | Stmt *SubStmt = *CI; |
| 3773 | if (!SubStmt) |
| 3774 | continue; |
| 3775 | if (isa<ReturnStmt>(SubStmt)) |
| 3776 | Self.Diag(SubStmt->getSourceRange().getBegin(), |
| 3777 | diag::err_return_in_constructor_handler); |
| 3778 | if (!isa<Expr>(SubStmt)) |
| 3779 | SearchForReturnInStmt(Self, SubStmt); |
| 3780 | } |
| 3781 | } |
| 3782 | |
| 3783 | void Sema::DiagnoseReturnInConstructorExceptionHandler(CXXTryStmt *TryBlock) { |
| 3784 | for (unsigned I = 0, E = TryBlock->getNumHandlers(); I != E; ++I) { |
| 3785 | CXXCatchStmt *Handler = TryBlock->getHandler(I); |
| 3786 | SearchForReturnInStmt(*this, Handler); |
| 3787 | } |
| 3788 | } |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 3789 | |
| 3790 | bool Sema::CheckOverridingFunctionReturnType(const CXXMethodDecl *New, |
| 3791 | const CXXMethodDecl *Old) { |
| 3792 | QualType NewTy = New->getType()->getAsFunctionType()->getResultType(); |
| 3793 | QualType OldTy = Old->getType()->getAsFunctionType()->getResultType(); |
| 3794 | |
| 3795 | QualType CNewTy = Context.getCanonicalType(NewTy); |
| 3796 | QualType COldTy = Context.getCanonicalType(OldTy); |
| 3797 | |
| 3798 | if (CNewTy == COldTy && |
| 3799 | CNewTy.getCVRQualifiers() == COldTy.getCVRQualifiers()) |
| 3800 | return false; |
| 3801 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 3802 | // Check if the return types are covariant |
| 3803 | QualType NewClassTy, OldClassTy; |
| 3804 | |
| 3805 | /// Both types must be pointers or references to classes. |
| 3806 | if (PointerType *NewPT = dyn_cast<PointerType>(NewTy)) { |
| 3807 | if (PointerType *OldPT = dyn_cast<PointerType>(OldTy)) { |
| 3808 | NewClassTy = NewPT->getPointeeType(); |
| 3809 | OldClassTy = OldPT->getPointeeType(); |
| 3810 | } |
| 3811 | } else if (ReferenceType *NewRT = dyn_cast<ReferenceType>(NewTy)) { |
| 3812 | if (ReferenceType *OldRT = dyn_cast<ReferenceType>(OldTy)) { |
| 3813 | NewClassTy = NewRT->getPointeeType(); |
| 3814 | OldClassTy = OldRT->getPointeeType(); |
| 3815 | } |
| 3816 | } |
| 3817 | |
| 3818 | // The return types aren't either both pointers or references to a class type. |
| 3819 | if (NewClassTy.isNull()) { |
| 3820 | Diag(New->getLocation(), |
| 3821 | diag::err_different_return_type_for_overriding_virtual_function) |
| 3822 | << New->getDeclName() << NewTy << OldTy; |
| 3823 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 3824 | |
| 3825 | return true; |
| 3826 | } |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 3827 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 3828 | if (NewClassTy.getUnqualifiedType() != OldClassTy.getUnqualifiedType()) { |
| 3829 | // Check if the new class derives from the old class. |
| 3830 | if (!IsDerivedFrom(NewClassTy, OldClassTy)) { |
| 3831 | Diag(New->getLocation(), |
| 3832 | diag::err_covariant_return_not_derived) |
| 3833 | << New->getDeclName() << NewTy << OldTy; |
| 3834 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 3835 | return true; |
| 3836 | } |
| 3837 | |
| 3838 | // Check if we the conversion from derived to base is valid. |
| 3839 | if (CheckDerivedToBaseConversion(NewClassTy, OldClassTy, |
| 3840 | diag::err_covariant_return_inaccessible_base, |
| 3841 | diag::err_covariant_return_ambiguous_derived_to_base_conv, |
| 3842 | // FIXME: Should this point to the return type? |
| 3843 | New->getLocation(), SourceRange(), New->getDeclName())) { |
| 3844 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 3845 | return true; |
| 3846 | } |
| 3847 | } |
| 3848 | |
| 3849 | // The qualifiers of the return types must be the same. |
| 3850 | if (CNewTy.getCVRQualifiers() != COldTy.getCVRQualifiers()) { |
| 3851 | Diag(New->getLocation(), |
| 3852 | diag::err_covariant_return_type_different_qualifications) |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 3853 | << New->getDeclName() << NewTy << OldTy; |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 3854 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 3855 | return true; |
| 3856 | }; |
| 3857 | |
| 3858 | |
| 3859 | // The new class type must have the same or less qualifiers as the old type. |
| 3860 | if (NewClassTy.isMoreQualifiedThan(OldClassTy)) { |
| 3861 | Diag(New->getLocation(), |
| 3862 | diag::err_covariant_return_type_class_type_more_qualified) |
| 3863 | << New->getDeclName() << NewTy << OldTy; |
| 3864 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 3865 | return true; |
| 3866 | }; |
| 3867 | |
| 3868 | return false; |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 3869 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 3870 | |
Sebastian Redl | 23c7d06 | 2009-07-07 20:29:57 +0000 | [diff] [blame] | 3871 | bool Sema::CheckOverridingFunctionExceptionSpec(const CXXMethodDecl *New, |
| 3872 | const CXXMethodDecl *Old) |
| 3873 | { |
| 3874 | return CheckExceptionSpecSubset(diag::err_override_exception_spec, |
| 3875 | diag::note_overridden_virtual_function, |
| 3876 | Old->getType()->getAsFunctionProtoType(), |
| 3877 | Old->getLocation(), |
| 3878 | New->getType()->getAsFunctionProtoType(), |
| 3879 | New->getLocation()); |
| 3880 | } |
| 3881 | |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 3882 | /// ActOnCXXEnterDeclInitializer - Invoked when we are about to parse an |
| 3883 | /// initializer for the declaration 'Dcl'. |
| 3884 | /// After this method is called, according to [C++ 3.4.1p13], if 'Dcl' is a |
| 3885 | /// static data member of class X, names should be looked up in the scope of |
| 3886 | /// class X. |
| 3887 | void Sema::ActOnCXXEnterDeclInitializer(Scope *S, DeclPtrTy Dcl) { |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 3888 | AdjustDeclIfTemplate(Dcl); |
| 3889 | |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 3890 | Decl *D = Dcl.getAs<Decl>(); |
| 3891 | // If there is no declaration, there was an error parsing it. |
| 3892 | if (D == 0) |
| 3893 | return; |
| 3894 | |
| 3895 | // Check whether it is a declaration with a nested name specifier like |
| 3896 | // int foo::bar; |
| 3897 | if (!D->isOutOfLine()) |
| 3898 | return; |
| 3899 | |
| 3900 | // C++ [basic.lookup.unqual]p13 |
| 3901 | // |
| 3902 | // A name used in the definition of a static data member of class X |
| 3903 | // (after the qualified-id of the static member) is looked up as if the name |
| 3904 | // was used in a member function of X. |
| 3905 | |
| 3906 | // Change current context into the context of the initializing declaration. |
Argyrios Kyrtzidis | 1d17553 | 2009-06-17 23:15:40 +0000 | [diff] [blame] | 3907 | EnterDeclaratorContext(S, D->getDeclContext()); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 3908 | } |
| 3909 | |
| 3910 | /// ActOnCXXExitDeclInitializer - Invoked after we are finished parsing an |
| 3911 | /// initializer for the declaration 'Dcl'. |
| 3912 | void Sema::ActOnCXXExitDeclInitializer(Scope *S, DeclPtrTy Dcl) { |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 3913 | AdjustDeclIfTemplate(Dcl); |
| 3914 | |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 3915 | Decl *D = Dcl.getAs<Decl>(); |
| 3916 | // If there is no declaration, there was an error parsing it. |
| 3917 | if (D == 0) |
| 3918 | return; |
| 3919 | |
| 3920 | // Check whether it is a declaration with a nested name specifier like |
| 3921 | // int foo::bar; |
| 3922 | if (!D->isOutOfLine()) |
| 3923 | return; |
| 3924 | |
| 3925 | assert(S->getEntity() == D->getDeclContext() && "Context imbalance!"); |
Argyrios Kyrtzidis | 1d17553 | 2009-06-17 23:15:40 +0000 | [diff] [blame] | 3926 | ExitDeclaratorContext(S); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 3927 | } |