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" |
Argyrios Kyrtzidis | a4755c6 | 2008-08-09 00:58:37 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTConsumer.h" |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 17 | #include "clang/AST/CXXInheritance.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> |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 28 | #include <set> |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 29 | |
| 30 | using namespace clang; |
| 31 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 32 | //===----------------------------------------------------------------------===// |
| 33 | // CheckDefaultArgumentVisitor |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 36 | namespace { |
| 37 | /// CheckDefaultArgumentVisitor - C++ [dcl.fct.default] Traverses |
| 38 | /// the default argument of a parameter to determine whether it |
| 39 | /// contains any ill-formed subexpressions. For example, this will |
| 40 | /// diagnose the use of local variables or parameters within the |
| 41 | /// default argument expression. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 42 | class VISIBILITY_HIDDEN CheckDefaultArgumentVisitor |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 43 | : public StmtVisitor<CheckDefaultArgumentVisitor, bool> { |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 44 | Expr *DefaultArg; |
| 45 | Sema *S; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 46 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 47 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 48 | CheckDefaultArgumentVisitor(Expr *defarg, Sema *s) |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 49 | : DefaultArg(defarg), S(s) {} |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 50 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 51 | bool VisitExpr(Expr *Node); |
| 52 | bool VisitDeclRefExpr(DeclRefExpr *DRE); |
Douglas Gregor | 796da18 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 53 | bool VisitCXXThisExpr(CXXThisExpr *ThisE); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 54 | }; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 55 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 56 | /// VisitExpr - Visit all of the children of this expression. |
| 57 | bool CheckDefaultArgumentVisitor::VisitExpr(Expr *Node) { |
| 58 | bool IsInvalid = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 59 | for (Stmt::child_iterator I = Node->child_begin(), |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 60 | E = Node->child_end(); I != E; ++I) |
| 61 | IsInvalid |= Visit(*I); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 62 | return IsInvalid; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 65 | /// VisitDeclRefExpr - Visit a reference to a declaration, to |
| 66 | /// determine whether this declaration can be used in the default |
| 67 | /// argument expression. |
| 68 | bool CheckDefaultArgumentVisitor::VisitDeclRefExpr(DeclRefExpr *DRE) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 69 | NamedDecl *Decl = DRE->getDecl(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 70 | if (ParmVarDecl *Param = dyn_cast<ParmVarDecl>(Decl)) { |
| 71 | // C++ [dcl.fct.default]p9 |
| 72 | // Default arguments are evaluated each time the function is |
| 73 | // called. The order of evaluation of function arguments is |
| 74 | // unspecified. Consequently, parameters of a function shall not |
| 75 | // be used in default argument expressions, even if they are not |
| 76 | // evaluated. Parameters of a function declared before a default |
| 77 | // argument expression are in scope and can hide namespace and |
| 78 | // class member names. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 79 | return S->Diag(DRE->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 80 | diag::err_param_default_argument_references_param) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 81 | << Param->getDeclName() << DefaultArg->getSourceRange(); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 82 | } else if (VarDecl *VDecl = dyn_cast<VarDecl>(Decl)) { |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 83 | // C++ [dcl.fct.default]p7 |
| 84 | // Local variables shall not be used in default argument |
| 85 | // expressions. |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 86 | if (VDecl->isBlockVarDecl()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 87 | return S->Diag(DRE->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 88 | diag::err_param_default_argument_references_local) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 89 | << VDecl->getDeclName() << DefaultArg->getSourceRange(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 90 | } |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 91 | |
Douglas Gregor | 3996f23 | 2008-11-04 13:41:56 +0000 | [diff] [blame] | 92 | return false; |
| 93 | } |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 94 | |
Douglas Gregor | 796da18 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 95 | /// VisitCXXThisExpr - Visit a C++ "this" expression. |
| 96 | bool CheckDefaultArgumentVisitor::VisitCXXThisExpr(CXXThisExpr *ThisE) { |
| 97 | // C++ [dcl.fct.default]p8: |
| 98 | // The keyword this shall not be used in a default argument of a |
| 99 | // member function. |
| 100 | return S->Diag(ThisE->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 101 | diag::err_param_default_argument_references_this) |
| 102 | << ThisE->getSourceRange(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 103 | } |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 106 | bool |
| 107 | Sema::SetParamDefaultArgument(ParmVarDecl *Param, ExprArg DefaultArg, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 108 | SourceLocation EqualLoc) { |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 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(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 118 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 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). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 125 | if (CheckInitializerTypes(Arg, ParamType, EqualLoc, |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 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); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 130 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 131 | // Okay: add the default argument to the parameter |
| 132 | Param->setDefaultArg(Arg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 133 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 134 | DefaultArg.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 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 |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +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; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 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 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 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. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +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; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 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(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 185 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 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; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 194 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 195 | ParmVarDecl *Param = cast<ParmVarDecl>(param.getAs<Decl>()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 196 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 197 | Param->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 198 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 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: |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 245 | // For non-template functions, default arguments can be added in |
| 246 | // later declarations of a function in the same |
| 247 | // scope. Declarations in different scopes have completely |
| 248 | // distinct sets of default arguments. That is, declarations in |
| 249 | // inner scopes do not acquire default arguments from |
| 250 | // declarations in outer scopes, and vice versa. In a given |
| 251 | // function declaration, all parameters subsequent to a |
| 252 | // parameter with a default argument shall have default |
| 253 | // arguments supplied in this or previous declarations. A |
| 254 | // default argument shall not be redefined by a later |
| 255 | // declaration (not even to the same value). |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 256 | // |
| 257 | // C++ [dcl.fct.default]p6: |
| 258 | // Except for member functions of class templates, the default arguments |
| 259 | // in a member function definition that appears outside of the class |
| 260 | // definition are added to the set of default arguments provided by the |
| 261 | // member function declaration in the class definition. |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 262 | for (unsigned p = 0, NumParams = Old->getNumParams(); p < NumParams; ++p) { |
| 263 | ParmVarDecl *OldParam = Old->getParamDecl(p); |
| 264 | ParmVarDecl *NewParam = New->getParamDecl(p); |
| 265 | |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 266 | if (OldParam->hasDefaultArg() && NewParam->hasDefaultArg()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 267 | Diag(NewParam->getLocation(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 268 | diag::err_param_default_argument_redefinition) |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 269 | << NewParam->getDefaultArgRange(); |
| 270 | |
| 271 | // Look for the function declaration where the default argument was |
| 272 | // actually written, which may be a declaration prior to Old. |
| 273 | for (FunctionDecl *Older = Old->getPreviousDeclaration(); |
| 274 | Older; Older = Older->getPreviousDeclaration()) { |
| 275 | if (!Older->getParamDecl(p)->hasDefaultArg()) |
| 276 | break; |
| 277 | |
| 278 | OldParam = Older->getParamDecl(p); |
| 279 | } |
| 280 | |
| 281 | Diag(OldParam->getLocation(), diag::note_previous_definition) |
| 282 | << OldParam->getDefaultArgRange(); |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 283 | Invalid = true; |
Douglas Gregor | d85cef5 | 2009-09-17 19:51:30 +0000 | [diff] [blame] | 284 | } else if (OldParam->hasDefaultArg()) { |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 285 | // Merge the old default argument into the new parameter |
Douglas Gregor | d85cef5 | 2009-09-17 19:51:30 +0000 | [diff] [blame] | 286 | if (OldParam->hasUninstantiatedDefaultArg()) |
| 287 | NewParam->setUninstantiatedDefaultArg( |
| 288 | OldParam->getUninstantiatedDefaultArg()); |
| 289 | else |
| 290 | NewParam->setDefaultArg(OldParam->getDefaultArg()); |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 291 | } else if (NewParam->hasDefaultArg()) { |
| 292 | if (New->getDescribedFunctionTemplate()) { |
| 293 | // Paragraph 4, quoted above, only applies to non-template functions. |
| 294 | Diag(NewParam->getLocation(), |
| 295 | diag::err_param_default_argument_template_redecl) |
| 296 | << NewParam->getDefaultArgRange(); |
| 297 | Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 298 | << false; |
Douglas Gregor | 096ebfd | 2009-10-13 17:02:54 +0000 | [diff] [blame] | 299 | } else if (New->getTemplateSpecializationKind() |
| 300 | != TSK_ImplicitInstantiation && |
| 301 | New->getTemplateSpecializationKind() != TSK_Undeclared) { |
| 302 | // C++ [temp.expr.spec]p21: |
| 303 | // Default function arguments shall not be specified in a declaration |
| 304 | // or a definition for one of the following explicit specializations: |
| 305 | // - the explicit specialization of a function template; |
Douglas Gregor | 8c638ab | 2009-10-13 23:52:38 +0000 | [diff] [blame] | 306 | // - the explicit specialization of a member function template; |
| 307 | // - the explicit specialization of a member function of a class |
Douglas Gregor | 096ebfd | 2009-10-13 17:02:54 +0000 | [diff] [blame] | 308 | // template where the class template specialization to which the |
| 309 | // member function specialization belongs is implicitly |
| 310 | // instantiated. |
| 311 | Diag(NewParam->getLocation(), diag::err_template_spec_default_arg) |
| 312 | << (New->getTemplateSpecializationKind() ==TSK_ExplicitSpecialization) |
| 313 | << New->getDeclName() |
| 314 | << NewParam->getDefaultArgRange(); |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 315 | } else if (New->getDeclContext()->isDependentContext()) { |
| 316 | // C++ [dcl.fct.default]p6 (DR217): |
| 317 | // Default arguments for a member function of a class template shall |
| 318 | // be specified on the initial declaration of the member function |
| 319 | // within the class template. |
| 320 | // |
| 321 | // Reading the tea leaves a bit in DR217 and its reference to DR205 |
| 322 | // leads me to the conclusion that one cannot add default function |
| 323 | // arguments for an out-of-line definition of a member function of a |
| 324 | // dependent type. |
| 325 | int WhichKind = 2; |
| 326 | if (CXXRecordDecl *Record |
| 327 | = dyn_cast<CXXRecordDecl>(New->getDeclContext())) { |
| 328 | if (Record->getDescribedClassTemplate()) |
| 329 | WhichKind = 0; |
| 330 | else if (isa<ClassTemplatePartialSpecializationDecl>(Record)) |
| 331 | WhichKind = 1; |
| 332 | else |
| 333 | WhichKind = 2; |
| 334 | } |
| 335 | |
| 336 | Diag(NewParam->getLocation(), |
| 337 | diag::err_param_default_argument_member_template_redecl) |
| 338 | << WhichKind |
| 339 | << NewParam->getDefaultArgRange(); |
| 340 | } |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 344 | if (CheckEquivalentExceptionSpec( |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 345 | Old->getType()->getAs<FunctionProtoType>(), Old->getLocation(), |
| 346 | New->getType()->getAs<FunctionProtoType>(), New->getLocation())) { |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 347 | Invalid = true; |
| 348 | } |
| 349 | |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 350 | return Invalid; |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | /// CheckCXXDefaultArguments - Verify that the default arguments for a |
| 354 | /// function declaration are well-formed according to C++ |
| 355 | /// [dcl.fct.default]. |
| 356 | void Sema::CheckCXXDefaultArguments(FunctionDecl *FD) { |
| 357 | unsigned NumParams = FD->getNumParams(); |
| 358 | unsigned p; |
| 359 | |
| 360 | // Find first parameter with a default argument |
| 361 | for (p = 0; p < NumParams; ++p) { |
| 362 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5f49a0c | 2009-08-25 01:23:32 +0000 | [diff] [blame] | 363 | if (Param->hasDefaultArg()) |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 364 | break; |
| 365 | } |
| 366 | |
| 367 | // C++ [dcl.fct.default]p4: |
| 368 | // In a given function declaration, all parameters |
| 369 | // subsequent to a parameter with a default argument shall |
| 370 | // have default arguments supplied in this or previous |
| 371 | // declarations. A default argument shall not be redefined |
| 372 | // by a later declaration (not even to the same value). |
| 373 | unsigned LastMissingDefaultArg = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 374 | for (; p < NumParams; ++p) { |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 375 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5f49a0c | 2009-08-25 01:23:32 +0000 | [diff] [blame] | 376 | if (!Param->hasDefaultArg()) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 377 | if (Param->isInvalidDecl()) |
| 378 | /* We already complained about this parameter. */; |
| 379 | else if (Param->getIdentifier()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 380 | Diag(Param->getLocation(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 381 | diag::err_param_default_argument_missing_name) |
Chris Lattner | 43b628c | 2008-11-19 07:32:16 +0000 | [diff] [blame] | 382 | << Param->getIdentifier(); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 383 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 384 | Diag(Param->getLocation(), |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 385 | diag::err_param_default_argument_missing); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 386 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 387 | LastMissingDefaultArg = p; |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | if (LastMissingDefaultArg > 0) { |
| 392 | // Some default arguments were missing. Clear out all of the |
| 393 | // default arguments up to (and including) the last missing |
| 394 | // default argument, so that we leave the function parameters |
| 395 | // in a semantically valid state. |
| 396 | for (p = 0; p <= LastMissingDefaultArg; ++p) { |
| 397 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 398 | if (Param->hasDefaultArg()) { |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 399 | if (!Param->hasUnparsedDefaultArg()) |
| 400 | Param->getDefaultArg()->Destroy(Context); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 401 | Param->setDefaultArg(0); |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 406 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 407 | /// isCurrentClassName - Determine whether the identifier II is the |
| 408 | /// name of the class type currently being defined. In the case of |
| 409 | /// nested classes, this will only return true if II is the name of |
| 410 | /// the innermost class. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 411 | bool Sema::isCurrentClassName(const IdentifierInfo &II, Scope *, |
| 412 | const CXXScopeSpec *SS) { |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 413 | CXXRecordDecl *CurDecl; |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 414 | if (SS && SS->isSet() && !SS->isInvalid()) { |
Douglas Gregor | ac373c4 | 2009-08-21 22:16:40 +0000 | [diff] [blame] | 415 | DeclContext *DC = computeDeclContext(*SS, true); |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 416 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(DC); |
| 417 | } else |
| 418 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(CurContext); |
| 419 | |
| 420 | if (CurDecl) |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 421 | return &II == CurDecl->getIdentifier(); |
| 422 | else |
| 423 | return false; |
| 424 | } |
| 425 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 426 | /// \brief Check the validity of a C++ base class specifier. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 427 | /// |
| 428 | /// \returns a new CXXBaseSpecifier if well-formed, emits diagnostics |
| 429 | /// and returns NULL otherwise. |
| 430 | CXXBaseSpecifier * |
| 431 | Sema::CheckBaseSpecifier(CXXRecordDecl *Class, |
| 432 | SourceRange SpecifierRange, |
| 433 | bool Virtual, AccessSpecifier Access, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 434 | QualType BaseType, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 435 | SourceLocation BaseLoc) { |
| 436 | // C++ [class.union]p1: |
| 437 | // A union shall not have base classes. |
| 438 | if (Class->isUnion()) { |
| 439 | Diag(Class->getLocation(), diag::err_base_clause_on_union) |
| 440 | << SpecifierRange; |
| 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | if (BaseType->isDependentType()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 445 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 446 | Class->getTagKind() == RecordDecl::TK_class, |
| 447 | Access, BaseType); |
| 448 | |
| 449 | // Base specifiers must be record types. |
| 450 | if (!BaseType->isRecordType()) { |
| 451 | Diag(BaseLoc, diag::err_base_must_be_class) << SpecifierRange; |
| 452 | return 0; |
| 453 | } |
| 454 | |
| 455 | // C++ [class.union]p1: |
| 456 | // A union shall not be used as a base class. |
| 457 | if (BaseType->isUnionType()) { |
| 458 | Diag(BaseLoc, diag::err_union_as_base_class) << SpecifierRange; |
| 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | // C++ [class.derived]p2: |
| 463 | // The class-name in a base-specifier shall not be an incompletely |
| 464 | // defined class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 465 | if (RequireCompleteType(BaseLoc, BaseType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 466 | PDiag(diag::err_incomplete_base_class) |
| 467 | << SpecifierRange)) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 468 | return 0; |
| 469 | |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 470 | // 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] | 471 | RecordDecl *BaseDecl = BaseType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 472 | assert(BaseDecl && "Record type has no declaration"); |
| 473 | BaseDecl = BaseDecl->getDefinition(Context); |
| 474 | assert(BaseDecl && "Base type is not incomplete, but has no definition"); |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 475 | CXXRecordDecl * CXXBaseDecl = cast<CXXRecordDecl>(BaseDecl); |
| 476 | assert(CXXBaseDecl && "Base type is not a C++ type"); |
| 477 | if (!CXXBaseDecl->isEmpty()) |
| 478 | Class->setEmpty(false); |
| 479 | if (CXXBaseDecl->isPolymorphic()) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 480 | Class->setPolymorphic(true); |
| 481 | |
| 482 | // C++ [dcl.init.aggr]p1: |
| 483 | // An aggregate is [...] a class with [...] no base classes [...]. |
| 484 | Class->setAggregate(false); |
| 485 | Class->setPOD(false); |
| 486 | |
Anders Carlsson | 347ba89 | 2009-04-16 00:08:20 +0000 | [diff] [blame] | 487 | if (Virtual) { |
| 488 | // C++ [class.ctor]p5: |
| 489 | // A constructor is trivial if its class has no virtual base classes. |
| 490 | Class->setHasTrivialConstructor(false); |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 491 | |
| 492 | // C++ [class.copy]p6: |
| 493 | // A copy constructor is trivial if its class has no virtual base classes. |
| 494 | Class->setHasTrivialCopyConstructor(false); |
| 495 | |
| 496 | // C++ [class.copy]p11: |
| 497 | // A copy assignment operator is trivial if its class has no virtual |
| 498 | // base classes. |
| 499 | Class->setHasTrivialCopyAssignment(false); |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 500 | |
| 501 | // C++0x [meta.unary.prop] is_empty: |
| 502 | // T is a class type, but not a union type, with ... no virtual base |
| 503 | // classes |
| 504 | Class->setEmpty(false); |
Anders Carlsson | 347ba89 | 2009-04-16 00:08:20 +0000 | [diff] [blame] | 505 | } else { |
| 506 | // C++ [class.ctor]p5: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 507 | // A constructor is trivial if all the direct base classes of its |
Anders Carlsson | 347ba89 | 2009-04-16 00:08:20 +0000 | [diff] [blame] | 508 | // class have trivial constructors. |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 509 | if (!cast<CXXRecordDecl>(BaseDecl)->hasTrivialConstructor()) |
| 510 | Class->setHasTrivialConstructor(false); |
| 511 | |
| 512 | // C++ [class.copy]p6: |
| 513 | // A copy constructor is trivial if all the direct base classes of its |
| 514 | // class have trivial copy constructors. |
| 515 | if (!cast<CXXRecordDecl>(BaseDecl)->hasTrivialCopyConstructor()) |
| 516 | Class->setHasTrivialCopyConstructor(false); |
| 517 | |
| 518 | // C++ [class.copy]p11: |
| 519 | // A copy assignment operator is trivial if all the direct base classes |
| 520 | // of its class have trivial copy assignment operators. |
| 521 | if (!cast<CXXRecordDecl>(BaseDecl)->hasTrivialCopyAssignment()) |
| 522 | Class->setHasTrivialCopyAssignment(false); |
Anders Carlsson | 347ba89 | 2009-04-16 00:08:20 +0000 | [diff] [blame] | 523 | } |
Anders Carlsson | 072abef | 2009-04-17 02:34:54 +0000 | [diff] [blame] | 524 | |
| 525 | // C++ [class.ctor]p3: |
| 526 | // A destructor is trivial if all the direct base classes of its class |
| 527 | // have trivial destructors. |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 528 | if (!cast<CXXRecordDecl>(BaseDecl)->hasTrivialDestructor()) |
| 529 | Class->setHasTrivialDestructor(false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 530 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 531 | // Create the base specifier. |
| 532 | // FIXME: Allocate via ASTContext? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 533 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
| 534 | Class->getTagKind() == RecordDecl::TK_class, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 535 | Access, BaseType); |
| 536 | } |
| 537 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 538 | /// ActOnBaseSpecifier - Parsed a base specifier. A base specifier is |
| 539 | /// one entry in the base class list of a class specifier, for |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 540 | /// example: |
| 541 | /// class foo : public bar, virtual private baz { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 542 | /// 'public bar' and 'virtual private baz' are each base-specifiers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 543 | Sema::BaseResult |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 544 | Sema::ActOnBaseSpecifier(DeclPtrTy classdecl, SourceRange SpecifierRange, |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 545 | bool Virtual, AccessSpecifier Access, |
| 546 | TypeTy *basetype, SourceLocation BaseLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 547 | if (!classdecl) |
| 548 | return true; |
| 549 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 550 | AdjustDeclIfTemplate(classdecl); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 551 | CXXRecordDecl *Class = cast<CXXRecordDecl>(classdecl.getAs<Decl>()); |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 552 | QualType BaseType = GetTypeFromParser(basetype); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 553 | if (CXXBaseSpecifier *BaseSpec = CheckBaseSpecifier(Class, SpecifierRange, |
| 554 | Virtual, Access, |
| 555 | BaseType, BaseLoc)) |
| 556 | return BaseSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 557 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 558 | return true; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 559 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 560 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 561 | /// \brief Performs the actual work of attaching the given base class |
| 562 | /// specifiers to a C++ class. |
| 563 | bool Sema::AttachBaseSpecifiers(CXXRecordDecl *Class, CXXBaseSpecifier **Bases, |
| 564 | unsigned NumBases) { |
| 565 | if (NumBases == 0) |
| 566 | return false; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 567 | |
| 568 | // Used to keep track of which base types we have already seen, so |
| 569 | // that we can properly diagnose redundant direct base types. Note |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 570 | // that the key is always the unqualified canonical type of the base |
| 571 | // class. |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 572 | std::map<QualType, CXXBaseSpecifier*, QualTypeOrdering> KnownBaseTypes; |
| 573 | |
| 574 | // Copy non-redundant base specifiers into permanent storage. |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 575 | unsigned NumGoodBases = 0; |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 576 | bool Invalid = false; |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 577 | for (unsigned idx = 0; idx < NumBases; ++idx) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 578 | QualType NewBaseType |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 579 | = Context.getCanonicalType(Bases[idx]->getType()); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 580 | NewBaseType = NewBaseType.getUnqualifiedType(); |
| 581 | |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 582 | if (KnownBaseTypes[NewBaseType]) { |
| 583 | // C++ [class.mi]p3: |
| 584 | // A class shall not be specified as a direct base class of a |
| 585 | // derived class more than once. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 586 | Diag(Bases[idx]->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 587 | diag::err_duplicate_base_class) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 588 | << KnownBaseTypes[NewBaseType]->getType() |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 589 | << Bases[idx]->getSourceRange(); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 590 | |
| 591 | // Delete the duplicate base class specifier; we're going to |
| 592 | // overwrite its pointer later. |
Douglas Gregor | 2aef06d | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 593 | Context.Deallocate(Bases[idx]); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 594 | |
| 595 | Invalid = true; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 596 | } else { |
| 597 | // Okay, add this new base class. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 598 | KnownBaseTypes[NewBaseType] = Bases[idx]; |
| 599 | Bases[NumGoodBases++] = Bases[idx]; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | |
| 603 | // Attach the remaining base class specifiers to the derived class. |
Fariborz Jahanian | 5ffcd7b | 2009-07-02 18:26:15 +0000 | [diff] [blame] | 604 | Class->setBases(Context, Bases, NumGoodBases); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 605 | |
| 606 | // Delete the remaining (good) base class specifiers, since their |
| 607 | // data has been copied into the CXXRecordDecl. |
| 608 | for (unsigned idx = 0; idx < NumGoodBases; ++idx) |
Douglas Gregor | 2aef06d | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 609 | Context.Deallocate(Bases[idx]); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 610 | |
| 611 | return Invalid; |
| 612 | } |
| 613 | |
| 614 | /// ActOnBaseSpecifiers - Attach the given base specifiers to the |
| 615 | /// class, after checking whether there are any duplicate base |
| 616 | /// classes. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 617 | void Sema::ActOnBaseSpecifiers(DeclPtrTy ClassDecl, BaseTy **Bases, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 618 | unsigned NumBases) { |
| 619 | if (!ClassDecl || !Bases || !NumBases) |
| 620 | return; |
| 621 | |
| 622 | AdjustDeclIfTemplate(ClassDecl); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 623 | AttachBaseSpecifiers(cast<CXXRecordDecl>(ClassDecl.getAs<Decl>()), |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 624 | (CXXBaseSpecifier**)(Bases), NumBases); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 625 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 626 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 627 | /// \brief Determine whether the type \p Derived is a C++ class that is |
| 628 | /// derived from the type \p Base. |
| 629 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base) { |
| 630 | if (!getLangOptions().CPlusPlus) |
| 631 | return false; |
| 632 | |
| 633 | const RecordType *DerivedRT = Derived->getAs<RecordType>(); |
| 634 | if (!DerivedRT) |
| 635 | return false; |
| 636 | |
| 637 | const RecordType *BaseRT = Base->getAs<RecordType>(); |
| 638 | if (!BaseRT) |
| 639 | return false; |
| 640 | |
| 641 | CXXRecordDecl *DerivedRD = cast<CXXRecordDecl>(DerivedRT->getDecl()); |
| 642 | CXXRecordDecl *BaseRD = cast<CXXRecordDecl>(BaseRT->getDecl()); |
| 643 | return DerivedRD->isDerivedFrom(BaseRD); |
| 644 | } |
| 645 | |
| 646 | /// \brief Determine whether the type \p Derived is a C++ class that is |
| 647 | /// derived from the type \p Base. |
| 648 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base, CXXBasePaths &Paths) { |
| 649 | if (!getLangOptions().CPlusPlus) |
| 650 | return false; |
| 651 | |
| 652 | const RecordType *DerivedRT = Derived->getAs<RecordType>(); |
| 653 | if (!DerivedRT) |
| 654 | return false; |
| 655 | |
| 656 | const RecordType *BaseRT = Base->getAs<RecordType>(); |
| 657 | if (!BaseRT) |
| 658 | return false; |
| 659 | |
| 660 | CXXRecordDecl *DerivedRD = cast<CXXRecordDecl>(DerivedRT->getDecl()); |
| 661 | CXXRecordDecl *BaseRD = cast<CXXRecordDecl>(BaseRT->getDecl()); |
| 662 | return DerivedRD->isDerivedFrom(BaseRD, Paths); |
| 663 | } |
| 664 | |
| 665 | /// CheckDerivedToBaseConversion - Check whether the Derived-to-Base |
| 666 | /// conversion (where Derived and Base are class types) is |
| 667 | /// well-formed, meaning that the conversion is unambiguous (and |
| 668 | /// that all of the base classes are accessible). Returns true |
| 669 | /// and emits a diagnostic if the code is ill-formed, returns false |
| 670 | /// otherwise. Loc is the location where this routine should point to |
| 671 | /// if there is an error, and Range is the source range to highlight |
| 672 | /// if there is an error. |
| 673 | bool |
| 674 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
| 675 | unsigned InaccessibleBaseID, |
| 676 | unsigned AmbigiousBaseConvID, |
| 677 | SourceLocation Loc, SourceRange Range, |
| 678 | DeclarationName Name) { |
| 679 | // First, determine whether the path from Derived to Base is |
| 680 | // ambiguous. This is slightly more expensive than checking whether |
| 681 | // the Derived to Base conversion exists, because here we need to |
| 682 | // explore multiple paths to determine if there is an ambiguity. |
| 683 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 684 | /*DetectVirtual=*/false); |
| 685 | bool DerivationOkay = IsDerivedFrom(Derived, Base, Paths); |
| 686 | assert(DerivationOkay && |
| 687 | "Can only be used with a derived-to-base conversion"); |
| 688 | (void)DerivationOkay; |
| 689 | |
| 690 | if (!Paths.isAmbiguous(Context.getCanonicalType(Base).getUnqualifiedType())) { |
| 691 | // Check that the base class can be accessed. |
| 692 | return CheckBaseClassAccess(Derived, Base, InaccessibleBaseID, Paths, Loc, |
| 693 | Name); |
| 694 | } |
| 695 | |
| 696 | // We know that the derived-to-base conversion is ambiguous, and |
| 697 | // we're going to produce a diagnostic. Perform the derived-to-base |
| 698 | // search just one more time to compute all of the possible paths so |
| 699 | // that we can print them out. This is more expensive than any of |
| 700 | // the previous derived-to-base checks we've done, but at this point |
| 701 | // performance isn't as much of an issue. |
| 702 | Paths.clear(); |
| 703 | Paths.setRecordingPaths(true); |
| 704 | bool StillOkay = IsDerivedFrom(Derived, Base, Paths); |
| 705 | assert(StillOkay && "Can only be used with a derived-to-base conversion"); |
| 706 | (void)StillOkay; |
| 707 | |
| 708 | // Build up a textual representation of the ambiguous paths, e.g., |
| 709 | // D -> B -> A, that will be used to illustrate the ambiguous |
| 710 | // conversions in the diagnostic. We only print one of the paths |
| 711 | // to each base class subobject. |
| 712 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 713 | |
| 714 | Diag(Loc, AmbigiousBaseConvID) |
| 715 | << Derived << Base << PathDisplayStr << Range << Name; |
| 716 | return true; |
| 717 | } |
| 718 | |
| 719 | bool |
| 720 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
| 721 | SourceLocation Loc, SourceRange Range) { |
| 722 | return CheckDerivedToBaseConversion(Derived, Base, |
| 723 | diag::err_conv_to_inaccessible_base, |
| 724 | diag::err_ambiguous_derived_to_base_conv, |
| 725 | Loc, Range, DeclarationName()); |
| 726 | } |
| 727 | |
| 728 | |
| 729 | /// @brief Builds a string representing ambiguous paths from a |
| 730 | /// specific derived class to different subobjects of the same base |
| 731 | /// class. |
| 732 | /// |
| 733 | /// This function builds a string that can be used in error messages |
| 734 | /// to show the different paths that one can take through the |
| 735 | /// inheritance hierarchy to go from the derived class to different |
| 736 | /// subobjects of a base class. The result looks something like this: |
| 737 | /// @code |
| 738 | /// struct D -> struct B -> struct A |
| 739 | /// struct D -> struct C -> struct A |
| 740 | /// @endcode |
| 741 | std::string Sema::getAmbiguousPathsDisplayString(CXXBasePaths &Paths) { |
| 742 | std::string PathDisplayStr; |
| 743 | std::set<unsigned> DisplayedPaths; |
| 744 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
| 745 | Path != Paths.end(); ++Path) { |
| 746 | if (DisplayedPaths.insert(Path->back().SubobjectNumber).second) { |
| 747 | // We haven't displayed a path to this particular base |
| 748 | // class subobject yet. |
| 749 | PathDisplayStr += "\n "; |
| 750 | PathDisplayStr += Context.getTypeDeclType(Paths.getOrigin()).getAsString(); |
| 751 | for (CXXBasePath::const_iterator Element = Path->begin(); |
| 752 | Element != Path->end(); ++Element) |
| 753 | PathDisplayStr += " -> " + Element->Base->getType().getAsString(); |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | return PathDisplayStr; |
| 758 | } |
| 759 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 760 | //===----------------------------------------------------------------------===// |
| 761 | // C++ class member Handling |
| 762 | //===----------------------------------------------------------------------===// |
| 763 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 764 | /// ActOnCXXMemberDeclarator - This is invoked when a C++ class member |
| 765 | /// declarator is parsed. 'AS' is the access specifier, 'BW' specifies the |
| 766 | /// bitfield width if there is one and 'InitExpr' specifies the initializer if |
Chris Lattner | b6688e0 | 2009-04-12 22:37:57 +0000 | [diff] [blame] | 767 | /// any. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 768 | Sema::DeclPtrTy |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 769 | Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D, |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 770 | MultiTemplateParamsArg TemplateParameterLists, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 771 | ExprTy *BW, ExprTy *InitExpr, bool Deleted) { |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 772 | const DeclSpec &DS = D.getDeclSpec(); |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 773 | DeclarationName Name = GetNameForDeclarator(D); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 774 | Expr *BitWidth = static_cast<Expr*>(BW); |
| 775 | Expr *Init = static_cast<Expr*>(InitExpr); |
| 776 | SourceLocation Loc = D.getIdentifierLoc(); |
| 777 | |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 778 | bool isFunc = D.isFunctionDeclarator(); |
| 779 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 780 | assert(!DS.isFriendSpecified()); |
| 781 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 782 | // C++ 9.2p6: A member shall not be declared to have automatic storage |
| 783 | // duration (auto, register) or with the extern storage-class-specifier. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 784 | // C++ 7.1.1p8: The mutable specifier can be applied only to names of class |
| 785 | // data members and cannot be applied to names declared const or static, |
| 786 | // and cannot be applied to reference members. |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 787 | switch (DS.getStorageClassSpec()) { |
| 788 | case DeclSpec::SCS_unspecified: |
| 789 | case DeclSpec::SCS_typedef: |
| 790 | case DeclSpec::SCS_static: |
| 791 | // FALL THROUGH. |
| 792 | break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 793 | case DeclSpec::SCS_mutable: |
| 794 | if (isFunc) { |
| 795 | if (DS.getStorageClassSpecLoc().isValid()) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 796 | Diag(DS.getStorageClassSpecLoc(), diag::err_mutable_function); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 797 | else |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 798 | Diag(DS.getThreadSpecLoc(), diag::err_mutable_function); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 799 | |
Sebastian Redl | a11f42f | 2008-11-17 23:24:37 +0000 | [diff] [blame] | 800 | // FIXME: It would be nicer if the keyword was ignored only for this |
| 801 | // declarator. Otherwise we could get follow-up errors. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 802 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 803 | } else { |
| 804 | QualType T = GetTypeForDeclarator(D, S); |
| 805 | diag::kind err = static_cast<diag::kind>(0); |
| 806 | if (T->isReferenceType()) |
| 807 | err = diag::err_mutable_reference; |
| 808 | else if (T.isConstQualified()) |
| 809 | err = diag::err_mutable_const; |
| 810 | if (err != 0) { |
| 811 | if (DS.getStorageClassSpecLoc().isValid()) |
| 812 | Diag(DS.getStorageClassSpecLoc(), err); |
| 813 | else |
| 814 | Diag(DS.getThreadSpecLoc(), err); |
Sebastian Redl | a11f42f | 2008-11-17 23:24:37 +0000 | [diff] [blame] | 815 | // FIXME: It would be nicer if the keyword was ignored only for this |
| 816 | // declarator. Otherwise we could get follow-up errors. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 817 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 818 | } |
| 819 | } |
| 820 | break; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 821 | default: |
| 822 | if (DS.getStorageClassSpecLoc().isValid()) |
| 823 | Diag(DS.getStorageClassSpecLoc(), |
| 824 | diag::err_storageclass_invalid_for_member); |
| 825 | else |
| 826 | Diag(DS.getThreadSpecLoc(), diag::err_storageclass_invalid_for_member); |
| 827 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 828 | } |
| 829 | |
Argyrios Kyrtzidis | d6caa9e | 2008-10-15 20:23:22 +0000 | [diff] [blame] | 830 | if (!isFunc && |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 831 | D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_typename && |
Argyrios Kyrtzidis | d6caa9e | 2008-10-15 20:23:22 +0000 | [diff] [blame] | 832 | D.getNumTypeObjects() == 0) { |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 833 | // Check also for this case: |
| 834 | // |
| 835 | // typedef int f(); |
| 836 | // f a; |
| 837 | // |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 838 | QualType TDType = GetTypeFromParser(DS.getTypeRep()); |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 839 | isFunc = TDType->isFunctionType(); |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 840 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 841 | |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 842 | bool isInstField = ((DS.getStorageClassSpec() == DeclSpec::SCS_unspecified || |
| 843 | DS.getStorageClassSpec() == DeclSpec::SCS_mutable) && |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 844 | !isFunc); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 845 | |
| 846 | Decl *Member; |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 847 | if (isInstField) { |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 848 | // FIXME: Check for template parameters! |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 849 | Member = HandleField(S, cast<CXXRecordDecl>(CurContext), Loc, D, BitWidth, |
| 850 | AS); |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 851 | assert(Member && "HandleField never returns null"); |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 852 | } else { |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 853 | Member = HandleDeclarator(S, D, move(TemplateParameterLists), false) |
| 854 | .getAs<Decl>(); |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 855 | if (!Member) { |
| 856 | if (BitWidth) DeleteExpr(BitWidth); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 857 | return DeclPtrTy(); |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 858 | } |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 859 | |
| 860 | // Non-instance-fields can't have a bitfield. |
| 861 | if (BitWidth) { |
| 862 | if (Member->isInvalidDecl()) { |
| 863 | // don't emit another diagnostic. |
Douglas Gregor | 2d2e9cf | 2009-03-11 20:22:50 +0000 | [diff] [blame] | 864 | } else if (isa<VarDecl>(Member)) { |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 865 | // C++ 9.6p3: A bit-field shall not be a static member. |
| 866 | // "static member 'A' cannot be a bit-field" |
| 867 | Diag(Loc, diag::err_static_not_bitfield) |
| 868 | << Name << BitWidth->getSourceRange(); |
| 869 | } else if (isa<TypedefDecl>(Member)) { |
| 870 | // "typedef member 'x' cannot be a bit-field" |
| 871 | Diag(Loc, diag::err_typedef_not_bitfield) |
| 872 | << Name << BitWidth->getSourceRange(); |
| 873 | } else { |
| 874 | // A function typedef ("typedef int f(); f a;"). |
| 875 | // C++ 9.6p3: A bit-field shall have integral or enumeration type. |
| 876 | Diag(Loc, diag::err_not_integral_type_bitfield) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 877 | << Name << cast<ValueDecl>(Member)->getType() |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 878 | << BitWidth->getSourceRange(); |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 879 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 880 | |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 881 | DeleteExpr(BitWidth); |
| 882 | BitWidth = 0; |
| 883 | Member->setInvalidDecl(); |
| 884 | } |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 885 | |
| 886 | Member->setAccess(AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 887 | |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 888 | // If we have declared a member function template, set the access of the |
| 889 | // templated declaration as well. |
| 890 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Member)) |
| 891 | FunTmpl->getTemplatedDecl()->setAccess(AS); |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 892 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 893 | |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 894 | assert((Name || isInstField) && "No identifier for non-field ?"); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 895 | |
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 896 | if (Init) |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 897 | AddInitializerToDecl(DeclPtrTy::make(Member), ExprArg(*this, Init), false); |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 898 | if (Deleted) // FIXME: Source location is not very good. |
| 899 | SetDeclDeleted(DeclPtrTy::make(Member), D.getSourceRange().getBegin()); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 900 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 901 | if (isInstField) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 902 | FieldCollector->Add(cast<FieldDecl>(Member)); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 903 | return DeclPtrTy(); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 904 | } |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 905 | return DeclPtrTy::make(Member); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 906 | } |
| 907 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 908 | /// ActOnMemInitializer - Handle a C++ member initializer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 909 | Sema::MemInitResult |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 910 | Sema::ActOnMemInitializer(DeclPtrTy ConstructorD, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 911 | Scope *S, |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 912 | const CXXScopeSpec &SS, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 913 | IdentifierInfo *MemberOrBase, |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 914 | TypeTy *TemplateTypeTy, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 915 | SourceLocation IdLoc, |
| 916 | SourceLocation LParenLoc, |
| 917 | ExprTy **Args, unsigned NumArgs, |
| 918 | SourceLocation *CommaLocs, |
| 919 | SourceLocation RParenLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 920 | if (!ConstructorD) |
| 921 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 922 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 923 | AdjustDeclIfTemplate(ConstructorD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 924 | |
| 925 | CXXConstructorDecl *Constructor |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 926 | = dyn_cast<CXXConstructorDecl>(ConstructorD.getAs<Decl>()); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 927 | if (!Constructor) { |
| 928 | // The user wrote a constructor initializer on a function that is |
| 929 | // not a C++ constructor. Ignore the error for now, because we may |
| 930 | // have more member initializers coming; we'll diagnose it just |
| 931 | // once in ActOnMemInitializers. |
| 932 | return true; |
| 933 | } |
| 934 | |
| 935 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 936 | |
| 937 | // C++ [class.base.init]p2: |
| 938 | // Names in a mem-initializer-id are looked up in the scope of the |
| 939 | // constructor’s class and, if not found in that scope, are looked |
| 940 | // up in the scope containing the constructor’s |
| 941 | // definition. [Note: if the constructor’s class contains a member |
| 942 | // with the same name as a direct or virtual base class of the |
| 943 | // class, a mem-initializer-id naming the member or base class and |
| 944 | // composed of a single identifier refers to the class member. A |
| 945 | // mem-initializer-id for the hidden base class may be specified |
| 946 | // using a qualified name. ] |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 947 | if (!SS.getScopeRep() && !TemplateTypeTy) { |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 948 | // Look for a member, first. |
| 949 | FieldDecl *Member = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 950 | DeclContext::lookup_result Result |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 951 | = ClassDecl->lookup(MemberOrBase); |
| 952 | if (Result.first != Result.second) |
| 953 | Member = dyn_cast<FieldDecl>(*Result.first); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 954 | |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 955 | // FIXME: Handle members of an anonymous union. |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 956 | |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 957 | if (Member) |
| 958 | return BuildMemberInitializer(Member, (Expr**)Args, NumArgs, IdLoc, |
| 959 | RParenLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 960 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 961 | // It didn't name a member, so see if it names a class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 962 | TypeTy *BaseTy = TemplateTypeTy ? TemplateTypeTy |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 963 | : getTypeName(*MemberOrBase, IdLoc, S, &SS); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 964 | if (!BaseTy) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 965 | return Diag(IdLoc, diag::err_mem_init_not_member_or_class) |
| 966 | << MemberOrBase << SourceRange(IdLoc, RParenLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 967 | |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 968 | QualType BaseType = GetTypeFromParser(BaseTy); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 969 | |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 970 | return BuildBaseInitializer(BaseType, (Expr **)Args, NumArgs, IdLoc, |
| 971 | RParenLoc, ClassDecl); |
| 972 | } |
| 973 | |
| 974 | Sema::MemInitResult |
| 975 | Sema::BuildMemberInitializer(FieldDecl *Member, Expr **Args, |
| 976 | unsigned NumArgs, SourceLocation IdLoc, |
| 977 | SourceLocation RParenLoc) { |
| 978 | bool HasDependentArg = false; |
| 979 | for (unsigned i = 0; i < NumArgs; i++) |
| 980 | HasDependentArg |= Args[i]->isTypeDependent(); |
| 981 | |
| 982 | CXXConstructorDecl *C = 0; |
| 983 | QualType FieldType = Member->getType(); |
| 984 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 985 | FieldType = Array->getElementType(); |
| 986 | if (FieldType->isDependentType()) { |
| 987 | // Can't check init for dependent type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 988 | } else if (FieldType->getAs<RecordType>()) { |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 989 | if (!HasDependentArg) { |
| 990 | ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this); |
| 991 | |
| 992 | C = PerformInitializationByConstructor(FieldType, |
| 993 | MultiExprArg(*this, |
| 994 | (void**)Args, |
| 995 | NumArgs), |
| 996 | IdLoc, |
| 997 | SourceRange(IdLoc, RParenLoc), |
| 998 | Member->getDeclName(), IK_Direct, |
| 999 | ConstructorArgs); |
| 1000 | |
| 1001 | if (C) { |
| 1002 | // Take over the constructor arguments as our own. |
| 1003 | NumArgs = ConstructorArgs.size(); |
| 1004 | Args = (Expr **)ConstructorArgs.take(); |
| 1005 | } |
| 1006 | } |
Fariborz Jahanian | 636a0ff | 2009-09-02 17:10:17 +0000 | [diff] [blame] | 1007 | } else if (NumArgs != 1 && NumArgs != 0) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1008 | return Diag(IdLoc, diag::err_mem_initializer_mismatch) |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1009 | << Member->getDeclName() << SourceRange(IdLoc, RParenLoc); |
| 1010 | } else if (!HasDependentArg) { |
Fariborz Jahanian | 636a0ff | 2009-09-02 17:10:17 +0000 | [diff] [blame] | 1011 | Expr *NewExp; |
| 1012 | if (NumArgs == 0) { |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1013 | if (FieldType->isReferenceType()) { |
| 1014 | Diag(IdLoc, diag::err_null_intialized_reference_member) |
| 1015 | << Member->getDeclName(); |
| 1016 | return Diag(Member->getLocation(), diag::note_declared_at); |
| 1017 | } |
Fariborz Jahanian | 636a0ff | 2009-09-02 17:10:17 +0000 | [diff] [blame] | 1018 | NewExp = new (Context) CXXZeroInitValueExpr(FieldType, IdLoc, RParenLoc); |
| 1019 | NumArgs = 1; |
| 1020 | } |
| 1021 | else |
| 1022 | NewExp = (Expr*)Args[0]; |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1023 | if (PerformCopyInitialization(NewExp, FieldType, "passing")) |
| 1024 | return true; |
| 1025 | Args[0] = NewExp; |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1026 | } |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1027 | // FIXME: Perform direct initialization of the member. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1028 | return new (Context) CXXBaseOrMemberInitializer(Member, (Expr **)Args, |
Anders Carlsson | 8c57a66 | 2009-08-29 01:31:33 +0000 | [diff] [blame] | 1029 | NumArgs, C, IdLoc, RParenLoc); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | Sema::MemInitResult |
| 1033 | Sema::BuildBaseInitializer(QualType BaseType, Expr **Args, |
| 1034 | unsigned NumArgs, SourceLocation IdLoc, |
| 1035 | SourceLocation RParenLoc, CXXRecordDecl *ClassDecl) { |
| 1036 | bool HasDependentArg = false; |
| 1037 | for (unsigned i = 0; i < NumArgs; i++) |
| 1038 | HasDependentArg |= Args[i]->isTypeDependent(); |
| 1039 | |
| 1040 | if (!BaseType->isDependentType()) { |
| 1041 | if (!BaseType->isRecordType()) |
| 1042 | return Diag(IdLoc, diag::err_base_init_does_not_name_class) |
| 1043 | << BaseType << SourceRange(IdLoc, RParenLoc); |
| 1044 | |
| 1045 | // C++ [class.base.init]p2: |
| 1046 | // [...] Unless the mem-initializer-id names a nonstatic data |
| 1047 | // member of the constructor’s class or a direct or virtual base |
| 1048 | // of that class, the mem-initializer is ill-formed. A |
| 1049 | // mem-initializer-list can initialize a base class using any |
| 1050 | // name that denotes that base class type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1051 | |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1052 | // First, check for a direct base class. |
| 1053 | const CXXBaseSpecifier *DirectBaseSpec = 0; |
| 1054 | for (CXXRecordDecl::base_class_const_iterator Base = |
| 1055 | ClassDecl->bases_begin(); Base != ClassDecl->bases_end(); ++Base) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1056 | if (Context.getCanonicalType(BaseType).getUnqualifiedType() == |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1057 | Context.getCanonicalType(Base->getType()).getUnqualifiedType()) { |
| 1058 | // We found a direct base of this type. That's what we're |
| 1059 | // initializing. |
| 1060 | DirectBaseSpec = &*Base; |
| 1061 | break; |
| 1062 | } |
| 1063 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1064 | |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1065 | // Check for a virtual base class. |
| 1066 | // FIXME: We might be able to short-circuit this if we know in advance that |
| 1067 | // there are no virtual bases. |
| 1068 | const CXXBaseSpecifier *VirtualBaseSpec = 0; |
| 1069 | if (!DirectBaseSpec || !DirectBaseSpec->isVirtual()) { |
| 1070 | // We haven't found a base yet; search the class hierarchy for a |
| 1071 | // virtual base class. |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1072 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 1073 | /*DetectVirtual=*/false); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1074 | if (IsDerivedFrom(Context.getTypeDeclType(ClassDecl), BaseType, Paths)) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1075 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1076 | Path != Paths.end(); ++Path) { |
| 1077 | if (Path->back().Base->isVirtual()) { |
| 1078 | VirtualBaseSpec = Path->back().Base; |
| 1079 | break; |
| 1080 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1081 | } |
| 1082 | } |
| 1083 | } |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1084 | |
| 1085 | // C++ [base.class.init]p2: |
| 1086 | // If a mem-initializer-id is ambiguous because it designates both |
| 1087 | // a direct non-virtual base class and an inherited virtual base |
| 1088 | // class, the mem-initializer is ill-formed. |
| 1089 | if (DirectBaseSpec && VirtualBaseSpec) |
| 1090 | return Diag(IdLoc, diag::err_base_init_direct_and_virtual) |
| 1091 | << BaseType << SourceRange(IdLoc, RParenLoc); |
| 1092 | // C++ [base.class.init]p2: |
| 1093 | // Unless the mem-initializer-id names a nonstatic data membeer of the |
| 1094 | // constructor's class ot a direst or virtual base of that class, the |
| 1095 | // mem-initializer is ill-formed. |
| 1096 | if (!DirectBaseSpec && !VirtualBaseSpec) |
| 1097 | return Diag(IdLoc, diag::err_not_direct_base_or_virtual) |
| 1098 | << BaseType << ClassDecl->getNameAsCString() |
| 1099 | << SourceRange(IdLoc, RParenLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
Fariborz Jahanian | d7b27e1 | 2009-07-23 00:42:24 +0000 | [diff] [blame] | 1102 | CXXConstructorDecl *C = 0; |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1103 | if (!BaseType->isDependentType() && !HasDependentArg) { |
| 1104 | DeclarationName Name = Context.DeclarationNames.getCXXConstructorName( |
| 1105 | Context.getCanonicalType(BaseType)); |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 1106 | ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this); |
| 1107 | |
| 1108 | C = PerformInitializationByConstructor(BaseType, |
| 1109 | MultiExprArg(*this, |
| 1110 | (void**)Args, NumArgs), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1111 | IdLoc, SourceRange(IdLoc, RParenLoc), |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 1112 | Name, IK_Direct, |
| 1113 | ConstructorArgs); |
| 1114 | if (C) { |
| 1115 | // Take over the constructor arguments as our own. |
| 1116 | NumArgs = ConstructorArgs.size(); |
| 1117 | Args = (Expr **)ConstructorArgs.take(); |
| 1118 | } |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1121 | return new (Context) CXXBaseOrMemberInitializer(BaseType, (Expr **)Args, |
Anders Carlsson | 8c57a66 | 2009-08-29 01:31:33 +0000 | [diff] [blame] | 1122 | NumArgs, C, IdLoc, RParenLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
Fariborz Jahanian | 87595e4 | 2009-07-23 23:32:59 +0000 | [diff] [blame] | 1125 | void |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1126 | Sema::SetBaseOrMemberInitializers(CXXConstructorDecl *Constructor, |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1127 | CXXBaseOrMemberInitializer **Initializers, |
| 1128 | unsigned NumInitializers, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1129 | llvm::SmallVectorImpl<CXXBaseSpecifier *>& Bases, |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1130 | llvm::SmallVectorImpl<FieldDecl *>&Fields) { |
| 1131 | // We need to build the initializer AST according to order of construction |
| 1132 | // and not what user specified in the Initializers list. |
| 1133 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Constructor->getDeclContext()); |
| 1134 | llvm::SmallVector<CXXBaseOrMemberInitializer*, 32> AllToInit; |
| 1135 | llvm::DenseMap<const void *, CXXBaseOrMemberInitializer*> AllBaseFields; |
| 1136 | bool HasDependentBaseInit = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1137 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1138 | for (unsigned i = 0; i < NumInitializers; i++) { |
| 1139 | CXXBaseOrMemberInitializer *Member = Initializers[i]; |
| 1140 | if (Member->isBaseInitializer()) { |
| 1141 | if (Member->getBaseClass()->isDependentType()) |
| 1142 | HasDependentBaseInit = true; |
| 1143 | AllBaseFields[Member->getBaseClass()->getAs<RecordType>()] = Member; |
| 1144 | } else { |
| 1145 | AllBaseFields[Member->getMember()] = Member; |
| 1146 | } |
| 1147 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1148 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1149 | if (HasDependentBaseInit) { |
| 1150 | // FIXME. This does not preserve the ordering of the initializers. |
| 1151 | // Try (with -Wreorder) |
| 1152 | // template<class X> struct A {}; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1153 | // template<class X> struct B : A<X> { |
| 1154 | // B() : x1(10), A<X>() {} |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1155 | // int x1; |
| 1156 | // }; |
| 1157 | // B<int> x; |
| 1158 | // On seeing one dependent type, we should essentially exit this routine |
| 1159 | // while preserving user-declared initializer list. When this routine is |
| 1160 | // called during instantiatiation process, this routine will rebuild the |
| 1161 | // oderdered initializer list correctly. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1162 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1163 | // If we have a dependent base initialization, we can't determine the |
| 1164 | // association between initializers and bases; just dump the known |
| 1165 | // initializers into the list, and don't try to deal with other bases. |
| 1166 | for (unsigned i = 0; i < NumInitializers; i++) { |
| 1167 | CXXBaseOrMemberInitializer *Member = Initializers[i]; |
| 1168 | if (Member->isBaseInitializer()) |
| 1169 | AllToInit.push_back(Member); |
| 1170 | } |
| 1171 | } else { |
| 1172 | // Push virtual bases before others. |
| 1173 | for (CXXRecordDecl::base_class_iterator VBase = |
| 1174 | ClassDecl->vbases_begin(), |
| 1175 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
| 1176 | if (VBase->getType()->isDependentType()) |
| 1177 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1178 | if (CXXBaseOrMemberInitializer *Value = |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 1179 | AllBaseFields.lookup(VBase->getType()->getAs<RecordType>())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1180 | CXXRecordDecl *BaseDecl = |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 1181 | cast<CXXRecordDecl>(VBase->getType()->getAs<RecordType>()->getDecl()); |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1182 | assert(BaseDecl && "SetBaseOrMemberInitializers - BaseDecl null"); |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 1183 | if (CXXConstructorDecl *Ctor = BaseDecl->getDefaultConstructor(Context)) |
| 1184 | MarkDeclarationReferenced(Value->getSourceLocation(), Ctor); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1185 | AllToInit.push_back(Value); |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 1186 | } |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1187 | else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1188 | CXXRecordDecl *VBaseDecl = |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1189 | cast<CXXRecordDecl>(VBase->getType()->getAs<RecordType>()->getDecl()); |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1190 | assert(VBaseDecl && "SetBaseOrMemberInitializers - VBaseDecl null"); |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 1191 | CXXConstructorDecl *Ctor = VBaseDecl->getDefaultConstructor(Context); |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1192 | if (!Ctor) { |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1193 | Bases.push_back(VBase); |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1194 | continue; |
| 1195 | } |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 1196 | |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1197 | ASTOwningVector<&ActionBase::DeleteExpr> CtorArgs(*this); |
| 1198 | if (CompleteConstructorCall(Ctor, MultiExprArg(*this, 0, 0), |
| 1199 | Constructor->getLocation(), CtorArgs)) |
| 1200 | continue; |
| 1201 | |
| 1202 | MarkDeclarationReferenced(Constructor->getLocation(), Ctor); |
| 1203 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1204 | CXXBaseOrMemberInitializer *Member = |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1205 | new (Context) CXXBaseOrMemberInitializer(VBase->getType(), |
| 1206 | CtorArgs.takeAs<Expr>(), |
| 1207 | CtorArgs.size(), Ctor, |
| 1208 | SourceLocation(), |
| 1209 | SourceLocation()); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1210 | AllToInit.push_back(Member); |
| 1211 | } |
| 1212 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1213 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1214 | for (CXXRecordDecl::base_class_iterator Base = |
| 1215 | ClassDecl->bases_begin(), |
| 1216 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 1217 | // Virtuals are in the virtual base list and already constructed. |
| 1218 | if (Base->isVirtual()) |
| 1219 | continue; |
| 1220 | // Skip dependent types. |
| 1221 | if (Base->getType()->isDependentType()) |
| 1222 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1223 | if (CXXBaseOrMemberInitializer *Value = |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 1224 | AllBaseFields.lookup(Base->getType()->getAs<RecordType>())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1225 | CXXRecordDecl *BaseDecl = |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 1226 | cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1227 | assert(BaseDecl && "SetBaseOrMemberInitializers - BaseDecl null"); |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 1228 | if (CXXConstructorDecl *Ctor = BaseDecl->getDefaultConstructor(Context)) |
| 1229 | MarkDeclarationReferenced(Value->getSourceLocation(), Ctor); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1230 | AllToInit.push_back(Value); |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 1231 | } |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1232 | else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1233 | CXXRecordDecl *BaseDecl = |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 1234 | cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1235 | assert(BaseDecl && "SetBaseOrMemberInitializers - BaseDecl null"); |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 1236 | CXXConstructorDecl *Ctor = BaseDecl->getDefaultConstructor(Context); |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1237 | if (!Ctor) { |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1238 | Bases.push_back(Base); |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1239 | continue; |
| 1240 | } |
| 1241 | |
| 1242 | ASTOwningVector<&ActionBase::DeleteExpr> CtorArgs(*this); |
| 1243 | if (CompleteConstructorCall(Ctor, MultiExprArg(*this, 0, 0), |
| 1244 | Constructor->getLocation(), CtorArgs)) |
| 1245 | continue; |
| 1246 | |
| 1247 | MarkDeclarationReferenced(Constructor->getLocation(), Ctor); |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 1248 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1249 | CXXBaseOrMemberInitializer *Member = |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1250 | new (Context) CXXBaseOrMemberInitializer(Base->getType(), |
| 1251 | CtorArgs.takeAs<Expr>(), |
| 1252 | CtorArgs.size(), Ctor, |
| 1253 | SourceLocation(), |
| 1254 | SourceLocation()); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1255 | AllToInit.push_back(Member); |
| 1256 | } |
| 1257 | } |
| 1258 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1259 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1260 | // non-static data members. |
| 1261 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 1262 | E = ClassDecl->field_end(); Field != E; ++Field) { |
| 1263 | if ((*Field)->isAnonymousStructOrUnion()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1264 | if (const RecordType *FieldClassType = |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1265 | Field->getType()->getAs<RecordType>()) { |
| 1266 | CXXRecordDecl *FieldClassDecl |
| 1267 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1268 | for (RecordDecl::field_iterator FA = FieldClassDecl->field_begin(), |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1269 | EA = FieldClassDecl->field_end(); FA != EA; FA++) { |
| 1270 | if (CXXBaseOrMemberInitializer *Value = AllBaseFields.lookup(*FA)) { |
| 1271 | // 'Member' is the anonymous union field and 'AnonUnionMember' is |
| 1272 | // set to the anonymous union data member used in the initializer |
| 1273 | // list. |
| 1274 | Value->setMember(*Field); |
| 1275 | Value->setAnonUnionMember(*FA); |
| 1276 | AllToInit.push_back(Value); |
| 1277 | break; |
| 1278 | } |
| 1279 | } |
| 1280 | } |
| 1281 | continue; |
| 1282 | } |
| 1283 | if (CXXBaseOrMemberInitializer *Value = AllBaseFields.lookup(*Field)) { |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 1284 | QualType FT = (*Field)->getType(); |
| 1285 | if (const RecordType* RT = FT->getAs<RecordType>()) { |
| 1286 | CXXRecordDecl *FieldRecDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1287 | assert(FieldRecDecl && "SetBaseOrMemberInitializers - BaseDecl null"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1288 | if (CXXConstructorDecl *Ctor = |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 1289 | FieldRecDecl->getDefaultConstructor(Context)) |
| 1290 | MarkDeclarationReferenced(Value->getSourceLocation(), Ctor); |
| 1291 | } |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1292 | AllToInit.push_back(Value); |
| 1293 | continue; |
| 1294 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1295 | |
Douglas Gregor | 1fe6b91 | 2009-11-04 17:16:11 +0000 | [diff] [blame^] | 1296 | if ((*Field)->getType()->isDependentType()) { |
| 1297 | Fields.push_back(*Field); |
| 1298 | continue; |
| 1299 | } |
| 1300 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1301 | QualType FT = Context.getBaseElementType((*Field)->getType()); |
| 1302 | if (const RecordType* RT = FT->getAs<RecordType>()) { |
| 1303 | CXXConstructorDecl *Ctor = |
| 1304 | cast<CXXRecordDecl>(RT->getDecl())->getDefaultConstructor(Context); |
Douglas Gregor | 1fe6b91 | 2009-11-04 17:16:11 +0000 | [diff] [blame^] | 1305 | if (!Ctor) { |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1306 | Fields.push_back(*Field); |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1307 | continue; |
| 1308 | } |
| 1309 | |
| 1310 | ASTOwningVector<&ActionBase::DeleteExpr> CtorArgs(*this); |
| 1311 | if (CompleteConstructorCall(Ctor, MultiExprArg(*this, 0, 0), |
| 1312 | Constructor->getLocation(), CtorArgs)) |
| 1313 | continue; |
| 1314 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1315 | CXXBaseOrMemberInitializer *Member = |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1316 | new (Context) CXXBaseOrMemberInitializer(*Field,CtorArgs.takeAs<Expr>(), |
| 1317 | CtorArgs.size(), Ctor, |
| 1318 | SourceLocation(), |
| 1319 | SourceLocation()); |
| 1320 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1321 | AllToInit.push_back(Member); |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 1322 | if (Ctor) |
| 1323 | MarkDeclarationReferenced(Constructor->getLocation(), Ctor); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1324 | if (FT.isConstQualified() && (!Ctor || Ctor->isTrivial())) { |
| 1325 | Diag(Constructor->getLocation(), diag::err_unintialized_member_in_ctor) |
| 1326 | << Context.getTagDeclType(ClassDecl) << 1 << (*Field)->getDeclName(); |
| 1327 | Diag((*Field)->getLocation(), diag::note_declared_at); |
| 1328 | } |
| 1329 | } |
| 1330 | else if (FT->isReferenceType()) { |
| 1331 | Diag(Constructor->getLocation(), diag::err_unintialized_member_in_ctor) |
| 1332 | << Context.getTagDeclType(ClassDecl) << 0 << (*Field)->getDeclName(); |
| 1333 | Diag((*Field)->getLocation(), diag::note_declared_at); |
| 1334 | } |
| 1335 | else if (FT.isConstQualified()) { |
| 1336 | Diag(Constructor->getLocation(), diag::err_unintialized_member_in_ctor) |
| 1337 | << Context.getTagDeclType(ClassDecl) << 1 << (*Field)->getDeclName(); |
| 1338 | Diag((*Field)->getLocation(), diag::note_declared_at); |
| 1339 | } |
| 1340 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1341 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1342 | NumInitializers = AllToInit.size(); |
| 1343 | if (NumInitializers > 0) { |
| 1344 | Constructor->setNumBaseOrMemberInitializers(NumInitializers); |
| 1345 | CXXBaseOrMemberInitializer **baseOrMemberInitializers = |
| 1346 | new (Context) CXXBaseOrMemberInitializer*[NumInitializers]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1347 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1348 | Constructor->setBaseOrMemberInitializers(baseOrMemberInitializers); |
| 1349 | for (unsigned Idx = 0; Idx < NumInitializers; ++Idx) |
| 1350 | baseOrMemberInitializers[Idx] = AllToInit[Idx]; |
| 1351 | } |
| 1352 | } |
| 1353 | |
| 1354 | void |
Fariborz Jahanian | 87595e4 | 2009-07-23 23:32:59 +0000 | [diff] [blame] | 1355 | Sema::BuildBaseOrMemberInitializers(ASTContext &C, |
| 1356 | CXXConstructorDecl *Constructor, |
| 1357 | CXXBaseOrMemberInitializer **Initializers, |
| 1358 | unsigned NumInitializers |
| 1359 | ) { |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1360 | llvm::SmallVector<CXXBaseSpecifier *, 4> Bases; |
| 1361 | llvm::SmallVector<FieldDecl *, 4> Members; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1362 | |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1363 | SetBaseOrMemberInitializers(Constructor, |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1364 | Initializers, NumInitializers, Bases, Members); |
Douglas Gregor | 1fe6b91 | 2009-11-04 17:16:11 +0000 | [diff] [blame^] | 1365 | for (unsigned int i = 0; i < Bases.size(); i++) { |
| 1366 | if (!Bases[i]->getType()->isDependentType()) |
| 1367 | Diag(Bases[i]->getSourceRange().getBegin(), |
| 1368 | diag::err_missing_default_constructor) << 0 << Bases[i]->getType(); |
| 1369 | } |
| 1370 | for (unsigned int i = 0; i < Members.size(); i++) { |
| 1371 | if (!Members[i]->getType()->isDependentType()) |
| 1372 | Diag(Members[i]->getLocation(), diag::err_missing_default_constructor) |
| 1373 | << 1 << Members[i]->getType(); |
| 1374 | } |
Fariborz Jahanian | 87595e4 | 2009-07-23 23:32:59 +0000 | [diff] [blame] | 1375 | } |
| 1376 | |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1377 | static void *GetKeyForTopLevelField(FieldDecl *Field) { |
| 1378 | // For anonymous unions, use the class declaration as the key. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1379 | if (const RecordType *RT = Field->getType()->getAs<RecordType>()) { |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1380 | if (RT->getDecl()->isAnonymousStructOrUnion()) |
| 1381 | return static_cast<void *>(RT->getDecl()); |
| 1382 | } |
| 1383 | return static_cast<void *>(Field); |
| 1384 | } |
| 1385 | |
Anders Carlsson | cdc83c7 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 1386 | static void *GetKeyForBase(QualType BaseType) { |
| 1387 | if (const RecordType *RT = BaseType->getAs<RecordType>()) |
| 1388 | return (void *)RT; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1389 | |
Anders Carlsson | cdc83c7 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 1390 | assert(0 && "Unexpected base type!"); |
| 1391 | return 0; |
| 1392 | } |
| 1393 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1394 | static void *GetKeyForMember(CXXBaseOrMemberInitializer *Member, |
Anders Carlsson | cdc83c7 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 1395 | bool MemberMaybeAnon = false) { |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1396 | // For fields injected into the class via declaration of an anonymous union, |
| 1397 | // use its anonymous union class declaration as the unique key. |
Anders Carlsson | cdc83c7 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 1398 | if (Member->isMemberInitializer()) { |
| 1399 | FieldDecl *Field = Member->getMember(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1400 | |
Fariborz Jahanian | e649412 | 2009-08-11 18:49:54 +0000 | [diff] [blame] | 1401 | // After BuildBaseOrMemberInitializers call, Field is the anonymous union |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1402 | // data member of the class. Data member used in the initializer list is |
Fariborz Jahanian | e649412 | 2009-08-11 18:49:54 +0000 | [diff] [blame] | 1403 | // in AnonUnionMember field. |
| 1404 | if (MemberMaybeAnon && Field->isAnonymousStructOrUnion()) |
| 1405 | Field = Member->getAnonUnionMember(); |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1406 | if (Field->getDeclContext()->isRecord()) { |
| 1407 | RecordDecl *RD = cast<RecordDecl>(Field->getDeclContext()); |
| 1408 | if (RD->isAnonymousStructOrUnion()) |
| 1409 | return static_cast<void *>(RD); |
| 1410 | } |
| 1411 | return static_cast<void *>(Field); |
| 1412 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1413 | |
Anders Carlsson | cdc83c7 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 1414 | return GetKeyForBase(QualType(Member->getBaseClass(), 0)); |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1415 | } |
| 1416 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1417 | void Sema::ActOnMemInitializers(DeclPtrTy ConstructorDecl, |
Anders Carlsson | a7b3521 | 2009-03-25 02:58:17 +0000 | [diff] [blame] | 1418 | SourceLocation ColonLoc, |
| 1419 | MemInitTy **MemInits, unsigned NumMemInits) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 1420 | if (!ConstructorDecl) |
| 1421 | return; |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 1422 | |
| 1423 | AdjustDeclIfTemplate(ConstructorDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1424 | |
| 1425 | CXXConstructorDecl *Constructor |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 1426 | = dyn_cast<CXXConstructorDecl>(ConstructorDecl.getAs<Decl>()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1427 | |
Anders Carlsson | a7b3521 | 2009-03-25 02:58:17 +0000 | [diff] [blame] | 1428 | if (!Constructor) { |
| 1429 | Diag(ColonLoc, diag::err_only_constructors_take_base_inits); |
| 1430 | return; |
| 1431 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1432 | |
Anders Carlsson | 8d4c5ea | 2009-08-27 05:57:30 +0000 | [diff] [blame] | 1433 | if (!Constructor->isDependentContext()) { |
| 1434 | llvm::DenseMap<void*, CXXBaseOrMemberInitializer *>Members; |
| 1435 | bool err = false; |
| 1436 | for (unsigned i = 0; i < NumMemInits; i++) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1437 | CXXBaseOrMemberInitializer *Member = |
Anders Carlsson | 8d4c5ea | 2009-08-27 05:57:30 +0000 | [diff] [blame] | 1438 | static_cast<CXXBaseOrMemberInitializer*>(MemInits[i]); |
| 1439 | void *KeyToMember = GetKeyForMember(Member); |
| 1440 | CXXBaseOrMemberInitializer *&PrevMember = Members[KeyToMember]; |
| 1441 | if (!PrevMember) { |
| 1442 | PrevMember = Member; |
| 1443 | continue; |
| 1444 | } |
| 1445 | if (FieldDecl *Field = Member->getMember()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1446 | Diag(Member->getSourceLocation(), |
Anders Carlsson | 8d4c5ea | 2009-08-27 05:57:30 +0000 | [diff] [blame] | 1447 | diag::error_multiple_mem_initialization) |
| 1448 | << Field->getNameAsString(); |
| 1449 | else { |
| 1450 | Type *BaseClass = Member->getBaseClass(); |
| 1451 | assert(BaseClass && "ActOnMemInitializers - neither field or base"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1452 | Diag(Member->getSourceLocation(), |
Anders Carlsson | 8d4c5ea | 2009-08-27 05:57:30 +0000 | [diff] [blame] | 1453 | diag::error_multiple_base_initialization) |
John McCall | bf1cc05 | 2009-09-29 23:03:30 +0000 | [diff] [blame] | 1454 | << QualType(BaseClass, 0); |
Anders Carlsson | 8d4c5ea | 2009-08-27 05:57:30 +0000 | [diff] [blame] | 1455 | } |
| 1456 | Diag(PrevMember->getSourceLocation(), diag::note_previous_initializer) |
| 1457 | << 0; |
| 1458 | err = true; |
| 1459 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1460 | |
Anders Carlsson | 8d4c5ea | 2009-08-27 05:57:30 +0000 | [diff] [blame] | 1461 | if (err) |
| 1462 | return; |
| 1463 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1464 | |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1465 | BuildBaseOrMemberInitializers(Context, Constructor, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1466 | reinterpret_cast<CXXBaseOrMemberInitializer **>(MemInits), |
Fariborz Jahanian | 87595e4 | 2009-07-23 23:32:59 +0000 | [diff] [blame] | 1467 | NumMemInits); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1468 | |
Anders Carlsson | 8d4c5ea | 2009-08-27 05:57:30 +0000 | [diff] [blame] | 1469 | if (Constructor->isDependentContext()) |
| 1470 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1471 | |
| 1472 | if (Diags.getDiagnosticLevel(diag::warn_base_initialized) == |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1473 | Diagnostic::Ignored && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1474 | Diags.getDiagnosticLevel(diag::warn_field_initialized) == |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1475 | Diagnostic::Ignored) |
| 1476 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1477 | |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1478 | // Also issue warning if order of ctor-initializer list does not match order |
| 1479 | // of 1) base class declarations and 2) order of non-static data members. |
| 1480 | llvm::SmallVector<const void*, 32> AllBaseOrMembers; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1481 | |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1482 | CXXRecordDecl *ClassDecl |
| 1483 | = cast<CXXRecordDecl>(Constructor->getDeclContext()); |
| 1484 | // Push virtual bases before others. |
| 1485 | for (CXXRecordDecl::base_class_iterator VBase = |
| 1486 | ClassDecl->vbases_begin(), |
| 1487 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) |
Anders Carlsson | cdc83c7 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 1488 | AllBaseOrMembers.push_back(GetKeyForBase(VBase->getType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1489 | |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1490 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 1491 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 1492 | // Virtuals are alread in the virtual base list and are constructed |
| 1493 | // first. |
| 1494 | if (Base->isVirtual()) |
| 1495 | continue; |
Anders Carlsson | cdc83c7 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 1496 | AllBaseOrMembers.push_back(GetKeyForBase(Base->getType())); |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1497 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1498 | |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1499 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 1500 | E = ClassDecl->field_end(); Field != E; ++Field) |
| 1501 | AllBaseOrMembers.push_back(GetKeyForTopLevelField(*Field)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1502 | |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1503 | int Last = AllBaseOrMembers.size(); |
| 1504 | int curIndex = 0; |
| 1505 | CXXBaseOrMemberInitializer *PrevMember = 0; |
| 1506 | for (unsigned i = 0; i < NumMemInits; i++) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1507 | CXXBaseOrMemberInitializer *Member = |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1508 | static_cast<CXXBaseOrMemberInitializer*>(MemInits[i]); |
| 1509 | void *MemberInCtorList = GetKeyForMember(Member, true); |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1510 | |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1511 | for (; curIndex < Last; curIndex++) |
| 1512 | if (MemberInCtorList == AllBaseOrMembers[curIndex]) |
| 1513 | break; |
| 1514 | if (curIndex == Last) { |
| 1515 | assert(PrevMember && "Member not in member list?!"); |
| 1516 | // Initializer as specified in ctor-initializer list is out of order. |
| 1517 | // Issue a warning diagnostic. |
| 1518 | if (PrevMember->isBaseInitializer()) { |
| 1519 | // Diagnostics is for an initialized base class. |
| 1520 | Type *BaseClass = PrevMember->getBaseClass(); |
| 1521 | Diag(PrevMember->getSourceLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1522 | diag::warn_base_initialized) |
John McCall | bf1cc05 | 2009-09-29 23:03:30 +0000 | [diff] [blame] | 1523 | << QualType(BaseClass, 0); |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1524 | } else { |
| 1525 | FieldDecl *Field = PrevMember->getMember(); |
| 1526 | Diag(PrevMember->getSourceLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1527 | diag::warn_field_initialized) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1528 | << Field->getNameAsString(); |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 1529 | } |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1530 | // Also the note! |
| 1531 | if (FieldDecl *Field = Member->getMember()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1532 | Diag(Member->getSourceLocation(), |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1533 | diag::note_fieldorbase_initialized_here) << 0 |
| 1534 | << Field->getNameAsString(); |
| 1535 | else { |
| 1536 | Type *BaseClass = Member->getBaseClass(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1537 | Diag(Member->getSourceLocation(), |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1538 | diag::note_fieldorbase_initialized_here) << 1 |
John McCall | bf1cc05 | 2009-09-29 23:03:30 +0000 | [diff] [blame] | 1539 | << QualType(BaseClass, 0); |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1540 | } |
| 1541 | for (curIndex = 0; curIndex < Last; curIndex++) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1542 | if (MemberInCtorList == AllBaseOrMembers[curIndex]) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1543 | break; |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 1544 | } |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1545 | PrevMember = Member; |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 1546 | } |
Anders Carlsson | a7b3521 | 2009-03-25 02:58:17 +0000 | [diff] [blame] | 1547 | } |
| 1548 | |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 1549 | void |
| 1550 | Sema::computeBaseOrMembersToDestroy(CXXDestructorDecl *Destructor) { |
| 1551 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Destructor->getDeclContext()); |
| 1552 | llvm::SmallVector<uintptr_t, 32> AllToDestruct; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1553 | |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 1554 | for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), |
| 1555 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
| 1556 | if (VBase->getType()->isDependentType()) |
| 1557 | continue; |
| 1558 | // Skip over virtual bases which have trivial destructors. |
| 1559 | CXXRecordDecl *BaseClassDecl |
| 1560 | = cast<CXXRecordDecl>(VBase->getType()->getAs<RecordType>()->getDecl()); |
| 1561 | if (BaseClassDecl->hasTrivialDestructor()) |
| 1562 | continue; |
| 1563 | if (const CXXDestructorDecl *Dtor = BaseClassDecl->getDestructor(Context)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1564 | MarkDeclarationReferenced(Destructor->getLocation(), |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 1565 | const_cast<CXXDestructorDecl*>(Dtor)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1566 | |
| 1567 | uintptr_t Member = |
| 1568 | reinterpret_cast<uintptr_t>(VBase->getType().getTypePtr()) |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 1569 | | CXXDestructorDecl::VBASE; |
| 1570 | AllToDestruct.push_back(Member); |
| 1571 | } |
| 1572 | for (CXXRecordDecl::base_class_iterator Base = |
| 1573 | ClassDecl->bases_begin(), |
| 1574 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 1575 | if (Base->isVirtual()) |
| 1576 | continue; |
| 1577 | if (Base->getType()->isDependentType()) |
| 1578 | continue; |
| 1579 | // Skip over virtual bases which have trivial destructors. |
| 1580 | CXXRecordDecl *BaseClassDecl |
| 1581 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 1582 | if (BaseClassDecl->hasTrivialDestructor()) |
| 1583 | continue; |
| 1584 | if (const CXXDestructorDecl *Dtor = BaseClassDecl->getDestructor(Context)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1585 | MarkDeclarationReferenced(Destructor->getLocation(), |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 1586 | const_cast<CXXDestructorDecl*>(Dtor)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1587 | uintptr_t Member = |
| 1588 | reinterpret_cast<uintptr_t>(Base->getType().getTypePtr()) |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 1589 | | CXXDestructorDecl::DRCTNONVBASE; |
| 1590 | AllToDestruct.push_back(Member); |
| 1591 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1592 | |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 1593 | // non-static data members. |
| 1594 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 1595 | E = ClassDecl->field_end(); Field != E; ++Field) { |
| 1596 | QualType FieldType = Context.getBaseElementType((*Field)->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1597 | |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 1598 | if (const RecordType* RT = FieldType->getAs<RecordType>()) { |
| 1599 | // Skip over virtual bases which have trivial destructors. |
| 1600 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 1601 | if (FieldClassDecl->hasTrivialDestructor()) |
| 1602 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1603 | if (const CXXDestructorDecl *Dtor = |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 1604 | FieldClassDecl->getDestructor(Context)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1605 | MarkDeclarationReferenced(Destructor->getLocation(), |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 1606 | const_cast<CXXDestructorDecl*>(Dtor)); |
| 1607 | uintptr_t Member = reinterpret_cast<uintptr_t>(*Field); |
| 1608 | AllToDestruct.push_back(Member); |
| 1609 | } |
| 1610 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1611 | |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 1612 | unsigned NumDestructions = AllToDestruct.size(); |
| 1613 | if (NumDestructions > 0) { |
| 1614 | Destructor->setNumBaseOrMemberDestructions(NumDestructions); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1615 | uintptr_t *BaseOrMemberDestructions = |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 1616 | new (Context) uintptr_t [NumDestructions]; |
| 1617 | // Insert in reverse order. |
| 1618 | for (int Idx = NumDestructions-1, i=0 ; Idx >= 0; --Idx) |
| 1619 | BaseOrMemberDestructions[i++] = AllToDestruct[Idx]; |
| 1620 | Destructor->setBaseOrMemberDestructions(BaseOrMemberDestructions); |
| 1621 | } |
| 1622 | } |
| 1623 | |
Fariborz Jahanian | 393612e | 2009-07-21 22:36:06 +0000 | [diff] [blame] | 1624 | void Sema::ActOnDefaultCtorInitializers(DeclPtrTy CDtorDecl) { |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 1625 | if (!CDtorDecl) |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 1626 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1627 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 1628 | AdjustDeclIfTemplate(CDtorDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1629 | |
| 1630 | if (CXXConstructorDecl *Constructor |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 1631 | = dyn_cast<CXXConstructorDecl>(CDtorDecl.getAs<Decl>())) |
Fariborz Jahanian | 87595e4 | 2009-07-23 23:32:59 +0000 | [diff] [blame] | 1632 | BuildBaseOrMemberInitializers(Context, |
| 1633 | Constructor, |
| 1634 | (CXXBaseOrMemberInitializer **)0, 0); |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 1635 | } |
| 1636 | |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1637 | namespace { |
| 1638 | /// PureVirtualMethodCollector - traverses a class and its superclasses |
| 1639 | /// and determines if it has any pure virtual methods. |
| 1640 | class VISIBILITY_HIDDEN PureVirtualMethodCollector { |
| 1641 | ASTContext &Context; |
| 1642 | |
Sebastian Redl | dfe292d | 2009-03-22 21:28:55 +0000 | [diff] [blame] | 1643 | public: |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1644 | typedef llvm::SmallVector<const CXXMethodDecl*, 8> MethodList; |
Sebastian Redl | dfe292d | 2009-03-22 21:28:55 +0000 | [diff] [blame] | 1645 | |
| 1646 | private: |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1647 | MethodList Methods; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1648 | |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1649 | void Collect(const CXXRecordDecl* RD, MethodList& Methods); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1650 | |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1651 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1652 | PureVirtualMethodCollector(ASTContext &Ctx, const CXXRecordDecl* RD) |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1653 | : Context(Ctx) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1654 | |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1655 | MethodList List; |
| 1656 | Collect(RD, List); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1657 | |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1658 | // Copy the temporary list to methods, and make sure to ignore any |
| 1659 | // null entries. |
| 1660 | for (size_t i = 0, e = List.size(); i != e; ++i) { |
| 1661 | if (List[i]) |
| 1662 | Methods.push_back(List[i]); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1663 | } |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1664 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1665 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1666 | bool empty() const { return Methods.empty(); } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1667 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1668 | MethodList::const_iterator methods_begin() { return Methods.begin(); } |
| 1669 | MethodList::const_iterator methods_end() { return Methods.end(); } |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1670 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1671 | |
| 1672 | void PureVirtualMethodCollector::Collect(const CXXRecordDecl* RD, |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1673 | MethodList& Methods) { |
| 1674 | // First, collect the pure virtual methods for the base classes. |
| 1675 | for (CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin(), |
| 1676 | BaseEnd = RD->bases_end(); Base != BaseEnd; ++Base) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1677 | if (const RecordType *RT = Base->getType()->getAs<RecordType>()) { |
Chris Lattner | 64540d7 | 2009-03-29 05:01:10 +0000 | [diff] [blame] | 1678 | const CXXRecordDecl *BaseDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1679 | if (BaseDecl && BaseDecl->isAbstract()) |
| 1680 | Collect(BaseDecl, Methods); |
| 1681 | } |
| 1682 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1683 | |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1684 | // Next, zero out any pure virtual methods that this class overrides. |
Anders Carlsson | 8ff8c22 | 2009-05-17 00:00:05 +0000 | [diff] [blame] | 1685 | typedef llvm::SmallPtrSet<const CXXMethodDecl*, 4> MethodSetTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1686 | |
Anders Carlsson | 8ff8c22 | 2009-05-17 00:00:05 +0000 | [diff] [blame] | 1687 | MethodSetTy OverriddenMethods; |
| 1688 | size_t MethodsSize = Methods.size(); |
| 1689 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1690 | for (RecordDecl::decl_iterator i = RD->decls_begin(), e = RD->decls_end(); |
Anders Carlsson | 8ff8c22 | 2009-05-17 00:00:05 +0000 | [diff] [blame] | 1691 | i != e; ++i) { |
| 1692 | // Traverse the record, looking for methods. |
| 1693 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(*i)) { |
Sebastian Redl | 23c7d06 | 2009-07-07 20:29:57 +0000 | [diff] [blame] | 1694 | // If the method is pure virtual, add it to the methods vector. |
Anders Carlsson | 2782302 | 2009-10-18 19:34:08 +0000 | [diff] [blame] | 1695 | if (MD->isPure()) |
Anders Carlsson | 8ff8c22 | 2009-05-17 00:00:05 +0000 | [diff] [blame] | 1696 | Methods.push_back(MD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1697 | |
Anders Carlsson | 2782302 | 2009-10-18 19:34:08 +0000 | [diff] [blame] | 1698 | // Record all the overridden methods in our set. |
Anders Carlsson | 8ff8c22 | 2009-05-17 00:00:05 +0000 | [diff] [blame] | 1699 | for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), |
| 1700 | E = MD->end_overridden_methods(); I != E; ++I) { |
| 1701 | // Keep track of the overridden methods. |
| 1702 | OverriddenMethods.insert(*I); |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1703 | } |
| 1704 | } |
| 1705 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1706 | |
| 1707 | // Now go through the methods and zero out all the ones we know are |
Anders Carlsson | 8ff8c22 | 2009-05-17 00:00:05 +0000 | [diff] [blame] | 1708 | // overridden. |
| 1709 | for (size_t i = 0, e = MethodsSize; i != e; ++i) { |
| 1710 | if (OverriddenMethods.count(Methods[i])) |
| 1711 | Methods[i] = 0; |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1712 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1713 | |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1714 | } |
| 1715 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1716 | |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 1717 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1718 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1719 | unsigned DiagID, AbstractDiagSelID SelID, |
| 1720 | const CXXRecordDecl *CurrentRD) { |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 1721 | if (SelID == -1) |
| 1722 | return RequireNonAbstractType(Loc, T, |
| 1723 | PDiag(DiagID), CurrentRD); |
| 1724 | else |
| 1725 | return RequireNonAbstractType(Loc, T, |
| 1726 | PDiag(DiagID) << SelID, CurrentRD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1727 | } |
| 1728 | |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 1729 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
| 1730 | const PartialDiagnostic &PD, |
| 1731 | const CXXRecordDecl *CurrentRD) { |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1732 | if (!getLangOptions().CPlusPlus) |
| 1733 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1734 | |
Anders Carlsson | 11f21a0 | 2009-03-23 19:10:31 +0000 | [diff] [blame] | 1735 | if (const ArrayType *AT = Context.getAsArrayType(T)) |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 1736 | return RequireNonAbstractType(Loc, AT->getElementType(), PD, |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1737 | CurrentRD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1738 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1739 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 1740 | // Find the innermost pointer type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1741 | while (const PointerType *T = PT->getPointeeType()->getAs<PointerType>()) |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 1742 | PT = T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1743 | |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 1744 | if (const ArrayType *AT = Context.getAsArrayType(PT->getPointeeType())) |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 1745 | return RequireNonAbstractType(Loc, AT->getElementType(), PD, CurrentRD); |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 1746 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1747 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1748 | const RecordType *RT = T->getAs<RecordType>(); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1749 | if (!RT) |
| 1750 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1751 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1752 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
| 1753 | if (!RD) |
| 1754 | return false; |
| 1755 | |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1756 | if (CurrentRD && CurrentRD != RD) |
| 1757 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1758 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1759 | if (!RD->isAbstract()) |
| 1760 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1761 | |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 1762 | Diag(Loc, PD) << RD->getDeclName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1763 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1764 | // Check if we've already emitted the list of pure virtual functions for this |
| 1765 | // class. |
| 1766 | if (PureVirtualClassDiagSet && PureVirtualClassDiagSet->count(RD)) |
| 1767 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1768 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1769 | PureVirtualMethodCollector Collector(Context, RD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1770 | |
| 1771 | for (PureVirtualMethodCollector::MethodList::const_iterator I = |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1772 | Collector.methods_begin(), E = Collector.methods_end(); I != E; ++I) { |
| 1773 | const CXXMethodDecl *MD = *I; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1774 | |
| 1775 | Diag(MD->getLocation(), diag::note_pure_virtual_function) << |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1776 | MD->getDeclName(); |
| 1777 | } |
| 1778 | |
| 1779 | if (!PureVirtualClassDiagSet) |
| 1780 | PureVirtualClassDiagSet.reset(new RecordDeclSetTy); |
| 1781 | PureVirtualClassDiagSet->insert(RD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1782 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1783 | return true; |
| 1784 | } |
| 1785 | |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1786 | namespace { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1787 | class VISIBILITY_HIDDEN AbstractClassUsageDiagnoser |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1788 | : public DeclVisitor<AbstractClassUsageDiagnoser, bool> { |
| 1789 | Sema &SemaRef; |
| 1790 | CXXRecordDecl *AbstractClass; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1791 | |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1792 | bool VisitDeclContext(const DeclContext *DC) { |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1793 | bool Invalid = false; |
| 1794 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1795 | for (CXXRecordDecl::decl_iterator I = DC->decls_begin(), |
| 1796 | E = DC->decls_end(); I != E; ++I) |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1797 | Invalid |= Visit(*I); |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1798 | |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1799 | return Invalid; |
| 1800 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1801 | |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1802 | public: |
| 1803 | AbstractClassUsageDiagnoser(Sema& SemaRef, CXXRecordDecl *ac) |
| 1804 | : SemaRef(SemaRef), AbstractClass(ac) { |
| 1805 | Visit(SemaRef.Context.getTranslationUnitDecl()); |
| 1806 | } |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1807 | |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1808 | bool VisitFunctionDecl(const FunctionDecl *FD) { |
| 1809 | if (FD->isThisDeclarationADefinition()) { |
| 1810 | // No need to do the check if we're in a definition, because it requires |
| 1811 | // that the return/param types are complete. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1812 | // because that requires |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1813 | return VisitDeclContext(FD); |
| 1814 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1815 | |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1816 | // Check the return type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1817 | QualType RTy = FD->getType()->getAs<FunctionType>()->getResultType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1818 | bool Invalid = |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1819 | SemaRef.RequireNonAbstractType(FD->getLocation(), RTy, |
| 1820 | diag::err_abstract_type_in_decl, |
| 1821 | Sema::AbstractReturnType, |
| 1822 | AbstractClass); |
| 1823 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1824 | for (FunctionDecl::param_const_iterator I = FD->param_begin(), |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1825 | E = FD->param_end(); I != E; ++I) { |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1826 | const ParmVarDecl *VD = *I; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1827 | Invalid |= |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1828 | SemaRef.RequireNonAbstractType(VD->getLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1829 | VD->getOriginalType(), |
| 1830 | diag::err_abstract_type_in_decl, |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1831 | Sema::AbstractParamType, |
| 1832 | AbstractClass); |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1833 | } |
| 1834 | |
| 1835 | return Invalid; |
| 1836 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1837 | |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1838 | bool VisitDecl(const Decl* D) { |
| 1839 | if (const DeclContext *DC = dyn_cast<DeclContext>(D)) |
| 1840 | return VisitDeclContext(DC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1841 | |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1842 | return false; |
| 1843 | } |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1844 | }; |
| 1845 | } |
| 1846 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1847 | void Sema::ActOnFinishCXXMemberSpecification(Scope* S, SourceLocation RLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1848 | DeclPtrTy TagDecl, |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1849 | SourceLocation LBrac, |
| 1850 | SourceLocation RBrac) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 1851 | if (!TagDecl) |
| 1852 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1853 | |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 1854 | AdjustDeclIfTemplate(TagDecl); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1855 | ActOnFields(S, RLoc, TagDecl, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1856 | (DeclPtrTy*)FieldCollector->getCurFields(), |
Daniel Dunbar | 1bfe1c2 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 1857 | FieldCollector->getCurNumFields(), LBrac, RBrac, 0); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1858 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1859 | CXXRecordDecl *RD = cast<CXXRecordDecl>(TagDecl.getAs<Decl>()); |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1860 | if (!RD->isAbstract()) { |
| 1861 | // Collect all the pure virtual methods and see if this is an abstract |
| 1862 | // class after all. |
| 1863 | PureVirtualMethodCollector Collector(Context, RD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1864 | if (!Collector.empty()) |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1865 | RD->setAbstract(true); |
| 1866 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1867 | |
| 1868 | if (RD->isAbstract()) |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1869 | AbstractClassUsageDiagnoser(*this, RD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1870 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 1871 | if (!RD->isDependentType() && !RD->isInvalidDecl()) |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1872 | AddImplicitlyDeclaredMembersToClass(RD); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1873 | } |
| 1874 | |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1875 | /// AddImplicitlyDeclaredMembersToClass - Adds any implicitly-declared |
| 1876 | /// special functions, such as the default constructor, copy |
| 1877 | /// constructor, or destructor, to the given C++ class (C++ |
| 1878 | /// [special]p1). This routine can only be executed just before the |
| 1879 | /// definition of the class is complete. |
| 1880 | void Sema::AddImplicitlyDeclaredMembersToClass(CXXRecordDecl *ClassDecl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1881 | CanQualType ClassType |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 1882 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1883 | |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1884 | // FIXME: Implicit declarations have exception specifications, which are |
| 1885 | // the union of the specifications of the implicitly called functions. |
| 1886 | |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1887 | if (!ClassDecl->hasUserDeclaredConstructor()) { |
| 1888 | // C++ [class.ctor]p5: |
| 1889 | // A default constructor for a class X is a constructor of class X |
| 1890 | // that can be called without an argument. If there is no |
| 1891 | // user-declared constructor for class X, a default constructor is |
| 1892 | // implicitly declared. An implicitly-declared default constructor |
| 1893 | // is an inline public member of its class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1894 | DeclarationName Name |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1895 | = Context.DeclarationNames.getCXXConstructorName(ClassType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1896 | CXXConstructorDecl *DefaultCon = |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1897 | CXXConstructorDecl::Create(Context, ClassDecl, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1898 | ClassDecl->getLocation(), Name, |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1899 | Context.getFunctionType(Context.VoidTy, |
| 1900 | 0, 0, false, 0), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 1901 | /*DInfo=*/0, |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1902 | /*isExplicit=*/false, |
| 1903 | /*isInline=*/true, |
| 1904 | /*isImplicitlyDeclared=*/true); |
| 1905 | DefaultCon->setAccess(AS_public); |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 1906 | DefaultCon->setImplicit(); |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 1907 | DefaultCon->setTrivial(ClassDecl->hasTrivialConstructor()); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1908 | ClassDecl->addDecl(DefaultCon); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1909 | } |
| 1910 | |
| 1911 | if (!ClassDecl->hasUserDeclaredCopyConstructor()) { |
| 1912 | // C++ [class.copy]p4: |
| 1913 | // If the class definition does not explicitly declare a copy |
| 1914 | // constructor, one is declared implicitly. |
| 1915 | |
| 1916 | // C++ [class.copy]p5: |
| 1917 | // The implicitly-declared copy constructor for a class X will |
| 1918 | // have the form |
| 1919 | // |
| 1920 | // X::X(const X&) |
| 1921 | // |
| 1922 | // if |
| 1923 | bool HasConstCopyConstructor = true; |
| 1924 | |
| 1925 | // -- each direct or virtual base class B of X has a copy |
| 1926 | // constructor whose first parameter is of type const B& or |
| 1927 | // const volatile B&, and |
| 1928 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(); |
| 1929 | HasConstCopyConstructor && Base != ClassDecl->bases_end(); ++Base) { |
| 1930 | const CXXRecordDecl *BaseClassDecl |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1931 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1932 | HasConstCopyConstructor |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1933 | = BaseClassDecl->hasConstCopyConstructor(Context); |
| 1934 | } |
| 1935 | |
| 1936 | // -- for all the nonstatic data members of X that are of a |
| 1937 | // class type M (or array thereof), each such class type |
| 1938 | // has a copy constructor whose first parameter is of type |
| 1939 | // const M& or const volatile M&. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1940 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(); |
| 1941 | HasConstCopyConstructor && Field != ClassDecl->field_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1942 | ++Field) { |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1943 | QualType FieldType = (*Field)->getType(); |
| 1944 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 1945 | FieldType = Array->getElementType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1946 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1947 | const CXXRecordDecl *FieldClassDecl |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1948 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1949 | HasConstCopyConstructor |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1950 | = FieldClassDecl->hasConstCopyConstructor(Context); |
| 1951 | } |
| 1952 | } |
| 1953 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1954 | // Otherwise, the implicitly declared copy constructor will have |
| 1955 | // the form |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1956 | // |
| 1957 | // X::X(X&) |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1958 | QualType ArgType = ClassType; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1959 | if (HasConstCopyConstructor) |
| 1960 | ArgType = ArgType.withConst(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1961 | ArgType = Context.getLValueReferenceType(ArgType); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1962 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1963 | // An implicitly-declared copy constructor is an inline public |
| 1964 | // member of its class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1965 | DeclarationName Name |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1966 | = Context.DeclarationNames.getCXXConstructorName(ClassType); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1967 | CXXConstructorDecl *CopyConstructor |
| 1968 | = CXXConstructorDecl::Create(Context, ClassDecl, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1969 | ClassDecl->getLocation(), Name, |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1970 | Context.getFunctionType(Context.VoidTy, |
| 1971 | &ArgType, 1, |
| 1972 | false, 0), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 1973 | /*DInfo=*/0, |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1974 | /*isExplicit=*/false, |
| 1975 | /*isInline=*/true, |
| 1976 | /*isImplicitlyDeclared=*/true); |
| 1977 | CopyConstructor->setAccess(AS_public); |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 1978 | CopyConstructor->setImplicit(); |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 1979 | CopyConstructor->setTrivial(ClassDecl->hasTrivialCopyConstructor()); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1980 | |
| 1981 | // Add the parameter to the constructor. |
| 1982 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyConstructor, |
| 1983 | ClassDecl->getLocation(), |
| 1984 | /*IdentifierInfo=*/0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 1985 | ArgType, /*DInfo=*/0, |
| 1986 | VarDecl::None, 0); |
Ted Kremenek | fc76761 | 2009-01-14 00:42:25 +0000 | [diff] [blame] | 1987 | CopyConstructor->setParams(Context, &FromParam, 1); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1988 | ClassDecl->addDecl(CopyConstructor); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1989 | } |
| 1990 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1991 | if (!ClassDecl->hasUserDeclaredCopyAssignment()) { |
| 1992 | // Note: The following rules are largely analoguous to the copy |
| 1993 | // constructor rules. Note that virtual bases are not taken into account |
| 1994 | // for determining the argument type of the operator. Note also that |
| 1995 | // operators taking an object instead of a reference are allowed. |
| 1996 | // |
| 1997 | // C++ [class.copy]p10: |
| 1998 | // If the class definition does not explicitly declare a copy |
| 1999 | // assignment operator, one is declared implicitly. |
| 2000 | // The implicitly-defined copy assignment operator for a class X |
| 2001 | // will have the form |
| 2002 | // |
| 2003 | // X& X::operator=(const X&) |
| 2004 | // |
| 2005 | // if |
| 2006 | bool HasConstCopyAssignment = true; |
| 2007 | |
| 2008 | // -- each direct base class B of X has a copy assignment operator |
| 2009 | // whose parameter is of type const B&, const volatile B& or B, |
| 2010 | // and |
| 2011 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(); |
| 2012 | HasConstCopyAssignment && Base != ClassDecl->bases_end(); ++Base) { |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 2013 | assert(!Base->getType()->isDependentType() && |
| 2014 | "Cannot generate implicit members for class with dependent bases."); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2015 | const CXXRecordDecl *BaseClassDecl |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2016 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 2017 | const CXXMethodDecl *MD = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2018 | HasConstCopyAssignment = BaseClassDecl->hasConstCopyAssignment(Context, |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 2019 | MD); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2020 | } |
| 2021 | |
| 2022 | // -- for all the nonstatic data members of X that are of a class |
| 2023 | // type M (or array thereof), each such class type has a copy |
| 2024 | // assignment operator whose parameter is of type const M&, |
| 2025 | // const volatile M& or M. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2026 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(); |
| 2027 | HasConstCopyAssignment && Field != ClassDecl->field_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2028 | ++Field) { |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2029 | QualType FieldType = (*Field)->getType(); |
| 2030 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 2031 | FieldType = Array->getElementType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2032 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2033 | const CXXRecordDecl *FieldClassDecl |
| 2034 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 2035 | const CXXMethodDecl *MD = 0; |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2036 | HasConstCopyAssignment |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 2037 | = FieldClassDecl->hasConstCopyAssignment(Context, MD); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2038 | } |
| 2039 | } |
| 2040 | |
| 2041 | // Otherwise, the implicitly declared copy assignment operator will |
| 2042 | // have the form |
| 2043 | // |
| 2044 | // X& X::operator=(X&) |
| 2045 | QualType ArgType = ClassType; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2046 | QualType RetType = Context.getLValueReferenceType(ArgType); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2047 | if (HasConstCopyAssignment) |
| 2048 | ArgType = ArgType.withConst(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2049 | ArgType = Context.getLValueReferenceType(ArgType); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2050 | |
| 2051 | // An implicitly-declared copy assignment operator is an inline public |
| 2052 | // member of its class. |
| 2053 | DeclarationName Name = |
| 2054 | Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 2055 | CXXMethodDecl *CopyAssignment = |
| 2056 | CXXMethodDecl::Create(Context, ClassDecl, ClassDecl->getLocation(), Name, |
| 2057 | Context.getFunctionType(RetType, &ArgType, 1, |
| 2058 | false, 0), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2059 | /*DInfo=*/0, /*isStatic=*/false, /*isInline=*/true); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2060 | CopyAssignment->setAccess(AS_public); |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 2061 | CopyAssignment->setImplicit(); |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 2062 | CopyAssignment->setTrivial(ClassDecl->hasTrivialCopyAssignment()); |
Fariborz Jahanian | 2198ba1 | 2009-08-12 21:14:35 +0000 | [diff] [blame] | 2063 | CopyAssignment->setCopyAssignment(true); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2064 | |
| 2065 | // Add the parameter to the operator. |
| 2066 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyAssignment, |
| 2067 | ClassDecl->getLocation(), |
| 2068 | /*IdentifierInfo=*/0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2069 | ArgType, /*DInfo=*/0, |
| 2070 | VarDecl::None, 0); |
Ted Kremenek | fc76761 | 2009-01-14 00:42:25 +0000 | [diff] [blame] | 2071 | CopyAssignment->setParams(Context, &FromParam, 1); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2072 | |
| 2073 | // Don't call addedAssignmentOperator. There is no way to distinguish an |
| 2074 | // implicit from an explicit assignment operator. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2075 | ClassDecl->addDecl(CopyAssignment); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2076 | } |
| 2077 | |
Douglas Gregor | 9e7d9de | 2008-12-15 21:24:18 +0000 | [diff] [blame] | 2078 | if (!ClassDecl->hasUserDeclaredDestructor()) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2079 | // C++ [class.dtor]p2: |
| 2080 | // If a class has no user-declared destructor, a destructor is |
| 2081 | // declared implicitly. An implicitly-declared destructor is an |
| 2082 | // inline public member of its class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2083 | DeclarationName Name |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2084 | = Context.DeclarationNames.getCXXDestructorName(ClassType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2085 | CXXDestructorDecl *Destructor |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2086 | = CXXDestructorDecl::Create(Context, ClassDecl, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2087 | ClassDecl->getLocation(), Name, |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2088 | Context.getFunctionType(Context.VoidTy, |
| 2089 | 0, 0, false, 0), |
| 2090 | /*isInline=*/true, |
| 2091 | /*isImplicitlyDeclared=*/true); |
| 2092 | Destructor->setAccess(AS_public); |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 2093 | Destructor->setImplicit(); |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 2094 | Destructor->setTrivial(ClassDecl->hasTrivialDestructor()); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2095 | ClassDecl->addDecl(Destructor); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2096 | } |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2097 | } |
| 2098 | |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2099 | void Sema::ActOnReenterTemplateScope(Scope *S, DeclPtrTy TemplateD) { |
Douglas Gregor | 1cdcc57 | 2009-09-10 00:12:48 +0000 | [diff] [blame] | 2100 | Decl *D = TemplateD.getAs<Decl>(); |
| 2101 | if (!D) |
| 2102 | return; |
| 2103 | |
| 2104 | TemplateParameterList *Params = 0; |
| 2105 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) |
| 2106 | Params = Template->getTemplateParameters(); |
| 2107 | else if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 2108 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) |
| 2109 | Params = PartialSpec->getTemplateParameters(); |
| 2110 | else |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2111 | return; |
| 2112 | |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2113 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 2114 | ParamEnd = Params->end(); |
| 2115 | Param != ParamEnd; ++Param) { |
| 2116 | NamedDecl *Named = cast<NamedDecl>(*Param); |
| 2117 | if (Named->getDeclName()) { |
| 2118 | S->AddDecl(DeclPtrTy::make(Named)); |
| 2119 | IdResolver.AddDecl(Named); |
| 2120 | } |
| 2121 | } |
| 2122 | } |
| 2123 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2124 | /// ActOnStartDelayedCXXMethodDeclaration - We have completed |
| 2125 | /// parsing a top-level (non-nested) C++ class, and we are now |
| 2126 | /// parsing those parts of the given Method declaration that could |
| 2127 | /// not be parsed earlier (C++ [class.mem]p2), such as default |
| 2128 | /// arguments. This action should enter the scope of the given |
| 2129 | /// Method declaration as if we had just parsed the qualified method |
| 2130 | /// name. However, it should not bring the parameters into scope; |
| 2131 | /// that will be performed by ActOnDelayedCXXMethodParameter. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2132 | void Sema::ActOnStartDelayedCXXMethodDeclaration(Scope *S, DeclPtrTy MethodD) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 2133 | if (!MethodD) |
| 2134 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2135 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 2136 | AdjustDeclIfTemplate(MethodD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2137 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2138 | CXXScopeSpec SS; |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2139 | FunctionDecl *Method = cast<FunctionDecl>(MethodD.getAs<Decl>()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2140 | QualType ClassTy |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 2141 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 2142 | SS.setScopeRep( |
| 2143 | NestedNameSpecifier::Create(Context, 0, false, ClassTy.getTypePtr())); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2144 | ActOnCXXEnterDeclaratorScope(S, SS); |
| 2145 | } |
| 2146 | |
| 2147 | /// ActOnDelayedCXXMethodParameter - We've already started a delayed |
| 2148 | /// C++ method declaration. We're (re-)introducing the given |
| 2149 | /// function parameter into scope for use in parsing later parts of |
| 2150 | /// the method declaration. For example, we could see an |
| 2151 | /// ActOnParamDefaultArgument event for this parameter. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2152 | void Sema::ActOnDelayedCXXMethodParameter(Scope *S, DeclPtrTy ParamD) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 2153 | if (!ParamD) |
| 2154 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2155 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2156 | ParmVarDecl *Param = cast<ParmVarDecl>(ParamD.getAs<Decl>()); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2157 | |
| 2158 | // If this parameter has an unparsed default argument, clear it out |
| 2159 | // to make way for the parsed default argument. |
| 2160 | if (Param->hasUnparsedDefaultArg()) |
| 2161 | Param->setDefaultArg(0); |
| 2162 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2163 | S->AddDecl(DeclPtrTy::make(Param)); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2164 | if (Param->getDeclName()) |
| 2165 | IdResolver.AddDecl(Param); |
| 2166 | } |
| 2167 | |
| 2168 | /// ActOnFinishDelayedCXXMethodDeclaration - We have finished |
| 2169 | /// processing the delayed method declaration for Method. The method |
| 2170 | /// declaration is now considered finished. There may be a separate |
| 2171 | /// ActOnStartOfFunctionDef action later (not necessarily |
| 2172 | /// immediately!) for this method, if it was also defined inside the |
| 2173 | /// class body. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2174 | void Sema::ActOnFinishDelayedCXXMethodDeclaration(Scope *S, DeclPtrTy MethodD) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 2175 | if (!MethodD) |
| 2176 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2177 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 2178 | AdjustDeclIfTemplate(MethodD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2179 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2180 | FunctionDecl *Method = cast<FunctionDecl>(MethodD.getAs<Decl>()); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2181 | CXXScopeSpec SS; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2182 | QualType ClassTy |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 2183 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 2184 | SS.setScopeRep( |
| 2185 | NestedNameSpecifier::Create(Context, 0, false, ClassTy.getTypePtr())); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2186 | ActOnCXXExitDeclaratorScope(S, SS); |
| 2187 | |
| 2188 | // Now that we have our default arguments, check the constructor |
| 2189 | // again. It could produce additional diagnostics or affect whether |
| 2190 | // the class has implicitly-declared destructors, among other |
| 2191 | // things. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2192 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Method)) |
| 2193 | CheckConstructor(Constructor); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2194 | |
| 2195 | // Check the default arguments, which we may have added. |
| 2196 | if (!Method->isInvalidDecl()) |
| 2197 | CheckCXXDefaultArguments(Method); |
| 2198 | } |
| 2199 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2200 | /// CheckConstructorDeclarator - Called by ActOnDeclarator to check |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2201 | /// the well-formedness of the constructor declarator @p D with type @p |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2202 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2203 | /// emit diagnostics and set the invalid bit to true. In any case, the type |
| 2204 | /// will be updated to reflect a well-formed type for the constructor and |
| 2205 | /// returned. |
| 2206 | QualType Sema::CheckConstructorDeclarator(Declarator &D, QualType R, |
| 2207 | FunctionDecl::StorageClass &SC) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2208 | bool isVirtual = D.getDeclSpec().isVirtualSpecified(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2209 | |
| 2210 | // C++ [class.ctor]p3: |
| 2211 | // A constructor shall not be virtual (10.3) or static (9.4). A |
| 2212 | // constructor can be invoked for a const, volatile or const |
| 2213 | // volatile object. A constructor shall not be declared const, |
| 2214 | // volatile, or const volatile (9.3.2). |
| 2215 | if (isVirtual) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2216 | if (!D.isInvalidType()) |
| 2217 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 2218 | << "virtual" << SourceRange(D.getDeclSpec().getVirtualSpecLoc()) |
| 2219 | << SourceRange(D.getIdentifierLoc()); |
| 2220 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2221 | } |
| 2222 | if (SC == FunctionDecl::Static) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2223 | if (!D.isInvalidType()) |
| 2224 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 2225 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 2226 | << SourceRange(D.getIdentifierLoc()); |
| 2227 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2228 | SC = FunctionDecl::None; |
| 2229 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2230 | |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2231 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 2232 | if (FTI.TypeQuals != 0) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2233 | if (FTI.TypeQuals & Qualifiers::Const) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2234 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 2235 | << "const" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2236 | if (FTI.TypeQuals & Qualifiers::Volatile) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2237 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 2238 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2239 | if (FTI.TypeQuals & Qualifiers::Restrict) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2240 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 2241 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2242 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2243 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2244 | // Rebuild the function type "R" without any type qualifiers (in |
| 2245 | // case any of the errors above fired) and with "void" as the |
| 2246 | // return type, since constructors don't have return types. We |
| 2247 | // *always* have to do this, because GetTypeForDeclarator will |
| 2248 | // put in a result type of "int" when none was specified. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2249 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2250 | return Context.getFunctionType(Context.VoidTy, Proto->arg_type_begin(), |
| 2251 | Proto->getNumArgs(), |
| 2252 | Proto->isVariadic(), 0); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2253 | } |
| 2254 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2255 | /// CheckConstructor - Checks a fully-formed constructor for |
| 2256 | /// well-formedness, issuing any diagnostics required. Returns true if |
| 2257 | /// the constructor declarator is invalid. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2258 | void Sema::CheckConstructor(CXXConstructorDecl *Constructor) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2259 | CXXRecordDecl *ClassDecl |
Douglas Gregor | 3329756 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 2260 | = dyn_cast<CXXRecordDecl>(Constructor->getDeclContext()); |
| 2261 | if (!ClassDecl) |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2262 | return Constructor->setInvalidDecl(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2263 | |
| 2264 | // C++ [class.copy]p3: |
| 2265 | // A declaration of a constructor for a class X is ill-formed if |
| 2266 | // its first parameter is of type (optionally cv-qualified) X and |
| 2267 | // either there are no other parameters or else all other |
| 2268 | // parameters have default arguments. |
Douglas Gregor | 3329756 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 2269 | if (!Constructor->isInvalidDecl() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2270 | ((Constructor->getNumParams() == 1) || |
| 2271 | (Constructor->getNumParams() > 1 && |
Anders Carlsson | ae0b4e7 | 2009-06-06 04:14:07 +0000 | [diff] [blame] | 2272 | Constructor->getParamDecl(1)->hasDefaultArg()))) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2273 | QualType ParamType = Constructor->getParamDecl(0)->getType(); |
| 2274 | QualType ClassTy = Context.getTagDeclType(ClassDecl); |
| 2275 | if (Context.getCanonicalType(ParamType).getUnqualifiedType() == ClassTy) { |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 2276 | SourceLocation ParamLoc = Constructor->getParamDecl(0)->getLocation(); |
| 2277 | Diag(ParamLoc, diag::err_constructor_byvalue_arg) |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 2278 | << CodeModificationHint::CreateInsertion(ParamLoc, " const &"); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2279 | Constructor->setInvalidDecl(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2280 | } |
| 2281 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2282 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2283 | // Notify the class that we've added a constructor. |
| 2284 | ClassDecl->addedConstructor(Context, Constructor); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2285 | } |
| 2286 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2287 | static inline bool |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 2288 | FTIHasSingleVoidArgument(DeclaratorChunk::FunctionTypeInfo &FTI) { |
| 2289 | return (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
| 2290 | FTI.ArgInfo[0].Param && |
| 2291 | FTI.ArgInfo[0].Param.getAs<ParmVarDecl>()->getType()->isVoidType()); |
| 2292 | } |
| 2293 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2294 | /// CheckDestructorDeclarator - Called by ActOnDeclarator to check |
| 2295 | /// the well-formednes of the destructor declarator @p D with type @p |
| 2296 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2297 | /// emit diagnostics and set the declarator to invalid. Even if this happens, |
| 2298 | /// will be updated to reflect a well-formed type for the destructor and |
| 2299 | /// returned. |
| 2300 | QualType Sema::CheckDestructorDeclarator(Declarator &D, |
| 2301 | FunctionDecl::StorageClass& SC) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2302 | // C++ [class.dtor]p1: |
| 2303 | // [...] A typedef-name that names a class is a class-name |
| 2304 | // (7.1.3); however, a typedef-name that names a class shall not |
| 2305 | // be used as the identifier in the declarator for a destructor |
| 2306 | // declaration. |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2307 | QualType DeclaratorType = GetTypeFromParser(D.getName().DestructorName); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2308 | if (isa<TypedefType>(DeclaratorType)) { |
| 2309 | Diag(D.getIdentifierLoc(), diag::err_destructor_typedef_name) |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2310 | << DeclaratorType; |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2311 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2312 | } |
| 2313 | |
| 2314 | // C++ [class.dtor]p2: |
| 2315 | // A destructor is used to destroy objects of its class type. A |
| 2316 | // destructor takes no parameters, and no return type can be |
| 2317 | // specified for it (not even void). The address of a destructor |
| 2318 | // shall not be taken. A destructor shall not be static. A |
| 2319 | // destructor can be invoked for a const, volatile or const |
| 2320 | // volatile object. A destructor shall not be declared const, |
| 2321 | // volatile or const volatile (9.3.2). |
| 2322 | if (SC == FunctionDecl::Static) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2323 | if (!D.isInvalidType()) |
| 2324 | Diag(D.getIdentifierLoc(), diag::err_destructor_cannot_be) |
| 2325 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 2326 | << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2327 | SC = FunctionDecl::None; |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2328 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2329 | } |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2330 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2331 | // Destructors don't have return types, but the parser will |
| 2332 | // happily parse something like: |
| 2333 | // |
| 2334 | // class X { |
| 2335 | // float ~X(); |
| 2336 | // }; |
| 2337 | // |
| 2338 | // The return type will be eliminated later. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2339 | Diag(D.getIdentifierLoc(), diag::err_destructor_return_type) |
| 2340 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 2341 | << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2342 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2343 | |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2344 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 2345 | if (FTI.TypeQuals != 0 && !D.isInvalidType()) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2346 | if (FTI.TypeQuals & Qualifiers::Const) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2347 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 2348 | << "const" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2349 | if (FTI.TypeQuals & Qualifiers::Volatile) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2350 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 2351 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2352 | if (FTI.TypeQuals & Qualifiers::Restrict) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2353 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 2354 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2355 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2356 | } |
| 2357 | |
| 2358 | // Make sure we don't have any parameters. |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 2359 | if (FTI.NumArgs > 0 && !FTIHasSingleVoidArgument(FTI)) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2360 | Diag(D.getIdentifierLoc(), diag::err_destructor_with_params); |
| 2361 | |
| 2362 | // Delete the parameters. |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2363 | FTI.freeArgs(); |
| 2364 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2365 | } |
| 2366 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2367 | // Make sure the destructor isn't variadic. |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2368 | if (FTI.isVariadic) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2369 | Diag(D.getIdentifierLoc(), diag::err_destructor_variadic); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2370 | D.setInvalidType(); |
| 2371 | } |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2372 | |
| 2373 | // Rebuild the function type "R" without any type qualifiers or |
| 2374 | // parameters (in case any of the errors above fired) and with |
| 2375 | // "void" as the return type, since destructors don't have return |
| 2376 | // types. We *always* have to do this, because GetTypeForDeclarator |
| 2377 | // will put in a result type of "int" when none was specified. |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2378 | return Context.getFunctionType(Context.VoidTy, 0, 0, false, 0); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2379 | } |
| 2380 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2381 | /// CheckConversionDeclarator - Called by ActOnDeclarator to check the |
| 2382 | /// well-formednes of the conversion function declarator @p D with |
| 2383 | /// type @p R. If there are any errors in the declarator, this routine |
| 2384 | /// will emit diagnostics and return true. Otherwise, it will return |
| 2385 | /// false. Either way, the type @p R will be updated to reflect a |
| 2386 | /// well-formed type for the conversion operator. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2387 | void Sema::CheckConversionDeclarator(Declarator &D, QualType &R, |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2388 | FunctionDecl::StorageClass& SC) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2389 | // C++ [class.conv.fct]p1: |
| 2390 | // Neither parameter types nor return type can be specified. The |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 2391 | // type of a conversion function (8.3.5) is "function taking no |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2392 | // parameter returning conversion-type-id." |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2393 | if (SC == FunctionDecl::Static) { |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2394 | if (!D.isInvalidType()) |
| 2395 | Diag(D.getIdentifierLoc(), diag::err_conv_function_not_member) |
| 2396 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 2397 | << SourceRange(D.getIdentifierLoc()); |
| 2398 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2399 | SC = FunctionDecl::None; |
| 2400 | } |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2401 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2402 | // Conversion functions don't have return types, but the parser will |
| 2403 | // happily parse something like: |
| 2404 | // |
| 2405 | // class X { |
| 2406 | // float operator bool(); |
| 2407 | // }; |
| 2408 | // |
| 2409 | // The return type will be changed later anyway. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2410 | Diag(D.getIdentifierLoc(), diag::err_conv_function_return_type) |
| 2411 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 2412 | << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2413 | } |
| 2414 | |
| 2415 | // Make sure we don't have any parameters. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2416 | if (R->getAs<FunctionProtoType>()->getNumArgs() > 0) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2417 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_params); |
| 2418 | |
| 2419 | // Delete the parameters. |
Chris Lattner | 1833a83 | 2009-01-20 21:06:38 +0000 | [diff] [blame] | 2420 | D.getTypeObject(0).Fun.freeArgs(); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2421 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2422 | } |
| 2423 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2424 | // Make sure the conversion function isn't variadic. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2425 | if (R->getAs<FunctionProtoType>()->isVariadic() && !D.isInvalidType()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2426 | Diag(D.getIdentifierLoc(), diag::err_conv_function_variadic); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2427 | D.setInvalidType(); |
| 2428 | } |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2429 | |
| 2430 | // C++ [class.conv.fct]p4: |
| 2431 | // The conversion-type-id shall not represent a function type nor |
| 2432 | // an array type. |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2433 | QualType ConvType = GetTypeFromParser(D.getName().ConversionFunctionId); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2434 | if (ConvType->isArrayType()) { |
| 2435 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_array); |
| 2436 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2437 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2438 | } else if (ConvType->isFunctionType()) { |
| 2439 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_function); |
| 2440 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2441 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2442 | } |
| 2443 | |
| 2444 | // Rebuild the function type "R" without any parameters (in case any |
| 2445 | // of the errors above fired) and with the conversion type as the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2446 | // return type. |
| 2447 | R = Context.getFunctionType(ConvType, 0, 0, false, |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2448 | R->getAs<FunctionProtoType>()->getTypeQuals()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2449 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2450 | // C++0x explicit conversion operators. |
| 2451 | if (D.getDeclSpec().isExplicitSpecified() && !getLangOptions().CPlusPlus0x) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2452 | Diag(D.getDeclSpec().getExplicitSpecLoc(), |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2453 | diag::warn_explicit_conversion_functions) |
| 2454 | << SourceRange(D.getDeclSpec().getExplicitSpecLoc()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2455 | } |
| 2456 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2457 | /// ActOnConversionDeclarator - Called by ActOnDeclarator to complete |
| 2458 | /// the declaration of the given C++ conversion function. This routine |
| 2459 | /// is responsible for recording the conversion function in the C++ |
| 2460 | /// class, if possible. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2461 | Sema::DeclPtrTy Sema::ActOnConversionDeclarator(CXXConversionDecl *Conversion) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2462 | assert(Conversion && "Expected to receive a conversion function declaration"); |
| 2463 | |
Douglas Gregor | 9d35097 | 2008-12-12 08:25:50 +0000 | [diff] [blame] | 2464 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Conversion->getDeclContext()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2465 | |
| 2466 | // Make sure we aren't redeclaring the conversion function. |
| 2467 | QualType ConvType = Context.getCanonicalType(Conversion->getConversionType()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2468 | |
| 2469 | // C++ [class.conv.fct]p1: |
| 2470 | // [...] A conversion function is never used to convert a |
| 2471 | // (possibly cv-qualified) object to the (possibly cv-qualified) |
| 2472 | // same object type (or a reference to it), to a (possibly |
| 2473 | // cv-qualified) base class of that type (or a reference to it), |
| 2474 | // or to (possibly cv-qualified) void. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2475 | // FIXME: Suppress this warning if the conversion function ends up being a |
| 2476 | // virtual function that overrides a virtual function in a base class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2477 | QualType ClassType |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2478 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2479 | if (const ReferenceType *ConvTypeRef = ConvType->getAs<ReferenceType>()) |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2480 | ConvType = ConvTypeRef->getPointeeType(); |
| 2481 | if (ConvType->isRecordType()) { |
| 2482 | ConvType = Context.getCanonicalType(ConvType).getUnqualifiedType(); |
| 2483 | if (ConvType == ClassType) |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 2484 | Diag(Conversion->getLocation(), diag::warn_conv_to_self_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2485 | << ClassType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2486 | else if (IsDerivedFrom(ClassType, ConvType)) |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 2487 | Diag(Conversion->getLocation(), diag::warn_conv_to_base_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2488 | << ClassType << ConvType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2489 | } else if (ConvType->isVoidType()) { |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 2490 | Diag(Conversion->getLocation(), diag::warn_conv_to_void_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2491 | << ClassType << ConvType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2492 | } |
| 2493 | |
Douglas Gregor | 70316a0 | 2008-12-26 15:00:45 +0000 | [diff] [blame] | 2494 | if (Conversion->getPreviousDeclaration()) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2495 | const NamedDecl *ExpectedPrevDecl = Conversion->getPreviousDeclaration(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2496 | if (FunctionTemplateDecl *ConversionTemplate |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2497 | = Conversion->getDescribedFunctionTemplate()) |
| 2498 | ExpectedPrevDecl = ConversionTemplate->getPreviousDeclaration(); |
Douglas Gregor | 70316a0 | 2008-12-26 15:00:45 +0000 | [diff] [blame] | 2499 | OverloadedFunctionDecl *Conversions = ClassDecl->getConversionFunctions(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2500 | for (OverloadedFunctionDecl::function_iterator |
Douglas Gregor | 70316a0 | 2008-12-26 15:00:45 +0000 | [diff] [blame] | 2501 | Conv = Conversions->function_begin(), |
| 2502 | ConvEnd = Conversions->function_end(); |
| 2503 | Conv != ConvEnd; ++Conv) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2504 | if (*Conv == ExpectedPrevDecl) { |
Douglas Gregor | 70316a0 | 2008-12-26 15:00:45 +0000 | [diff] [blame] | 2505 | *Conv = Conversion; |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2506 | return DeclPtrTy::make(Conversion); |
Douglas Gregor | 70316a0 | 2008-12-26 15:00:45 +0000 | [diff] [blame] | 2507 | } |
| 2508 | } |
| 2509 | assert(Conversion->isInvalidDecl() && "Conversion should not get here."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2510 | } else if (FunctionTemplateDecl *ConversionTemplate |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2511 | = Conversion->getDescribedFunctionTemplate()) |
Fariborz Jahanian | debc629 | 2009-09-12 19:02:34 +0000 | [diff] [blame] | 2512 | ClassDecl->addConversionFunction(ConversionTemplate); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2513 | else if (!Conversion->getPrimaryTemplate()) // ignore specializations |
Fariborz Jahanian | debc629 | 2009-09-12 19:02:34 +0000 | [diff] [blame] | 2514 | ClassDecl->addConversionFunction(Conversion); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2515 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2516 | return DeclPtrTy::make(Conversion); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2517 | } |
| 2518 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2519 | //===----------------------------------------------------------------------===// |
| 2520 | // Namespace Handling |
| 2521 | //===----------------------------------------------------------------------===// |
| 2522 | |
| 2523 | /// ActOnStartNamespaceDef - This is called at the start of a namespace |
| 2524 | /// definition. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2525 | Sema::DeclPtrTy Sema::ActOnStartNamespaceDef(Scope *NamespcScope, |
| 2526 | SourceLocation IdentLoc, |
| 2527 | IdentifierInfo *II, |
| 2528 | SourceLocation LBrace) { |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2529 | NamespaceDecl *Namespc = |
| 2530 | NamespaceDecl::Create(Context, CurContext, IdentLoc, II); |
| 2531 | Namespc->setLBracLoc(LBrace); |
| 2532 | |
| 2533 | Scope *DeclRegionScope = NamespcScope->getParent(); |
| 2534 | |
| 2535 | if (II) { |
| 2536 | // C++ [namespace.def]p2: |
| 2537 | // The identifier in an original-namespace-definition shall not have been |
| 2538 | // previously defined in the declarative region in which the |
| 2539 | // original-namespace-definition appears. The identifier in an |
| 2540 | // original-namespace-definition is the name of the namespace. Subsequently |
| 2541 | // in that declarative region, it is treated as an original-namespace-name. |
| 2542 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 2543 | NamedDecl *PrevDecl |
| 2544 | = LookupSingleName(DeclRegionScope, II, LookupOrdinaryName, true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2545 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2546 | if (NamespaceDecl *OrigNS = dyn_cast_or_null<NamespaceDecl>(PrevDecl)) { |
| 2547 | // This is an extended namespace definition. |
| 2548 | // Attach this namespace decl to the chain of extended namespace |
| 2549 | // definitions. |
| 2550 | OrigNS->setNextNamespace(Namespc); |
| 2551 | Namespc->setOriginalNamespace(OrigNS->getOriginalNamespace()); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2552 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2553 | // Remove the previous declaration from the scope. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2554 | if (DeclRegionScope->isDeclScope(DeclPtrTy::make(OrigNS))) { |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 2555 | IdResolver.RemoveDecl(OrigNS); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2556 | DeclRegionScope->RemoveDecl(DeclPtrTy::make(OrigNS)); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2557 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2558 | } else if (PrevDecl) { |
| 2559 | // This is an invalid name redefinition. |
| 2560 | Diag(Namespc->getLocation(), diag::err_redefinition_different_kind) |
| 2561 | << Namespc->getDeclName(); |
| 2562 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 2563 | Namespc->setInvalidDecl(); |
| 2564 | // Continue on to push Namespc as current DeclContext and return it. |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 2565 | } else if (II->isStr("std") && |
| 2566 | CurContext->getLookupContext()->isTranslationUnit()) { |
| 2567 | // This is the first "real" definition of the namespace "std", so update |
| 2568 | // our cache of the "std" namespace to point at this definition. |
| 2569 | if (StdNamespace) { |
| 2570 | // We had already defined a dummy namespace "std". Link this new |
| 2571 | // namespace definition to the dummy namespace "std". |
| 2572 | StdNamespace->setNextNamespace(Namespc); |
| 2573 | StdNamespace->setLocation(IdentLoc); |
| 2574 | Namespc->setOriginalNamespace(StdNamespace->getOriginalNamespace()); |
| 2575 | } |
| 2576 | |
| 2577 | // Make our StdNamespace cache point at the first real definition of the |
| 2578 | // "std" namespace. |
| 2579 | StdNamespace = Namespc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2580 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2581 | |
| 2582 | PushOnScopeChains(Namespc, DeclRegionScope); |
| 2583 | } else { |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 2584 | // Anonymous namespaces. |
| 2585 | |
| 2586 | // C++ [namespace.unnamed]p1. An unnamed-namespace-definition |
| 2587 | // behaves as if it were replaced by |
| 2588 | // namespace unique { /* empty body */ } |
| 2589 | // using namespace unique; |
| 2590 | // namespace unique { namespace-body } |
| 2591 | // where all occurrences of 'unique' in a translation unit are |
| 2592 | // replaced by the same identifier and this identifier differs |
| 2593 | // from all other identifiers in the entire program. |
| 2594 | |
| 2595 | // We just create the namespace with an empty name and then add an |
| 2596 | // implicit using declaration, just like the standard suggests. |
| 2597 | // |
| 2598 | // CodeGen enforces the "universally unique" aspect by giving all |
| 2599 | // declarations semantically contained within an anonymous |
| 2600 | // namespace internal linkage. |
| 2601 | |
| 2602 | assert(Namespc->isAnonymousNamespace()); |
| 2603 | CurContext->addDecl(Namespc); |
| 2604 | |
| 2605 | UsingDirectiveDecl* UD |
| 2606 | = UsingDirectiveDecl::Create(Context, CurContext, |
| 2607 | /* 'using' */ LBrace, |
| 2608 | /* 'namespace' */ SourceLocation(), |
| 2609 | /* qualifier */ SourceRange(), |
| 2610 | /* NNS */ NULL, |
| 2611 | /* identifier */ SourceLocation(), |
| 2612 | Namespc, |
| 2613 | /* Ancestor */ CurContext); |
| 2614 | UD->setImplicit(); |
| 2615 | CurContext->addDecl(UD); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2616 | } |
| 2617 | |
| 2618 | // Although we could have an invalid decl (i.e. the namespace name is a |
| 2619 | // redefinition), push it as current DeclContext and try to continue parsing. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2620 | // FIXME: We should be able to push Namespc here, so that the each DeclContext |
| 2621 | // for the namespace has the declarations that showed up in that particular |
| 2622 | // namespace definition. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2623 | PushDeclContext(NamespcScope, Namespc); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2624 | return DeclPtrTy::make(Namespc); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2625 | } |
| 2626 | |
| 2627 | /// ActOnFinishNamespaceDef - This callback is called after a namespace is |
| 2628 | /// exited. Decl is the DeclTy returned by ActOnStartNamespaceDef. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2629 | void Sema::ActOnFinishNamespaceDef(DeclPtrTy D, SourceLocation RBrace) { |
| 2630 | Decl *Dcl = D.getAs<Decl>(); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2631 | NamespaceDecl *Namespc = dyn_cast_or_null<NamespaceDecl>(Dcl); |
| 2632 | assert(Namespc && "Invalid parameter, expected NamespaceDecl"); |
| 2633 | Namespc->setRBracLoc(RBrace); |
| 2634 | PopDeclContext(); |
| 2635 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2636 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2637 | Sema::DeclPtrTy Sema::ActOnUsingDirective(Scope *S, |
| 2638 | SourceLocation UsingLoc, |
| 2639 | SourceLocation NamespcLoc, |
| 2640 | const CXXScopeSpec &SS, |
| 2641 | SourceLocation IdentLoc, |
| 2642 | IdentifierInfo *NamespcName, |
| 2643 | AttributeList *AttrList) { |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 2644 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
| 2645 | assert(NamespcName && "Invalid NamespcName."); |
| 2646 | assert(IdentLoc.isValid() && "Invalid NamespceName location."); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2647 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 2648 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2649 | UsingDirectiveDecl *UDir = 0; |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 2650 | |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 2651 | // Lookup namespace name. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 2652 | LookupResult R; |
| 2653 | LookupParsedName(R, S, &SS, NamespcName, LookupNamespaceName, false); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2654 | if (R.isAmbiguous()) { |
| 2655 | DiagnoseAmbiguousLookup(R, NamespcName, IdentLoc); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2656 | return DeclPtrTy(); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2657 | } |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 2658 | if (!R.empty()) { |
| 2659 | NamedDecl *NS = R.getFoundDecl(); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 2660 | assert(isa<NamespaceDecl>(NS) && "expected namespace decl"); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2661 | // C++ [namespace.udir]p1: |
| 2662 | // A using-directive specifies that the names in the nominated |
| 2663 | // namespace can be used in the scope in which the |
| 2664 | // using-directive appears after the using-directive. During |
| 2665 | // unqualified name lookup (3.4.1), the names appear as if they |
| 2666 | // were declared in the nearest enclosing namespace which |
| 2667 | // contains both the using-directive and the nominated |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 2668 | // namespace. [Note: in this context, "contains" means "contains |
| 2669 | // directly or indirectly". ] |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2670 | |
| 2671 | // Find enclosing context containing both using-directive and |
| 2672 | // nominated namespace. |
| 2673 | DeclContext *CommonAncestor = cast<DeclContext>(NS); |
| 2674 | while (CommonAncestor && !CommonAncestor->Encloses(CurContext)) |
| 2675 | CommonAncestor = CommonAncestor->getParent(); |
| 2676 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2677 | UDir = UsingDirectiveDecl::Create(Context, |
Douglas Gregor | 8419fa3 | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 2678 | CurContext, UsingLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2679 | NamespcLoc, |
Douglas Gregor | 8419fa3 | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 2680 | SS.getRange(), |
| 2681 | (NestedNameSpecifier *)SS.getScopeRep(), |
| 2682 | IdentLoc, |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2683 | cast<NamespaceDecl>(NS), |
| 2684 | CommonAncestor); |
| 2685 | PushUsingDirective(S, UDir); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 2686 | } else { |
Chris Lattner | ead013e | 2009-01-06 07:24:29 +0000 | [diff] [blame] | 2687 | Diag(IdentLoc, diag::err_expected_namespace_name) << SS.getRange(); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 2688 | } |
| 2689 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2690 | // FIXME: We ignore attributes for now. |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 2691 | delete AttrList; |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2692 | return DeclPtrTy::make(UDir); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2693 | } |
| 2694 | |
| 2695 | void Sema::PushUsingDirective(Scope *S, UsingDirectiveDecl *UDir) { |
| 2696 | // If scope has associated entity, then using directive is at namespace |
| 2697 | // or translation unit scope. We add UsingDirectiveDecls, into |
| 2698 | // it's lookup structure. |
| 2699 | if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity())) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2700 | Ctx->addDecl(UDir); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2701 | else |
| 2702 | // Otherwise it is block-sope. using-directives will affect lookup |
| 2703 | // only to the end of scope. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2704 | S->PushUsingDirective(DeclPtrTy::make(UDir)); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 2705 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2706 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2707 | |
| 2708 | Sema::DeclPtrTy Sema::ActOnUsingDeclaration(Scope *S, |
Anders Carlsson | 595adc1 | 2009-08-29 19:54:19 +0000 | [diff] [blame] | 2709 | AccessSpecifier AS, |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 2710 | SourceLocation UsingLoc, |
| 2711 | const CXXScopeSpec &SS, |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 2712 | UnqualifiedId &Name, |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 2713 | AttributeList *AttrList, |
| 2714 | bool IsTypeName) { |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2715 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2716 | |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 2717 | switch (Name.getKind()) { |
| 2718 | case UnqualifiedId::IK_Identifier: |
| 2719 | case UnqualifiedId::IK_OperatorFunctionId: |
| 2720 | case UnqualifiedId::IK_ConversionFunctionId: |
| 2721 | break; |
| 2722 | |
| 2723 | case UnqualifiedId::IK_ConstructorName: |
| 2724 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_constructor) |
| 2725 | << SS.getRange(); |
| 2726 | return DeclPtrTy(); |
| 2727 | |
| 2728 | case UnqualifiedId::IK_DestructorName: |
| 2729 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_destructor) |
| 2730 | << SS.getRange(); |
| 2731 | return DeclPtrTy(); |
| 2732 | |
| 2733 | case UnqualifiedId::IK_TemplateId: |
| 2734 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_template_id) |
| 2735 | << SourceRange(Name.TemplateId->LAngleLoc, Name.TemplateId->RAngleLoc); |
| 2736 | return DeclPtrTy(); |
| 2737 | } |
| 2738 | |
| 2739 | DeclarationName TargetName = GetNameFromUnqualifiedId(Name); |
| 2740 | NamedDecl *UD = BuildUsingDeclaration(UsingLoc, SS, |
| 2741 | Name.getSourceRange().getBegin(), |
| 2742 | TargetName, AttrList, IsTypeName); |
Anders Carlsson | 595adc1 | 2009-08-29 19:54:19 +0000 | [diff] [blame] | 2743 | if (UD) { |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 2744 | PushOnScopeChains(UD, S); |
Anders Carlsson | 595adc1 | 2009-08-29 19:54:19 +0000 | [diff] [blame] | 2745 | UD->setAccess(AS); |
| 2746 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2747 | |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 2748 | return DeclPtrTy::make(UD); |
| 2749 | } |
| 2750 | |
| 2751 | NamedDecl *Sema::BuildUsingDeclaration(SourceLocation UsingLoc, |
| 2752 | const CXXScopeSpec &SS, |
| 2753 | SourceLocation IdentLoc, |
| 2754 | DeclarationName Name, |
| 2755 | AttributeList *AttrList, |
| 2756 | bool IsTypeName) { |
| 2757 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
| 2758 | assert(IdentLoc.isValid() && "Invalid TargetName location."); |
Eli Friedman | 2a16a13 | 2009-08-27 05:09:36 +0000 | [diff] [blame] | 2759 | |
Anders Carlsson | 550b14b | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 2760 | // FIXME: We ignore attributes for now. |
| 2761 | delete AttrList; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2762 | |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 2763 | if (SS.isEmpty()) { |
| 2764 | Diag(IdentLoc, diag::err_using_requires_qualname); |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 2765 | return 0; |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 2766 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2767 | |
| 2768 | NestedNameSpecifier *NNS = |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 2769 | static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 2770 | |
Anders Carlsson | 550b14b | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 2771 | if (isUnknownSpecialization(SS)) { |
| 2772 | return UnresolvedUsingDecl::Create(Context, CurContext, UsingLoc, |
| 2773 | SS.getRange(), NNS, |
| 2774 | IdentLoc, Name, IsTypeName); |
| 2775 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2776 | |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 2777 | DeclContext *LookupContext = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2778 | |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 2779 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 2780 | // C++0x N2914 [namespace.udecl]p3: |
| 2781 | // A using-declaration used as a member-declaration shall refer to a member |
| 2782 | // of a base class of the class being defined, shall refer to a member of an |
| 2783 | // anonymous union that is a member of a base class of the class being |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2784 | // defined, or shall refer to an enumerator for an enumeration type that is |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 2785 | // a member of a base class of the class being defined. |
| 2786 | const Type *Ty = NNS->getAsType(); |
| 2787 | if (!Ty || !IsDerivedFrom(Context.getTagDeclType(RD), QualType(Ty, 0))) { |
| 2788 | Diag(SS.getRange().getBegin(), |
| 2789 | diag::err_using_decl_nested_name_specifier_is_not_a_base_class) |
| 2790 | << NNS << RD->getDeclName(); |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 2791 | return 0; |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 2792 | } |
Anders Carlsson | 0dde18e | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 2793 | |
| 2794 | QualType BaseTy = Context.getCanonicalType(QualType(Ty, 0)); |
| 2795 | LookupContext = BaseTy->getAs<RecordType>()->getDecl(); |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 2796 | } else { |
| 2797 | // C++0x N2914 [namespace.udecl]p8: |
| 2798 | // A using-declaration for a class member shall be a member-declaration. |
| 2799 | if (NNS->getKind() == NestedNameSpecifier::TypeSpec) { |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 2800 | Diag(IdentLoc, diag::err_using_decl_can_not_refer_to_class_member) |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 2801 | << SS.getRange(); |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 2802 | return 0; |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 2803 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2804 | |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 2805 | // C++0x N2914 [namespace.udecl]p9: |
| 2806 | // In a using-declaration, a prefix :: refers to the global namespace. |
| 2807 | if (NNS->getKind() == NestedNameSpecifier::Global) |
| 2808 | LookupContext = Context.getTranslationUnitDecl(); |
| 2809 | else |
| 2810 | LookupContext = NNS->getAsNamespace(); |
| 2811 | } |
| 2812 | |
| 2813 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2814 | // Lookup target name. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 2815 | LookupResult R; |
| 2816 | LookupQualifiedName(R, LookupContext, Name, LookupOrdinaryName); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2817 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 2818 | if (R.empty()) { |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 2819 | Diag(IdentLoc, diag::err_no_member) |
| 2820 | << Name << LookupContext << SS.getRange(); |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 2821 | return 0; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2822 | } |
| 2823 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 2824 | // FIXME: handle ambiguity? |
| 2825 | NamedDecl *ND = R.getAsSingleDecl(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2826 | |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 2827 | if (IsTypeName && !isa<TypeDecl>(ND)) { |
| 2828 | Diag(IdentLoc, diag::err_using_typename_non_type); |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 2829 | return 0; |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 2830 | } |
| 2831 | |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 2832 | // C++0x N2914 [namespace.udecl]p6: |
| 2833 | // A using-declaration shall not name a namespace. |
| 2834 | if (isa<NamespaceDecl>(ND)) { |
| 2835 | Diag(IdentLoc, diag::err_using_decl_can_not_refer_to_namespace) |
| 2836 | << SS.getRange(); |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 2837 | return 0; |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 2838 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2839 | |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 2840 | return UsingDecl::Create(Context, CurContext, IdentLoc, SS.getRange(), |
| 2841 | ND->getLocation(), UsingLoc, ND, NNS, IsTypeName); |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2842 | } |
| 2843 | |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 2844 | /// getNamespaceDecl - Returns the namespace a decl represents. If the decl |
| 2845 | /// is a namespace alias, returns the namespace it points to. |
| 2846 | static inline NamespaceDecl *getNamespaceDecl(NamedDecl *D) { |
| 2847 | if (NamespaceAliasDecl *AD = dyn_cast_or_null<NamespaceAliasDecl>(D)) |
| 2848 | return AD->getNamespace(); |
| 2849 | return dyn_cast_or_null<NamespaceDecl>(D); |
| 2850 | } |
| 2851 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2852 | Sema::DeclPtrTy Sema::ActOnNamespaceAliasDef(Scope *S, |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 2853 | SourceLocation NamespaceLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2854 | SourceLocation AliasLoc, |
| 2855 | IdentifierInfo *Alias, |
| 2856 | const CXXScopeSpec &SS, |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 2857 | SourceLocation IdentLoc, |
| 2858 | IdentifierInfo *Ident) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2859 | |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 2860 | // Lookup the namespace name. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 2861 | LookupResult R; |
| 2862 | LookupParsedName(R, S, &SS, Ident, LookupNamespaceName, false); |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 2863 | |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 2864 | // Check if we have a previous declaration with the same name. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 2865 | if (NamedDecl *PrevDecl |
| 2866 | = LookupSingleName(S, Alias, LookupOrdinaryName, true)) { |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 2867 | if (NamespaceAliasDecl *AD = dyn_cast<NamespaceAliasDecl>(PrevDecl)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2868 | // We already have an alias with the same name that points to the same |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 2869 | // namespace, so don't create a new one. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 2870 | if (!R.isAmbiguous() && !R.empty() && |
| 2871 | AD->getNamespace() == getNamespaceDecl(R.getFoundDecl())) |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 2872 | return DeclPtrTy(); |
| 2873 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2874 | |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 2875 | unsigned DiagID = isa<NamespaceDecl>(PrevDecl) ? diag::err_redefinition : |
| 2876 | diag::err_redefinition_different_kind; |
| 2877 | Diag(AliasLoc, DiagID) << Alias; |
| 2878 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2879 | return DeclPtrTy(); |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 2880 | } |
| 2881 | |
Anders Carlsson | 5721c68 | 2009-03-28 06:42:02 +0000 | [diff] [blame] | 2882 | if (R.isAmbiguous()) { |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 2883 | DiagnoseAmbiguousLookup(R, Ident, IdentLoc); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2884 | return DeclPtrTy(); |
Anders Carlsson | 5721c68 | 2009-03-28 06:42:02 +0000 | [diff] [blame] | 2885 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2886 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 2887 | if (R.empty()) { |
Anders Carlsson | 5721c68 | 2009-03-28 06:42:02 +0000 | [diff] [blame] | 2888 | Diag(NamespaceLoc, diag::err_expected_namespace_name) << SS.getRange(); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2889 | return DeclPtrTy(); |
Anders Carlsson | 5721c68 | 2009-03-28 06:42:02 +0000 | [diff] [blame] | 2890 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2891 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2892 | NamespaceAliasDecl *AliasDecl = |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2893 | NamespaceAliasDecl::Create(Context, CurContext, NamespaceLoc, AliasLoc, |
| 2894 | Alias, SS.getRange(), |
Douglas Gregor | 6c9c940 | 2009-05-30 06:48:27 +0000 | [diff] [blame] | 2895 | (NestedNameSpecifier *)SS.getScopeRep(), |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 2896 | IdentLoc, R.getFoundDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2897 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2898 | CurContext->addDecl(AliasDecl); |
Anders Carlsson | 68771c7 | 2009-03-28 22:58:02 +0000 | [diff] [blame] | 2899 | return DeclPtrTy::make(AliasDecl); |
Anders Carlsson | dbb0094 | 2009-03-28 05:27:17 +0000 | [diff] [blame] | 2900 | } |
| 2901 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2902 | void Sema::DefineImplicitDefaultConstructor(SourceLocation CurrentLocation, |
| 2903 | CXXConstructorDecl *Constructor) { |
Fariborz Jahanian | 05a5c45 | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 2904 | assert((Constructor->isImplicit() && Constructor->isDefaultConstructor() && |
| 2905 | !Constructor->isUsed()) && |
| 2906 | "DefineImplicitDefaultConstructor - call it for implicit default ctor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2907 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2908 | CXXRecordDecl *ClassDecl |
| 2909 | = cast<CXXRecordDecl>(Constructor->getDeclContext()); |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 2910 | assert(ClassDecl && "DefineImplicitDefaultConstructor - invalid constructor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2911 | // Before the implicitly-declared default constructor for a class is |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2912 | // implicitly defined, all the implicitly-declared default constructors |
| 2913 | // for its base class and its non-static data members shall have been |
| 2914 | // implicitly defined. |
| 2915 | bool err = false; |
Fariborz Jahanian | 514b7b1 | 2009-06-30 16:36:53 +0000 | [diff] [blame] | 2916 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 2917 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2918 | CXXRecordDecl *BaseClassDecl |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2919 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2920 | if (!BaseClassDecl->hasTrivialConstructor()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2921 | if (CXXConstructorDecl *BaseCtor = |
Fariborz Jahanian | 220a0f3 | 2009-06-23 23:42:10 +0000 | [diff] [blame] | 2922 | BaseClassDecl->getDefaultConstructor(Context)) |
| 2923 | MarkDeclarationReferenced(CurrentLocation, BaseCtor); |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2924 | else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2925 | Diag(CurrentLocation, diag::err_defining_default_ctor) |
Fariborz Jahanian | 0c728f1 | 2009-10-08 22:15:49 +0000 | [diff] [blame] | 2926 | << Context.getTagDeclType(ClassDecl) << 0 |
Fariborz Jahanian | 3da83eb | 2009-06-20 20:23:38 +0000 | [diff] [blame] | 2927 | << Context.getTagDeclType(BaseClassDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2928 | Diag(BaseClassDecl->getLocation(), diag::note_previous_class_decl) |
Fariborz Jahanian | 3da83eb | 2009-06-20 20:23:38 +0000 | [diff] [blame] | 2929 | << Context.getTagDeclType(BaseClassDecl); |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2930 | err = true; |
| 2931 | } |
| 2932 | } |
| 2933 | } |
Fariborz Jahanian | 514b7b1 | 2009-06-30 16:36:53 +0000 | [diff] [blame] | 2934 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 2935 | E = ClassDecl->field_end(); Field != E; ++Field) { |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2936 | QualType FieldType = Context.getCanonicalType((*Field)->getType()); |
| 2937 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 2938 | FieldType = Array->getElementType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2939 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2940 | CXXRecordDecl *FieldClassDecl |
| 2941 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Duncan Sands | 6887e63 | 2009-06-25 09:03:06 +0000 | [diff] [blame] | 2942 | if (!FieldClassDecl->hasTrivialConstructor()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2943 | if (CXXConstructorDecl *FieldCtor = |
Fariborz Jahanian | 220a0f3 | 2009-06-23 23:42:10 +0000 | [diff] [blame] | 2944 | FieldClassDecl->getDefaultConstructor(Context)) |
| 2945 | MarkDeclarationReferenced(CurrentLocation, FieldCtor); |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2946 | else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2947 | Diag(CurrentLocation, diag::err_defining_default_ctor) |
Fariborz Jahanian | 0c728f1 | 2009-10-08 22:15:49 +0000 | [diff] [blame] | 2948 | << Context.getTagDeclType(ClassDecl) << 1 << |
Fariborz Jahanian | 3da83eb | 2009-06-20 20:23:38 +0000 | [diff] [blame] | 2949 | Context.getTagDeclType(FieldClassDecl); |
Fariborz Jahanian | 0c728f1 | 2009-10-08 22:15:49 +0000 | [diff] [blame] | 2950 | Diag((*Field)->getLocation(), diag::note_field_decl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2951 | Diag(FieldClassDecl->getLocation(), diag::note_previous_class_decl) |
Fariborz Jahanian | 3da83eb | 2009-06-20 20:23:38 +0000 | [diff] [blame] | 2952 | << Context.getTagDeclType(FieldClassDecl); |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2953 | err = true; |
| 2954 | } |
| 2955 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2956 | } else if (FieldType->isReferenceType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2957 | Diag(CurrentLocation, diag::err_unintialized_member) |
Anders Carlsson | 5eda816 | 2009-07-09 17:37:12 +0000 | [diff] [blame] | 2958 | << Context.getTagDeclType(ClassDecl) << 0 << Field->getDeclName(); |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2959 | Diag((*Field)->getLocation(), diag::note_declared_at); |
| 2960 | err = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2961 | } else if (FieldType.isConstQualified()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2962 | Diag(CurrentLocation, diag::err_unintialized_member) |
Anders Carlsson | 5eda816 | 2009-07-09 17:37:12 +0000 | [diff] [blame] | 2963 | << Context.getTagDeclType(ClassDecl) << 1 << Field->getDeclName(); |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2964 | Diag((*Field)->getLocation(), diag::note_declared_at); |
| 2965 | err = true; |
| 2966 | } |
| 2967 | } |
| 2968 | if (!err) |
Fariborz Jahanian | c6249b9 | 2009-06-26 16:08:57 +0000 | [diff] [blame] | 2969 | Constructor->setUsed(); |
| 2970 | else |
| 2971 | Constructor->setInvalidDecl(); |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2972 | } |
| 2973 | |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 2974 | void Sema::DefineImplicitDestructor(SourceLocation CurrentLocation, |
Douglas Gregor | 4fe95f9 | 2009-09-04 19:04:08 +0000 | [diff] [blame] | 2975 | CXXDestructorDecl *Destructor) { |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 2976 | assert((Destructor->isImplicit() && !Destructor->isUsed()) && |
| 2977 | "DefineImplicitDestructor - call it for implicit default dtor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2978 | |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 2979 | CXXRecordDecl *ClassDecl |
| 2980 | = cast<CXXRecordDecl>(Destructor->getDeclContext()); |
| 2981 | assert(ClassDecl && "DefineImplicitDestructor - invalid destructor"); |
| 2982 | // C++ [class.dtor] p5 |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2983 | // Before the implicitly-declared default destructor for a class is |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 2984 | // implicitly defined, all the implicitly-declared default destructors |
| 2985 | // for its base class and its non-static data members shall have been |
| 2986 | // implicitly defined. |
Fariborz Jahanian | 514b7b1 | 2009-06-30 16:36:53 +0000 | [diff] [blame] | 2987 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 2988 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 2989 | CXXRecordDecl *BaseClassDecl |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2990 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 2991 | if (!BaseClassDecl->hasTrivialDestructor()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2992 | if (CXXDestructorDecl *BaseDtor = |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 2993 | const_cast<CXXDestructorDecl*>(BaseClassDecl->getDestructor(Context))) |
| 2994 | MarkDeclarationReferenced(CurrentLocation, BaseDtor); |
| 2995 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2996 | assert(false && |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 2997 | "DefineImplicitDestructor - missing dtor in a base class"); |
| 2998 | } |
| 2999 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3000 | |
Fariborz Jahanian | 514b7b1 | 2009-06-30 16:36:53 +0000 | [diff] [blame] | 3001 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 3002 | E = ClassDecl->field_end(); Field != E; ++Field) { |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 3003 | QualType FieldType = Context.getCanonicalType((*Field)->getType()); |
| 3004 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 3005 | FieldType = Array->getElementType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3006 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 3007 | CXXRecordDecl *FieldClassDecl |
| 3008 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
| 3009 | if (!FieldClassDecl->hasTrivialDestructor()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3010 | if (CXXDestructorDecl *FieldDtor = |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 3011 | const_cast<CXXDestructorDecl*>( |
| 3012 | FieldClassDecl->getDestructor(Context))) |
| 3013 | MarkDeclarationReferenced(CurrentLocation, FieldDtor); |
| 3014 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3015 | assert(false && |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 3016 | "DefineImplicitDestructor - missing dtor in class of a data member"); |
| 3017 | } |
| 3018 | } |
| 3019 | } |
| 3020 | Destructor->setUsed(); |
| 3021 | } |
| 3022 | |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3023 | void Sema::DefineImplicitOverloadedAssign(SourceLocation CurrentLocation, |
| 3024 | CXXMethodDecl *MethodDecl) { |
| 3025 | assert((MethodDecl->isImplicit() && MethodDecl->isOverloadedOperator() && |
| 3026 | MethodDecl->getOverloadedOperator() == OO_Equal && |
| 3027 | !MethodDecl->isUsed()) && |
| 3028 | "DefineImplicitOverloadedAssign - call it for implicit assignment op"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3029 | |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3030 | CXXRecordDecl *ClassDecl |
| 3031 | = cast<CXXRecordDecl>(MethodDecl->getDeclContext()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3032 | |
Fariborz Jahanian | c6249b9 | 2009-06-26 16:08:57 +0000 | [diff] [blame] | 3033 | // C++[class.copy] p12 |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3034 | // Before the implicitly-declared copy assignment operator for a class is |
| 3035 | // implicitly defined, all implicitly-declared copy assignment operators |
| 3036 | // for its direct base classes and its nonstatic data members shall have |
| 3037 | // been implicitly defined. |
| 3038 | bool err = false; |
Fariborz Jahanian | 514b7b1 | 2009-06-30 16:36:53 +0000 | [diff] [blame] | 3039 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 3040 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3041 | CXXRecordDecl *BaseClassDecl |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3042 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3043 | if (CXXMethodDecl *BaseAssignOpMethod = |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3044 | getAssignOperatorMethod(MethodDecl->getParamDecl(0), BaseClassDecl)) |
| 3045 | MarkDeclarationReferenced(CurrentLocation, BaseAssignOpMethod); |
| 3046 | } |
Fariborz Jahanian | 514b7b1 | 2009-06-30 16:36:53 +0000 | [diff] [blame] | 3047 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 3048 | E = ClassDecl->field_end(); Field != E; ++Field) { |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3049 | QualType FieldType = Context.getCanonicalType((*Field)->getType()); |
| 3050 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 3051 | FieldType = Array->getElementType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3052 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3053 | CXXRecordDecl *FieldClassDecl |
| 3054 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3055 | if (CXXMethodDecl *FieldAssignOpMethod = |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3056 | getAssignOperatorMethod(MethodDecl->getParamDecl(0), FieldClassDecl)) |
| 3057 | MarkDeclarationReferenced(CurrentLocation, FieldAssignOpMethod); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 3058 | } else if (FieldType->isReferenceType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3059 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
Anders Carlsson | 5e09d4c | 2009-07-09 17:47:25 +0000 | [diff] [blame] | 3060 | << Context.getTagDeclType(ClassDecl) << 0 << Field->getDeclName(); |
| 3061 | Diag(Field->getLocation(), diag::note_declared_at); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3062 | Diag(CurrentLocation, diag::note_first_required_here); |
| 3063 | err = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 3064 | } else if (FieldType.isConstQualified()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3065 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
Anders Carlsson | 5e09d4c | 2009-07-09 17:47:25 +0000 | [diff] [blame] | 3066 | << Context.getTagDeclType(ClassDecl) << 1 << Field->getDeclName(); |
| 3067 | Diag(Field->getLocation(), diag::note_declared_at); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3068 | Diag(CurrentLocation, diag::note_first_required_here); |
| 3069 | err = true; |
| 3070 | } |
| 3071 | } |
| 3072 | if (!err) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3073 | MethodDecl->setUsed(); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3074 | } |
| 3075 | |
| 3076 | CXXMethodDecl * |
| 3077 | Sema::getAssignOperatorMethod(ParmVarDecl *ParmDecl, |
| 3078 | CXXRecordDecl *ClassDecl) { |
| 3079 | QualType LHSType = Context.getTypeDeclType(ClassDecl); |
| 3080 | QualType RHSType(LHSType); |
| 3081 | // If class's assignment operator argument is const/volatile qualified, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3082 | // look for operator = (const/volatile B&). Otherwise, look for |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3083 | // operator = (B&). |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3084 | RHSType = Context.getCVRQualifiedType(RHSType, |
| 3085 | ParmDecl->getType().getCVRQualifiers()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3086 | ExprOwningPtr<Expr> LHS(this, new (Context) DeclRefExpr(ParmDecl, |
| 3087 | LHSType, |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3088 | SourceLocation())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3089 | ExprOwningPtr<Expr> RHS(this, new (Context) DeclRefExpr(ParmDecl, |
| 3090 | RHSType, |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3091 | SourceLocation())); |
| 3092 | Expr *Args[2] = { &*LHS, &*RHS }; |
| 3093 | OverloadCandidateSet CandidateSet; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3094 | AddMemberOperatorCandidates(clang::OO_Equal, SourceLocation(), Args, 2, |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3095 | CandidateSet); |
| 3096 | OverloadCandidateSet::iterator Best; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3097 | if (BestViableFunction(CandidateSet, |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3098 | ClassDecl->getLocation(), Best) == OR_Success) |
| 3099 | return cast<CXXMethodDecl>(Best->Function); |
| 3100 | assert(false && |
| 3101 | "getAssignOperatorMethod - copy assignment operator method not found"); |
| 3102 | return 0; |
| 3103 | } |
| 3104 | |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 3105 | void Sema::DefineImplicitCopyConstructor(SourceLocation CurrentLocation, |
| 3106 | CXXConstructorDecl *CopyConstructor, |
| 3107 | unsigned TypeQuals) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3108 | assert((CopyConstructor->isImplicit() && |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 3109 | CopyConstructor->isCopyConstructor(Context, TypeQuals) && |
| 3110 | !CopyConstructor->isUsed()) && |
| 3111 | "DefineImplicitCopyConstructor - call it for implicit copy ctor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3112 | |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 3113 | CXXRecordDecl *ClassDecl |
| 3114 | = cast<CXXRecordDecl>(CopyConstructor->getDeclContext()); |
| 3115 | assert(ClassDecl && "DefineImplicitCopyConstructor - invalid constructor"); |
Fariborz Jahanian | 220a0f3 | 2009-06-23 23:42:10 +0000 | [diff] [blame] | 3116 | // C++ [class.copy] p209 |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3117 | // Before the implicitly-declared copy constructor for a class is |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 3118 | // implicitly defined, all the implicitly-declared copy constructors |
| 3119 | // for its base class and its non-static data members shall have been |
| 3120 | // implicitly defined. |
| 3121 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(); |
| 3122 | Base != ClassDecl->bases_end(); ++Base) { |
| 3123 | CXXRecordDecl *BaseClassDecl |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3124 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3125 | if (CXXConstructorDecl *BaseCopyCtor = |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 3126 | BaseClassDecl->getCopyConstructor(Context, TypeQuals)) |
Fariborz Jahanian | 220a0f3 | 2009-06-23 23:42:10 +0000 | [diff] [blame] | 3127 | MarkDeclarationReferenced(CurrentLocation, BaseCopyCtor); |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 3128 | } |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3129 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 3130 | FieldEnd = ClassDecl->field_end(); |
| 3131 | Field != FieldEnd; ++Field) { |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 3132 | QualType FieldType = Context.getCanonicalType((*Field)->getType()); |
| 3133 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 3134 | FieldType = Array->getElementType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3135 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 3136 | CXXRecordDecl *FieldClassDecl |
| 3137 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3138 | if (CXXConstructorDecl *FieldCopyCtor = |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 3139 | FieldClassDecl->getCopyConstructor(Context, TypeQuals)) |
Fariborz Jahanian | 220a0f3 | 2009-06-23 23:42:10 +0000 | [diff] [blame] | 3140 | MarkDeclarationReferenced(CurrentLocation, FieldCopyCtor); |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 3141 | } |
| 3142 | } |
| 3143 | CopyConstructor->setUsed(); |
| 3144 | } |
| 3145 | |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 3146 | Sema::OwningExprResult |
Anders Carlsson | ec8e5ea | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 3147 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3148 | CXXConstructorDecl *Constructor, |
Anders Carlsson | f47511a | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 3149 | MultiExprArg ExprArgs) { |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 3150 | bool Elidable = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3151 | |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3152 | // C++ [class.copy]p15: |
| 3153 | // Whenever a temporary class object is copied using a copy constructor, and |
| 3154 | // this object and the copy have the same cv-unqualified type, an |
| 3155 | // implementation is permitted to treat the original and the copy as two |
| 3156 | // different ways of referring to the same object and not perform a copy at |
| 3157 | // all, even if the class copy constructor or destructor have side effects. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3158 | |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 3159 | // FIXME: Is this enough? |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3160 | if (Constructor->isCopyConstructor(Context)) { |
Anders Carlsson | f47511a | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 3161 | Expr *E = ((Expr **)ExprArgs.get())[0]; |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 3162 | while (CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E)) |
| 3163 | E = BE->getSubExpr(); |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3164 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) |
| 3165 | if (ICE->getCastKind() == CastExpr::CK_NoOp) |
| 3166 | E = ICE->getSubExpr(); |
| 3167 | |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 3168 | if (isa<CallExpr>(E) || isa<CXXTemporaryObjectExpr>(E)) |
| 3169 | Elidable = true; |
| 3170 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3171 | |
| 3172 | return BuildCXXConstructExpr(ConstructLoc, DeclInitType, Constructor, |
Anders Carlsson | f47511a | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 3173 | Elidable, move(ExprArgs)); |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 3174 | } |
| 3175 | |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 3176 | /// BuildCXXConstructExpr - Creates a complete call to a constructor, |
| 3177 | /// including handling of its default argument expressions. |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 3178 | Sema::OwningExprResult |
Anders Carlsson | ec8e5ea | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 3179 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
| 3180 | CXXConstructorDecl *Constructor, bool Elidable, |
Anders Carlsson | f47511a | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 3181 | MultiExprArg ExprArgs) { |
| 3182 | unsigned NumExprs = ExprArgs.size(); |
| 3183 | Expr **Exprs = (Expr **)ExprArgs.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3184 | |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3185 | return Owned(CXXConstructExpr::Create(Context, DeclInitType, Constructor, |
| 3186 | Elidable, Exprs, NumExprs)); |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 3187 | } |
| 3188 | |
Anders Carlsson | e7624a7 | 2009-08-27 05:08:22 +0000 | [diff] [blame] | 3189 | Sema::OwningExprResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3190 | Sema::BuildCXXTemporaryObjectExpr(CXXConstructorDecl *Constructor, |
| 3191 | QualType Ty, |
| 3192 | SourceLocation TyBeginLoc, |
Anders Carlsson | e7624a7 | 2009-08-27 05:08:22 +0000 | [diff] [blame] | 3193 | MultiExprArg Args, |
| 3194 | SourceLocation RParenLoc) { |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3195 | unsigned NumExprs = Args.size(); |
| 3196 | Expr **Exprs = (Expr **)Args.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3197 | |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3198 | return Owned(new (Context) CXXTemporaryObjectExpr(Context, Constructor, Ty, |
| 3199 | TyBeginLoc, Exprs, |
| 3200 | NumExprs, RParenLoc)); |
Anders Carlsson | e7624a7 | 2009-08-27 05:08:22 +0000 | [diff] [blame] | 3201 | } |
| 3202 | |
| 3203 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3204 | bool Sema::InitializeVarWithConstructor(VarDecl *VD, |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 3205 | CXXConstructorDecl *Constructor, |
Anders Carlsson | f47511a | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 3206 | MultiExprArg Exprs) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3207 | OwningExprResult TempResult = |
Fariborz Jahanian | c0fcce4 | 2009-10-28 18:41:06 +0000 | [diff] [blame] | 3208 | BuildCXXConstructExpr(VD->getLocation(), VD->getType(), Constructor, |
Anders Carlsson | f47511a | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 3209 | move(Exprs)); |
Anders Carlsson | fe2de49 | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 3210 | if (TempResult.isInvalid()) |
| 3211 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3212 | |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 3213 | Expr *Temp = TempResult.takeAs<Expr>(); |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 3214 | MarkDeclarationReferenced(VD->getLocation(), Constructor); |
Fariborz Jahanian | caa499b | 2009-08-05 18:17:32 +0000 | [diff] [blame] | 3215 | Temp = MaybeCreateCXXExprWithTemporaries(Temp, /*DestroyTemps=*/true); |
Douglas Gregor | 78d1583 | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 3216 | VD->setInit(Context, Temp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3217 | |
Anders Carlsson | fe2de49 | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 3218 | return false; |
Anders Carlsson | 930e8d0 | 2009-04-16 23:50:50 +0000 | [diff] [blame] | 3219 | } |
| 3220 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3221 | void Sema::FinalizeVarWithDestructor(VarDecl *VD, QualType DeclInitType) { |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 3222 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>( |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3223 | DeclInitType->getAs<RecordType>()->getDecl()); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 3224 | if (!ClassDecl->hasTrivialDestructor()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3225 | if (CXXDestructorDecl *Destructor = |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 3226 | const_cast<CXXDestructorDecl*>(ClassDecl->getDestructor(Context))) |
Fariborz Jahanian | a83f7ed | 2009-08-03 19:13:25 +0000 | [diff] [blame] | 3227 | MarkDeclarationReferenced(VD->getLocation(), Destructor); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 3228 | } |
| 3229 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3230 | /// AddCXXDirectInitializerToDecl - This action is called immediately after |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3231 | /// ActOnDeclarator, when a C++ direct initializer is present. |
| 3232 | /// e.g: "int x(1);" |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3233 | void Sema::AddCXXDirectInitializerToDecl(DeclPtrTy Dcl, |
| 3234 | SourceLocation LParenLoc, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 3235 | MultiExprArg Exprs, |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3236 | SourceLocation *CommaLocs, |
| 3237 | SourceLocation RParenLoc) { |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 3238 | unsigned NumExprs = Exprs.size(); |
| 3239 | assert(NumExprs != 0 && Exprs.get() && "missing expressions"); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3240 | Decl *RealDecl = Dcl.getAs<Decl>(); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3241 | |
| 3242 | // If there is no declaration, there was an error parsing it. Just ignore |
| 3243 | // the initializer. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3244 | if (RealDecl == 0) |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3245 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3246 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3247 | VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl); |
| 3248 | if (!VDecl) { |
| 3249 | Diag(RealDecl->getLocation(), diag::err_illegal_initializer); |
| 3250 | RealDecl->setInvalidDecl(); |
| 3251 | return; |
| 3252 | } |
| 3253 | |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 3254 | // We will represent direct-initialization similarly to copy-initialization: |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 3255 | // int x(1); -as-> int x = 1; |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3256 | // ClassType x(a,b,c); -as-> ClassType x = ClassType(a,b,c); |
| 3257 | // |
| 3258 | // Clients that want to distinguish between the two forms, can check for |
| 3259 | // direct initializer using VarDecl::hasCXXDirectInitializer(). |
| 3260 | // A major benefit is that clients that don't particularly care about which |
| 3261 | // exactly form was it (like the CodeGen) can handle both cases without |
| 3262 | // special case code. |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 3263 | |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 3264 | // If either the declaration has a dependent type or if any of the expressions |
| 3265 | // is type-dependent, we represent the initialization via a ParenListExpr for |
| 3266 | // later use during template instantiation. |
| 3267 | if (VDecl->getType()->isDependentType() || |
| 3268 | Expr::hasAnyTypeDependentArguments((Expr **)Exprs.get(), Exprs.size())) { |
| 3269 | // Let clients know that initialization was done with a direct initializer. |
| 3270 | VDecl->setCXXDirectInitializer(true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3271 | |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 3272 | // Store the initialization expressions as a ParenListExpr. |
| 3273 | unsigned NumExprs = Exprs.size(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3274 | VDecl->setInit(Context, |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 3275 | new (Context) ParenListExpr(Context, LParenLoc, |
| 3276 | (Expr **)Exprs.release(), |
| 3277 | NumExprs, RParenLoc)); |
| 3278 | return; |
| 3279 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3280 | |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 3281 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3282 | // C++ 8.5p11: |
| 3283 | // The form of initialization (using parentheses or '=') is generally |
| 3284 | // insignificant, but does matter when the entity being initialized has a |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 3285 | // class type. |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 3286 | QualType DeclInitType = VDecl->getType(); |
| 3287 | if (const ArrayType *Array = Context.getAsArrayType(DeclInitType)) |
Fariborz Jahanian | 680a3f3 | 2009-10-28 19:04:36 +0000 | [diff] [blame] | 3288 | DeclInitType = Context.getBaseElementType(Array); |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 3289 | |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 3290 | // FIXME: This isn't the right place to complete the type. |
| 3291 | if (RequireCompleteType(VDecl->getLocation(), VDecl->getType(), |
| 3292 | diag::err_typecheck_decl_incomplete_type)) { |
| 3293 | VDecl->setInvalidDecl(); |
| 3294 | return; |
| 3295 | } |
| 3296 | |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 3297 | if (VDecl->getType()->isRecordType()) { |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3298 | ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this); |
| 3299 | |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 3300 | CXXConstructorDecl *Constructor |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 3301 | = PerformInitializationByConstructor(DeclInitType, |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3302 | move(Exprs), |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 3303 | VDecl->getLocation(), |
| 3304 | SourceRange(VDecl->getLocation(), |
| 3305 | RParenLoc), |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3306 | VDecl->getDeclName(), |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3307 | IK_Direct, |
| 3308 | ConstructorArgs); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 3309 | if (!Constructor) |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 3310 | RealDecl->setInvalidDecl(); |
Anders Carlsson | ca29ad9 | 2009-04-15 21:48:18 +0000 | [diff] [blame] | 3311 | else { |
Anders Carlsson | ca29ad9 | 2009-04-15 21:48:18 +0000 | [diff] [blame] | 3312 | VDecl->setCXXDirectInitializer(true); |
Fariborz Jahanian | c0fcce4 | 2009-10-28 18:41:06 +0000 | [diff] [blame] | 3313 | if (InitializeVarWithConstructor(VDecl, Constructor, |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3314 | move_arg(ConstructorArgs))) |
Anders Carlsson | fe2de49 | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 3315 | RealDecl->setInvalidDecl(); |
Fariborz Jahanian | a83f7ed | 2009-08-03 19:13:25 +0000 | [diff] [blame] | 3316 | FinalizeVarWithDestructor(VDecl, DeclInitType); |
Anders Carlsson | ca29ad9 | 2009-04-15 21:48:18 +0000 | [diff] [blame] | 3317 | } |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 3318 | return; |
| 3319 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3320 | |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 3321 | if (NumExprs > 1) { |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 3322 | Diag(CommaLocs[0], diag::err_builtin_direct_init_more_than_one_arg) |
| 3323 | << SourceRange(VDecl->getLocation(), RParenLoc); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3324 | RealDecl->setInvalidDecl(); |
| 3325 | return; |
| 3326 | } |
| 3327 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3328 | // Let clients know that initialization was done with a direct initializer. |
| 3329 | VDecl->setCXXDirectInitializer(true); |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 3330 | |
| 3331 | assert(NumExprs == 1 && "Expected 1 expression"); |
| 3332 | // Set the init expression, handles conversions. |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 3333 | AddInitializerToDecl(Dcl, ExprArg(*this, Exprs.release()[0]), |
| 3334 | /*DirectInit=*/true); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3335 | } |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3336 | |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3337 | /// \brief Perform initialization by constructor (C++ [dcl.init]p14), which |
| 3338 | /// may occur as part of direct-initialization or copy-initialization. |
| 3339 | /// |
| 3340 | /// \param ClassType the type of the object being initialized, which must have |
| 3341 | /// class type. |
| 3342 | /// |
| 3343 | /// \param ArgsPtr the arguments provided to initialize the object |
| 3344 | /// |
| 3345 | /// \param Loc the source location where the initialization occurs |
| 3346 | /// |
| 3347 | /// \param Range the source range that covers the entire initialization |
| 3348 | /// |
| 3349 | /// \param InitEntity the name of the entity being initialized, if known |
| 3350 | /// |
| 3351 | /// \param Kind the type of initialization being performed |
| 3352 | /// |
| 3353 | /// \param ConvertedArgs a vector that will be filled in with the |
| 3354 | /// appropriately-converted arguments to the constructor (if initialization |
| 3355 | /// succeeded). |
| 3356 | /// |
| 3357 | /// \returns the constructor used to initialize the object, if successful. |
| 3358 | /// Otherwise, emits a diagnostic and returns NULL. |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 3359 | CXXConstructorDecl * |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 3360 | Sema::PerformInitializationByConstructor(QualType ClassType, |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3361 | MultiExprArg ArgsPtr, |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 3362 | SourceLocation Loc, SourceRange Range, |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3363 | DeclarationName InitEntity, |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3364 | InitializationKind Kind, |
| 3365 | ASTOwningVector<&ActionBase::DeleteExpr> &ConvertedArgs) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3366 | const RecordType *ClassRec = ClassType->getAs<RecordType>(); |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 3367 | assert(ClassRec && "Can only initialize a class type here"); |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3368 | Expr **Args = (Expr **)ArgsPtr.get(); |
| 3369 | unsigned NumArgs = ArgsPtr.size(); |
| 3370 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3371 | // C++ [dcl.init]p14: |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 3372 | // If the initialization is direct-initialization, or if it is |
| 3373 | // copy-initialization where the cv-unqualified version of the |
| 3374 | // source type is the same class as, or a derived class of, the |
| 3375 | // class of the destination, constructors are considered. The |
| 3376 | // applicable constructors are enumerated (13.3.1.3), and the |
| 3377 | // best one is chosen through overload resolution (13.3). The |
| 3378 | // constructor so selected is called to initialize the object, |
| 3379 | // with the initializer expression(s) as its argument(s). If no |
| 3380 | // constructor applies, or the overload resolution is ambiguous, |
| 3381 | // the initialization is ill-formed. |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 3382 | const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(ClassRec->getDecl()); |
| 3383 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 3384 | |
| 3385 | // Add constructors to the overload set. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3386 | DeclarationName ConstructorName |
Douglas Gregor | 9e7d9de | 2008-12-15 21:24:18 +0000 | [diff] [blame] | 3387 | = Context.DeclarationNames.getCXXConstructorName( |
| 3388 | Context.getCanonicalType(ClassType.getUnqualifiedType())); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 3389 | DeclContext::lookup_const_iterator Con, ConEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3390 | for (llvm::tie(Con, ConEnd) = ClassDecl->lookup(ConstructorName); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 3391 | Con != ConEnd; ++Con) { |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3392 | // Find the constructor (which may be a template). |
| 3393 | CXXConstructorDecl *Constructor = 0; |
| 3394 | FunctionTemplateDecl *ConstructorTmpl= dyn_cast<FunctionTemplateDecl>(*Con); |
| 3395 | if (ConstructorTmpl) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3396 | Constructor |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3397 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 3398 | else |
| 3399 | Constructor = cast<CXXConstructorDecl>(*Con); |
| 3400 | |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 3401 | if ((Kind == IK_Direct) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3402 | (Kind == IK_Copy && |
Anders Carlsson | faccd72 | 2009-08-28 16:57:08 +0000 | [diff] [blame] | 3403 | Constructor->isConvertingConstructor(/*AllowExplicit=*/false)) || |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3404 | (Kind == IK_Default && Constructor->isDefaultConstructor())) { |
| 3405 | if (ConstructorTmpl) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3406 | AddTemplateOverloadCandidate(ConstructorTmpl, false, 0, 0, |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3407 | Args, NumArgs, CandidateSet); |
| 3408 | else |
| 3409 | AddOverloadCandidate(Constructor, Args, NumArgs, CandidateSet); |
| 3410 | } |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 3411 | } |
| 3412 | |
Douglas Gregor | 9e7d9de | 2008-12-15 21:24:18 +0000 | [diff] [blame] | 3413 | // FIXME: When we decide not to synthesize the implicitly-declared |
| 3414 | // constructors, we'll need to make them appear here. |
| 3415 | |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 3416 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 3417 | switch (BestViableFunction(CandidateSet, Loc, Best)) { |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 3418 | case OR_Success: |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3419 | // We found a constructor. Break out so that we can convert the arguments |
| 3420 | // appropriately. |
| 3421 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3422 | |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 3423 | case OR_No_Viable_Function: |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 3424 | if (InitEntity) |
| 3425 | Diag(Loc, diag::err_ovl_no_viable_function_in_init) |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 3426 | << InitEntity << Range; |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 3427 | else |
| 3428 | Diag(Loc, diag::err_ovl_no_viable_function_in_init) |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 3429 | << ClassType << Range; |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 3430 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 3431 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3432 | |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 3433 | case OR_Ambiguous: |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 3434 | if (InitEntity) |
| 3435 | Diag(Loc, diag::err_ovl_ambiguous_init) << InitEntity << Range; |
| 3436 | else |
| 3437 | Diag(Loc, diag::err_ovl_ambiguous_init) << ClassType << Range; |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 3438 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 3439 | return 0; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3440 | |
| 3441 | case OR_Deleted: |
| 3442 | if (InitEntity) |
| 3443 | Diag(Loc, diag::err_ovl_deleted_init) |
| 3444 | << Best->Function->isDeleted() |
| 3445 | << InitEntity << Range; |
| 3446 | else |
| 3447 | Diag(Loc, diag::err_ovl_deleted_init) |
| 3448 | << Best->Function->isDeleted() |
| 3449 | << InitEntity << Range; |
| 3450 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 3451 | return 0; |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 3452 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3453 | |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3454 | // Convert the arguments, fill in default arguments, etc. |
| 3455 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
| 3456 | if (CompleteConstructorCall(Constructor, move(ArgsPtr), Loc, ConvertedArgs)) |
| 3457 | return 0; |
| 3458 | |
| 3459 | return Constructor; |
| 3460 | } |
| 3461 | |
| 3462 | /// \brief Given a constructor and the set of arguments provided for the |
| 3463 | /// constructor, convert the arguments and add any required default arguments |
| 3464 | /// to form a proper call to this constructor. |
| 3465 | /// |
| 3466 | /// \returns true if an error occurred, false otherwise. |
| 3467 | bool |
| 3468 | Sema::CompleteConstructorCall(CXXConstructorDecl *Constructor, |
| 3469 | MultiExprArg ArgsPtr, |
| 3470 | SourceLocation Loc, |
| 3471 | ASTOwningVector<&ActionBase::DeleteExpr> &ConvertedArgs) { |
| 3472 | // FIXME: This duplicates a lot of code from Sema::ConvertArgumentsForCall. |
| 3473 | unsigned NumArgs = ArgsPtr.size(); |
| 3474 | Expr **Args = (Expr **)ArgsPtr.get(); |
| 3475 | |
| 3476 | const FunctionProtoType *Proto |
| 3477 | = Constructor->getType()->getAs<FunctionProtoType>(); |
| 3478 | assert(Proto && "Constructor without a prototype?"); |
| 3479 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 3480 | unsigned NumArgsToCheck = NumArgs; |
| 3481 | |
| 3482 | // If too few arguments are available, we'll fill in the rest with defaults. |
| 3483 | if (NumArgs < NumArgsInProto) { |
| 3484 | NumArgsToCheck = NumArgsInProto; |
| 3485 | ConvertedArgs.reserve(NumArgsInProto); |
| 3486 | } else { |
| 3487 | ConvertedArgs.reserve(NumArgs); |
| 3488 | if (NumArgs > NumArgsInProto) |
| 3489 | NumArgsToCheck = NumArgsInProto; |
| 3490 | } |
| 3491 | |
| 3492 | // Convert arguments |
| 3493 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
| 3494 | QualType ProtoArgType = Proto->getArgType(i); |
| 3495 | |
| 3496 | Expr *Arg; |
| 3497 | if (i < NumArgs) { |
| 3498 | Arg = Args[i]; |
Anders Carlsson | 7171011 | 2009-09-15 21:14:33 +0000 | [diff] [blame] | 3499 | |
| 3500 | // Pass the argument. |
| 3501 | if (PerformCopyInitialization(Arg, ProtoArgType, "passing")) |
| 3502 | return true; |
| 3503 | |
| 3504 | Args[i] = 0; |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3505 | } else { |
| 3506 | ParmVarDecl *Param = Constructor->getParamDecl(i); |
| 3507 | |
| 3508 | OwningExprResult DefArg = BuildCXXDefaultArgExpr(Loc, Constructor, Param); |
| 3509 | if (DefArg.isInvalid()) |
| 3510 | return true; |
| 3511 | |
| 3512 | Arg = DefArg.takeAs<Expr>(); |
| 3513 | } |
| 3514 | |
| 3515 | ConvertedArgs.push_back(Arg); |
| 3516 | } |
| 3517 | |
| 3518 | // If this is a variadic call, handle args passed through "...". |
| 3519 | if (Proto->isVariadic()) { |
| 3520 | // Promote the arguments (C99 6.5.2.2p7). |
| 3521 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
| 3522 | Expr *Arg = Args[i]; |
| 3523 | if (DefaultVariadicArgumentPromotion(Arg, VariadicConstructor)) |
| 3524 | return true; |
| 3525 | |
| 3526 | ConvertedArgs.push_back(Arg); |
| 3527 | Args[i] = 0; |
| 3528 | } |
| 3529 | } |
| 3530 | |
| 3531 | return false; |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 3532 | } |
| 3533 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3534 | /// CompareReferenceRelationship - Compare the two types T1 and T2 to |
| 3535 | /// determine whether they are reference-related, |
| 3536 | /// reference-compatible, reference-compatible with added |
| 3537 | /// qualification, or incompatible, for use in C++ initialization by |
| 3538 | /// reference (C++ [dcl.ref.init]p4). Neither type can be a reference |
| 3539 | /// type, and the first type (T1) is the pointee type of the reference |
| 3540 | /// type being initialized. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3541 | Sema::ReferenceCompareResult |
| 3542 | Sema::CompareReferenceRelationship(QualType T1, QualType T2, |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3543 | bool& DerivedToBase) { |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3544 | assert(!T1->isReferenceType() && |
| 3545 | "T1 must be the pointee type of the reference type"); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3546 | assert(!T2->isReferenceType() && "T2 cannot be a reference type"); |
| 3547 | |
| 3548 | T1 = Context.getCanonicalType(T1); |
| 3549 | T2 = Context.getCanonicalType(T2); |
| 3550 | QualType UnqualT1 = T1.getUnqualifiedType(); |
| 3551 | QualType UnqualT2 = T2.getUnqualifiedType(); |
| 3552 | |
| 3553 | // C++ [dcl.init.ref]p4: |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 3554 | // Given types "cv1 T1" and "cv2 T2," "cv1 T1" is |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3555 | // 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] | 3556 | // T1 is a base class of T2. |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3557 | if (UnqualT1 == UnqualT2) |
| 3558 | DerivedToBase = false; |
| 3559 | else if (IsDerivedFrom(UnqualT2, UnqualT1)) |
| 3560 | DerivedToBase = true; |
| 3561 | else |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3562 | return Ref_Incompatible; |
| 3563 | |
| 3564 | // At this point, we know that T1 and T2 are reference-related (at |
| 3565 | // least). |
| 3566 | |
| 3567 | // C++ [dcl.init.ref]p4: |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 3568 | // "cv1 T1" is reference-compatible with "cv2 T2" if T1 is |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3569 | // reference-related to T2 and cv1 is the same cv-qualification |
| 3570 | // as, or greater cv-qualification than, cv2. For purposes of |
| 3571 | // overload resolution, cases for which cv1 is greater |
| 3572 | // cv-qualification than cv2 are identified as |
| 3573 | // reference-compatible with added qualification (see 13.3.3.2). |
| 3574 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 3575 | return Ref_Compatible; |
| 3576 | else if (T1.isMoreQualifiedThan(T2)) |
| 3577 | return Ref_Compatible_With_Added_Qualification; |
| 3578 | else |
| 3579 | return Ref_Related; |
| 3580 | } |
| 3581 | |
| 3582 | /// CheckReferenceInit - Check the initialization of a reference |
| 3583 | /// variable with the given initializer (C++ [dcl.init.ref]). Init is |
| 3584 | /// the initializer (either a simple initializer or an initializer |
Douglas Gregor | 3205a78 | 2008-10-29 23:31:03 +0000 | [diff] [blame] | 3585 | /// list), and DeclType is the type of the declaration. When ICS is |
| 3586 | /// non-null, this routine will compute the implicit conversion |
| 3587 | /// sequence according to C++ [over.ics.ref] and will not produce any |
| 3588 | /// diagnostics; when ICS is null, it will emit diagnostics when any |
| 3589 | /// errors are found. Either way, a return value of true indicates |
| 3590 | /// that there was a failure, a return value of false indicates that |
| 3591 | /// the reference initialization succeeded. |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3592 | /// |
| 3593 | /// When @p SuppressUserConversions, user-defined conversions are |
| 3594 | /// suppressed. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3595 | /// When @p AllowExplicit, we also permit explicit user-defined |
| 3596 | /// conversion functions. |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 3597 | /// When @p ForceRValue, we unconditionally treat the initializer as an rvalue. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3598 | bool |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3599 | Sema::CheckReferenceInit(Expr *&Init, QualType DeclType, |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 3600 | SourceLocation DeclLoc, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3601 | bool SuppressUserConversions, |
Anders Carlsson | 2de3ace | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 3602 | bool AllowExplicit, bool ForceRValue, |
| 3603 | ImplicitConversionSequence *ICS) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3604 | assert(DeclType->isReferenceType() && "Reference init needs a reference"); |
| 3605 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3606 | QualType T1 = DeclType->getAs<ReferenceType>()->getPointeeType(); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3607 | QualType T2 = Init->getType(); |
| 3608 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3609 | // If the initializer is the address of an overloaded function, try |
| 3610 | // to resolve the overloaded function. If all goes well, T2 is the |
| 3611 | // type of the resulting function. |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3612 | if (Context.getCanonicalType(T2) == Context.OverloadTy) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3613 | FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Init, DeclType, |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3614 | ICS != 0); |
| 3615 | if (Fn) { |
| 3616 | // Since we're performing this reference-initialization for |
| 3617 | // real, update the initializer with the resulting function. |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3618 | if (!ICS) { |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 3619 | if (DiagnoseUseOfDecl(Fn, DeclLoc)) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3620 | return true; |
| 3621 | |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 3622 | Init = FixOverloadedFunctionReference(Init, Fn); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3623 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3624 | |
| 3625 | T2 = Fn->getType(); |
| 3626 | } |
| 3627 | } |
| 3628 | |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3629 | // Compute some basic properties of the types and the initializer. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3630 | bool isRValRef = DeclType->isRValueReferenceType(); |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3631 | bool DerivedToBase = false; |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 3632 | Expr::isLvalueResult InitLvalue = ForceRValue ? Expr::LV_InvalidExpression : |
| 3633 | Init->isLvalue(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3634 | ReferenceCompareResult RefRelationship |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3635 | = CompareReferenceRelationship(T1, T2, DerivedToBase); |
| 3636 | |
| 3637 | // Most paths end in a failed conversion. |
| 3638 | if (ICS) |
| 3639 | ICS->ConversionKind = ImplicitConversionSequence::BadConversion; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3640 | |
| 3641 | // C++ [dcl.init.ref]p5: |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 3642 | // A reference to type "cv1 T1" is initialized by an expression |
| 3643 | // of type "cv2 T2" as follows: |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3644 | |
| 3645 | // -- If the initializer expression |
| 3646 | |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 3647 | // Rvalue references cannot bind to lvalues (N2812). |
| 3648 | // There is absolutely no situation where they can. In particular, note that |
| 3649 | // this is ill-formed, even if B has a user-defined conversion to A&&: |
| 3650 | // B b; |
| 3651 | // A&& r = b; |
| 3652 | if (isRValRef && InitLvalue == Expr::LV_Valid) { |
| 3653 | if (!ICS) |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 3654 | Diag(DeclLoc, diag::err_lvalue_to_rvalue_ref) |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 3655 | << Init->getSourceRange(); |
| 3656 | return true; |
| 3657 | } |
| 3658 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3659 | bool BindsDirectly = false; |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 3660 | // -- is an lvalue (but is not a bit-field), and "cv1 T1" is |
| 3661 | // reference-compatible with "cv2 T2," or |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3662 | // |
| 3663 | // Note that the bit-field check is skipped if we are just computing |
| 3664 | // the implicit conversion sequence (C++ [over.best.ics]p2). |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3665 | if (InitLvalue == Expr::LV_Valid && (ICS || !Init->getBitField()) && |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3666 | RefRelationship >= Ref_Compatible_With_Added_Qualification) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3667 | BindsDirectly = true; |
| 3668 | |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3669 | if (ICS) { |
| 3670 | // C++ [over.ics.ref]p1: |
| 3671 | // When a parameter of reference type binds directly (8.5.3) |
| 3672 | // to an argument expression, the implicit conversion sequence |
| 3673 | // is the identity conversion, unless the argument expression |
| 3674 | // has a type that is a derived class of the parameter type, |
| 3675 | // in which case the implicit conversion sequence is a |
| 3676 | // derived-to-base Conversion (13.3.3.1). |
| 3677 | ICS->ConversionKind = ImplicitConversionSequence::StandardConversion; |
| 3678 | ICS->Standard.First = ICK_Identity; |
| 3679 | ICS->Standard.Second = DerivedToBase? ICK_Derived_To_Base : ICK_Identity; |
| 3680 | ICS->Standard.Third = ICK_Identity; |
| 3681 | ICS->Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 3682 | ICS->Standard.ToTypePtr = T1.getAsOpaquePtr(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3683 | ICS->Standard.ReferenceBinding = true; |
| 3684 | ICS->Standard.DirectBinding = true; |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 3685 | ICS->Standard.RRefBinding = false; |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 3686 | ICS->Standard.CopyConstructor = 0; |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3687 | |
| 3688 | // Nothing more to do: the inaccessibility/ambiguity check for |
| 3689 | // derived-to-base conversions is suppressed when we're |
| 3690 | // computing the implicit conversion sequence (C++ |
| 3691 | // [over.best.ics]p2). |
| 3692 | return false; |
| 3693 | } else { |
| 3694 | // Perform the conversion. |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3695 | CastExpr::CastKind CK = CastExpr::CK_NoOp; |
| 3696 | if (DerivedToBase) |
| 3697 | CK = CastExpr::CK_DerivedToBase; |
Sebastian Redl | 2c7588f | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 3698 | else if(CheckExceptionSpecCompatibility(Init, T1)) |
| 3699 | return true; |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3700 | ImpCastExprToType(Init, T1, CK, /*isLvalue=*/true); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3701 | } |
| 3702 | } |
| 3703 | |
| 3704 | // -- 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] | 3705 | // implicitly converted to an lvalue of type "cv3 T3," |
| 3706 | // where "cv1 T1" is reference-compatible with "cv3 T3" |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3707 | // 92) (this conversion is selected by enumerating the |
| 3708 | // applicable conversion functions (13.3.1.6) and choosing |
| 3709 | // the best one through overload resolution (13.3)), |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3710 | if (!isRValRef && !SuppressUserConversions && T2->isRecordType() && |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3711 | !RequireCompleteType(DeclLoc, T2, 0)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3712 | CXXRecordDecl *T2RecordDecl |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3713 | = dyn_cast<CXXRecordDecl>(T2->getAs<RecordType>()->getDecl()); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3714 | |
Douglas Gregor | cb9b977 | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 3715 | OverloadCandidateSet CandidateSet; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3716 | OverloadedFunctionDecl *Conversions |
Fariborz Jahanian | 51bebc8 | 2009-09-23 20:55:32 +0000 | [diff] [blame] | 3717 | = T2RecordDecl->getVisibleConversionFunctions(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3718 | for (OverloadedFunctionDecl::function_iterator Func |
Douglas Gregor | cb9b977 | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 3719 | = Conversions->function_begin(); |
| 3720 | Func != Conversions->function_end(); ++Func) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3721 | FunctionTemplateDecl *ConvTemplate |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3722 | = dyn_cast<FunctionTemplateDecl>(*Func); |
| 3723 | CXXConversionDecl *Conv; |
| 3724 | if (ConvTemplate) |
| 3725 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 3726 | else |
| 3727 | Conv = cast<CXXConversionDecl>(*Func); |
Fariborz Jahanian | 51bebc8 | 2009-09-23 20:55:32 +0000 | [diff] [blame] | 3728 | |
Douglas Gregor | cb9b977 | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 3729 | // If the conversion function doesn't return a reference type, |
| 3730 | // it can't be considered for this conversion. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3731 | if (Conv->getConversionType()->isLValueReferenceType() && |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3732 | (AllowExplicit || !Conv->isExplicit())) { |
| 3733 | if (ConvTemplate) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3734 | AddTemplateConversionCandidate(ConvTemplate, Init, DeclType, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3735 | CandidateSet); |
| 3736 | else |
| 3737 | AddConversionCandidate(Conv, Init, DeclType, CandidateSet); |
| 3738 | } |
Douglas Gregor | cb9b977 | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 3739 | } |
| 3740 | |
| 3741 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 3742 | switch (BestViableFunction(CandidateSet, DeclLoc, Best)) { |
Douglas Gregor | cb9b977 | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 3743 | case OR_Success: |
| 3744 | // This is a direct binding. |
| 3745 | BindsDirectly = true; |
| 3746 | |
| 3747 | if (ICS) { |
| 3748 | // C++ [over.ics.ref]p1: |
| 3749 | // |
| 3750 | // [...] If the parameter binds directly to the result of |
| 3751 | // applying a conversion function to the argument |
| 3752 | // expression, the implicit conversion sequence is a |
| 3753 | // user-defined conversion sequence (13.3.3.1.2), with the |
| 3754 | // second standard conversion sequence either an identity |
| 3755 | // conversion or, if the conversion function returns an |
| 3756 | // entity of a type that is a derived class of the parameter |
| 3757 | // type, a derived-to-base Conversion. |
| 3758 | ICS->ConversionKind = ImplicitConversionSequence::UserDefinedConversion; |
| 3759 | ICS->UserDefined.Before = Best->Conversions[0].Standard; |
| 3760 | ICS->UserDefined.After = Best->FinalConversion; |
| 3761 | ICS->UserDefined.ConversionFunction = Best->Function; |
| 3762 | assert(ICS->UserDefined.After.ReferenceBinding && |
| 3763 | ICS->UserDefined.After.DirectBinding && |
| 3764 | "Expected a direct reference binding!"); |
| 3765 | return false; |
| 3766 | } else { |
Fariborz Jahanian | 8f489d6 | 2009-09-23 22:34:00 +0000 | [diff] [blame] | 3767 | OwningExprResult InitConversion = |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 3768 | BuildCXXCastArgument(DeclLoc, QualType(), |
Fariborz Jahanian | 8f489d6 | 2009-09-23 22:34:00 +0000 | [diff] [blame] | 3769 | CastExpr::CK_UserDefinedConversion, |
| 3770 | cast<CXXMethodDecl>(Best->Function), |
| 3771 | Owned(Init)); |
| 3772 | Init = InitConversion.takeAs<Expr>(); |
Sebastian Redl | 2c7588f | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 3773 | |
| 3774 | if (CheckExceptionSpecCompatibility(Init, T1)) |
| 3775 | return true; |
Fariborz Jahanian | 8f489d6 | 2009-09-23 22:34:00 +0000 | [diff] [blame] | 3776 | ImpCastExprToType(Init, T1, CastExpr::CK_UserDefinedConversion, |
| 3777 | /*isLvalue=*/true); |
Douglas Gregor | cb9b977 | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 3778 | } |
| 3779 | break; |
| 3780 | |
| 3781 | case OR_Ambiguous: |
Fariborz Jahanian | d9290cb | 2009-10-14 00:52:43 +0000 | [diff] [blame] | 3782 | if (ICS) { |
| 3783 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 3784 | Cand != CandidateSet.end(); ++Cand) |
| 3785 | if (Cand->Viable) |
| 3786 | ICS->ConversionFunctionSet.push_back(Cand->Function); |
| 3787 | break; |
| 3788 | } |
| 3789 | Diag(DeclLoc, diag::err_ref_init_ambiguous) << DeclType << Init->getType() |
| 3790 | << Init->getSourceRange(); |
| 3791 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Douglas Gregor | cb9b977 | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 3792 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3793 | |
Douglas Gregor | cb9b977 | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 3794 | case OR_No_Viable_Function: |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3795 | case OR_Deleted: |
| 3796 | // There was no suitable conversion, or we found a deleted |
| 3797 | // conversion; continue with other checks. |
Douglas Gregor | cb9b977 | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 3798 | break; |
| 3799 | } |
| 3800 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3801 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3802 | if (BindsDirectly) { |
| 3803 | // C++ [dcl.init.ref]p4: |
| 3804 | // [...] In all cases where the reference-related or |
| 3805 | // reference-compatible relationship of two types is used to |
| 3806 | // establish the validity of a reference binding, and T1 is a |
| 3807 | // base class of T2, a program that necessitates such a binding |
| 3808 | // is ill-formed if T1 is an inaccessible (clause 11) or |
| 3809 | // ambiguous (10.2) base class of T2. |
| 3810 | // |
| 3811 | // Note that we only check this condition when we're allowed to |
| 3812 | // complain about errors, because we should not be checking for |
| 3813 | // ambiguity (or inaccessibility) unless the reference binding |
| 3814 | // actually happens. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3815 | if (DerivedToBase) |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 3816 | return CheckDerivedToBaseConversion(T2, T1, DeclLoc, |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3817 | Init->getSourceRange()); |
| 3818 | else |
| 3819 | return false; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3820 | } |
| 3821 | |
| 3822 | // -- Otherwise, the reference shall be to a non-volatile const |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 3823 | // type (i.e., cv1 shall be const), or the reference shall be an |
| 3824 | // rvalue reference and the initializer expression shall be an rvalue. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3825 | if (!isRValRef && T1.getCVRQualifiers() != Qualifiers::Const) { |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3826 | if (!ICS) |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 3827 | Diag(DeclLoc, diag::err_not_reference_to_const_init) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3828 | << T1 << (InitLvalue != Expr::LV_Valid? "temporary" : "value") |
| 3829 | << T2 << Init->getSourceRange(); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3830 | return true; |
| 3831 | } |
| 3832 | |
| 3833 | // -- If the initializer expression is an rvalue, with T2 a |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 3834 | // class type, and "cv1 T1" is reference-compatible with |
| 3835 | // "cv2 T2," the reference is bound in one of the |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3836 | // following ways (the choice is implementation-defined): |
| 3837 | // |
| 3838 | // -- The reference is bound to the object represented by |
| 3839 | // the rvalue (see 3.10) or to a sub-object within that |
| 3840 | // object. |
| 3841 | // |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 3842 | // -- A temporary of type "cv1 T2" [sic] is created, and |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3843 | // a constructor is called to copy the entire rvalue |
| 3844 | // object into the temporary. The reference is bound to |
| 3845 | // the temporary or to a sub-object within the |
| 3846 | // temporary. |
| 3847 | // |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3848 | // The constructor that would be used to make the copy |
| 3849 | // shall be callable whether or not the copy is actually |
| 3850 | // done. |
| 3851 | // |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 3852 | // Note that C++0x [dcl.init.ref]p5 takes away this implementation |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3853 | // freedom, so we will always take the first option and never build |
| 3854 | // a temporary in this case. FIXME: We will, however, have to check |
| 3855 | // for the presence of a copy constructor in C++98/03 mode. |
| 3856 | if (InitLvalue != Expr::LV_Valid && T2->isRecordType() && |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3857 | RefRelationship >= Ref_Compatible_With_Added_Qualification) { |
| 3858 | if (ICS) { |
| 3859 | ICS->ConversionKind = ImplicitConversionSequence::StandardConversion; |
| 3860 | ICS->Standard.First = ICK_Identity; |
| 3861 | ICS->Standard.Second = DerivedToBase? ICK_Derived_To_Base : ICK_Identity; |
| 3862 | ICS->Standard.Third = ICK_Identity; |
| 3863 | ICS->Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 3864 | ICS->Standard.ToTypePtr = T1.getAsOpaquePtr(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3865 | ICS->Standard.ReferenceBinding = true; |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 3866 | ICS->Standard.DirectBinding = false; |
| 3867 | ICS->Standard.RRefBinding = isRValRef; |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 3868 | ICS->Standard.CopyConstructor = 0; |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3869 | } else { |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3870 | CastExpr::CastKind CK = CastExpr::CK_NoOp; |
| 3871 | if (DerivedToBase) |
| 3872 | CK = CastExpr::CK_DerivedToBase; |
Sebastian Redl | 2c7588f | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 3873 | else if(CheckExceptionSpecCompatibility(Init, T1)) |
| 3874 | return true; |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3875 | ImpCastExprToType(Init, T1, CK, /*isLvalue=*/false); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3876 | } |
| 3877 | return false; |
| 3878 | } |
| 3879 | |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 3880 | // -- Otherwise, a temporary of type "cv1 T1" is created and |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3881 | // initialized from the initializer expression using the |
| 3882 | // rules for a non-reference copy initialization (8.5). The |
| 3883 | // reference is then bound to the temporary. If T1 is |
| 3884 | // reference-related to T2, cv1 must be the same |
| 3885 | // cv-qualification as, or greater cv-qualification than, |
| 3886 | // cv2; otherwise, the program is ill-formed. |
| 3887 | if (RefRelationship == Ref_Related) { |
| 3888 | // If cv1 == cv2 or cv1 is a greater cv-qualified than cv2, then |
| 3889 | // we would be reference-compatible or reference-compatible with |
| 3890 | // added qualification. But that wasn't the case, so the reference |
| 3891 | // initialization fails. |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3892 | if (!ICS) |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 3893 | Diag(DeclLoc, diag::err_reference_init_drops_quals) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3894 | << T1 << (InitLvalue != Expr::LV_Valid? "temporary" : "value") |
| 3895 | << T2 << Init->getSourceRange(); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3896 | return true; |
| 3897 | } |
| 3898 | |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 3899 | // If at least one of the types is a class type, the types are not |
| 3900 | // related, and we aren't allowed any user conversions, the |
| 3901 | // reference binding fails. This case is important for breaking |
| 3902 | // recursion, since TryImplicitConversion below will attempt to |
| 3903 | // create a temporary through the use of a copy constructor. |
| 3904 | if (SuppressUserConversions && RefRelationship == Ref_Incompatible && |
| 3905 | (T1->isRecordType() || T2->isRecordType())) { |
| 3906 | if (!ICS) |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 3907 | Diag(DeclLoc, diag::err_typecheck_convert_incompatible) |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 3908 | << DeclType << Init->getType() << "initializing" << Init->getSourceRange(); |
| 3909 | return true; |
| 3910 | } |
| 3911 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3912 | // Actually try to convert the initializer to T1. |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3913 | if (ICS) { |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 3914 | // C++ [over.ics.ref]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3915 | // |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 3916 | // When a parameter of reference type is not bound directly to |
| 3917 | // an argument expression, the conversion sequence is the one |
| 3918 | // required to convert the argument expression to the |
| 3919 | // underlying type of the reference according to |
| 3920 | // 13.3.3.1. Conceptually, this conversion sequence corresponds |
| 3921 | // to copy-initializing a temporary of the underlying type with |
| 3922 | // the argument expression. Any difference in top-level |
| 3923 | // cv-qualification is subsumed by the initialization itself |
| 3924 | // and does not constitute a conversion. |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 3925 | *ICS = TryImplicitConversion(Init, T1, SuppressUserConversions, |
| 3926 | /*AllowExplicit=*/false, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 3927 | /*ForceRValue=*/false, |
| 3928 | /*InOverloadResolution=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3929 | |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 3930 | // Of course, that's still a reference binding. |
| 3931 | if (ICS->ConversionKind == ImplicitConversionSequence::StandardConversion) { |
| 3932 | ICS->Standard.ReferenceBinding = true; |
| 3933 | ICS->Standard.RRefBinding = isRValRef; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3934 | } else if (ICS->ConversionKind == |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 3935 | ImplicitConversionSequence::UserDefinedConversion) { |
| 3936 | ICS->UserDefined.After.ReferenceBinding = true; |
| 3937 | ICS->UserDefined.After.RRefBinding = isRValRef; |
| 3938 | } |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3939 | return ICS->ConversionKind == ImplicitConversionSequence::BadConversion; |
| 3940 | } else { |
Fariborz Jahanian | 51bebc8 | 2009-09-23 20:55:32 +0000 | [diff] [blame] | 3941 | ImplicitConversionSequence Conversions; |
| 3942 | bool badConversion = PerformImplicitConversion(Init, T1, "initializing", |
| 3943 | false, false, |
| 3944 | Conversions); |
| 3945 | if (badConversion) { |
| 3946 | if ((Conversions.ConversionKind == |
| 3947 | ImplicitConversionSequence::BadConversion) |
Fariborz Jahanian | 82ad87b | 2009-09-28 22:03:07 +0000 | [diff] [blame] | 3948 | && !Conversions.ConversionFunctionSet.empty()) { |
Fariborz Jahanian | 7ad2d56 | 2009-09-24 00:42:43 +0000 | [diff] [blame] | 3949 | Diag(DeclLoc, |
Fariborz Jahanian | 51bebc8 | 2009-09-23 20:55:32 +0000 | [diff] [blame] | 3950 | diag::err_lvalue_to_rvalue_ambig_ref) << Init->getSourceRange(); |
| 3951 | for (int j = Conversions.ConversionFunctionSet.size()-1; |
| 3952 | j >= 0; j--) { |
| 3953 | FunctionDecl *Func = Conversions.ConversionFunctionSet[j]; |
| 3954 | Diag(Func->getLocation(), diag::err_ovl_candidate); |
| 3955 | } |
| 3956 | } |
Fariborz Jahanian | 893f955 | 2009-09-30 21:23:30 +0000 | [diff] [blame] | 3957 | else { |
| 3958 | if (isRValRef) |
| 3959 | Diag(DeclLoc, diag::err_lvalue_to_rvalue_ref) |
| 3960 | << Init->getSourceRange(); |
| 3961 | else |
| 3962 | Diag(DeclLoc, diag::err_invalid_initialization) |
| 3963 | << DeclType << Init->getType() << Init->getSourceRange(); |
| 3964 | } |
Fariborz Jahanian | 51bebc8 | 2009-09-23 20:55:32 +0000 | [diff] [blame] | 3965 | } |
| 3966 | return badConversion; |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3967 | } |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3968 | } |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3969 | |
| 3970 | /// CheckOverloadedOperatorDeclaration - Check whether the declaration |
| 3971 | /// of this overloaded operator is well-formed. If so, returns false; |
| 3972 | /// otherwise, emits appropriate diagnostics and returns true. |
| 3973 | bool Sema::CheckOverloadedOperatorDeclaration(FunctionDecl *FnDecl) { |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 3974 | assert(FnDecl && FnDecl->isOverloadedOperator() && |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3975 | "Expected an overloaded operator declaration"); |
| 3976 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3977 | OverloadedOperatorKind Op = FnDecl->getOverloadedOperator(); |
| 3978 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3979 | // C++ [over.oper]p5: |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3980 | // The allocation and deallocation functions, operator new, |
| 3981 | // operator new[], operator delete and operator delete[], are |
| 3982 | // described completely in 3.7.3. The attributes and restrictions |
| 3983 | // found in the rest of this subclause do not apply to them unless |
| 3984 | // explicitly stated in 3.7.3. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3985 | // 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] | 3986 | if (Op == OO_New || Op == OO_Array_New || |
| 3987 | Op == OO_Delete || Op == OO_Array_Delete) |
| 3988 | return false; |
| 3989 | |
| 3990 | // C++ [over.oper]p6: |
| 3991 | // An operator function shall either be a non-static member |
| 3992 | // function or be a non-member function and have at least one |
| 3993 | // parameter whose type is a class, a reference to a class, an |
| 3994 | // enumeration, or a reference to an enumeration. |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 3995 | if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 3996 | if (MethodDecl->isStatic()) |
| 3997 | return Diag(FnDecl->getLocation(), |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3998 | diag::err_operator_overload_static) << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3999 | } else { |
| 4000 | bool ClassOrEnumParam = false; |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 4001 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(), |
| 4002 | ParamEnd = FnDecl->param_end(); |
| 4003 | Param != ParamEnd; ++Param) { |
| 4004 | QualType ParamType = (*Param)->getType().getNonReferenceType(); |
Eli Friedman | 5d39dee | 2009-06-27 05:59:59 +0000 | [diff] [blame] | 4005 | if (ParamType->isDependentType() || ParamType->isRecordType() || |
| 4006 | ParamType->isEnumeralType()) { |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4007 | ClassOrEnumParam = true; |
| 4008 | break; |
| 4009 | } |
| 4010 | } |
| 4011 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 4012 | if (!ClassOrEnumParam) |
| 4013 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 4014 | diag::err_operator_overload_needs_class_or_enum) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4015 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4016 | } |
| 4017 | |
| 4018 | // C++ [over.oper]p8: |
| 4019 | // An operator function cannot have default arguments (8.3.6), |
| 4020 | // except where explicitly stated below. |
| 4021 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4022 | // Only the function-call operator allows default arguments |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4023 | // (C++ [over.call]p1). |
| 4024 | if (Op != OO_Call) { |
| 4025 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 4026 | Param != FnDecl->param_end(); ++Param) { |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4027 | if ((*Param)->hasUnparsedDefaultArg()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4028 | return Diag((*Param)->getLocation(), |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4029 | diag::err_operator_overload_default_arg) |
| 4030 | << FnDecl->getDeclName(); |
| 4031 | else if (Expr *DefArg = (*Param)->getDefaultArg()) |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 4032 | return Diag((*Param)->getLocation(), |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 4033 | diag::err_operator_overload_default_arg) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4034 | << FnDecl->getDeclName() << DefArg->getSourceRange(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4035 | } |
| 4036 | } |
| 4037 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 4038 | static const bool OperatorUses[NUM_OVERLOADED_OPERATORS][3] = { |
| 4039 | { false, false, false } |
| 4040 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 4041 | , { Unary, Binary, MemberOnly } |
| 4042 | #include "clang/Basic/OperatorKinds.def" |
| 4043 | }; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4044 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 4045 | bool CanBeUnaryOperator = OperatorUses[Op][0]; |
| 4046 | bool CanBeBinaryOperator = OperatorUses[Op][1]; |
| 4047 | bool MustBeMemberOperator = OperatorUses[Op][2]; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4048 | |
| 4049 | // C++ [over.oper]p8: |
| 4050 | // [...] Operator functions cannot have more or fewer parameters |
| 4051 | // than the number required for the corresponding operator, as |
| 4052 | // described in the rest of this subclause. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4053 | unsigned NumParams = FnDecl->getNumParams() |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 4054 | + (isa<CXXMethodDecl>(FnDecl)? 1 : 0); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4055 | if (Op != OO_Call && |
| 4056 | ((NumParams == 1 && !CanBeUnaryOperator) || |
| 4057 | (NumParams == 2 && !CanBeBinaryOperator) || |
| 4058 | (NumParams < 1) || (NumParams > 2))) { |
| 4059 | // We have the wrong number of parameters. |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 4060 | unsigned ErrorKind; |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 4061 | if (CanBeUnaryOperator && CanBeBinaryOperator) { |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 4062 | ErrorKind = 2; // 2 -> unary or binary. |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 4063 | } else if (CanBeUnaryOperator) { |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 4064 | ErrorKind = 0; // 0 -> unary |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 4065 | } else { |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 4066 | assert(CanBeBinaryOperator && |
| 4067 | "All non-call overloaded operators are unary or binary!"); |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 4068 | ErrorKind = 1; // 1 -> binary |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 4069 | } |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4070 | |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 4071 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_must_be) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4072 | << FnDecl->getDeclName() << NumParams << ErrorKind; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4073 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 4074 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 4075 | // Overloaded operators other than operator() cannot be variadic. |
| 4076 | if (Op != OO_Call && |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4077 | FnDecl->getType()->getAs<FunctionProtoType>()->isVariadic()) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 4078 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_variadic) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4079 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4080 | } |
| 4081 | |
| 4082 | // Some operators must be non-static member functions. |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 4083 | if (MustBeMemberOperator && !isa<CXXMethodDecl>(FnDecl)) { |
| 4084 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 4085 | diag::err_operator_overload_must_be_member) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4086 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4087 | } |
| 4088 | |
| 4089 | // C++ [over.inc]p1: |
| 4090 | // The user-defined function called operator++ implements the |
| 4091 | // prefix and postfix ++ operator. If this function is a member |
| 4092 | // function with no parameters, or a non-member function with one |
| 4093 | // parameter of class or enumeration type, it defines the prefix |
| 4094 | // increment operator ++ for objects of that type. If the function |
| 4095 | // is a member function with one parameter (which shall be of type |
| 4096 | // int) or a non-member function with two parameters (the second |
| 4097 | // of which shall be of type int), it defines the postfix |
| 4098 | // increment operator ++ for objects of that type. |
| 4099 | if ((Op == OO_PlusPlus || Op == OO_MinusMinus) && NumParams == 2) { |
| 4100 | ParmVarDecl *LastParam = FnDecl->getParamDecl(FnDecl->getNumParams() - 1); |
| 4101 | bool ParamIsInt = false; |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4102 | if (const BuiltinType *BT = LastParam->getType()->getAs<BuiltinType>()) |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4103 | ParamIsInt = BT->getKind() == BuiltinType::Int; |
| 4104 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 4105 | if (!ParamIsInt) |
| 4106 | return Diag(LastParam->getLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4107 | diag::err_operator_overload_post_incdec_must_be_int) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4108 | << LastParam->getType() << (Op == OO_MinusMinus); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4109 | } |
| 4110 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 4111 | // Notify the class if it got an assignment operator. |
| 4112 | if (Op == OO_Equal) { |
| 4113 | // Would have returned earlier otherwise. |
| 4114 | assert(isa<CXXMethodDecl>(FnDecl) && |
| 4115 | "Overloaded = not member, but not filtered."); |
| 4116 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
Fariborz Jahanian | ad25883 | 2009-08-13 21:09:41 +0000 | [diff] [blame] | 4117 | Method->setCopyAssignment(true); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 4118 | Method->getParent()->addedAssignmentOperator(Context, Method); |
| 4119 | } |
| 4120 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 4121 | return false; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4122 | } |
Chris Lattner | 5a003a4 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 4123 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 4124 | /// ActOnStartLinkageSpecification - Parsed the beginning of a C++ |
| 4125 | /// linkage specification, including the language and (if present) |
| 4126 | /// the '{'. ExternLoc is the location of the 'extern', LangLoc is |
| 4127 | /// the location of the language string literal, which is provided |
| 4128 | /// by Lang/StrSize. LBraceLoc, if valid, provides the location of |
| 4129 | /// the '{' brace. Otherwise, this linkage specification does not |
| 4130 | /// have any braces. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4131 | Sema::DeclPtrTy Sema::ActOnStartLinkageSpecification(Scope *S, |
| 4132 | SourceLocation ExternLoc, |
| 4133 | SourceLocation LangLoc, |
| 4134 | const char *Lang, |
| 4135 | unsigned StrSize, |
| 4136 | SourceLocation LBraceLoc) { |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4137 | LinkageSpecDecl::LanguageIDs Language; |
| 4138 | if (strncmp(Lang, "\"C\"", StrSize) == 0) |
| 4139 | Language = LinkageSpecDecl::lang_c; |
| 4140 | else if (strncmp(Lang, "\"C++\"", StrSize) == 0) |
| 4141 | Language = LinkageSpecDecl::lang_cxx; |
| 4142 | else { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 4143 | Diag(LangLoc, diag::err_bad_language); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4144 | return DeclPtrTy(); |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4145 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4146 | |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4147 | // FIXME: Add all the various semantics of linkage specifications |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4148 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 4149 | LinkageSpecDecl *D = LinkageSpecDecl::Create(Context, CurContext, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4150 | LangLoc, Language, |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 4151 | LBraceLoc.isValid()); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4152 | CurContext->addDecl(D); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 4153 | PushDeclContext(S, D); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4154 | return DeclPtrTy::make(D); |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4155 | } |
| 4156 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 4157 | /// ActOnFinishLinkageSpecification - Completely the definition of |
| 4158 | /// the C++ linkage specification LinkageSpec. If RBraceLoc is |
| 4159 | /// valid, it's the position of the closing '}' brace in a linkage |
| 4160 | /// specification that uses braces. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4161 | Sema::DeclPtrTy Sema::ActOnFinishLinkageSpecification(Scope *S, |
| 4162 | DeclPtrTy LinkageSpec, |
| 4163 | SourceLocation RBraceLoc) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 4164 | if (LinkageSpec) |
| 4165 | PopDeclContext(); |
| 4166 | return LinkageSpec; |
Chris Lattner | 5a003a4 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 4167 | } |
| 4168 | |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 4169 | /// \brief Perform semantic analysis for the variable declaration that |
| 4170 | /// occurs within a C++ catch clause, returning the newly-created |
| 4171 | /// variable. |
| 4172 | VarDecl *Sema::BuildExceptionDeclaration(Scope *S, QualType ExDeclType, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 4173 | DeclaratorInfo *DInfo, |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 4174 | IdentifierInfo *Name, |
| 4175 | SourceLocation Loc, |
| 4176 | SourceRange Range) { |
| 4177 | bool Invalid = false; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4178 | |
| 4179 | // Arrays and functions decay. |
| 4180 | if (ExDeclType->isArrayType()) |
| 4181 | ExDeclType = Context.getArrayDecayedType(ExDeclType); |
| 4182 | else if (ExDeclType->isFunctionType()) |
| 4183 | ExDeclType = Context.getPointerType(ExDeclType); |
| 4184 | |
| 4185 | // C++ 15.3p1: The exception-declaration shall not denote an incomplete type. |
| 4186 | // The exception-declaration shall not denote a pointer or reference to an |
| 4187 | // incomplete type, other than [cv] void*. |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 4188 | // N2844 forbids rvalue references. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4189 | if (!ExDeclType->isDependentType() && ExDeclType->isRValueReferenceType()) { |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 4190 | Diag(Loc, diag::err_catch_rvalue_ref) << Range; |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 4191 | Invalid = true; |
| 4192 | } |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 4193 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4194 | QualType BaseType = ExDeclType; |
| 4195 | 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] | 4196 | unsigned DK = diag::err_catch_incomplete; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4197 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4198 | BaseType = Ptr->getPointeeType(); |
| 4199 | Mode = 1; |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4200 | DK = diag::err_catch_incomplete_ptr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4201 | } else if (const ReferenceType *Ref = BaseType->getAs<ReferenceType>()) { |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 4202 | // 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] | 4203 | BaseType = Ref->getPointeeType(); |
| 4204 | Mode = 2; |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4205 | DK = diag::err_catch_incomplete_ref; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4206 | } |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 4207 | if (!Invalid && (Mode == 0 || !BaseType->isVoidType()) && |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 4208 | !BaseType->isDependentType() && RequireCompleteType(Loc, BaseType, DK)) |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4209 | Invalid = true; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4210 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4211 | if (!Invalid && !ExDeclType->isDependentType() && |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 4212 | RequireNonAbstractType(Loc, ExDeclType, |
| 4213 | diag::err_abstract_type_in_decl, |
| 4214 | AbstractVariableType)) |
Sebastian Redl | fef9f59 | 2009-04-27 21:03:30 +0000 | [diff] [blame] | 4215 | Invalid = true; |
| 4216 | |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 4217 | // FIXME: Need to test for ability to copy-construct and destroy the |
| 4218 | // exception variable. |
| 4219 | |
Sebastian Redl | 8351da0 | 2008-12-22 21:35:02 +0000 | [diff] [blame] | 4220 | // FIXME: Need to check for abstract classes. |
| 4221 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4222 | VarDecl *ExDecl = VarDecl::Create(Context, CurContext, Loc, |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 4223 | Name, ExDeclType, DInfo, VarDecl::None); |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 4224 | |
| 4225 | if (Invalid) |
| 4226 | ExDecl->setInvalidDecl(); |
| 4227 | |
| 4228 | return ExDecl; |
| 4229 | } |
| 4230 | |
| 4231 | /// ActOnExceptionDeclarator - Parsed the exception-declarator in a C++ catch |
| 4232 | /// handler. |
| 4233 | Sema::DeclPtrTy Sema::ActOnExceptionDeclarator(Scope *S, Declarator &D) { |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 4234 | DeclaratorInfo *DInfo = 0; |
| 4235 | QualType ExDeclType = GetTypeForDeclarator(D, S, &DInfo); |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 4236 | |
| 4237 | bool Invalid = D.isInvalidType(); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4238 | IdentifierInfo *II = D.getIdentifier(); |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4239 | if (NamedDecl *PrevDecl = LookupSingleName(S, II, LookupOrdinaryName)) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4240 | // The scope should be freshly made just for us. There is just no way |
| 4241 | // it contains any previous declaration. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4242 | assert(!S->isDeclScope(DeclPtrTy::make(PrevDecl))); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4243 | if (PrevDecl->isTemplateParameter()) { |
| 4244 | // Maybe we will complain about the shadowed template parameter. |
| 4245 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4246 | } |
| 4247 | } |
| 4248 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 4249 | if (D.getCXXScopeSpec().isSet() && !Invalid) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4250 | Diag(D.getIdentifierLoc(), diag::err_qualified_catch_declarator) |
| 4251 | << D.getCXXScopeSpec().getRange(); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 4252 | Invalid = true; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4253 | } |
| 4254 | |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 4255 | VarDecl *ExDecl = BuildExceptionDeclaration(S, ExDeclType, DInfo, |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 4256 | D.getIdentifier(), |
| 4257 | D.getIdentifierLoc(), |
| 4258 | D.getDeclSpec().getSourceRange()); |
| 4259 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 4260 | if (Invalid) |
| 4261 | ExDecl->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4262 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4263 | // Add the exception declaration into this scope. |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4264 | if (II) |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 4265 | PushOnScopeChains(ExDecl, S); |
| 4266 | else |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4267 | CurContext->addDecl(ExDecl); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4268 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 4269 | ProcessDeclAttributes(S, ExDecl, D); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4270 | return DeclPtrTy::make(ExDecl); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4271 | } |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 4272 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4273 | Sema::DeclPtrTy Sema::ActOnStaticAssertDeclaration(SourceLocation AssertLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4274 | ExprArg assertexpr, |
| 4275 | ExprArg assertmessageexpr) { |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 4276 | Expr *AssertExpr = (Expr *)assertexpr.get(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4277 | StringLiteral *AssertMessage = |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 4278 | cast<StringLiteral>((Expr *)assertmessageexpr.get()); |
| 4279 | |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 4280 | if (!AssertExpr->isTypeDependent() && !AssertExpr->isValueDependent()) { |
| 4281 | llvm::APSInt Value(32); |
| 4282 | if (!AssertExpr->isIntegerConstantExpr(Value, Context)) { |
| 4283 | Diag(AssertLoc, diag::err_static_assert_expression_is_not_constant) << |
| 4284 | AssertExpr->getSourceRange(); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4285 | return DeclPtrTy(); |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 4286 | } |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 4287 | |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 4288 | if (Value == 0) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4289 | std::string str(AssertMessage->getStrData(), |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 4290 | AssertMessage->getByteLength()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4291 | Diag(AssertLoc, diag::err_static_assert_failed) |
Anders Carlsson | 94b15fb | 2009-03-15 18:44:04 +0000 | [diff] [blame] | 4292 | << str << AssertExpr->getSourceRange(); |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 4293 | } |
| 4294 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4295 | |
Anders Carlsson | 77d8142 | 2009-03-15 17:35:16 +0000 | [diff] [blame] | 4296 | assertexpr.release(); |
| 4297 | assertmessageexpr.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4298 | Decl *Decl = StaticAssertDecl::Create(Context, CurContext, AssertLoc, |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 4299 | AssertExpr, AssertMessage); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4300 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4301 | CurContext->addDecl(Decl); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4302 | return DeclPtrTy::make(Decl); |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 4303 | } |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 4304 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 4305 | /// Handle a friend type declaration. This works in tandem with |
| 4306 | /// ActOnTag. |
| 4307 | /// |
| 4308 | /// Notes on friend class templates: |
| 4309 | /// |
| 4310 | /// We generally treat friend class declarations as if they were |
| 4311 | /// declaring a class. So, for example, the elaborated type specifier |
| 4312 | /// in a friend declaration is required to obey the restrictions of a |
| 4313 | /// class-head (i.e. no typedefs in the scope chain), template |
| 4314 | /// parameters are required to match up with simple template-ids, &c. |
| 4315 | /// However, unlike when declaring a template specialization, it's |
| 4316 | /// okay to refer to a template specialization without an empty |
| 4317 | /// template parameter declaration, e.g. |
| 4318 | /// friend class A<T>::B<unsigned>; |
| 4319 | /// We permit this as a special case; if there are any template |
| 4320 | /// parameters present at all, require proper matching, i.e. |
| 4321 | /// template <> template <class T> friend class A<int>::B; |
Chris Lattner | c7f1904 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 4322 | Sema::DeclPtrTy Sema::ActOnFriendTypeDecl(Scope *S, const DeclSpec &DS, |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 4323 | MultiTemplateParamsArg TempParams) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4324 | SourceLocation Loc = DS.getSourceRange().getBegin(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4325 | |
| 4326 | assert(DS.isFriendSpecified()); |
| 4327 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 4328 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 4329 | // Try to convert the decl specifier to a type. This works for |
| 4330 | // friend templates because ActOnTag never produces a ClassTemplateDecl |
| 4331 | // for a TUK_Friend. |
Chris Lattner | c7f1904 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 4332 | Declarator TheDeclarator(DS, Declarator::MemberContext); |
Chris Lattner | c7f1904 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 4333 | QualType T = GetTypeForDeclarator(TheDeclarator, S); |
| 4334 | if (TheDeclarator.isInvalidType()) |
| 4335 | return DeclPtrTy(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4336 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 4337 | // This is definitely an error in C++98. It's probably meant to |
| 4338 | // be forbidden in C++0x, too, but the specification is just |
| 4339 | // poorly written. |
| 4340 | // |
| 4341 | // The problem is with declarations like the following: |
| 4342 | // template <T> friend A<T>::foo; |
| 4343 | // where deciding whether a class C is a friend or not now hinges |
| 4344 | // on whether there exists an instantiation of A that causes |
| 4345 | // 'foo' to equal C. There are restrictions on class-heads |
| 4346 | // (which we declare (by fiat) elaborated friend declarations to |
| 4347 | // be) that makes this tractable. |
| 4348 | // |
| 4349 | // FIXME: handle "template <> friend class A<T>;", which |
| 4350 | // is possibly well-formed? Who even knows? |
| 4351 | if (TempParams.size() && !isa<ElaboratedType>(T)) { |
| 4352 | Diag(Loc, diag::err_tagless_friend_type_template) |
| 4353 | << DS.getSourceRange(); |
| 4354 | return DeclPtrTy(); |
| 4355 | } |
| 4356 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4357 | // C++ [class.friend]p2: |
| 4358 | // An elaborated-type-specifier shall be used in a friend declaration |
| 4359 | // for a class.* |
| 4360 | // * The class-key of the elaborated-type-specifier is required. |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 4361 | // This is one of the rare places in Clang where it's legitimate to |
| 4362 | // ask about the "spelling" of the type. |
| 4363 | if (!getLangOptions().CPlusPlus0x && !isa<ElaboratedType>(T)) { |
| 4364 | // If we evaluated the type to a record type, suggest putting |
| 4365 | // a tag in front. |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4366 | if (const RecordType *RT = T->getAs<RecordType>()) { |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 4367 | RecordDecl *RD = RT->getDecl(); |
| 4368 | |
| 4369 | std::string InsertionText = std::string(" ") + RD->getKindName(); |
| 4370 | |
John McCall | e3af023 | 2009-10-07 23:34:25 +0000 | [diff] [blame] | 4371 | Diag(DS.getTypeSpecTypeLoc(), diag::err_unelaborated_friend_type) |
| 4372 | << (unsigned) RD->getTagKind() |
| 4373 | << T |
| 4374 | << SourceRange(DS.getFriendSpecLoc()) |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 4375 | << CodeModificationHint::CreateInsertion(DS.getTypeSpecTypeLoc(), |
| 4376 | InsertionText); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4377 | return DeclPtrTy(); |
| 4378 | }else { |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 4379 | Diag(DS.getFriendSpecLoc(), diag::err_unexpected_friend) |
| 4380 | << DS.getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4381 | return DeclPtrTy(); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4382 | } |
| 4383 | } |
| 4384 | |
John McCall | e3af023 | 2009-10-07 23:34:25 +0000 | [diff] [blame] | 4385 | // Enum types cannot be friends. |
| 4386 | if (T->getAs<EnumType>()) { |
| 4387 | Diag(DS.getTypeSpecTypeLoc(), diag::err_enum_friend) |
| 4388 | << SourceRange(DS.getFriendSpecLoc()); |
| 4389 | return DeclPtrTy(); |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 4390 | } |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4391 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4392 | // C++98 [class.friend]p1: A friend of a class is a function |
| 4393 | // or class that is not a member of the class . . . |
| 4394 | // But that's a silly restriction which nobody implements for |
| 4395 | // inner classes, and C++0x removes it anyway, so we only report |
| 4396 | // this (as a warning) if we're being pedantic. |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 4397 | if (!getLangOptions().CPlusPlus0x) |
| 4398 | if (const RecordType *RT = T->getAs<RecordType>()) |
| 4399 | if (RT->getDecl()->getDeclContext() == CurContext) |
| 4400 | Diag(DS.getFriendSpecLoc(), diag::ext_friend_inner_class); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4401 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 4402 | Decl *D; |
| 4403 | if (TempParams.size()) |
| 4404 | D = FriendTemplateDecl::Create(Context, CurContext, Loc, |
| 4405 | TempParams.size(), |
| 4406 | (TemplateParameterList**) TempParams.release(), |
| 4407 | T.getTypePtr(), |
| 4408 | DS.getFriendSpecLoc()); |
| 4409 | else |
| 4410 | D = FriendDecl::Create(Context, CurContext, Loc, T.getTypePtr(), |
| 4411 | DS.getFriendSpecLoc()); |
| 4412 | D->setAccess(AS_public); |
| 4413 | CurContext->addDecl(D); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4414 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 4415 | return DeclPtrTy::make(D); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4416 | } |
| 4417 | |
John McCall | bbbcdd9 | 2009-09-11 21:02:39 +0000 | [diff] [blame] | 4418 | Sema::DeclPtrTy |
| 4419 | Sema::ActOnFriendFunctionDecl(Scope *S, |
| 4420 | Declarator &D, |
| 4421 | bool IsDefinition, |
| 4422 | MultiTemplateParamsArg TemplateParams) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4423 | const DeclSpec &DS = D.getDeclSpec(); |
| 4424 | |
| 4425 | assert(DS.isFriendSpecified()); |
| 4426 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 4427 | |
| 4428 | SourceLocation Loc = D.getIdentifierLoc(); |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 4429 | DeclaratorInfo *DInfo = 0; |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4430 | QualType T = GetTypeForDeclarator(D, S, &DInfo); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4431 | |
| 4432 | // C++ [class.friend]p1 |
| 4433 | // A friend of a class is a function or class.... |
| 4434 | // Note that this sees through typedefs, which is intended. |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4435 | // It *doesn't* see through dependent types, which is correct |
| 4436 | // according to [temp.arg.type]p3: |
| 4437 | // If a declaration acquires a function type through a |
| 4438 | // type dependent on a template-parameter and this causes |
| 4439 | // a declaration that does not use the syntactic form of a |
| 4440 | // function declarator to have a function type, the program |
| 4441 | // is ill-formed. |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4442 | if (!T->isFunctionType()) { |
| 4443 | Diag(Loc, diag::err_unexpected_friend); |
| 4444 | |
| 4445 | // It might be worthwhile to try to recover by creating an |
| 4446 | // appropriate declaration. |
| 4447 | return DeclPtrTy(); |
| 4448 | } |
| 4449 | |
| 4450 | // C++ [namespace.memdef]p3 |
| 4451 | // - If a friend declaration in a non-local class first declares a |
| 4452 | // class or function, the friend class or function is a member |
| 4453 | // of the innermost enclosing namespace. |
| 4454 | // - The name of the friend is not found by simple name lookup |
| 4455 | // until a matching declaration is provided in that namespace |
| 4456 | // scope (either before or after the class declaration granting |
| 4457 | // friendship). |
| 4458 | // - If a friend function is called, its name may be found by the |
| 4459 | // name lookup that considers functions from namespaces and |
| 4460 | // classes associated with the types of the function arguments. |
| 4461 | // - When looking for a prior declaration of a class or a function |
| 4462 | // declared as a friend, scopes outside the innermost enclosing |
| 4463 | // namespace scope are not considered. |
| 4464 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4465 | CXXScopeSpec &ScopeQual = D.getCXXScopeSpec(); |
| 4466 | DeclarationName Name = GetNameForDeclarator(D); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4467 | assert(Name); |
| 4468 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4469 | // The context we found the declaration in, or in which we should |
| 4470 | // create the declaration. |
| 4471 | DeclContext *DC; |
| 4472 | |
| 4473 | // FIXME: handle local classes |
| 4474 | |
| 4475 | // Recover from invalid scope qualifiers as if they just weren't there. |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 4476 | NamedDecl *PrevDecl = 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4477 | if (!ScopeQual.isInvalid() && ScopeQual.isSet()) { |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 4478 | // FIXME: RequireCompleteDeclContext |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4479 | DC = computeDeclContext(ScopeQual); |
| 4480 | |
| 4481 | // FIXME: handle dependent contexts |
| 4482 | if (!DC) return DeclPtrTy(); |
| 4483 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4484 | LookupResult R; |
| 4485 | LookupQualifiedName(R, DC, Name, LookupOrdinaryName, true); |
| 4486 | PrevDecl = R.getAsSingleDecl(Context); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4487 | |
| 4488 | // If searching in that context implicitly found a declaration in |
| 4489 | // a different context, treat it like it wasn't found at all. |
| 4490 | // TODO: better diagnostics for this case. Suggesting the right |
| 4491 | // qualified scope would be nice... |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 4492 | if (!PrevDecl || !PrevDecl->getDeclContext()->Equals(DC)) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4493 | D.setInvalidType(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4494 | Diag(Loc, diag::err_qualified_friend_not_found) << Name << T; |
| 4495 | return DeclPtrTy(); |
| 4496 | } |
| 4497 | |
| 4498 | // C++ [class.friend]p1: A friend of a class is a function or |
| 4499 | // class that is not a member of the class . . . |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 4500 | if (DC->Equals(CurContext)) |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4501 | Diag(DS.getFriendSpecLoc(), diag::err_friend_is_member); |
| 4502 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4503 | // Otherwise walk out to the nearest namespace scope looking for matches. |
| 4504 | } else { |
| 4505 | // TODO: handle local class contexts. |
| 4506 | |
| 4507 | DC = CurContext; |
| 4508 | while (true) { |
| 4509 | // Skip class contexts. If someone can cite chapter and verse |
| 4510 | // for this behavior, that would be nice --- it's what GCC and |
| 4511 | // EDG do, and it seems like a reasonable intent, but the spec |
| 4512 | // really only says that checks for unqualified existing |
| 4513 | // declarations should stop at the nearest enclosing namespace, |
| 4514 | // not that they should only consider the nearest enclosing |
| 4515 | // namespace. |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 4516 | while (DC->isRecord()) |
| 4517 | DC = DC->getParent(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4518 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4519 | LookupResult R; |
| 4520 | LookupQualifiedName(R, DC, Name, LookupOrdinaryName, true); |
| 4521 | PrevDecl = R.getAsSingleDecl(Context); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4522 | |
| 4523 | // TODO: decide what we think about using declarations. |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 4524 | if (PrevDecl) |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4525 | break; |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 4526 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4527 | if (DC->isFileContext()) break; |
| 4528 | DC = DC->getParent(); |
| 4529 | } |
| 4530 | |
| 4531 | // C++ [class.friend]p1: A friend of a class is a function or |
| 4532 | // class that is not a member of the class . . . |
John McCall | 7f27d92 | 2009-08-06 20:49:32 +0000 | [diff] [blame] | 4533 | // C++0x changes this for both friend types and functions. |
| 4534 | // Most C++ 98 compilers do seem to give an error here, so |
| 4535 | // we do, too. |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 4536 | if (PrevDecl && DC->Equals(CurContext) && !getLangOptions().CPlusPlus0x) |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4537 | Diag(DS.getFriendSpecLoc(), diag::err_friend_is_member); |
| 4538 | } |
| 4539 | |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 4540 | if (DC->isFileContext()) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4541 | // This implies that it has to be an operator or function. |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4542 | if (D.getName().getKind() == UnqualifiedId::IK_ConstructorName || |
| 4543 | D.getName().getKind() == UnqualifiedId::IK_DestructorName || |
| 4544 | D.getName().getKind() == UnqualifiedId::IK_ConversionFunctionId) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4545 | Diag(Loc, diag::err_introducing_special_friend) << |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4546 | (D.getName().getKind() == UnqualifiedId::IK_ConstructorName ? 0 : |
| 4547 | D.getName().getKind() == UnqualifiedId::IK_DestructorName ? 1 : 2); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4548 | return DeclPtrTy(); |
| 4549 | } |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4550 | } |
| 4551 | |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 4552 | bool Redeclaration = false; |
| 4553 | NamedDecl *ND = ActOnFunctionDeclarator(S, D, DC, T, DInfo, PrevDecl, |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 4554 | move(TemplateParams), |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 4555 | IsDefinition, |
| 4556 | Redeclaration); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4557 | if (!ND) return DeclPtrTy(); |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 4558 | |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 4559 | assert(ND->getDeclContext() == DC); |
| 4560 | assert(ND->getLexicalDeclContext() == CurContext); |
John McCall | 88232aa | 2009-08-18 00:00:49 +0000 | [diff] [blame] | 4561 | |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 4562 | // Add the function declaration to the appropriate lookup tables, |
| 4563 | // adjusting the redeclarations list as necessary. We don't |
| 4564 | // want to do this yet if the friending class is dependent. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4565 | // |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 4566 | // Also update the scope-based lookup if the target context's |
| 4567 | // lookup context is in lexical scope. |
| 4568 | if (!CurContext->isDependentContext()) { |
| 4569 | DC = DC->getLookupContext(); |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 4570 | DC->makeDeclVisibleInContext(ND, /* Recoverable=*/ false); |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 4571 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 4572 | PushOnScopeChains(ND, EnclosingScope, /*AddToContext=*/ false); |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 4573 | } |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4574 | |
| 4575 | FriendDecl *FrD = FriendDecl::Create(Context, CurContext, |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 4576 | D.getIdentifierLoc(), ND, |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4577 | DS.getFriendSpecLoc()); |
John McCall | 5fee110 | 2009-08-29 03:50:18 +0000 | [diff] [blame] | 4578 | FrD->setAccess(AS_public); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4579 | CurContext->addDecl(FrD); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4580 | |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 4581 | return DeclPtrTy::make(ND); |
Anders Carlsson | 0033836 | 2009-05-11 22:55:49 +0000 | [diff] [blame] | 4582 | } |
| 4583 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4584 | void Sema::SetDeclDeleted(DeclPtrTy dcl, SourceLocation DelLoc) { |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 4585 | AdjustDeclIfTemplate(dcl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4586 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4587 | Decl *Dcl = dcl.getAs<Decl>(); |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 4588 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(Dcl); |
| 4589 | if (!Fn) { |
| 4590 | Diag(DelLoc, diag::err_deleted_non_function); |
| 4591 | return; |
| 4592 | } |
| 4593 | if (const FunctionDecl *Prev = Fn->getPreviousDeclaration()) { |
| 4594 | Diag(DelLoc, diag::err_deleted_decl_not_first); |
| 4595 | Diag(Prev->getLocation(), diag::note_previous_declaration); |
| 4596 | // If the declaration wasn't the first, we delete the function anyway for |
| 4597 | // recovery. |
| 4598 | } |
| 4599 | Fn->setDeleted(); |
| 4600 | } |
Sebastian Redl | 13e8854 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 4601 | |
| 4602 | static void SearchForReturnInStmt(Sema &Self, Stmt *S) { |
| 4603 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); CI != E; |
| 4604 | ++CI) { |
| 4605 | Stmt *SubStmt = *CI; |
| 4606 | if (!SubStmt) |
| 4607 | continue; |
| 4608 | if (isa<ReturnStmt>(SubStmt)) |
| 4609 | Self.Diag(SubStmt->getSourceRange().getBegin(), |
| 4610 | diag::err_return_in_constructor_handler); |
| 4611 | if (!isa<Expr>(SubStmt)) |
| 4612 | SearchForReturnInStmt(Self, SubStmt); |
| 4613 | } |
| 4614 | } |
| 4615 | |
| 4616 | void Sema::DiagnoseReturnInConstructorExceptionHandler(CXXTryStmt *TryBlock) { |
| 4617 | for (unsigned I = 0, E = TryBlock->getNumHandlers(); I != E; ++I) { |
| 4618 | CXXCatchStmt *Handler = TryBlock->getHandler(I); |
| 4619 | SearchForReturnInStmt(*this, Handler); |
| 4620 | } |
| 4621 | } |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 4622 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4623 | bool Sema::CheckOverridingFunctionReturnType(const CXXMethodDecl *New, |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 4624 | const CXXMethodDecl *Old) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4625 | QualType NewTy = New->getType()->getAs<FunctionType>()->getResultType(); |
| 4626 | QualType OldTy = Old->getType()->getAs<FunctionType>()->getResultType(); |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 4627 | |
| 4628 | QualType CNewTy = Context.getCanonicalType(NewTy); |
| 4629 | QualType COldTy = Context.getCanonicalType(OldTy); |
| 4630 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4631 | if (CNewTy == COldTy && |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 4632 | CNewTy.getCVRQualifiers() == COldTy.getCVRQualifiers()) |
| 4633 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4634 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 4635 | // Check if the return types are covariant |
| 4636 | QualType NewClassTy, OldClassTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4637 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 4638 | /// Both types must be pointers or references to classes. |
| 4639 | if (PointerType *NewPT = dyn_cast<PointerType>(NewTy)) { |
| 4640 | if (PointerType *OldPT = dyn_cast<PointerType>(OldTy)) { |
| 4641 | NewClassTy = NewPT->getPointeeType(); |
| 4642 | OldClassTy = OldPT->getPointeeType(); |
| 4643 | } |
| 4644 | } else if (ReferenceType *NewRT = dyn_cast<ReferenceType>(NewTy)) { |
| 4645 | if (ReferenceType *OldRT = dyn_cast<ReferenceType>(OldTy)) { |
| 4646 | NewClassTy = NewRT->getPointeeType(); |
| 4647 | OldClassTy = OldRT->getPointeeType(); |
| 4648 | } |
| 4649 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4650 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 4651 | // The return types aren't either both pointers or references to a class type. |
| 4652 | if (NewClassTy.isNull()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4653 | Diag(New->getLocation(), |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 4654 | diag::err_different_return_type_for_overriding_virtual_function) |
| 4655 | << New->getDeclName() << NewTy << OldTy; |
| 4656 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4657 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 4658 | return true; |
| 4659 | } |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 4660 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 4661 | if (NewClassTy.getUnqualifiedType() != OldClassTy.getUnqualifiedType()) { |
| 4662 | // Check if the new class derives from the old class. |
| 4663 | if (!IsDerivedFrom(NewClassTy, OldClassTy)) { |
| 4664 | Diag(New->getLocation(), |
| 4665 | diag::err_covariant_return_not_derived) |
| 4666 | << New->getDeclName() << NewTy << OldTy; |
| 4667 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 4668 | return true; |
| 4669 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4670 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 4671 | // Check if we the conversion from derived to base is valid. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4672 | if (CheckDerivedToBaseConversion(NewClassTy, OldClassTy, |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 4673 | diag::err_covariant_return_inaccessible_base, |
| 4674 | diag::err_covariant_return_ambiguous_derived_to_base_conv, |
| 4675 | // FIXME: Should this point to the return type? |
| 4676 | New->getLocation(), SourceRange(), New->getDeclName())) { |
| 4677 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 4678 | return true; |
| 4679 | } |
| 4680 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4681 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 4682 | // The qualifiers of the return types must be the same. |
| 4683 | if (CNewTy.getCVRQualifiers() != COldTy.getCVRQualifiers()) { |
| 4684 | Diag(New->getLocation(), |
| 4685 | diag::err_covariant_return_type_different_qualifications) |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 4686 | << New->getDeclName() << NewTy << OldTy; |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 4687 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 4688 | return true; |
| 4689 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4690 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 4691 | |
| 4692 | // The new class type must have the same or less qualifiers as the old type. |
| 4693 | if (NewClassTy.isMoreQualifiedThan(OldClassTy)) { |
| 4694 | Diag(New->getLocation(), |
| 4695 | diag::err_covariant_return_type_class_type_more_qualified) |
| 4696 | << New->getDeclName() << NewTy << OldTy; |
| 4697 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 4698 | return true; |
| 4699 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4700 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 4701 | return false; |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 4702 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 4703 | |
| 4704 | /// ActOnCXXEnterDeclInitializer - Invoked when we are about to parse an |
| 4705 | /// initializer for the declaration 'Dcl'. |
| 4706 | /// After this method is called, according to [C++ 3.4.1p13], if 'Dcl' is a |
| 4707 | /// static data member of class X, names should be looked up in the scope of |
| 4708 | /// class X. |
| 4709 | void Sema::ActOnCXXEnterDeclInitializer(Scope *S, DeclPtrTy Dcl) { |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 4710 | AdjustDeclIfTemplate(Dcl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4711 | |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 4712 | Decl *D = Dcl.getAs<Decl>(); |
| 4713 | // If there is no declaration, there was an error parsing it. |
| 4714 | if (D == 0) |
| 4715 | return; |
| 4716 | |
| 4717 | // Check whether it is a declaration with a nested name specifier like |
| 4718 | // int foo::bar; |
| 4719 | if (!D->isOutOfLine()) |
| 4720 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4721 | |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 4722 | // C++ [basic.lookup.unqual]p13 |
| 4723 | // |
| 4724 | // A name used in the definition of a static data member of class X |
| 4725 | // (after the qualified-id of the static member) is looked up as if the name |
| 4726 | // was used in a member function of X. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4727 | |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 4728 | // Change current context into the context of the initializing declaration. |
Argyrios Kyrtzidis | 1d17553 | 2009-06-17 23:15:40 +0000 | [diff] [blame] | 4729 | EnterDeclaratorContext(S, D->getDeclContext()); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 4730 | } |
| 4731 | |
| 4732 | /// ActOnCXXExitDeclInitializer - Invoked after we are finished parsing an |
| 4733 | /// initializer for the declaration 'Dcl'. |
| 4734 | void Sema::ActOnCXXExitDeclInitializer(Scope *S, DeclPtrTy Dcl) { |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 4735 | AdjustDeclIfTemplate(Dcl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4736 | |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 4737 | Decl *D = Dcl.getAs<Decl>(); |
| 4738 | // If there is no declaration, there was an error parsing it. |
| 4739 | if (D == 0) |
| 4740 | return; |
| 4741 | |
| 4742 | // Check whether it is a declaration with a nested name specifier like |
| 4743 | // int foo::bar; |
| 4744 | if (!D->isOutOfLine()) |
| 4745 | return; |
| 4746 | |
| 4747 | assert(S->getEntity() == D->getDeclContext() && "Context imbalance!"); |
Argyrios Kyrtzidis | 1d17553 | 2009-06-17 23:15:40 +0000 | [diff] [blame] | 4748 | ExitDeclaratorContext(S); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 4749 | } |