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