Chris Lattner | ac7b83a | 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 | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 15 | #include "SemaInherit.h" |
Argiris Kirtzidis | 7c210ea | 2008-08-09 00:58:37 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTConsumer.h" |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Anders Carlsson | 412c340 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclVisitor.h" |
Douglas Gregor | 0590402 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 19 | #include "clang/AST/TypeOrdering.h" |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 20 | #include "clang/AST/StmtVisitor.h" |
Anders Carlsson | a21e787 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 21 | #include "clang/Basic/PartialDiagnostic.h" |
Argiris Kirtzidis | ffcb503 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 22 | #include "clang/Lex/Preprocessor.h" |
Daniel Dunbar | 8d03cbe | 2008-08-11 03:27:53 +0000 | [diff] [blame] | 23 | #include "clang/Parse/DeclSpec.h" |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Compiler.h" |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 26 | #include <algorithm> // for std::equal |
Douglas Gregor | abed217 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 27 | #include <map> |
Chris Lattner | ac7b83a | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 28 | |
| 29 | using namespace clang; |
| 30 | |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
| 32 | // CheckDefaultArgumentVisitor |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | |
Chris Lattner | b1856db | 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 | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 42 | : public StmtVisitor<CheckDefaultArgumentVisitor, bool> { |
Chris Lattner | b1856db | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 43 | Expr *DefaultArg; |
| 44 | Sema *S; |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 45 | |
Chris Lattner | b1856db | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 46 | public: |
| 47 | CheckDefaultArgumentVisitor(Expr *defarg, Sema *s) |
| 48 | : DefaultArg(defarg), S(s) {} |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 49 | |
Chris Lattner | b1856db | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 50 | bool VisitExpr(Expr *Node); |
| 51 | bool VisitDeclRefExpr(DeclRefExpr *DRE); |
Douglas Gregor | a5b022a | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 52 | bool VisitCXXThisExpr(CXXThisExpr *ThisE); |
Chris Lattner | b1856db | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 53 | }; |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 54 | |
Chris Lattner | b1856db | 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 | d5a56aa | 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 | b1856db | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 61 | return IsInvalid; |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Chris Lattner | b1856db | 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 | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 68 | NamedDecl *Decl = DRE->getDecl(); |
Chris Lattner | b1856db | 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 | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 79 | diag::err_param_default_argument_references_param) |
Chris Lattner | b175342 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 80 | << Param->getDeclName() << DefaultArg->getSourceRange(); |
Steve Naroff | 72a6ebc | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 81 | } else if (VarDecl *VDecl = dyn_cast<VarDecl>(Decl)) { |
Chris Lattner | b1856db | 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 | 72a6ebc | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 85 | if (VDecl->isBlockVarDecl()) |
| 86 | return S->Diag(DRE->getSourceRange().getBegin(), |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 87 | diag::err_param_default_argument_references_local) |
Chris Lattner | b175342 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 88 | << VDecl->getDeclName() << DefaultArg->getSourceRange(); |
Chris Lattner | b1856db | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 89 | } |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 90 | |
Douglas Gregor | 3c24695 | 2008-11-04 13:41:56 +0000 | [diff] [blame] | 91 | return false; |
| 92 | } |
Chris Lattner | b1856db | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 93 | |
Douglas Gregor | a5b022a | 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 | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 100 | diag::err_param_default_argument_references_this) |
| 101 | << ThisE->getSourceRange(); |
Chris Lattner | b1856db | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 102 | } |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Anders Carlsson | 60eb3be | 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 | ec92687 | 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 | 60eb3be | 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 | 92936c4 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 127 | return true; |
Anders Carlsson | 60eb3be | 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 | 92936c4 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 136 | return false; |
Anders Carlsson | 60eb3be | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Chris Lattner | 97316c0 | 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 | ac7b83a | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 142 | void |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 143 | Sema::ActOnParamDefaultArgument(DeclPtrTy param, SourceLocation EqualLoc, |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 144 | ExprArg defarg) { |
Douglas Gregor | ac77dd6 | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 145 | if (!param || !defarg.get()) |
| 146 | return; |
| 147 | |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 148 | ParmVarDecl *Param = cast<ParmVarDecl>(param.getAs<Decl>()); |
Anders Carlsson | a116e6e | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 149 | UnparsedDefaultArgLocs.erase(Param); |
| 150 | |
Anders Carlsson | c154a72 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 151 | ExprOwningPtr<Expr> DefaultArg(this, defarg.takeAs<Expr>()); |
Chris Lattner | ac7b83a | 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 | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 156 | Diag(EqualLoc, diag::err_param_default_argument) |
| 157 | << DefaultArg->getSourceRange(); |
Douglas Gregor | 605de8d | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 158 | Param->setInvalidDecl(); |
Chris Lattner | ac7b83a | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 159 | return; |
| 160 | } |
| 161 | |
Anders Carlsson | e049819 | 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 | 60eb3be | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 169 | SetParamDefaultArgument(Param, move(DefaultArg), EqualLoc); |
Chris Lattner | ac7b83a | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Douglas Gregor | 62ae25a | 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 | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 176 | void Sema::ActOnParamUnparsedDefaultArgument(DeclPtrTy param, |
Anders Carlsson | a116e6e | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 177 | SourceLocation EqualLoc, |
| 178 | SourceLocation ArgLoc) { |
Douglas Gregor | ac77dd6 | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 179 | if (!param) |
| 180 | return; |
| 181 | |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 182 | ParmVarDecl *Param = cast<ParmVarDecl>(param.getAs<Decl>()); |
Douglas Gregor | 62ae25a | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 183 | if (Param) |
| 184 | Param->setUnparsedDefaultArg(); |
Anders Carlsson | a116e6e | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 185 | |
| 186 | UnparsedDefaultArgLocs[Param] = ArgLoc; |
Douglas Gregor | 62ae25a | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Douglas Gregor | 605de8d | 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 | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 191 | void Sema::ActOnParamDefaultArgumentError(DeclPtrTy param) { |
Douglas Gregor | ac77dd6 | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 192 | if (!param) |
| 193 | return; |
| 194 | |
Anders Carlsson | a116e6e | 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 | 605de8d | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Douglas Gregor | 2b9422f | 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 | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 215 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { |
Douglas Gregor | 2b9422f | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 216 | DeclaratorChunk &chunk = D.getTypeObject(i); |
| 217 | if (chunk.Kind == DeclaratorChunk::Function) { |
Chris Lattner | 5261d0c | 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 | 62ae25a | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 221 | if (Param->hasUnparsedDefaultArg()) { |
| 222 | CachedTokens *Toks = chunk.Fun.ArgInfo[argIdx].DefaultArgTokens; |
Douglas Gregor | 605de8d | 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 | 62ae25a | 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 | 2b9422f | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
Chris Lattner | ac7b83a | 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 | 083c23e | 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 | ac7b83a | 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 | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 263 | diag::err_param_default_argument_redefinition) |
| 264 | << NewParam->getDefaultArg()->getSourceRange(); |
Chris Lattner | 1336cab | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 265 | Diag(OldParam->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 083c23e | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 266 | Invalid = true; |
Chris Lattner | ac7b83a | 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 | 5e1e0a0 | 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 | 083c23e | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 279 | return Invalid; |
Chris Lattner | ac7b83a | 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 | 73af519 | 2009-08-25 01:23:32 +0000 | [diff] [blame] | 292 | if (Param->hasDefaultArg()) |
Chris Lattner | ac7b83a | 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 | 73af519 | 2009-08-25 01:23:32 +0000 | [diff] [blame] | 305 | if (!Param->hasDefaultArg()) { |
Douglas Gregor | 605de8d | 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 | ac7b83a | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 309 | Diag(Param->getLocation(), |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 310 | diag::err_param_default_argument_missing_name) |
Chris Lattner | e46b879 | 2008-11-19 07:32:16 +0000 | [diff] [blame] | 311 | << Param->getIdentifier(); |
Chris Lattner | ac7b83a | 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 | a116e6e | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 327 | if (Param->hasDefaultArg()) { |
Douglas Gregor | 62ae25a | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 328 | if (!Param->hasUnparsedDefaultArg()) |
| 329 | Param->getDefaultArg()->Destroy(Context); |
Chris Lattner | ac7b83a | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 330 | Param->setDefaultArg(0); |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | } |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 335 | |
Douglas Gregor | f15ac4b | 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. |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 340 | bool Sema::isCurrentClassName(const IdentifierInfo &II, Scope *, |
| 341 | const CXXScopeSpec *SS) { |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 342 | CXXRecordDecl *CurDecl; |
Douglas Gregor | 734b4ba | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 343 | if (SS && SS->isSet() && !SS->isInvalid()) { |
Douglas Gregor | 0c3c9d0 | 2009-08-21 22:16:40 +0000 | [diff] [blame] | 344 | DeclContext *DC = computeDeclContext(*SS, true); |
Argiris Kirtzidis | 054a263 | 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 | f15ac4b | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 350 | return &II == CurDecl->getIdentifier(); |
| 351 | else |
| 352 | return false; |
| 353 | } |
| 354 | |
Douglas Gregor | ed3a398 | 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 | 1373b6f | 2009-07-22 17:41:53 +0000 | [diff] [blame] | 374 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
Douglas Gregor | ed3a398 | 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 | a21e787 | 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 | ed3a398 | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 397 | return 0; |
| 398 | |
Eli Friedman | 10747ff | 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 | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 400 | RecordDecl *BaseDecl = BaseType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | ed3a398 | 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 | 10747ff | 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 | ed3a398 | 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 | c636371 | 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 | f73c851 | 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 | 10747ff | 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 | c636371 | 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 | f73c851 | 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 | c636371 | 2009-04-16 00:08:20 +0000 | [diff] [blame] | 452 | } |
Anders Carlsson | 39a10db | 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 | f73c851 | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 457 | if (!cast<CXXRecordDecl>(BaseDecl)->hasTrivialDestructor()) |
| 458 | Class->setHasTrivialDestructor(false); |
Anders Carlsson | c636371 | 2009-04-16 00:08:20 +0000 | [diff] [blame] | 459 | |
Douglas Gregor | ed3a398 | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 460 | // Create the base specifier. |
| 461 | // FIXME: Allocate via ASTContext? |
Fariborz Jahanian | 1373b6f | 2009-07-22 17:41:53 +0000 | [diff] [blame] | 462 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
Douglas Gregor | ed3a398 | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 463 | Class->getTagKind() == RecordDecl::TK_class, |
| 464 | Access, BaseType); |
| 465 | } |
| 466 | |
Douglas Gregor | ec93f44 | 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 | abed217 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 472 | Sema::BaseResult |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 473 | Sema::ActOnBaseSpecifier(DeclPtrTy classdecl, SourceRange SpecifierRange, |
Douglas Gregor | abed217 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 474 | bool Virtual, AccessSpecifier Access, |
| 475 | TypeTy *basetype, SourceLocation BaseLoc) { |
Douglas Gregor | ac77dd6 | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 476 | if (!classdecl) |
| 477 | return true; |
| 478 | |
Douglas Gregor | f9ff4b1 | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 479 | AdjustDeclIfTemplate(classdecl); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 480 | CXXRecordDecl *Class = cast<CXXRecordDecl>(classdecl.getAs<Decl>()); |
Argiris Kirtzidis | d6802ba | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 481 | QualType BaseType = GetTypeFromParser(basetype); |
Douglas Gregor | ed3a398 | 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 | abed217 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 488 | } |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 489 | |
Douglas Gregor | ed3a398 | 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 | abed217 | 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 | 4fd8590 | 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 | abed217 | 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 | 4fd8590 | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 504 | unsigned NumGoodBases = 0; |
Douglas Gregor | ed3a398 | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 505 | bool Invalid = false; |
Douglas Gregor | 4fd8590 | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 506 | for (unsigned idx = 0; idx < NumBases; ++idx) { |
Douglas Gregor | abed217 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 507 | QualType NewBaseType |
Douglas Gregor | ed3a398 | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 508 | = Context.getCanonicalType(Bases[idx]->getType()); |
Douglas Gregor | 4fd8590 | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 509 | NewBaseType = NewBaseType.getUnqualifiedType(); |
| 510 | |
Douglas Gregor | abed217 | 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 | ed3a398 | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 515 | Diag(Bases[idx]->getSourceRange().getBegin(), |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 516 | diag::err_duplicate_base_class) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 517 | << KnownBaseTypes[NewBaseType]->getType() |
Douglas Gregor | ed3a398 | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 518 | << Bases[idx]->getSourceRange(); |
Douglas Gregor | 4fd8590 | 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 | f2fedc6 | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 522 | Context.Deallocate(Bases[idx]); |
Douglas Gregor | ed3a398 | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 523 | |
| 524 | Invalid = true; |
Douglas Gregor | abed217 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 525 | } else { |
| 526 | // Okay, add this new base class. |
Douglas Gregor | ed3a398 | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 527 | KnownBaseTypes[NewBaseType] = Bases[idx]; |
| 528 | Bases[NumGoodBases++] = Bases[idx]; |
Douglas Gregor | abed217 | 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 | 9cd0a3c | 2009-07-02 18:26:15 +0000 | [diff] [blame] | 533 | Class->setBases(Context, Bases, NumGoodBases); |
Douglas Gregor | 4fd8590 | 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 | f2fedc6 | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 538 | Context.Deallocate(Bases[idx]); |
Douglas Gregor | ed3a398 | 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 | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 546 | void Sema::ActOnBaseSpecifiers(DeclPtrTy ClassDecl, BaseTy **Bases, |
Douglas Gregor | ed3a398 | 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 | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 552 | AttachBaseSpecifiers(cast<CXXRecordDecl>(ClassDecl.getAs<Decl>()), |
Douglas Gregor | ed3a398 | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 553 | (CXXBaseSpecifier**)(Bases), NumBases); |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 554 | } |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 555 | |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 556 | //===----------------------------------------------------------------------===// |
| 557 | // C++ class member Handling |
| 558 | //===----------------------------------------------------------------------===// |
| 559 | |
Argiris Kirtzidis | 38f1671 | 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 | d6c7809 | 2009-04-12 22:37:57 +0000 | [diff] [blame] | 563 | /// any. |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 564 | Sema::DeclPtrTy |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 565 | Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D, |
Douglas Gregor | 398a801 | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 566 | MultiTemplateParamsArg TemplateParameterLists, |
Sebastian Redl | a55834a | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 567 | ExprTy *BW, ExprTy *InitExpr, bool Deleted) { |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 568 | const DeclSpec &DS = D.getDeclSpec(); |
Douglas Gregor | 6704b31 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 569 | DeclarationName Name = GetNameForDeclarator(D); |
Argiris Kirtzidis | 38f1671 | 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 | 9f5337b | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 574 | bool isFunc = D.isFunctionDeclarator(); |
| 575 | |
John McCall | 140607b | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 576 | assert(!DS.isFriendSpecified()); |
| 577 | |
Argiris Kirtzidis | 38f1671 | 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 | 9f5337b | 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. |
Argiris Kirtzidis | 38f1671 | 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 | 9f5337b | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 589 | case DeclSpec::SCS_mutable: |
| 590 | if (isFunc) { |
| 591 | if (DS.getStorageClassSpecLoc().isValid()) |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 592 | Diag(DS.getStorageClassSpecLoc(), diag::err_mutable_function); |
Sebastian Redl | 9f5337b | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 593 | else |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 594 | Diag(DS.getThreadSpecLoc(), diag::err_mutable_function); |
| 595 | |
Sebastian Redl | 6a2b7fd | 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 | 9f5337b | 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 | 6a2b7fd | 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 | 9f5337b | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 613 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 614 | } |
| 615 | } |
| 616 | break; |
Argiris Kirtzidis | 38f1671 | 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 | |
Argiris Kirtzidis | e2900c6 | 2008-10-15 20:23:22 +0000 | [diff] [blame] | 626 | if (!isFunc && |
Douglas Gregor | a60c62e | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 627 | D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_typename && |
Argiris Kirtzidis | e2900c6 | 2008-10-15 20:23:22 +0000 | [diff] [blame] | 628 | D.getNumTypeObjects() == 0) { |
Argiris Kirtzidis | 1f0d4c2 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 629 | // Check also for this case: |
| 630 | // |
| 631 | // typedef int f(); |
| 632 | // f a; |
| 633 | // |
Argiris Kirtzidis | d6802ba | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 634 | QualType TDType = GetTypeFromParser(DS.getTypeRep()); |
Douglas Gregor | a60c62e | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 635 | isFunc = TDType->isFunctionType(); |
Argiris Kirtzidis | 1f0d4c2 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 636 | } |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 637 | |
Sebastian Redl | 9f5337b | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 638 | bool isInstField = ((DS.getStorageClassSpec() == DeclSpec::SCS_unspecified || |
| 639 | DS.getStorageClassSpec() == DeclSpec::SCS_mutable) && |
Argiris Kirtzidis | 1f0d4c2 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 640 | !isFunc); |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 641 | |
| 642 | Decl *Member; |
Chris Lattner | 9cffefc | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 643 | if (isInstField) { |
Douglas Gregor | 398a801 | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 644 | // FIXME: Check for template parameters! |
Douglas Gregor | 2f0ecef | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 645 | Member = HandleField(S, cast<CXXRecordDecl>(CurContext), Loc, D, BitWidth, |
| 646 | AS); |
Chris Lattner | e384c18 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 647 | assert(Member && "HandleField never returns null"); |
Chris Lattner | 9cffefc | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 648 | } else { |
Douglas Gregor | 398a801 | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 649 | Member = HandleDeclarator(S, D, move(TemplateParameterLists), false) |
| 650 | .getAs<Decl>(); |
Chris Lattner | e384c18 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 651 | if (!Member) { |
| 652 | if (BitWidth) DeleteExpr(BitWidth); |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 653 | return DeclPtrTy(); |
Chris Lattner | e384c18 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 654 | } |
Chris Lattner | 432780c | 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 | 0066058 | 2009-03-11 20:22:50 +0000 | [diff] [blame] | 660 | } else if (isa<VarDecl>(Member)) { |
Chris Lattner | 432780c | 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 | 0e518af | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 673 | << Name << cast<ValueDecl>(Member)->getType() |
| 674 | << BitWidth->getSourceRange(); |
Chris Lattner | 432780c | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | DeleteExpr(BitWidth); |
| 678 | BitWidth = 0; |
| 679 | Member->setInvalidDecl(); |
| 680 | } |
Douglas Gregor | 2f0ecef | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 681 | |
| 682 | Member->setAccess(AS); |
Douglas Gregor | 398a801 | 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 | 9cffefc | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 688 | } |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 689 | |
Douglas Gregor | 6704b31 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 690 | assert((Name || isInstField) && "No identifier for non-field ?"); |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 691 | |
Douglas Gregor | 8f53bb7 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 692 | if (Init) |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 693 | AddInitializerToDecl(DeclPtrTy::make(Member), ExprArg(*this, Init), false); |
Sebastian Redl | a55834a | 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()); |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 696 | |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 697 | if (isInstField) { |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 698 | FieldCollector->Add(cast<FieldDecl>(Member)); |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 699 | return DeclPtrTy(); |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 700 | } |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 701 | return DeclPtrTy::make(Member); |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 702 | } |
| 703 | |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 704 | /// ActOnMemInitializer - Handle a C++ member initializer. |
| 705 | Sema::MemInitResult |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 706 | Sema::ActOnMemInitializer(DeclPtrTy ConstructorD, |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 707 | Scope *S, |
Fariborz Jahanian | c37d306 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 708 | const CXXScopeSpec &SS, |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 709 | IdentifierInfo *MemberOrBase, |
Fariborz Jahanian | 2b2b736 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 710 | TypeTy *TemplateTypeTy, |
Douglas Gregor | a65e8dd | 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 | ac77dd6 | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 716 | if (!ConstructorD) |
| 717 | return true; |
| 718 | |
Douglas Gregor | 84164f0 | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 719 | AdjustDeclIfTemplate(ConstructorD); |
| 720 | |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 721 | CXXConstructorDecl *Constructor |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 722 | = dyn_cast<CXXConstructorDecl>(ConstructorD.getAs<Decl>()); |
Douglas Gregor | a65e8dd | 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 | 2b2b736 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 743 | if (!SS.getScopeRep() && !TemplateTypeTy) { |
Fariborz Jahanian | c37d306 | 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 | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 750 | |
Fariborz Jahanian | c37d306 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 751 | // FIXME: Handle members of an anonymous union. |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 752 | |
Eli Friedman | 724478c | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 753 | if (Member) |
| 754 | return BuildMemberInitializer(Member, (Expr**)Args, NumArgs, IdLoc, |
| 755 | RParenLoc); |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 756 | } |
Douglas Gregor | a65e8dd | 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 | 2b2b736 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 758 | TypeTy *BaseTy = TemplateTypeTy ? TemplateTypeTy |
| 759 | : getTypeName(*MemberOrBase, IdLoc, S, &SS); |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 760 | if (!BaseTy) |
Chris Lattner | 65cae29 | 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 | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 763 | |
Argiris Kirtzidis | d6802ba | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 764 | QualType BaseType = GetTypeFromParser(BaseTy); |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 765 | |
Eli Friedman | 724478c | 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 | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 784 | } else if (FieldType->getAs<RecordType>()) { |
Eli Friedman | 724478c | 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 | 7c9f783 | 2009-09-02 17:10:17 +0000 | [diff] [blame] | 789 | } else if (NumArgs != 1 && NumArgs != 0) { |
Eli Friedman | 724478c | 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 | 7c9f783 | 2009-09-02 17:10:17 +0000 | [diff] [blame] | 793 | Expr *NewExp; |
| 794 | if (NumArgs == 0) { |
Fariborz Jahanian | f988e22 | 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 | 7c9f783 | 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 | 724478c | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 805 | if (PerformCopyInitialization(NewExp, FieldType, "passing")) |
| 806 | return true; |
| 807 | Args[0] = NewExp; |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 808 | } |
Eli Friedman | 724478c | 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 | 6d68ad4 | 2009-08-29 01:31:33 +0000 | [diff] [blame] | 811 | NumArgs, C, IdLoc, RParenLoc); |
Eli Friedman | 724478c | 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 | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 863 | } |
| 864 | } |
| 865 | } |
Eli Friedman | 724478c | 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 | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 882 | } |
| 883 | |
Fariborz Jahanian | 898f574 | 2009-07-23 00:42:24 +0000 | [diff] [blame] | 884 | CXXConstructorDecl *C = 0; |
Eli Friedman | 724478c | 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 | 6d68ad4 | 2009-08-29 01:31:33 +0000 | [diff] [blame] | 894 | NumArgs, C, IdLoc, RParenLoc); |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 895 | } |
| 896 | |
Fariborz Jahanian | 7c1771c | 2009-07-23 23:32:59 +0000 | [diff] [blame] | 897 | void |
Fariborz Jahanian | f988e22 | 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 = |
| 951 | AllBaseFields.lookup(VBase->getType()->getAs<RecordType>())) |
| 952 | AllToInit.push_back(Value); |
| 953 | else { |
| 954 | CXXRecordDecl *VBaseDecl = |
| 955 | cast<CXXRecordDecl>(VBase->getType()->getAs<RecordType>()->getDecl()); |
| 956 | assert(VBaseDecl && "setBaseOrMemberInitializers - VBaseDecl null"); |
| 957 | if (!VBaseDecl->getDefaultConstructor(Context)) |
| 958 | Bases.push_back(VBase); |
| 959 | CXXBaseOrMemberInitializer *Member = |
| 960 | new (Context) CXXBaseOrMemberInitializer(VBase->getType(), 0, 0, |
| 961 | VBaseDecl->getDefaultConstructor(Context), |
| 962 | SourceLocation(), |
| 963 | SourceLocation()); |
| 964 | AllToInit.push_back(Member); |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | for (CXXRecordDecl::base_class_iterator Base = |
| 969 | ClassDecl->bases_begin(), |
| 970 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 971 | // Virtuals are in the virtual base list and already constructed. |
| 972 | if (Base->isVirtual()) |
| 973 | continue; |
| 974 | // Skip dependent types. |
| 975 | if (Base->getType()->isDependentType()) |
| 976 | continue; |
| 977 | if (CXXBaseOrMemberInitializer *Value = |
| 978 | AllBaseFields.lookup(Base->getType()->getAs<RecordType>())) |
| 979 | AllToInit.push_back(Value); |
| 980 | else { |
| 981 | CXXRecordDecl *BaseDecl = |
| 982 | cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 983 | assert(BaseDecl && "setBaseOrMemberInitializers - BaseDecl null"); |
| 984 | if (!BaseDecl->getDefaultConstructor(Context)) |
| 985 | Bases.push_back(Base); |
| 986 | CXXBaseOrMemberInitializer *Member = |
| 987 | new (Context) CXXBaseOrMemberInitializer(Base->getType(), 0, 0, |
| 988 | BaseDecl->getDefaultConstructor(Context), |
| 989 | SourceLocation(), |
| 990 | SourceLocation()); |
| 991 | AllToInit.push_back(Member); |
| 992 | } |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | // non-static data members. |
| 997 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 998 | E = ClassDecl->field_end(); Field != E; ++Field) { |
| 999 | if ((*Field)->isAnonymousStructOrUnion()) { |
| 1000 | if (const RecordType *FieldClassType = |
| 1001 | Field->getType()->getAs<RecordType>()) { |
| 1002 | CXXRecordDecl *FieldClassDecl |
| 1003 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
| 1004 | for(RecordDecl::field_iterator FA = FieldClassDecl->field_begin(), |
| 1005 | EA = FieldClassDecl->field_end(); FA != EA; FA++) { |
| 1006 | if (CXXBaseOrMemberInitializer *Value = AllBaseFields.lookup(*FA)) { |
| 1007 | // 'Member' is the anonymous union field and 'AnonUnionMember' is |
| 1008 | // set to the anonymous union data member used in the initializer |
| 1009 | // list. |
| 1010 | Value->setMember(*Field); |
| 1011 | Value->setAnonUnionMember(*FA); |
| 1012 | AllToInit.push_back(Value); |
| 1013 | break; |
| 1014 | } |
| 1015 | } |
| 1016 | } |
| 1017 | continue; |
| 1018 | } |
| 1019 | if (CXXBaseOrMemberInitializer *Value = AllBaseFields.lookup(*Field)) { |
| 1020 | AllToInit.push_back(Value); |
| 1021 | continue; |
| 1022 | } |
| 1023 | |
| 1024 | QualType FT = Context.getBaseElementType((*Field)->getType()); |
| 1025 | if (const RecordType* RT = FT->getAs<RecordType>()) { |
| 1026 | CXXConstructorDecl *Ctor = |
| 1027 | cast<CXXRecordDecl>(RT->getDecl())->getDefaultConstructor(Context); |
| 1028 | if (!Ctor && !FT->isDependentType()) |
| 1029 | Fields.push_back(*Field); |
| 1030 | CXXBaseOrMemberInitializer *Member = |
| 1031 | new (Context) CXXBaseOrMemberInitializer((*Field), 0, 0, |
| 1032 | Ctor, |
| 1033 | SourceLocation(), |
| 1034 | SourceLocation()); |
| 1035 | AllToInit.push_back(Member); |
| 1036 | if (FT.isConstQualified() && (!Ctor || Ctor->isTrivial())) { |
| 1037 | Diag(Constructor->getLocation(), diag::err_unintialized_member_in_ctor) |
| 1038 | << Context.getTagDeclType(ClassDecl) << 1 << (*Field)->getDeclName(); |
| 1039 | Diag((*Field)->getLocation(), diag::note_declared_at); |
| 1040 | } |
| 1041 | } |
| 1042 | else if (FT->isReferenceType()) { |
| 1043 | Diag(Constructor->getLocation(), diag::err_unintialized_member_in_ctor) |
| 1044 | << Context.getTagDeclType(ClassDecl) << 0 << (*Field)->getDeclName(); |
| 1045 | Diag((*Field)->getLocation(), diag::note_declared_at); |
| 1046 | } |
| 1047 | else if (FT.isConstQualified()) { |
| 1048 | Diag(Constructor->getLocation(), diag::err_unintialized_member_in_ctor) |
| 1049 | << Context.getTagDeclType(ClassDecl) << 1 << (*Field)->getDeclName(); |
| 1050 | Diag((*Field)->getLocation(), diag::note_declared_at); |
| 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | NumInitializers = AllToInit.size(); |
| 1055 | if (NumInitializers > 0) { |
| 1056 | Constructor->setNumBaseOrMemberInitializers(NumInitializers); |
| 1057 | CXXBaseOrMemberInitializer **baseOrMemberInitializers = |
| 1058 | new (Context) CXXBaseOrMemberInitializer*[NumInitializers]; |
| 1059 | |
| 1060 | Constructor->setBaseOrMemberInitializers(baseOrMemberInitializers); |
| 1061 | for (unsigned Idx = 0; Idx < NumInitializers; ++Idx) |
| 1062 | baseOrMemberInitializers[Idx] = AllToInit[Idx]; |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | void |
Fariborz Jahanian | 7c1771c | 2009-07-23 23:32:59 +0000 | [diff] [blame] | 1067 | Sema::BuildBaseOrMemberInitializers(ASTContext &C, |
| 1068 | CXXConstructorDecl *Constructor, |
| 1069 | CXXBaseOrMemberInitializer **Initializers, |
| 1070 | unsigned NumInitializers |
| 1071 | ) { |
| 1072 | llvm::SmallVector<CXXBaseSpecifier *, 4>Bases; |
| 1073 | llvm::SmallVector<FieldDecl *, 4>Members; |
| 1074 | |
Fariborz Jahanian | f988e22 | 2009-09-03 19:36:46 +0000 | [diff] [blame^] | 1075 | setBaseOrMemberInitializers(Constructor, |
| 1076 | Initializers, NumInitializers, Bases, Members); |
Fariborz Jahanian | 7c1771c | 2009-07-23 23:32:59 +0000 | [diff] [blame] | 1077 | for (unsigned int i = 0; i < Bases.size(); i++) |
| 1078 | Diag(Bases[i]->getSourceRange().getBegin(), |
| 1079 | diag::err_missing_default_constructor) << 0 << Bases[i]->getType(); |
| 1080 | for (unsigned int i = 0; i < Members.size(); i++) |
| 1081 | Diag(Members[i]->getLocation(), diag::err_missing_default_constructor) |
| 1082 | << 1 << Members[i]->getType(); |
| 1083 | } |
| 1084 | |
Eli Friedman | 16a1ca7 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1085 | static void *GetKeyForTopLevelField(FieldDecl *Field) { |
| 1086 | // For anonymous unions, use the class declaration as the key. |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1087 | if (const RecordType *RT = Field->getType()->getAs<RecordType>()) { |
Eli Friedman | 16a1ca7 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1088 | if (RT->getDecl()->isAnonymousStructOrUnion()) |
| 1089 | return static_cast<void *>(RT->getDecl()); |
| 1090 | } |
| 1091 | return static_cast<void *>(Field); |
| 1092 | } |
| 1093 | |
Anders Carlsson | bc5aff5 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 1094 | static void *GetKeyForBase(QualType BaseType) { |
| 1095 | if (const RecordType *RT = BaseType->getAs<RecordType>()) |
| 1096 | return (void *)RT; |
| 1097 | |
| 1098 | assert(0 && "Unexpected base type!"); |
| 1099 | return 0; |
| 1100 | } |
| 1101 | |
Fariborz Jahanian | fef75cb | 2009-08-11 18:49:54 +0000 | [diff] [blame] | 1102 | static void *GetKeyForMember(CXXBaseOrMemberInitializer *Member, |
Anders Carlsson | bc5aff5 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 1103 | bool MemberMaybeAnon = false) { |
Eli Friedman | 16a1ca7 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1104 | // For fields injected into the class via declaration of an anonymous union, |
| 1105 | // use its anonymous union class declaration as the unique key. |
Anders Carlsson | bc5aff5 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 1106 | if (Member->isMemberInitializer()) { |
| 1107 | FieldDecl *Field = Member->getMember(); |
| 1108 | |
Fariborz Jahanian | fef75cb | 2009-08-11 18:49:54 +0000 | [diff] [blame] | 1109 | // After BuildBaseOrMemberInitializers call, Field is the anonymous union |
| 1110 | // data member of the class. Data member used in the initializer list is |
| 1111 | // in AnonUnionMember field. |
| 1112 | if (MemberMaybeAnon && Field->isAnonymousStructOrUnion()) |
| 1113 | Field = Member->getAnonUnionMember(); |
Eli Friedman | 16a1ca7 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1114 | if (Field->getDeclContext()->isRecord()) { |
| 1115 | RecordDecl *RD = cast<RecordDecl>(Field->getDeclContext()); |
| 1116 | if (RD->isAnonymousStructOrUnion()) |
| 1117 | return static_cast<void *>(RD); |
| 1118 | } |
| 1119 | return static_cast<void *>(Field); |
| 1120 | } |
Anders Carlsson | bc5aff5 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 1121 | |
| 1122 | return GetKeyForBase(QualType(Member->getBaseClass(), 0)); |
Eli Friedman | 16a1ca7 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1125 | void Sema::ActOnMemInitializers(DeclPtrTy ConstructorDecl, |
Anders Carlsson | c7f8720 | 2009-03-25 02:58:17 +0000 | [diff] [blame] | 1126 | SourceLocation ColonLoc, |
| 1127 | MemInitTy **MemInits, unsigned NumMemInits) { |
Douglas Gregor | ac77dd6 | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 1128 | if (!ConstructorDecl) |
| 1129 | return; |
Douglas Gregor | 84164f0 | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 1130 | |
| 1131 | AdjustDeclIfTemplate(ConstructorDecl); |
Douglas Gregor | ac77dd6 | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 1132 | |
| 1133 | CXXConstructorDecl *Constructor |
| 1134 | = dyn_cast<CXXConstructorDecl>(ConstructorDecl.getAs<Decl>()); |
Anders Carlsson | c7f8720 | 2009-03-25 02:58:17 +0000 | [diff] [blame] | 1135 | |
| 1136 | if (!Constructor) { |
| 1137 | Diag(ColonLoc, diag::err_only_constructors_take_base_inits); |
| 1138 | return; |
| 1139 | } |
Anders Carlsson | 82b3fd2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1140 | |
Anders Carlsson | 897bb09 | 2009-08-27 05:57:30 +0000 | [diff] [blame] | 1141 | if (!Constructor->isDependentContext()) { |
| 1142 | llvm::DenseMap<void*, CXXBaseOrMemberInitializer *>Members; |
| 1143 | bool err = false; |
| 1144 | for (unsigned i = 0; i < NumMemInits; i++) { |
| 1145 | CXXBaseOrMemberInitializer *Member = |
| 1146 | static_cast<CXXBaseOrMemberInitializer*>(MemInits[i]); |
| 1147 | void *KeyToMember = GetKeyForMember(Member); |
| 1148 | CXXBaseOrMemberInitializer *&PrevMember = Members[KeyToMember]; |
| 1149 | if (!PrevMember) { |
| 1150 | PrevMember = Member; |
| 1151 | continue; |
| 1152 | } |
| 1153 | if (FieldDecl *Field = Member->getMember()) |
| 1154 | Diag(Member->getSourceLocation(), |
| 1155 | diag::error_multiple_mem_initialization) |
| 1156 | << Field->getNameAsString(); |
| 1157 | else { |
| 1158 | Type *BaseClass = Member->getBaseClass(); |
| 1159 | assert(BaseClass && "ActOnMemInitializers - neither field or base"); |
| 1160 | Diag(Member->getSourceLocation(), |
| 1161 | diag::error_multiple_base_initialization) |
| 1162 | << BaseClass->getDesugaredType(true); |
| 1163 | } |
| 1164 | Diag(PrevMember->getSourceLocation(), diag::note_previous_initializer) |
| 1165 | << 0; |
| 1166 | err = true; |
| 1167 | } |
| 1168 | |
| 1169 | if (err) |
| 1170 | return; |
| 1171 | } |
Anders Carlsson | 82b3fd2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1172 | |
| 1173 | BuildBaseOrMemberInitializers(Context, Constructor, |
Fariborz Jahanian | 7c1771c | 2009-07-23 23:32:59 +0000 | [diff] [blame] | 1174 | reinterpret_cast<CXXBaseOrMemberInitializer **>(MemInits), |
| 1175 | NumMemInits); |
| 1176 | |
Anders Carlsson | 897bb09 | 2009-08-27 05:57:30 +0000 | [diff] [blame] | 1177 | if (Constructor->isDependentContext()) |
| 1178 | return; |
Fariborz Jahanian | a141638 | 2009-09-02 23:02:57 +0000 | [diff] [blame] | 1179 | // Mark all constructors used in initialization of class's members |
| 1180 | // as referenced. |
| 1181 | // FIXME. We can do this while building the initializer list. But |
| 1182 | // MarkDeclarationReferenced is not accessible in ASTContext. |
| 1183 | for (CXXConstructorDecl::init_const_iterator B = Constructor->init_begin(), |
| 1184 | E = Constructor->init_end(); |
| 1185 | B != E; ++B) { |
| 1186 | CXXBaseOrMemberInitializer *Member = (*B); |
| 1187 | if (!Member->isMemberInitializer()) |
| 1188 | continue; |
| 1189 | FieldDecl *Field = Member->getMember(); |
| 1190 | QualType FT = Context.getBaseElementType(Field->getType()); |
Fariborz Jahanian | cba3204 | 2009-09-02 23:11:48 +0000 | [diff] [blame] | 1191 | if (FT->isDependentType()) |
| 1192 | continue; |
| 1193 | if (const RecordType* RT = FT->getAs<RecordType>()) |
| 1194 | if (CXXConstructorDecl *Ctor = |
| 1195 | cast<CXXRecordDecl>(RT->getDecl())->getDefaultConstructor(Context)) |
Fariborz Jahanian | a141638 | 2009-09-02 23:02:57 +0000 | [diff] [blame] | 1196 | MarkDeclarationReferenced(Ctor->getLocation(), Ctor); |
Fariborz Jahanian | a141638 | 2009-09-02 23:02:57 +0000 | [diff] [blame] | 1197 | } |
Anders Carlsson | 82b3fd2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1198 | if (Diags.getDiagnosticLevel(diag::warn_base_initialized) == |
| 1199 | Diagnostic::Ignored && |
| 1200 | Diags.getDiagnosticLevel(diag::warn_field_initialized) == |
| 1201 | Diagnostic::Ignored) |
| 1202 | return; |
| 1203 | |
| 1204 | // Also issue warning if order of ctor-initializer list does not match order |
| 1205 | // of 1) base class declarations and 2) order of non-static data members. |
| 1206 | llvm::SmallVector<const void*, 32> AllBaseOrMembers; |
| 1207 | |
| 1208 | CXXRecordDecl *ClassDecl |
| 1209 | = cast<CXXRecordDecl>(Constructor->getDeclContext()); |
| 1210 | // Push virtual bases before others. |
| 1211 | for (CXXRecordDecl::base_class_iterator VBase = |
| 1212 | ClassDecl->vbases_begin(), |
| 1213 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) |
Anders Carlsson | bc5aff5 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 1214 | AllBaseOrMembers.push_back(GetKeyForBase(VBase->getType())); |
Fariborz Jahanian | cca8d8d | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 1215 | |
Anders Carlsson | 82b3fd2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1216 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 1217 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 1218 | // Virtuals are alread in the virtual base list and are constructed |
| 1219 | // first. |
| 1220 | if (Base->isVirtual()) |
| 1221 | continue; |
Anders Carlsson | bc5aff5 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 1222 | AllBaseOrMembers.push_back(GetKeyForBase(Base->getType())); |
Anders Carlsson | 82b3fd2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
| 1225 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 1226 | E = ClassDecl->field_end(); Field != E; ++Field) |
| 1227 | AllBaseOrMembers.push_back(GetKeyForTopLevelField(*Field)); |
| 1228 | |
| 1229 | int Last = AllBaseOrMembers.size(); |
| 1230 | int curIndex = 0; |
| 1231 | CXXBaseOrMemberInitializer *PrevMember = 0; |
| 1232 | for (unsigned i = 0; i < NumMemInits; i++) { |
| 1233 | CXXBaseOrMemberInitializer *Member = |
| 1234 | static_cast<CXXBaseOrMemberInitializer*>(MemInits[i]); |
| 1235 | void *MemberInCtorList = GetKeyForMember(Member, true); |
Eli Friedman | 16a1ca7 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1236 | |
Anders Carlsson | 82b3fd2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1237 | for (; curIndex < Last; curIndex++) |
| 1238 | if (MemberInCtorList == AllBaseOrMembers[curIndex]) |
| 1239 | break; |
| 1240 | if (curIndex == Last) { |
| 1241 | assert(PrevMember && "Member not in member list?!"); |
| 1242 | // Initializer as specified in ctor-initializer list is out of order. |
| 1243 | // Issue a warning diagnostic. |
| 1244 | if (PrevMember->isBaseInitializer()) { |
| 1245 | // Diagnostics is for an initialized base class. |
| 1246 | Type *BaseClass = PrevMember->getBaseClass(); |
| 1247 | Diag(PrevMember->getSourceLocation(), |
| 1248 | diag::warn_base_initialized) |
| 1249 | << BaseClass->getDesugaredType(true); |
| 1250 | } else { |
| 1251 | FieldDecl *Field = PrevMember->getMember(); |
| 1252 | Diag(PrevMember->getSourceLocation(), |
| 1253 | diag::warn_field_initialized) |
| 1254 | << Field->getNameAsString(); |
Fariborz Jahanian | cca8d8d | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 1255 | } |
Anders Carlsson | 82b3fd2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1256 | // Also the note! |
| 1257 | if (FieldDecl *Field = Member->getMember()) |
| 1258 | Diag(Member->getSourceLocation(), |
| 1259 | diag::note_fieldorbase_initialized_here) << 0 |
| 1260 | << Field->getNameAsString(); |
| 1261 | else { |
| 1262 | Type *BaseClass = Member->getBaseClass(); |
| 1263 | Diag(Member->getSourceLocation(), |
| 1264 | diag::note_fieldorbase_initialized_here) << 1 |
| 1265 | << BaseClass->getDesugaredType(true); |
| 1266 | } |
| 1267 | for (curIndex = 0; curIndex < Last; curIndex++) |
| 1268 | if (MemberInCtorList == AllBaseOrMembers[curIndex]) |
| 1269 | break; |
Fariborz Jahanian | cca8d8d | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 1270 | } |
Anders Carlsson | 82b3fd2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1271 | PrevMember = Member; |
Fariborz Jahanian | cca8d8d | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 1272 | } |
Anders Carlsson | c7f8720 | 2009-03-25 02:58:17 +0000 | [diff] [blame] | 1273 | } |
| 1274 | |
Fariborz Jahanian | 4e12723 | 2009-07-21 22:36:06 +0000 | [diff] [blame] | 1275 | void Sema::ActOnDefaultCtorInitializers(DeclPtrTy CDtorDecl) { |
Fariborz Jahanian | 21e25e7 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 1276 | if (!CDtorDecl) |
Fariborz Jahanian | 9294d19 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 1277 | return; |
| 1278 | |
Douglas Gregor | 84164f0 | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 1279 | AdjustDeclIfTemplate(CDtorDecl); |
| 1280 | |
Fariborz Jahanian | 9294d19 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 1281 | if (CXXConstructorDecl *Constructor |
Fariborz Jahanian | 21e25e7 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 1282 | = dyn_cast<CXXConstructorDecl>(CDtorDecl.getAs<Decl>())) |
Fariborz Jahanian | 7c1771c | 2009-07-23 23:32:59 +0000 | [diff] [blame] | 1283 | BuildBaseOrMemberInitializers(Context, |
| 1284 | Constructor, |
| 1285 | (CXXBaseOrMemberInitializer **)0, 0); |
Fariborz Jahanian | 9294d19 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 1286 | } |
| 1287 | |
Anders Carlsson | 1dae87f | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1288 | namespace { |
| 1289 | /// PureVirtualMethodCollector - traverses a class and its superclasses |
| 1290 | /// and determines if it has any pure virtual methods. |
| 1291 | class VISIBILITY_HIDDEN PureVirtualMethodCollector { |
| 1292 | ASTContext &Context; |
| 1293 | |
Sebastian Redl | 16ac38f | 2009-03-22 21:28:55 +0000 | [diff] [blame] | 1294 | public: |
Anders Carlsson | 1dae87f | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1295 | typedef llvm::SmallVector<const CXXMethodDecl*, 8> MethodList; |
Sebastian Redl | 16ac38f | 2009-03-22 21:28:55 +0000 | [diff] [blame] | 1296 | |
| 1297 | private: |
Anders Carlsson | 1dae87f | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1298 | MethodList Methods; |
| 1299 | |
| 1300 | void Collect(const CXXRecordDecl* RD, MethodList& Methods); |
| 1301 | |
| 1302 | public: |
| 1303 | PureVirtualMethodCollector(ASTContext &Ctx, const CXXRecordDecl* RD) |
| 1304 | : Context(Ctx) { |
| 1305 | |
| 1306 | MethodList List; |
| 1307 | Collect(RD, List); |
| 1308 | |
| 1309 | // Copy the temporary list to methods, and make sure to ignore any |
| 1310 | // null entries. |
| 1311 | for (size_t i = 0, e = List.size(); i != e; ++i) { |
| 1312 | if (List[i]) |
| 1313 | Methods.push_back(List[i]); |
| 1314 | } |
| 1315 | } |
| 1316 | |
Anders Carlsson | e1299b3 | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1317 | bool empty() const { return Methods.empty(); } |
| 1318 | |
| 1319 | MethodList::const_iterator methods_begin() { return Methods.begin(); } |
| 1320 | MethodList::const_iterator methods_end() { return Methods.end(); } |
Anders Carlsson | 1dae87f | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1321 | }; |
| 1322 | |
| 1323 | void PureVirtualMethodCollector::Collect(const CXXRecordDecl* RD, |
| 1324 | MethodList& Methods) { |
| 1325 | // First, collect the pure virtual methods for the base classes. |
| 1326 | for (CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin(), |
| 1327 | BaseEnd = RD->bases_end(); Base != BaseEnd; ++Base) { |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1328 | if (const RecordType *RT = Base->getType()->getAs<RecordType>()) { |
Chris Lattner | 330a05b | 2009-03-29 05:01:10 +0000 | [diff] [blame] | 1329 | const CXXRecordDecl *BaseDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Anders Carlsson | 1dae87f | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1330 | if (BaseDecl && BaseDecl->isAbstract()) |
| 1331 | Collect(BaseDecl, Methods); |
| 1332 | } |
| 1333 | } |
| 1334 | |
| 1335 | // Next, zero out any pure virtual methods that this class overrides. |
Anders Carlsson | 1eba0ac | 2009-05-17 00:00:05 +0000 | [diff] [blame] | 1336 | typedef llvm::SmallPtrSet<const CXXMethodDecl*, 4> MethodSetTy; |
| 1337 | |
| 1338 | MethodSetTy OverriddenMethods; |
| 1339 | size_t MethodsSize = Methods.size(); |
| 1340 | |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1341 | for (RecordDecl::decl_iterator i = RD->decls_begin(), e = RD->decls_end(); |
Anders Carlsson | 1eba0ac | 2009-05-17 00:00:05 +0000 | [diff] [blame] | 1342 | i != e; ++i) { |
| 1343 | // Traverse the record, looking for methods. |
| 1344 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(*i)) { |
Sebastian Redl | 953d12a | 2009-07-07 20:29:57 +0000 | [diff] [blame] | 1345 | // If the method is pure virtual, add it to the methods vector. |
Anders Carlsson | 1eba0ac | 2009-05-17 00:00:05 +0000 | [diff] [blame] | 1346 | if (MD->isPure()) { |
| 1347 | Methods.push_back(MD); |
| 1348 | continue; |
| 1349 | } |
| 1350 | |
| 1351 | // Otherwise, record all the overridden methods in our set. |
| 1352 | for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), |
| 1353 | E = MD->end_overridden_methods(); I != E; ++I) { |
| 1354 | // Keep track of the overridden methods. |
| 1355 | OverriddenMethods.insert(*I); |
Anders Carlsson | 1dae87f | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1356 | } |
| 1357 | } |
| 1358 | } |
| 1359 | |
Anders Carlsson | 1eba0ac | 2009-05-17 00:00:05 +0000 | [diff] [blame] | 1360 | // Now go through the methods and zero out all the ones we know are |
| 1361 | // overridden. |
| 1362 | for (size_t i = 0, e = MethodsSize; i != e; ++i) { |
| 1363 | if (OverriddenMethods.count(Methods[i])) |
| 1364 | Methods[i] = 0; |
Anders Carlsson | 1dae87f | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1365 | } |
Anders Carlsson | 1eba0ac | 2009-05-17 00:00:05 +0000 | [diff] [blame] | 1366 | |
Anders Carlsson | 1dae87f | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1367 | } |
| 1368 | } |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1369 | |
Anders Carlsson | 62ce583 | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 1370 | |
Anders Carlsson | e1299b3 | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1371 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
Anders Carlsson | de9e789 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1372 | unsigned DiagID, AbstractDiagSelID SelID, |
| 1373 | const CXXRecordDecl *CurrentRD) { |
Anders Carlsson | 62ce583 | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 1374 | if (SelID == -1) |
| 1375 | return RequireNonAbstractType(Loc, T, |
| 1376 | PDiag(DiagID), CurrentRD); |
| 1377 | else |
| 1378 | return RequireNonAbstractType(Loc, T, |
| 1379 | PDiag(DiagID) << SelID, CurrentRD); |
| 1380 | } |
Anders Carlsson | e1299b3 | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1381 | |
Anders Carlsson | 62ce583 | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 1382 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
| 1383 | const PartialDiagnostic &PD, |
| 1384 | const CXXRecordDecl *CurrentRD) { |
Anders Carlsson | e1299b3 | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1385 | if (!getLangOptions().CPlusPlus) |
| 1386 | return false; |
Anders Carlsson | c263c9b | 2009-03-23 19:10:31 +0000 | [diff] [blame] | 1387 | |
| 1388 | if (const ArrayType *AT = Context.getAsArrayType(T)) |
Anders Carlsson | 62ce583 | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 1389 | return RequireNonAbstractType(Loc, AT->getElementType(), PD, |
Anders Carlsson | de9e789 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1390 | CurrentRD); |
Anders Carlsson | ce9240e | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 1391 | |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1392 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Anders Carlsson | ce9240e | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 1393 | // Find the innermost pointer type. |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1394 | while (const PointerType *T = PT->getPointeeType()->getAs<PointerType>()) |
Anders Carlsson | ce9240e | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 1395 | PT = T; |
Anders Carlsson | e1299b3 | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1396 | |
Anders Carlsson | ce9240e | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 1397 | if (const ArrayType *AT = Context.getAsArrayType(PT->getPointeeType())) |
Anders Carlsson | 62ce583 | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 1398 | return RequireNonAbstractType(Loc, AT->getElementType(), PD, CurrentRD); |
Anders Carlsson | ce9240e | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 1399 | } |
| 1400 | |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1401 | const RecordType *RT = T->getAs<RecordType>(); |
Anders Carlsson | e1299b3 | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1402 | if (!RT) |
| 1403 | return false; |
| 1404 | |
| 1405 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
| 1406 | if (!RD) |
| 1407 | return false; |
| 1408 | |
Anders Carlsson | de9e789 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1409 | if (CurrentRD && CurrentRD != RD) |
| 1410 | return false; |
| 1411 | |
Anders Carlsson | e1299b3 | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1412 | if (!RD->isAbstract()) |
| 1413 | return false; |
| 1414 | |
Anders Carlsson | 62ce583 | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 1415 | Diag(Loc, PD) << RD->getDeclName(); |
Anders Carlsson | e1299b3 | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1416 | |
| 1417 | // Check if we've already emitted the list of pure virtual functions for this |
| 1418 | // class. |
| 1419 | if (PureVirtualClassDiagSet && PureVirtualClassDiagSet->count(RD)) |
| 1420 | return true; |
| 1421 | |
| 1422 | PureVirtualMethodCollector Collector(Context, RD); |
| 1423 | |
| 1424 | for (PureVirtualMethodCollector::MethodList::const_iterator I = |
| 1425 | Collector.methods_begin(), E = Collector.methods_end(); I != E; ++I) { |
| 1426 | const CXXMethodDecl *MD = *I; |
| 1427 | |
| 1428 | Diag(MD->getLocation(), diag::note_pure_virtual_function) << |
| 1429 | MD->getDeclName(); |
| 1430 | } |
| 1431 | |
| 1432 | if (!PureVirtualClassDiagSet) |
| 1433 | PureVirtualClassDiagSet.reset(new RecordDeclSetTy); |
| 1434 | PureVirtualClassDiagSet->insert(RD); |
| 1435 | |
| 1436 | return true; |
| 1437 | } |
| 1438 | |
Anders Carlsson | 412c340 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1439 | namespace { |
| 1440 | class VISIBILITY_HIDDEN AbstractClassUsageDiagnoser |
| 1441 | : public DeclVisitor<AbstractClassUsageDiagnoser, bool> { |
| 1442 | Sema &SemaRef; |
| 1443 | CXXRecordDecl *AbstractClass; |
| 1444 | |
Anders Carlsson | de9e789 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1445 | bool VisitDeclContext(const DeclContext *DC) { |
Anders Carlsson | 412c340 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1446 | bool Invalid = false; |
| 1447 | |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1448 | for (CXXRecordDecl::decl_iterator I = DC->decls_begin(), |
| 1449 | E = DC->decls_end(); I != E; ++I) |
Anders Carlsson | 412c340 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1450 | Invalid |= Visit(*I); |
Anders Carlsson | de9e789 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1451 | |
Anders Carlsson | 412c340 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1452 | return Invalid; |
| 1453 | } |
Anders Carlsson | de9e789 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1454 | |
| 1455 | public: |
| 1456 | AbstractClassUsageDiagnoser(Sema& SemaRef, CXXRecordDecl *ac) |
| 1457 | : SemaRef(SemaRef), AbstractClass(ac) { |
| 1458 | Visit(SemaRef.Context.getTranslationUnitDecl()); |
| 1459 | } |
Anders Carlsson | 412c340 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1460 | |
Anders Carlsson | de9e789 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1461 | bool VisitFunctionDecl(const FunctionDecl *FD) { |
| 1462 | if (FD->isThisDeclarationADefinition()) { |
| 1463 | // No need to do the check if we're in a definition, because it requires |
| 1464 | // that the return/param types are complete. |
| 1465 | // because that requires |
| 1466 | return VisitDeclContext(FD); |
| 1467 | } |
| 1468 | |
| 1469 | // Check the return type. |
| 1470 | QualType RTy = FD->getType()->getAsFunctionType()->getResultType(); |
| 1471 | bool Invalid = |
| 1472 | SemaRef.RequireNonAbstractType(FD->getLocation(), RTy, |
| 1473 | diag::err_abstract_type_in_decl, |
| 1474 | Sema::AbstractReturnType, |
| 1475 | AbstractClass); |
| 1476 | |
| 1477 | for (FunctionDecl::param_const_iterator I = FD->param_begin(), |
| 1478 | E = FD->param_end(); I != E; ++I) { |
Anders Carlsson | 412c340 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1479 | const ParmVarDecl *VD = *I; |
| 1480 | Invalid |= |
| 1481 | SemaRef.RequireNonAbstractType(VD->getLocation(), |
| 1482 | VD->getOriginalType(), |
| 1483 | diag::err_abstract_type_in_decl, |
Anders Carlsson | de9e789 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1484 | Sema::AbstractParamType, |
| 1485 | AbstractClass); |
Anders Carlsson | 412c340 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1486 | } |
| 1487 | |
| 1488 | return Invalid; |
| 1489 | } |
Anders Carlsson | de9e789 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1490 | |
| 1491 | bool VisitDecl(const Decl* D) { |
| 1492 | if (const DeclContext *DC = dyn_cast<DeclContext>(D)) |
| 1493 | return VisitDeclContext(DC); |
| 1494 | |
| 1495 | return false; |
| 1496 | } |
Anders Carlsson | 412c340 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1497 | }; |
| 1498 | } |
| 1499 | |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1500 | void Sema::ActOnFinishCXXMemberSpecification(Scope* S, SourceLocation RLoc, |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1501 | DeclPtrTy TagDecl, |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1502 | SourceLocation LBrac, |
| 1503 | SourceLocation RBrac) { |
Douglas Gregor | ac77dd6 | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 1504 | if (!TagDecl) |
| 1505 | return; |
| 1506 | |
Douglas Gregor | 3eb2070 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 1507 | AdjustDeclIfTemplate(TagDecl); |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1508 | ActOnFields(S, RLoc, TagDecl, |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1509 | (DeclPtrTy*)FieldCollector->getCurFields(), |
Daniel Dunbar | f394444 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 1510 | FieldCollector->getCurNumFields(), LBrac, RBrac, 0); |
Douglas Gregor | ed3a398 | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1511 | |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1512 | CXXRecordDecl *RD = cast<CXXRecordDecl>(TagDecl.getAs<Decl>()); |
Anders Carlsson | 1dae87f | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1513 | if (!RD->isAbstract()) { |
| 1514 | // Collect all the pure virtual methods and see if this is an abstract |
| 1515 | // class after all. |
| 1516 | PureVirtualMethodCollector Collector(Context, RD); |
| 1517 | if (!Collector.empty()) |
| 1518 | RD->setAbstract(true); |
| 1519 | } |
| 1520 | |
Anders Carlsson | de9e789 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1521 | if (RD->isAbstract()) |
| 1522 | AbstractClassUsageDiagnoser(*this, RD); |
Anders Carlsson | 412c340 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1523 | |
Douglas Gregor | 3eb2070 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 1524 | if (!RD->isDependentType()) |
Anders Carlsson | 1dae87f | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1525 | AddImplicitlyDeclaredMembersToClass(RD); |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
Douglas Gregor | e640ab6 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1528 | /// AddImplicitlyDeclaredMembersToClass - Adds any implicitly-declared |
| 1529 | /// special functions, such as the default constructor, copy |
| 1530 | /// constructor, or destructor, to the given C++ class (C++ |
| 1531 | /// [special]p1). This routine can only be executed just before the |
| 1532 | /// definition of the class is complete. |
| 1533 | void Sema::AddImplicitlyDeclaredMembersToClass(CXXRecordDecl *ClassDecl) { |
Douglas Gregor | cfe6ae5 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 1534 | CanQualType ClassType |
| 1535 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Douglas Gregor | 24afd4a | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1536 | |
Sebastian Redl | 2767d88 | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1537 | // FIXME: Implicit declarations have exception specifications, which are |
| 1538 | // the union of the specifications of the implicitly called functions. |
| 1539 | |
Douglas Gregor | e640ab6 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1540 | if (!ClassDecl->hasUserDeclaredConstructor()) { |
| 1541 | // C++ [class.ctor]p5: |
| 1542 | // A default constructor for a class X is a constructor of class X |
| 1543 | // that can be called without an argument. If there is no |
| 1544 | // user-declared constructor for class X, a default constructor is |
| 1545 | // implicitly declared. An implicitly-declared default constructor |
| 1546 | // is an inline public member of its class. |
Douglas Gregor | 24afd4a | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1547 | DeclarationName Name |
| 1548 | = Context.DeclarationNames.getCXXConstructorName(ClassType); |
Douglas Gregor | e640ab6 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1549 | CXXConstructorDecl *DefaultCon = |
| 1550 | CXXConstructorDecl::Create(Context, ClassDecl, |
Douglas Gregor | 24afd4a | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1551 | ClassDecl->getLocation(), Name, |
Douglas Gregor | e640ab6 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1552 | Context.getFunctionType(Context.VoidTy, |
| 1553 | 0, 0, false, 0), |
Argiris Kirtzidis | b17120c | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 1554 | /*DInfo=*/0, |
Douglas Gregor | e640ab6 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1555 | /*isExplicit=*/false, |
| 1556 | /*isInline=*/true, |
| 1557 | /*isImplicitlyDeclared=*/true); |
| 1558 | DefaultCon->setAccess(AS_public); |
Douglas Gregor | c7f0161 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 1559 | DefaultCon->setImplicit(); |
Douglas Gregor | f73c851 | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 1560 | DefaultCon->setTrivial(ClassDecl->hasTrivialConstructor()); |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1561 | ClassDecl->addDecl(DefaultCon); |
Douglas Gregor | e640ab6 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1562 | } |
| 1563 | |
| 1564 | if (!ClassDecl->hasUserDeclaredCopyConstructor()) { |
| 1565 | // C++ [class.copy]p4: |
| 1566 | // If the class definition does not explicitly declare a copy |
| 1567 | // constructor, one is declared implicitly. |
| 1568 | |
| 1569 | // C++ [class.copy]p5: |
| 1570 | // The implicitly-declared copy constructor for a class X will |
| 1571 | // have the form |
| 1572 | // |
| 1573 | // X::X(const X&) |
| 1574 | // |
| 1575 | // if |
| 1576 | bool HasConstCopyConstructor = true; |
| 1577 | |
| 1578 | // -- each direct or virtual base class B of X has a copy |
| 1579 | // constructor whose first parameter is of type const B& or |
| 1580 | // const volatile B&, and |
| 1581 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(); |
| 1582 | HasConstCopyConstructor && Base != ClassDecl->bases_end(); ++Base) { |
| 1583 | const CXXRecordDecl *BaseClassDecl |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1584 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Douglas Gregor | e640ab6 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1585 | HasConstCopyConstructor |
| 1586 | = BaseClassDecl->hasConstCopyConstructor(Context); |
| 1587 | } |
| 1588 | |
| 1589 | // -- for all the nonstatic data members of X that are of a |
| 1590 | // class type M (or array thereof), each such class type |
| 1591 | // has a copy constructor whose first parameter is of type |
| 1592 | // const M& or const volatile M&. |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1593 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(); |
| 1594 | HasConstCopyConstructor && Field != ClassDecl->field_end(); |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1595 | ++Field) { |
Douglas Gregor | e640ab6 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1596 | QualType FieldType = (*Field)->getType(); |
| 1597 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 1598 | FieldType = Array->getElementType(); |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1599 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Douglas Gregor | e640ab6 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1600 | const CXXRecordDecl *FieldClassDecl |
| 1601 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
| 1602 | HasConstCopyConstructor |
| 1603 | = FieldClassDecl->hasConstCopyConstructor(Context); |
| 1604 | } |
| 1605 | } |
| 1606 | |
Sebastian Redl | 39c0f6f | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1607 | // Otherwise, the implicitly declared copy constructor will have |
| 1608 | // the form |
Douglas Gregor | e640ab6 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1609 | // |
| 1610 | // X::X(X&) |
Sebastian Redl | 39c0f6f | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1611 | QualType ArgType = ClassType; |
Douglas Gregor | e640ab6 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1612 | if (HasConstCopyConstructor) |
| 1613 | ArgType = ArgType.withConst(); |
Sebastian Redl | ce6fff0 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1614 | ArgType = Context.getLValueReferenceType(ArgType); |
Douglas Gregor | e640ab6 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1615 | |
Sebastian Redl | 39c0f6f | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1616 | // An implicitly-declared copy constructor is an inline public |
| 1617 | // member of its class. |
Douglas Gregor | 24afd4a | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1618 | DeclarationName Name |
| 1619 | = Context.DeclarationNames.getCXXConstructorName(ClassType); |
Douglas Gregor | e640ab6 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1620 | CXXConstructorDecl *CopyConstructor |
| 1621 | = CXXConstructorDecl::Create(Context, ClassDecl, |
Douglas Gregor | 24afd4a | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1622 | ClassDecl->getLocation(), Name, |
Douglas Gregor | e640ab6 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1623 | Context.getFunctionType(Context.VoidTy, |
| 1624 | &ArgType, 1, |
| 1625 | false, 0), |
Argiris Kirtzidis | b17120c | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 1626 | /*DInfo=*/0, |
Douglas Gregor | e640ab6 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1627 | /*isExplicit=*/false, |
| 1628 | /*isInline=*/true, |
| 1629 | /*isImplicitlyDeclared=*/true); |
| 1630 | CopyConstructor->setAccess(AS_public); |
Douglas Gregor | c7f0161 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 1631 | CopyConstructor->setImplicit(); |
Douglas Gregor | f73c851 | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 1632 | CopyConstructor->setTrivial(ClassDecl->hasTrivialCopyConstructor()); |
Douglas Gregor | e640ab6 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1633 | |
| 1634 | // Add the parameter to the constructor. |
| 1635 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyConstructor, |
| 1636 | ClassDecl->getLocation(), |
| 1637 | /*IdentifierInfo=*/0, |
Argiris Kirtzidis | b17120c | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 1638 | ArgType, /*DInfo=*/0, |
| 1639 | VarDecl::None, 0); |
Ted Kremenek | 8494c96 | 2009-01-14 00:42:25 +0000 | [diff] [blame] | 1640 | CopyConstructor->setParams(Context, &FromParam, 1); |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1641 | ClassDecl->addDecl(CopyConstructor); |
Douglas Gregor | e640ab6 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1642 | } |
| 1643 | |
Sebastian Redl | 39c0f6f | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1644 | if (!ClassDecl->hasUserDeclaredCopyAssignment()) { |
| 1645 | // Note: The following rules are largely analoguous to the copy |
| 1646 | // constructor rules. Note that virtual bases are not taken into account |
| 1647 | // for determining the argument type of the operator. Note also that |
| 1648 | // operators taking an object instead of a reference are allowed. |
| 1649 | // |
| 1650 | // C++ [class.copy]p10: |
| 1651 | // If the class definition does not explicitly declare a copy |
| 1652 | // assignment operator, one is declared implicitly. |
| 1653 | // The implicitly-defined copy assignment operator for a class X |
| 1654 | // will have the form |
| 1655 | // |
| 1656 | // X& X::operator=(const X&) |
| 1657 | // |
| 1658 | // if |
| 1659 | bool HasConstCopyAssignment = true; |
| 1660 | |
| 1661 | // -- each direct base class B of X has a copy assignment operator |
| 1662 | // whose parameter is of type const B&, const volatile B& or B, |
| 1663 | // and |
| 1664 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(); |
| 1665 | HasConstCopyAssignment && Base != ClassDecl->bases_end(); ++Base) { |
| 1666 | const CXXRecordDecl *BaseClassDecl |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1667 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Fariborz Jahanian | 0450024 | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 1668 | const CXXMethodDecl *MD = 0; |
| 1669 | HasConstCopyAssignment = BaseClassDecl->hasConstCopyAssignment(Context, |
| 1670 | MD); |
Sebastian Redl | 39c0f6f | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1671 | } |
| 1672 | |
| 1673 | // -- for all the nonstatic data members of X that are of a class |
| 1674 | // type M (or array thereof), each such class type has a copy |
| 1675 | // assignment operator whose parameter is of type const M&, |
| 1676 | // const volatile M& or M. |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1677 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(); |
| 1678 | HasConstCopyAssignment && Field != ClassDecl->field_end(); |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1679 | ++Field) { |
Sebastian Redl | 39c0f6f | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1680 | QualType FieldType = (*Field)->getType(); |
| 1681 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 1682 | FieldType = Array->getElementType(); |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1683 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Sebastian Redl | 39c0f6f | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1684 | const CXXRecordDecl *FieldClassDecl |
| 1685 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Fariborz Jahanian | 0450024 | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 1686 | const CXXMethodDecl *MD = 0; |
Sebastian Redl | 39c0f6f | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1687 | HasConstCopyAssignment |
Fariborz Jahanian | 0450024 | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 1688 | = FieldClassDecl->hasConstCopyAssignment(Context, MD); |
Sebastian Redl | 39c0f6f | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1689 | } |
| 1690 | } |
| 1691 | |
| 1692 | // Otherwise, the implicitly declared copy assignment operator will |
| 1693 | // have the form |
| 1694 | // |
| 1695 | // X& X::operator=(X&) |
| 1696 | QualType ArgType = ClassType; |
Sebastian Redl | ce6fff0 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1697 | QualType RetType = Context.getLValueReferenceType(ArgType); |
Sebastian Redl | 39c0f6f | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1698 | if (HasConstCopyAssignment) |
| 1699 | ArgType = ArgType.withConst(); |
Sebastian Redl | ce6fff0 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1700 | ArgType = Context.getLValueReferenceType(ArgType); |
Sebastian Redl | 39c0f6f | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1701 | |
| 1702 | // An implicitly-declared copy assignment operator is an inline public |
| 1703 | // member of its class. |
| 1704 | DeclarationName Name = |
| 1705 | Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 1706 | CXXMethodDecl *CopyAssignment = |
| 1707 | CXXMethodDecl::Create(Context, ClassDecl, ClassDecl->getLocation(), Name, |
| 1708 | Context.getFunctionType(RetType, &ArgType, 1, |
| 1709 | false, 0), |
Argiris Kirtzidis | b17120c | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 1710 | /*DInfo=*/0, /*isStatic=*/false, /*isInline=*/true); |
Sebastian Redl | 39c0f6f | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1711 | CopyAssignment->setAccess(AS_public); |
Douglas Gregor | c7f0161 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 1712 | CopyAssignment->setImplicit(); |
Douglas Gregor | f73c851 | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 1713 | CopyAssignment->setTrivial(ClassDecl->hasTrivialCopyAssignment()); |
Fariborz Jahanian | 651efb7 | 2009-08-12 21:14:35 +0000 | [diff] [blame] | 1714 | CopyAssignment->setCopyAssignment(true); |
Sebastian Redl | 39c0f6f | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1715 | |
| 1716 | // Add the parameter to the operator. |
| 1717 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyAssignment, |
| 1718 | ClassDecl->getLocation(), |
| 1719 | /*IdentifierInfo=*/0, |
Argiris Kirtzidis | b17120c | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 1720 | ArgType, /*DInfo=*/0, |
| 1721 | VarDecl::None, 0); |
Ted Kremenek | 8494c96 | 2009-01-14 00:42:25 +0000 | [diff] [blame] | 1722 | CopyAssignment->setParams(Context, &FromParam, 1); |
Sebastian Redl | 39c0f6f | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1723 | |
| 1724 | // Don't call addedAssignmentOperator. There is no way to distinguish an |
| 1725 | // implicit from an explicit assignment operator. |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1726 | ClassDecl->addDecl(CopyAssignment); |
Sebastian Redl | 39c0f6f | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1727 | } |
| 1728 | |
Douglas Gregor | b921383 | 2008-12-15 21:24:18 +0000 | [diff] [blame] | 1729 | if (!ClassDecl->hasUserDeclaredDestructor()) { |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1730 | // C++ [class.dtor]p2: |
| 1731 | // If a class has no user-declared destructor, a destructor is |
| 1732 | // declared implicitly. An implicitly-declared destructor is an |
| 1733 | // inline public member of its class. |
Douglas Gregor | 24afd4a | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1734 | DeclarationName Name |
| 1735 | = Context.DeclarationNames.getCXXDestructorName(ClassType); |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1736 | CXXDestructorDecl *Destructor |
| 1737 | = CXXDestructorDecl::Create(Context, ClassDecl, |
Douglas Gregor | 24afd4a | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1738 | ClassDecl->getLocation(), Name, |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1739 | Context.getFunctionType(Context.VoidTy, |
| 1740 | 0, 0, false, 0), |
| 1741 | /*isInline=*/true, |
| 1742 | /*isImplicitlyDeclared=*/true); |
| 1743 | Destructor->setAccess(AS_public); |
Douglas Gregor | c7f0161 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 1744 | Destructor->setImplicit(); |
Douglas Gregor | f73c851 | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 1745 | Destructor->setTrivial(ClassDecl->hasTrivialDestructor()); |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1746 | ClassDecl->addDecl(Destructor); |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1747 | } |
Douglas Gregor | e640ab6 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1748 | } |
| 1749 | |
Douglas Gregor | a376cbd | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 1750 | void Sema::ActOnReenterTemplateScope(Scope *S, DeclPtrTy TemplateD) { |
| 1751 | TemplateDecl *Template = TemplateD.getAs<TemplateDecl>(); |
| 1752 | if (!Template) |
| 1753 | return; |
| 1754 | |
| 1755 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 1756 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 1757 | ParamEnd = Params->end(); |
| 1758 | Param != ParamEnd; ++Param) { |
| 1759 | NamedDecl *Named = cast<NamedDecl>(*Param); |
| 1760 | if (Named->getDeclName()) { |
| 1761 | S->AddDecl(DeclPtrTy::make(Named)); |
| 1762 | IdResolver.AddDecl(Named); |
| 1763 | } |
| 1764 | } |
| 1765 | } |
| 1766 | |
Douglas Gregor | 605de8d | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1767 | /// ActOnStartDelayedCXXMethodDeclaration - We have completed |
| 1768 | /// parsing a top-level (non-nested) C++ class, and we are now |
| 1769 | /// parsing those parts of the given Method declaration that could |
| 1770 | /// not be parsed earlier (C++ [class.mem]p2), such as default |
| 1771 | /// arguments. This action should enter the scope of the given |
| 1772 | /// Method declaration as if we had just parsed the qualified method |
| 1773 | /// name. However, it should not bring the parameters into scope; |
| 1774 | /// that will be performed by ActOnDelayedCXXMethodParameter. |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1775 | void Sema::ActOnStartDelayedCXXMethodDeclaration(Scope *S, DeclPtrTy MethodD) { |
Douglas Gregor | ac77dd6 | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 1776 | if (!MethodD) |
| 1777 | return; |
| 1778 | |
Douglas Gregor | 84164f0 | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 1779 | AdjustDeclIfTemplate(MethodD); |
| 1780 | |
Douglas Gregor | 605de8d | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1781 | CXXScopeSpec SS; |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1782 | FunctionDecl *Method = cast<FunctionDecl>(MethodD.getAs<Decl>()); |
Douglas Gregor | 1e589cc | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1783 | QualType ClassTy |
| 1784 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 1785 | SS.setScopeRep( |
| 1786 | NestedNameSpecifier::Create(Context, 0, false, ClassTy.getTypePtr())); |
Douglas Gregor | 605de8d | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1787 | ActOnCXXEnterDeclaratorScope(S, SS); |
| 1788 | } |
| 1789 | |
| 1790 | /// ActOnDelayedCXXMethodParameter - We've already started a delayed |
| 1791 | /// C++ method declaration. We're (re-)introducing the given |
| 1792 | /// function parameter into scope for use in parsing later parts of |
| 1793 | /// the method declaration. For example, we could see an |
| 1794 | /// ActOnParamDefaultArgument event for this parameter. |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1795 | void Sema::ActOnDelayedCXXMethodParameter(Scope *S, DeclPtrTy ParamD) { |
Douglas Gregor | ac77dd6 | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 1796 | if (!ParamD) |
| 1797 | return; |
| 1798 | |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1799 | ParmVarDecl *Param = cast<ParmVarDecl>(ParamD.getAs<Decl>()); |
Douglas Gregor | 62ae25a | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 1800 | |
| 1801 | // If this parameter has an unparsed default argument, clear it out |
| 1802 | // to make way for the parsed default argument. |
| 1803 | if (Param->hasUnparsedDefaultArg()) |
| 1804 | Param->setDefaultArg(0); |
| 1805 | |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1806 | S->AddDecl(DeclPtrTy::make(Param)); |
Douglas Gregor | 605de8d | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1807 | if (Param->getDeclName()) |
| 1808 | IdResolver.AddDecl(Param); |
| 1809 | } |
| 1810 | |
| 1811 | /// ActOnFinishDelayedCXXMethodDeclaration - We have finished |
| 1812 | /// processing the delayed method declaration for Method. The method |
| 1813 | /// declaration is now considered finished. There may be a separate |
| 1814 | /// ActOnStartOfFunctionDef action later (not necessarily |
| 1815 | /// immediately!) for this method, if it was also defined inside the |
| 1816 | /// class body. |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1817 | void Sema::ActOnFinishDelayedCXXMethodDeclaration(Scope *S, DeclPtrTy MethodD) { |
Douglas Gregor | ac77dd6 | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 1818 | if (!MethodD) |
| 1819 | return; |
| 1820 | |
Douglas Gregor | 84164f0 | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 1821 | AdjustDeclIfTemplate(MethodD); |
| 1822 | |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1823 | FunctionDecl *Method = cast<FunctionDecl>(MethodD.getAs<Decl>()); |
Douglas Gregor | 605de8d | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1824 | CXXScopeSpec SS; |
Douglas Gregor | 1e589cc | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1825 | QualType ClassTy |
| 1826 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 1827 | SS.setScopeRep( |
| 1828 | NestedNameSpecifier::Create(Context, 0, false, ClassTy.getTypePtr())); |
Douglas Gregor | 605de8d | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1829 | ActOnCXXExitDeclaratorScope(S, SS); |
| 1830 | |
| 1831 | // Now that we have our default arguments, check the constructor |
| 1832 | // again. It could produce additional diagnostics or affect whether |
| 1833 | // the class has implicitly-declared destructors, among other |
| 1834 | // things. |
Chris Lattner | 08da477 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 1835 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Method)) |
| 1836 | CheckConstructor(Constructor); |
Douglas Gregor | 605de8d | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1837 | |
| 1838 | // Check the default arguments, which we may have added. |
| 1839 | if (!Method->isInvalidDecl()) |
| 1840 | CheckCXXDefaultArguments(Method); |
| 1841 | } |
| 1842 | |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1843 | /// CheckConstructorDeclarator - Called by ActOnDeclarator to check |
Douglas Gregor | 605de8d | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1844 | /// the well-formedness of the constructor declarator @p D with type @p |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1845 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | c82dcd4 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 1846 | /// emit diagnostics and set the invalid bit to true. In any case, the type |
| 1847 | /// will be updated to reflect a well-formed type for the constructor and |
| 1848 | /// returned. |
| 1849 | QualType Sema::CheckConstructorDeclarator(Declarator &D, QualType R, |
| 1850 | FunctionDecl::StorageClass &SC) { |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1851 | bool isVirtual = D.getDeclSpec().isVirtualSpecified(); |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1852 | |
| 1853 | // C++ [class.ctor]p3: |
| 1854 | // A constructor shall not be virtual (10.3) or static (9.4). A |
| 1855 | // constructor can be invoked for a const, volatile or const |
| 1856 | // volatile object. A constructor shall not be declared const, |
| 1857 | // volatile, or const volatile (9.3.2). |
| 1858 | if (isVirtual) { |
Chris Lattner | c82dcd4 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 1859 | if (!D.isInvalidType()) |
| 1860 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 1861 | << "virtual" << SourceRange(D.getDeclSpec().getVirtualSpecLoc()) |
| 1862 | << SourceRange(D.getIdentifierLoc()); |
| 1863 | D.setInvalidType(); |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1864 | } |
| 1865 | if (SC == FunctionDecl::Static) { |
Chris Lattner | c82dcd4 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 1866 | if (!D.isInvalidType()) |
| 1867 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 1868 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 1869 | << SourceRange(D.getIdentifierLoc()); |
| 1870 | D.setInvalidType(); |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1871 | SC = FunctionDecl::None; |
| 1872 | } |
Chris Lattner | c82dcd4 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 1873 | |
| 1874 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 1875 | if (FTI.TypeQuals != 0) { |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1876 | if (FTI.TypeQuals & QualType::Const) |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1877 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 1878 | << "const" << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1879 | if (FTI.TypeQuals & QualType::Volatile) |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1880 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 1881 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1882 | if (FTI.TypeQuals & QualType::Restrict) |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1883 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 1884 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1885 | } |
| 1886 | |
| 1887 | // Rebuild the function type "R" without any type qualifiers (in |
| 1888 | // case any of the errors above fired) and with "void" as the |
| 1889 | // return type, since constructors don't have return types. We |
| 1890 | // *always* have to do this, because GetTypeForDeclarator will |
| 1891 | // put in a result type of "int" when none was specified. |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1892 | const FunctionProtoType *Proto = R->getAsFunctionProtoType(); |
Chris Lattner | c82dcd4 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 1893 | return Context.getFunctionType(Context.VoidTy, Proto->arg_type_begin(), |
| 1894 | Proto->getNumArgs(), |
| 1895 | Proto->isVariadic(), 0); |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1896 | } |
| 1897 | |
Douglas Gregor | 605de8d | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1898 | /// CheckConstructor - Checks a fully-formed constructor for |
| 1899 | /// well-formedness, issuing any diagnostics required. Returns true if |
| 1900 | /// the constructor declarator is invalid. |
Chris Lattner | 08da477 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 1901 | void Sema::CheckConstructor(CXXConstructorDecl *Constructor) { |
Douglas Gregor | 869cabf | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 1902 | CXXRecordDecl *ClassDecl |
| 1903 | = dyn_cast<CXXRecordDecl>(Constructor->getDeclContext()); |
| 1904 | if (!ClassDecl) |
Chris Lattner | 08da477 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 1905 | return Constructor->setInvalidDecl(); |
Douglas Gregor | 605de8d | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1906 | |
| 1907 | // C++ [class.copy]p3: |
| 1908 | // A declaration of a constructor for a class X is ill-formed if |
| 1909 | // its first parameter is of type (optionally cv-qualified) X and |
| 1910 | // either there are no other parameters or else all other |
| 1911 | // parameters have default arguments. |
Douglas Gregor | 869cabf | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 1912 | if (!Constructor->isInvalidDecl() && |
| 1913 | ((Constructor->getNumParams() == 1) || |
| 1914 | (Constructor->getNumParams() > 1 && |
Anders Carlsson | d2e57d9 | 2009-06-06 04:14:07 +0000 | [diff] [blame] | 1915 | Constructor->getParamDecl(1)->hasDefaultArg()))) { |
Douglas Gregor | 605de8d | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1916 | QualType ParamType = Constructor->getParamDecl(0)->getType(); |
| 1917 | QualType ClassTy = Context.getTagDeclType(ClassDecl); |
| 1918 | if (Context.getCanonicalType(ParamType).getUnqualifiedType() == ClassTy) { |
Douglas Gregor | 3faaa81 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 1919 | SourceLocation ParamLoc = Constructor->getParamDecl(0)->getLocation(); |
| 1920 | Diag(ParamLoc, diag::err_constructor_byvalue_arg) |
Douglas Gregor | 133d255 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 1921 | << CodeModificationHint::CreateInsertion(ParamLoc, " const &"); |
Chris Lattner | 08da477 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 1922 | Constructor->setInvalidDecl(); |
Douglas Gregor | 605de8d | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1923 | } |
| 1924 | } |
| 1925 | |
| 1926 | // Notify the class that we've added a constructor. |
| 1927 | ClassDecl->addedConstructor(Context, Constructor); |
Douglas Gregor | 605de8d | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1928 | } |
| 1929 | |
Anders Carlsson | fcfa244 | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 1930 | static inline bool |
| 1931 | FTIHasSingleVoidArgument(DeclaratorChunk::FunctionTypeInfo &FTI) { |
| 1932 | return (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
| 1933 | FTI.ArgInfo[0].Param && |
| 1934 | FTI.ArgInfo[0].Param.getAs<ParmVarDecl>()->getType()->isVoidType()); |
| 1935 | } |
| 1936 | |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1937 | /// CheckDestructorDeclarator - Called by ActOnDeclarator to check |
| 1938 | /// the well-formednes of the destructor declarator @p D with type @p |
| 1939 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | c82dcd4 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 1940 | /// emit diagnostics and set the declarator to invalid. Even if this happens, |
| 1941 | /// will be updated to reflect a well-formed type for the destructor and |
| 1942 | /// returned. |
| 1943 | QualType Sema::CheckDestructorDeclarator(Declarator &D, |
| 1944 | FunctionDecl::StorageClass& SC) { |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1945 | // C++ [class.dtor]p1: |
| 1946 | // [...] A typedef-name that names a class is a class-name |
| 1947 | // (7.1.3); however, a typedef-name that names a class shall not |
| 1948 | // be used as the identifier in the declarator for a destructor |
| 1949 | // declaration. |
Argiris Kirtzidis | d6802ba | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 1950 | QualType DeclaratorType = GetTypeFromParser(D.getDeclaratorIdType()); |
Chris Lattner | c82dcd4 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 1951 | if (isa<TypedefType>(DeclaratorType)) { |
| 1952 | Diag(D.getIdentifierLoc(), diag::err_destructor_typedef_name) |
Douglas Gregor | a60c62e | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 1953 | << DeclaratorType; |
Chris Lattner | c82dcd4 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 1954 | D.setInvalidType(); |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1955 | } |
| 1956 | |
| 1957 | // C++ [class.dtor]p2: |
| 1958 | // A destructor is used to destroy objects of its class type. A |
| 1959 | // destructor takes no parameters, and no return type can be |
| 1960 | // specified for it (not even void). The address of a destructor |
| 1961 | // shall not be taken. A destructor shall not be static. A |
| 1962 | // destructor can be invoked for a const, volatile or const |
| 1963 | // volatile object. A destructor shall not be declared const, |
| 1964 | // volatile or const volatile (9.3.2). |
| 1965 | if (SC == FunctionDecl::Static) { |
Chris Lattner | c82dcd4 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 1966 | if (!D.isInvalidType()) |
| 1967 | Diag(D.getIdentifierLoc(), diag::err_destructor_cannot_be) |
| 1968 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 1969 | << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1970 | SC = FunctionDecl::None; |
Chris Lattner | c82dcd4 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 1971 | D.setInvalidType(); |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1972 | } |
Chris Lattner | c82dcd4 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 1973 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1974 | // Destructors don't have return types, but the parser will |
| 1975 | // happily parse something like: |
| 1976 | // |
| 1977 | // class X { |
| 1978 | // float ~X(); |
| 1979 | // }; |
| 1980 | // |
| 1981 | // The return type will be eliminated later. |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1982 | Diag(D.getIdentifierLoc(), diag::err_destructor_return_type) |
| 1983 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 1984 | << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1985 | } |
Chris Lattner | c82dcd4 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 1986 | |
| 1987 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 1988 | if (FTI.TypeQuals != 0 && !D.isInvalidType()) { |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1989 | if (FTI.TypeQuals & QualType::Const) |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1990 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 1991 | << "const" << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1992 | if (FTI.TypeQuals & QualType::Volatile) |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1993 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 1994 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1995 | if (FTI.TypeQuals & QualType::Restrict) |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1996 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 1997 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
Chris Lattner | c82dcd4 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 1998 | D.setInvalidType(); |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1999 | } |
| 2000 | |
| 2001 | // Make sure we don't have any parameters. |
Anders Carlsson | fcfa244 | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 2002 | if (FTI.NumArgs > 0 && !FTIHasSingleVoidArgument(FTI)) { |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2003 | Diag(D.getIdentifierLoc(), diag::err_destructor_with_params); |
| 2004 | |
| 2005 | // Delete the parameters. |
Chris Lattner | c82dcd4 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2006 | FTI.freeArgs(); |
| 2007 | D.setInvalidType(); |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2008 | } |
| 2009 | |
| 2010 | // Make sure the destructor isn't variadic. |
Chris Lattner | c82dcd4 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2011 | if (FTI.isVariadic) { |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2012 | Diag(D.getIdentifierLoc(), diag::err_destructor_variadic); |
Chris Lattner | c82dcd4 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2013 | D.setInvalidType(); |
| 2014 | } |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2015 | |
| 2016 | // Rebuild the function type "R" without any type qualifiers or |
| 2017 | // parameters (in case any of the errors above fired) and with |
| 2018 | // "void" as the return type, since destructors don't have return |
| 2019 | // types. We *always* have to do this, because GetTypeForDeclarator |
| 2020 | // will put in a result type of "int" when none was specified. |
Chris Lattner | c82dcd4 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2021 | return Context.getFunctionType(Context.VoidTy, 0, 0, false, 0); |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2022 | } |
| 2023 | |
Douglas Gregor | 3ef6c97 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2024 | /// CheckConversionDeclarator - Called by ActOnDeclarator to check the |
| 2025 | /// well-formednes of the conversion function declarator @p D with |
| 2026 | /// type @p R. If there are any errors in the declarator, this routine |
| 2027 | /// will emit diagnostics and return true. Otherwise, it will return |
| 2028 | /// false. Either way, the type @p R will be updated to reflect a |
| 2029 | /// well-formed type for the conversion operator. |
Chris Lattner | 08da477 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2030 | void Sema::CheckConversionDeclarator(Declarator &D, QualType &R, |
Douglas Gregor | 3ef6c97 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2031 | FunctionDecl::StorageClass& SC) { |
Douglas Gregor | 3ef6c97 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2032 | // C++ [class.conv.fct]p1: |
| 2033 | // Neither parameter types nor return type can be specified. The |
Eli Friedman | d5a72f0 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 2034 | // type of a conversion function (8.3.5) is "function taking no |
| 2035 | // parameter returning conversion-type-id." |
Douglas Gregor | 3ef6c97 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2036 | if (SC == FunctionDecl::Static) { |
Chris Lattner | 08da477 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2037 | if (!D.isInvalidType()) |
| 2038 | Diag(D.getIdentifierLoc(), diag::err_conv_function_not_member) |
| 2039 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 2040 | << SourceRange(D.getIdentifierLoc()); |
| 2041 | D.setInvalidType(); |
Douglas Gregor | 3ef6c97 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2042 | SC = FunctionDecl::None; |
| 2043 | } |
Chris Lattner | 08da477 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2044 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | 3ef6c97 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2045 | // Conversion functions don't have return types, but the parser will |
| 2046 | // happily parse something like: |
| 2047 | // |
| 2048 | // class X { |
| 2049 | // float operator bool(); |
| 2050 | // }; |
| 2051 | // |
| 2052 | // The return type will be changed later anyway. |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2053 | Diag(D.getIdentifierLoc(), diag::err_conv_function_return_type) |
| 2054 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 2055 | << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 3ef6c97 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2056 | } |
| 2057 | |
| 2058 | // Make sure we don't have any parameters. |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2059 | if (R->getAsFunctionProtoType()->getNumArgs() > 0) { |
Douglas Gregor | 3ef6c97 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2060 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_params); |
| 2061 | |
| 2062 | // Delete the parameters. |
Chris Lattner | 5c6139b | 2009-01-20 21:06:38 +0000 | [diff] [blame] | 2063 | D.getTypeObject(0).Fun.freeArgs(); |
Chris Lattner | 08da477 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2064 | D.setInvalidType(); |
Douglas Gregor | 3ef6c97 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2065 | } |
| 2066 | |
| 2067 | // Make sure the conversion function isn't variadic. |
Chris Lattner | 08da477 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2068 | if (R->getAsFunctionProtoType()->isVariadic() && !D.isInvalidType()) { |
Douglas Gregor | 3ef6c97 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2069 | Diag(D.getIdentifierLoc(), diag::err_conv_function_variadic); |
Chris Lattner | 08da477 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2070 | D.setInvalidType(); |
| 2071 | } |
Douglas Gregor | 3ef6c97 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2072 | |
| 2073 | // C++ [class.conv.fct]p4: |
| 2074 | // The conversion-type-id shall not represent a function type nor |
| 2075 | // an array type. |
Argiris Kirtzidis | d6802ba | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 2076 | QualType ConvType = GetTypeFromParser(D.getDeclaratorIdType()); |
Douglas Gregor | 3ef6c97 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2077 | if (ConvType->isArrayType()) { |
| 2078 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_array); |
| 2079 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | 08da477 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2080 | D.setInvalidType(); |
Douglas Gregor | 3ef6c97 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2081 | } else if (ConvType->isFunctionType()) { |
| 2082 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_function); |
| 2083 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | 08da477 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2084 | D.setInvalidType(); |
Douglas Gregor | 3ef6c97 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2085 | } |
| 2086 | |
| 2087 | // Rebuild the function type "R" without any parameters (in case any |
| 2088 | // of the errors above fired) and with the conversion type as the |
| 2089 | // return type. |
| 2090 | R = Context.getFunctionType(ConvType, 0, 0, false, |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2091 | R->getAsFunctionProtoType()->getTypeQuals()); |
Douglas Gregor | 3ef6c97 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2092 | |
Douglas Gregor | 6214d8a | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2093 | // C++0x explicit conversion operators. |
| 2094 | if (D.getDeclSpec().isExplicitSpecified() && !getLangOptions().CPlusPlus0x) |
| 2095 | Diag(D.getDeclSpec().getExplicitSpecLoc(), |
| 2096 | diag::warn_explicit_conversion_functions) |
| 2097 | << SourceRange(D.getDeclSpec().getExplicitSpecLoc()); |
Douglas Gregor | 3ef6c97 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2098 | } |
| 2099 | |
Douglas Gregor | 3ef6c97 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2100 | /// ActOnConversionDeclarator - Called by ActOnDeclarator to complete |
| 2101 | /// the declaration of the given C++ conversion function. This routine |
| 2102 | /// is responsible for recording the conversion function in the C++ |
| 2103 | /// class, if possible. |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2104 | Sema::DeclPtrTy Sema::ActOnConversionDeclarator(CXXConversionDecl *Conversion) { |
Douglas Gregor | 3ef6c97 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2105 | assert(Conversion && "Expected to receive a conversion function declaration"); |
| 2106 | |
Douglas Gregor | 9834104 | 2008-12-12 08:25:50 +0000 | [diff] [blame] | 2107 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Conversion->getDeclContext()); |
Douglas Gregor | 3ef6c97 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2108 | |
| 2109 | // Make sure we aren't redeclaring the conversion function. |
| 2110 | QualType ConvType = Context.getCanonicalType(Conversion->getConversionType()); |
Douglas Gregor | 3ef6c97 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2111 | |
| 2112 | // C++ [class.conv.fct]p1: |
| 2113 | // [...] A conversion function is never used to convert a |
| 2114 | // (possibly cv-qualified) object to the (possibly cv-qualified) |
| 2115 | // same object type (or a reference to it), to a (possibly |
| 2116 | // cv-qualified) base class of that type (or a reference to it), |
| 2117 | // or to (possibly cv-qualified) void. |
Mike Stump | e127ae3 | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2118 | // FIXME: Suppress this warning if the conversion function ends up being a |
| 2119 | // virtual function that overrides a virtual function in a base class. |
Douglas Gregor | 3ef6c97 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2120 | QualType ClassType |
| 2121 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2122 | if (const ReferenceType *ConvTypeRef = ConvType->getAs<ReferenceType>()) |
Douglas Gregor | 3ef6c97 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2123 | ConvType = ConvTypeRef->getPointeeType(); |
| 2124 | if (ConvType->isRecordType()) { |
| 2125 | ConvType = Context.getCanonicalType(ConvType).getUnqualifiedType(); |
| 2126 | if (ConvType == ClassType) |
Chris Lattner | 8d75681 | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 2127 | Diag(Conversion->getLocation(), diag::warn_conv_to_self_not_used) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2128 | << ClassType; |
Douglas Gregor | 3ef6c97 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2129 | else if (IsDerivedFrom(ClassType, ConvType)) |
Chris Lattner | 8d75681 | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 2130 | Diag(Conversion->getLocation(), diag::warn_conv_to_base_not_used) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2131 | << ClassType << ConvType; |
Douglas Gregor | 3ef6c97 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2132 | } else if (ConvType->isVoidType()) { |
Chris Lattner | 8d75681 | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 2133 | Diag(Conversion->getLocation(), diag::warn_conv_to_void_not_used) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2134 | << ClassType << ConvType; |
Douglas Gregor | 3ef6c97 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2135 | } |
| 2136 | |
Douglas Gregor | 853dd39 | 2008-12-26 15:00:45 +0000 | [diff] [blame] | 2137 | if (Conversion->getPreviousDeclaration()) { |
Douglas Gregor | 8c860df | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2138 | const NamedDecl *ExpectedPrevDecl = Conversion->getPreviousDeclaration(); |
| 2139 | if (FunctionTemplateDecl *ConversionTemplate |
| 2140 | = Conversion->getDescribedFunctionTemplate()) |
| 2141 | ExpectedPrevDecl = ConversionTemplate->getPreviousDeclaration(); |
Douglas Gregor | 853dd39 | 2008-12-26 15:00:45 +0000 | [diff] [blame] | 2142 | OverloadedFunctionDecl *Conversions = ClassDecl->getConversionFunctions(); |
| 2143 | for (OverloadedFunctionDecl::function_iterator |
| 2144 | Conv = Conversions->function_begin(), |
| 2145 | ConvEnd = Conversions->function_end(); |
| 2146 | Conv != ConvEnd; ++Conv) { |
Douglas Gregor | 8c860df | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2147 | if (*Conv == ExpectedPrevDecl) { |
Douglas Gregor | 853dd39 | 2008-12-26 15:00:45 +0000 | [diff] [blame] | 2148 | *Conv = Conversion; |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2149 | return DeclPtrTy::make(Conversion); |
Douglas Gregor | 853dd39 | 2008-12-26 15:00:45 +0000 | [diff] [blame] | 2150 | } |
| 2151 | } |
| 2152 | assert(Conversion->isInvalidDecl() && "Conversion should not get here."); |
Douglas Gregor | 8c860df | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2153 | } else if (FunctionTemplateDecl *ConversionTemplate |
| 2154 | = Conversion->getDescribedFunctionTemplate()) |
| 2155 | ClassDecl->addConversionFunction(Context, ConversionTemplate); |
| 2156 | else if (!Conversion->getPrimaryTemplate()) // ignore specializations |
Douglas Gregor | 853dd39 | 2008-12-26 15:00:45 +0000 | [diff] [blame] | 2157 | ClassDecl->addConversionFunction(Context, Conversion); |
Douglas Gregor | 3ef6c97 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2158 | |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2159 | return DeclPtrTy::make(Conversion); |
Douglas Gregor | 3ef6c97 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2160 | } |
| 2161 | |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2162 | //===----------------------------------------------------------------------===// |
| 2163 | // Namespace Handling |
| 2164 | //===----------------------------------------------------------------------===// |
| 2165 | |
| 2166 | /// ActOnStartNamespaceDef - This is called at the start of a namespace |
| 2167 | /// definition. |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2168 | Sema::DeclPtrTy Sema::ActOnStartNamespaceDef(Scope *NamespcScope, |
| 2169 | SourceLocation IdentLoc, |
| 2170 | IdentifierInfo *II, |
| 2171 | SourceLocation LBrace) { |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2172 | NamespaceDecl *Namespc = |
| 2173 | NamespaceDecl::Create(Context, CurContext, IdentLoc, II); |
| 2174 | Namespc->setLBracLoc(LBrace); |
| 2175 | |
| 2176 | Scope *DeclRegionScope = NamespcScope->getParent(); |
| 2177 | |
| 2178 | if (II) { |
| 2179 | // C++ [namespace.def]p2: |
| 2180 | // The identifier in an original-namespace-definition shall not have been |
| 2181 | // previously defined in the declarative region in which the |
| 2182 | // original-namespace-definition appears. The identifier in an |
| 2183 | // original-namespace-definition is the name of the namespace. Subsequently |
| 2184 | // in that declarative region, it is treated as an original-namespace-name. |
| 2185 | |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 2186 | NamedDecl *PrevDecl = LookupName(DeclRegionScope, II, LookupOrdinaryName, |
| 2187 | true); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2188 | |
| 2189 | if (NamespaceDecl *OrigNS = dyn_cast_or_null<NamespaceDecl>(PrevDecl)) { |
| 2190 | // This is an extended namespace definition. |
| 2191 | // Attach this namespace decl to the chain of extended namespace |
| 2192 | // definitions. |
| 2193 | OrigNS->setNextNamespace(Namespc); |
| 2194 | Namespc->setOriginalNamespace(OrigNS->getOriginalNamespace()); |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2195 | |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2196 | // Remove the previous declaration from the scope. |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2197 | if (DeclRegionScope->isDeclScope(DeclPtrTy::make(OrigNS))) { |
Douglas Gregor | 3967762 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 2198 | IdResolver.RemoveDecl(OrigNS); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2199 | DeclRegionScope->RemoveDecl(DeclPtrTy::make(OrigNS)); |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2200 | } |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2201 | } else if (PrevDecl) { |
| 2202 | // This is an invalid name redefinition. |
| 2203 | Diag(Namespc->getLocation(), diag::err_redefinition_different_kind) |
| 2204 | << Namespc->getDeclName(); |
| 2205 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 2206 | Namespc->setInvalidDecl(); |
| 2207 | // Continue on to push Namespc as current DeclContext and return it. |
| 2208 | } |
| 2209 | |
| 2210 | PushOnScopeChains(Namespc, DeclRegionScope); |
| 2211 | } else { |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2212 | // FIXME: Handle anonymous namespaces |
| 2213 | } |
| 2214 | |
| 2215 | // Although we could have an invalid decl (i.e. the namespace name is a |
| 2216 | // redefinition), push it as current DeclContext and try to continue parsing. |
Mike Stump | e127ae3 | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2217 | // FIXME: We should be able to push Namespc here, so that the each DeclContext |
| 2218 | // for the namespace has the declarations that showed up in that particular |
| 2219 | // namespace definition. |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2220 | PushDeclContext(NamespcScope, Namespc); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2221 | return DeclPtrTy::make(Namespc); |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2222 | } |
| 2223 | |
| 2224 | /// ActOnFinishNamespaceDef - This callback is called after a namespace is |
| 2225 | /// exited. Decl is the DeclTy returned by ActOnStartNamespaceDef. |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2226 | void Sema::ActOnFinishNamespaceDef(DeclPtrTy D, SourceLocation RBrace) { |
| 2227 | Decl *Dcl = D.getAs<Decl>(); |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2228 | NamespaceDecl *Namespc = dyn_cast_or_null<NamespaceDecl>(Dcl); |
| 2229 | assert(Namespc && "Invalid parameter, expected NamespaceDecl"); |
| 2230 | Namespc->setRBracLoc(RBrace); |
| 2231 | PopDeclContext(); |
| 2232 | } |
Argiris Kirtzidis | 9e55d46 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2233 | |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2234 | Sema::DeclPtrTy Sema::ActOnUsingDirective(Scope *S, |
| 2235 | SourceLocation UsingLoc, |
| 2236 | SourceLocation NamespcLoc, |
| 2237 | const CXXScopeSpec &SS, |
| 2238 | SourceLocation IdentLoc, |
| 2239 | IdentifierInfo *NamespcName, |
| 2240 | AttributeList *AttrList) { |
Douglas Gregor | 5ff0ee5 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 2241 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
| 2242 | assert(NamespcName && "Invalid NamespcName."); |
| 2243 | assert(IdentLoc.isValid() && "Invalid NamespceName location."); |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2244 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Douglas Gregor | 5ff0ee5 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 2245 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2246 | UsingDirectiveDecl *UDir = 0; |
Douglas Gregor | 5ff0ee5 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 2247 | |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 2248 | // Lookup namespace name. |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2249 | LookupResult R = LookupParsedName(S, &SS, NamespcName, |
| 2250 | LookupNamespaceName, false); |
| 2251 | if (R.isAmbiguous()) { |
| 2252 | DiagnoseAmbiguousLookup(R, NamespcName, IdentLoc); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2253 | return DeclPtrTy(); |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2254 | } |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 2255 | if (NamedDecl *NS = R) { |
Douglas Gregor | 5ff0ee5 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 2256 | assert(isa<NamespaceDecl>(NS) && "expected namespace decl"); |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2257 | // C++ [namespace.udir]p1: |
| 2258 | // A using-directive specifies that the names in the nominated |
| 2259 | // namespace can be used in the scope in which the |
| 2260 | // using-directive appears after the using-directive. During |
| 2261 | // unqualified name lookup (3.4.1), the names appear as if they |
| 2262 | // were declared in the nearest enclosing namespace which |
| 2263 | // contains both the using-directive and the nominated |
Eli Friedman | d5a72f0 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 2264 | // namespace. [Note: in this context, "contains" means "contains |
| 2265 | // directly or indirectly". ] |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2266 | |
| 2267 | // Find enclosing context containing both using-directive and |
| 2268 | // nominated namespace. |
| 2269 | DeclContext *CommonAncestor = cast<DeclContext>(NS); |
| 2270 | while (CommonAncestor && !CommonAncestor->Encloses(CurContext)) |
| 2271 | CommonAncestor = CommonAncestor->getParent(); |
| 2272 | |
Douglas Gregor | 1d27d69 | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 2273 | UDir = UsingDirectiveDecl::Create(Context, |
| 2274 | CurContext, UsingLoc, |
| 2275 | NamespcLoc, |
| 2276 | SS.getRange(), |
| 2277 | (NestedNameSpecifier *)SS.getScopeRep(), |
| 2278 | IdentLoc, |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2279 | cast<NamespaceDecl>(NS), |
| 2280 | CommonAncestor); |
| 2281 | PushUsingDirective(S, UDir); |
Douglas Gregor | 5ff0ee5 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 2282 | } else { |
Chris Lattner | 954381a | 2009-01-06 07:24:29 +0000 | [diff] [blame] | 2283 | Diag(IdentLoc, diag::err_expected_namespace_name) << SS.getRange(); |
Douglas Gregor | 5ff0ee5 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 2284 | } |
| 2285 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2286 | // FIXME: We ignore attributes for now. |
Douglas Gregor | 5ff0ee5 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 2287 | delete AttrList; |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2288 | return DeclPtrTy::make(UDir); |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2289 | } |
| 2290 | |
| 2291 | void Sema::PushUsingDirective(Scope *S, UsingDirectiveDecl *UDir) { |
| 2292 | // If scope has associated entity, then using directive is at namespace |
| 2293 | // or translation unit scope. We add UsingDirectiveDecls, into |
| 2294 | // it's lookup structure. |
| 2295 | if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity())) |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2296 | Ctx->addDecl(UDir); |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2297 | else |
| 2298 | // Otherwise it is block-sope. using-directives will affect lookup |
| 2299 | // only to the end of scope. |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2300 | S->PushUsingDirective(DeclPtrTy::make(UDir)); |
Douglas Gregor | 5ff0ee5 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 2301 | } |
Argiris Kirtzidis | 9e55d46 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2302 | |
Douglas Gregor | 683a114 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2303 | |
| 2304 | Sema::DeclPtrTy Sema::ActOnUsingDeclaration(Scope *S, |
Anders Carlsson | e16b4fe | 2009-08-29 19:54:19 +0000 | [diff] [blame] | 2305 | AccessSpecifier AS, |
Anders Carlsson | b49f1bd | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 2306 | SourceLocation UsingLoc, |
| 2307 | const CXXScopeSpec &SS, |
| 2308 | SourceLocation IdentLoc, |
| 2309 | IdentifierInfo *TargetName, |
| 2310 | OverloadedOperatorKind Op, |
| 2311 | AttributeList *AttrList, |
| 2312 | bool IsTypeName) { |
Eli Friedman | a73d6b1 | 2009-06-27 05:59:59 +0000 | [diff] [blame] | 2313 | assert((TargetName || Op) && "Invalid TargetName."); |
Douglas Gregor | 683a114 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2314 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Anders Carlsson | 348af32 | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 2315 | |
Anders Carlsson | e8c36f2 | 2009-06-27 00:27:47 +0000 | [diff] [blame] | 2316 | DeclarationName Name; |
| 2317 | if (TargetName) |
| 2318 | Name = TargetName; |
| 2319 | else |
| 2320 | Name = Context.DeclarationNames.getCXXOperatorName(Op); |
Anders Carlsson | 348af32 | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 2321 | |
| 2322 | NamedDecl *UD = BuildUsingDeclaration(UsingLoc, SS, IdentLoc, |
| 2323 | Name, AttrList, IsTypeName); |
Anders Carlsson | e16b4fe | 2009-08-29 19:54:19 +0000 | [diff] [blame] | 2324 | if (UD) { |
Anders Carlsson | 348af32 | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 2325 | PushOnScopeChains(UD, S); |
Anders Carlsson | e16b4fe | 2009-08-29 19:54:19 +0000 | [diff] [blame] | 2326 | UD->setAccess(AS); |
| 2327 | } |
Anders Carlsson | 348af32 | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 2328 | |
| 2329 | return DeclPtrTy::make(UD); |
| 2330 | } |
| 2331 | |
| 2332 | NamedDecl *Sema::BuildUsingDeclaration(SourceLocation UsingLoc, |
| 2333 | const CXXScopeSpec &SS, |
| 2334 | SourceLocation IdentLoc, |
| 2335 | DeclarationName Name, |
| 2336 | AttributeList *AttrList, |
| 2337 | bool IsTypeName) { |
| 2338 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
| 2339 | assert(IdentLoc.isValid() && "Invalid TargetName location."); |
Eli Friedman | b30f1c8 | 2009-08-27 05:09:36 +0000 | [diff] [blame] | 2340 | |
Anders Carlsson | 50bc708 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 2341 | // FIXME: We ignore attributes for now. |
| 2342 | delete AttrList; |
| 2343 | |
Anders Carlsson | b49f1bd | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 2344 | if (SS.isEmpty()) { |
| 2345 | Diag(IdentLoc, diag::err_using_requires_qualname); |
Anders Carlsson | 348af32 | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 2346 | return 0; |
Anders Carlsson | b49f1bd | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 2347 | } |
| 2348 | |
| 2349 | NestedNameSpecifier *NNS = |
| 2350 | static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 2351 | |
Anders Carlsson | 50bc708 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 2352 | if (isUnknownSpecialization(SS)) { |
| 2353 | return UnresolvedUsingDecl::Create(Context, CurContext, UsingLoc, |
| 2354 | SS.getRange(), NNS, |
| 2355 | IdentLoc, Name, IsTypeName); |
| 2356 | } |
| 2357 | |
Anders Carlsson | b49f1bd | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 2358 | DeclContext *LookupContext = 0; |
| 2359 | |
| 2360 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 2361 | // C++0x N2914 [namespace.udecl]p3: |
| 2362 | // A using-declaration used as a member-declaration shall refer to a member |
| 2363 | // of a base class of the class being defined, shall refer to a member of an |
| 2364 | // anonymous union that is a member of a base class of the class being |
| 2365 | // defined, or shall refer to an enumerator for an enumeration type that is |
| 2366 | // a member of a base class of the class being defined. |
| 2367 | const Type *Ty = NNS->getAsType(); |
| 2368 | if (!Ty || !IsDerivedFrom(Context.getTagDeclType(RD), QualType(Ty, 0))) { |
| 2369 | Diag(SS.getRange().getBegin(), |
| 2370 | diag::err_using_decl_nested_name_specifier_is_not_a_base_class) |
| 2371 | << NNS << RD->getDeclName(); |
Anders Carlsson | 348af32 | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 2372 | return 0; |
Anders Carlsson | b49f1bd | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 2373 | } |
Anders Carlsson | 6145173 | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 2374 | |
| 2375 | QualType BaseTy = Context.getCanonicalType(QualType(Ty, 0)); |
| 2376 | LookupContext = BaseTy->getAs<RecordType>()->getDecl(); |
Anders Carlsson | b49f1bd | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 2377 | } else { |
| 2378 | // C++0x N2914 [namespace.udecl]p8: |
| 2379 | // A using-declaration for a class member shall be a member-declaration. |
| 2380 | if (NNS->getKind() == NestedNameSpecifier::TypeSpec) { |
Anders Carlsson | 66b082a | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 2381 | Diag(IdentLoc, diag::err_using_decl_can_not_refer_to_class_member) |
Anders Carlsson | b49f1bd | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 2382 | << SS.getRange(); |
Anders Carlsson | 348af32 | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 2383 | return 0; |
Anders Carlsson | b49f1bd | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 2384 | } |
| 2385 | |
| 2386 | // C++0x N2914 [namespace.udecl]p9: |
| 2387 | // In a using-declaration, a prefix :: refers to the global namespace. |
| 2388 | if (NNS->getKind() == NestedNameSpecifier::Global) |
| 2389 | LookupContext = Context.getTranslationUnitDecl(); |
| 2390 | else |
| 2391 | LookupContext = NNS->getAsNamespace(); |
| 2392 | } |
| 2393 | |
| 2394 | |
Douglas Gregor | 683a114 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2395 | // Lookup target name. |
Anders Carlsson | b49f1bd | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 2396 | LookupResult R = LookupQualifiedName(LookupContext, |
| 2397 | Name, LookupOrdinaryName); |
| 2398 | |
| 2399 | if (!R) { |
Anders Carlsson | 8c463c5 | 2009-08-30 00:58:45 +0000 | [diff] [blame] | 2400 | DiagnoseMissingMember(IdentLoc, Name, NNS, SS.getRange()); |
Anders Carlsson | 348af32 | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 2401 | return 0; |
Douglas Gregor | 683a114 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2402 | } |
| 2403 | |
Anders Carlsson | b49f1bd | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 2404 | NamedDecl *ND = R.getAsDecl(); |
| 2405 | |
| 2406 | if (IsTypeName && !isa<TypeDecl>(ND)) { |
| 2407 | Diag(IdentLoc, diag::err_using_typename_non_type); |
Anders Carlsson | 348af32 | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 2408 | return 0; |
Anders Carlsson | b49f1bd | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 2409 | } |
| 2410 | |
Anders Carlsson | 66b082a | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 2411 | // C++0x N2914 [namespace.udecl]p6: |
| 2412 | // A using-declaration shall not name a namespace. |
| 2413 | if (isa<NamespaceDecl>(ND)) { |
| 2414 | Diag(IdentLoc, diag::err_using_decl_can_not_refer_to_namespace) |
| 2415 | << SS.getRange(); |
Anders Carlsson | 348af32 | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 2416 | return 0; |
Anders Carlsson | 66b082a | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 2417 | } |
| 2418 | |
Anders Carlsson | 348af32 | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 2419 | return UsingDecl::Create(Context, CurContext, IdentLoc, SS.getRange(), |
| 2420 | ND->getLocation(), UsingLoc, ND, NNS, IsTypeName); |
Douglas Gregor | 683a114 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2421 | } |
| 2422 | |
Anders Carlsson | 7c1c548 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 2423 | /// getNamespaceDecl - Returns the namespace a decl represents. If the decl |
| 2424 | /// is a namespace alias, returns the namespace it points to. |
| 2425 | static inline NamespaceDecl *getNamespaceDecl(NamedDecl *D) { |
| 2426 | if (NamespaceAliasDecl *AD = dyn_cast_or_null<NamespaceAliasDecl>(D)) |
| 2427 | return AD->getNamespace(); |
| 2428 | return dyn_cast_or_null<NamespaceDecl>(D); |
| 2429 | } |
| 2430 | |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2431 | Sema::DeclPtrTy Sema::ActOnNamespaceAliasDef(Scope *S, |
Anders Carlsson | 26de788 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 2432 | SourceLocation NamespaceLoc, |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2433 | SourceLocation AliasLoc, |
| 2434 | IdentifierInfo *Alias, |
| 2435 | const CXXScopeSpec &SS, |
Anders Carlsson | 26de788 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 2436 | SourceLocation IdentLoc, |
| 2437 | IdentifierInfo *Ident) { |
Anders Carlsson | 640eb7c | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 2438 | |
Anders Carlsson | 7c1c548 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 2439 | // Lookup the namespace name. |
| 2440 | LookupResult R = LookupParsedName(S, &SS, Ident, LookupNamespaceName, false); |
| 2441 | |
Anders Carlsson | 640eb7c | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 2442 | // Check if we have a previous declaration with the same name. |
Anders Carlsson | 1cd05f5 | 2009-03-28 23:49:35 +0000 | [diff] [blame] | 2443 | if (NamedDecl *PrevDecl = LookupName(S, Alias, LookupOrdinaryName, true)) { |
Anders Carlsson | 7c1c548 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 2444 | if (NamespaceAliasDecl *AD = dyn_cast<NamespaceAliasDecl>(PrevDecl)) { |
| 2445 | // We already have an alias with the same name that points to the same |
| 2446 | // namespace, so don't create a new one. |
| 2447 | if (!R.isAmbiguous() && AD->getNamespace() == getNamespaceDecl(R)) |
| 2448 | return DeclPtrTy(); |
| 2449 | } |
| 2450 | |
Anders Carlsson | 640eb7c | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 2451 | unsigned DiagID = isa<NamespaceDecl>(PrevDecl) ? diag::err_redefinition : |
| 2452 | diag::err_redefinition_different_kind; |
| 2453 | Diag(AliasLoc, DiagID) << Alias; |
| 2454 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2455 | return DeclPtrTy(); |
Anders Carlsson | 640eb7c | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 2456 | } |
| 2457 | |
Anders Carlsson | 279ebc4 | 2009-03-28 06:42:02 +0000 | [diff] [blame] | 2458 | if (R.isAmbiguous()) { |
Anders Carlsson | 26de788 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 2459 | DiagnoseAmbiguousLookup(R, Ident, IdentLoc); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2460 | return DeclPtrTy(); |
Anders Carlsson | 279ebc4 | 2009-03-28 06:42:02 +0000 | [diff] [blame] | 2461 | } |
| 2462 | |
| 2463 | if (!R) { |
| 2464 | Diag(NamespaceLoc, diag::err_expected_namespace_name) << SS.getRange(); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2465 | return DeclPtrTy(); |
Anders Carlsson | 279ebc4 | 2009-03-28 06:42:02 +0000 | [diff] [blame] | 2466 | } |
| 2467 | |
Fariborz Jahanian | 3603e0c | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2468 | NamespaceAliasDecl *AliasDecl = |
Douglas Gregor | 8d8ddca | 2009-05-30 06:48:27 +0000 | [diff] [blame] | 2469 | NamespaceAliasDecl::Create(Context, CurContext, NamespaceLoc, AliasLoc, |
| 2470 | Alias, SS.getRange(), |
| 2471 | (NestedNameSpecifier *)SS.getScopeRep(), |
Anders Carlsson | ddb1d8b | 2009-03-28 22:58:02 +0000 | [diff] [blame] | 2472 | IdentLoc, R); |
| 2473 | |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2474 | CurContext->addDecl(AliasDecl); |
Anders Carlsson | ddb1d8b | 2009-03-28 22:58:02 +0000 | [diff] [blame] | 2475 | return DeclPtrTy::make(AliasDecl); |
Anders Carlsson | 8cffcd6 | 2009-03-28 05:27:17 +0000 | [diff] [blame] | 2476 | } |
| 2477 | |
Fariborz Jahanian | 3603e0c | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2478 | void Sema::DefineImplicitDefaultConstructor(SourceLocation CurrentLocation, |
| 2479 | CXXConstructorDecl *Constructor) { |
Fariborz Jahanian | 2f5a0a3 | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 2480 | assert((Constructor->isImplicit() && Constructor->isDefaultConstructor() && |
| 2481 | !Constructor->isUsed()) && |
| 2482 | "DefineImplicitDefaultConstructor - call it for implicit default ctor"); |
Fariborz Jahanian | 3603e0c | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2483 | |
| 2484 | CXXRecordDecl *ClassDecl |
| 2485 | = cast<CXXRecordDecl>(Constructor->getDeclContext()); |
Fariborz Jahanian | 599778e | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 2486 | assert(ClassDecl && "DefineImplicitDefaultConstructor - invalid constructor"); |
Fariborz Jahanian | 3603e0c | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2487 | // Before the implicitly-declared default constructor for a class is |
| 2488 | // implicitly defined, all the implicitly-declared default constructors |
| 2489 | // for its base class and its non-static data members shall have been |
| 2490 | // implicitly defined. |
| 2491 | bool err = false; |
Fariborz Jahanian | b394bd3 | 2009-06-30 16:36:53 +0000 | [diff] [blame] | 2492 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 2493 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
Fariborz Jahanian | 3603e0c | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2494 | CXXRecordDecl *BaseClassDecl |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2495 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Fariborz Jahanian | 3603e0c | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2496 | if (!BaseClassDecl->hasTrivialConstructor()) { |
| 2497 | if (CXXConstructorDecl *BaseCtor = |
Fariborz Jahanian | f6da0dd | 2009-06-23 23:42:10 +0000 | [diff] [blame] | 2498 | BaseClassDecl->getDefaultConstructor(Context)) |
| 2499 | MarkDeclarationReferenced(CurrentLocation, BaseCtor); |
Fariborz Jahanian | 3603e0c | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2500 | else { |
| 2501 | Diag(CurrentLocation, diag::err_defining_default_ctor) |
Fariborz Jahanian | 1644360 | 2009-06-20 20:23:38 +0000 | [diff] [blame] | 2502 | << Context.getTagDeclType(ClassDecl) << 1 |
| 2503 | << Context.getTagDeclType(BaseClassDecl); |
| 2504 | Diag(BaseClassDecl->getLocation(), diag::note_previous_class_decl) |
| 2505 | << Context.getTagDeclType(BaseClassDecl); |
Fariborz Jahanian | 3603e0c | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2506 | err = true; |
| 2507 | } |
| 2508 | } |
| 2509 | } |
Fariborz Jahanian | b394bd3 | 2009-06-30 16:36:53 +0000 | [diff] [blame] | 2510 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 2511 | E = ClassDecl->field_end(); Field != E; ++Field) { |
Fariborz Jahanian | 3603e0c | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2512 | QualType FieldType = Context.getCanonicalType((*Field)->getType()); |
| 2513 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 2514 | FieldType = Array->getElementType(); |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2515 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Fariborz Jahanian | 3603e0c | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2516 | CXXRecordDecl *FieldClassDecl |
| 2517 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Duncan Sands | 7814671 | 2009-06-25 09:03:06 +0000 | [diff] [blame] | 2518 | if (!FieldClassDecl->hasTrivialConstructor()) { |
Fariborz Jahanian | 3603e0c | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2519 | if (CXXConstructorDecl *FieldCtor = |
Fariborz Jahanian | f6da0dd | 2009-06-23 23:42:10 +0000 | [diff] [blame] | 2520 | FieldClassDecl->getDefaultConstructor(Context)) |
| 2521 | MarkDeclarationReferenced(CurrentLocation, FieldCtor); |
Fariborz Jahanian | 3603e0c | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2522 | else { |
| 2523 | Diag(CurrentLocation, diag::err_defining_default_ctor) |
Fariborz Jahanian | 1644360 | 2009-06-20 20:23:38 +0000 | [diff] [blame] | 2524 | << Context.getTagDeclType(ClassDecl) << 0 << |
| 2525 | Context.getTagDeclType(FieldClassDecl); |
| 2526 | Diag(FieldClassDecl->getLocation(), diag::note_previous_class_decl) |
| 2527 | << Context.getTagDeclType(FieldClassDecl); |
Fariborz Jahanian | 3603e0c | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2528 | err = true; |
| 2529 | } |
| 2530 | } |
Mike Stump | 90fc78e | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2531 | } else if (FieldType->isReferenceType()) { |
Fariborz Jahanian | 3603e0c | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2532 | Diag(CurrentLocation, diag::err_unintialized_member) |
Anders Carlsson | 6a66126 | 2009-07-09 17:37:12 +0000 | [diff] [blame] | 2533 | << Context.getTagDeclType(ClassDecl) << 0 << Field->getDeclName(); |
Fariborz Jahanian | 3603e0c | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2534 | Diag((*Field)->getLocation(), diag::note_declared_at); |
| 2535 | err = true; |
Mike Stump | 90fc78e | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2536 | } else if (FieldType.isConstQualified()) { |
Fariborz Jahanian | 3603e0c | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2537 | Diag(CurrentLocation, diag::err_unintialized_member) |
Anders Carlsson | 6a66126 | 2009-07-09 17:37:12 +0000 | [diff] [blame] | 2538 | << Context.getTagDeclType(ClassDecl) << 1 << Field->getDeclName(); |
Fariborz Jahanian | 3603e0c | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2539 | Diag((*Field)->getLocation(), diag::note_declared_at); |
| 2540 | err = true; |
| 2541 | } |
| 2542 | } |
| 2543 | if (!err) |
Fariborz Jahanian | 2c313fb | 2009-06-26 16:08:57 +0000 | [diff] [blame] | 2544 | Constructor->setUsed(); |
| 2545 | else |
| 2546 | Constructor->setInvalidDecl(); |
Fariborz Jahanian | 3603e0c | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 2547 | } |
| 2548 | |
Fariborz Jahanian | 368cc6c | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 2549 | void Sema::DefineImplicitDestructor(SourceLocation CurrentLocation, |
| 2550 | CXXDestructorDecl *Destructor) { |
| 2551 | assert((Destructor->isImplicit() && !Destructor->isUsed()) && |
| 2552 | "DefineImplicitDestructor - call it for implicit default dtor"); |
| 2553 | |
| 2554 | CXXRecordDecl *ClassDecl |
| 2555 | = cast<CXXRecordDecl>(Destructor->getDeclContext()); |
| 2556 | assert(ClassDecl && "DefineImplicitDestructor - invalid destructor"); |
| 2557 | // C++ [class.dtor] p5 |
| 2558 | // Before the implicitly-declared default destructor for a class is |
| 2559 | // implicitly defined, all the implicitly-declared default destructors |
| 2560 | // for its base class and its non-static data members shall have been |
| 2561 | // implicitly defined. |
Fariborz Jahanian | b394bd3 | 2009-06-30 16:36:53 +0000 | [diff] [blame] | 2562 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 2563 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
Fariborz Jahanian | 368cc6c | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 2564 | CXXRecordDecl *BaseClassDecl |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2565 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Fariborz Jahanian | 368cc6c | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 2566 | if (!BaseClassDecl->hasTrivialDestructor()) { |
| 2567 | if (CXXDestructorDecl *BaseDtor = |
| 2568 | const_cast<CXXDestructorDecl*>(BaseClassDecl->getDestructor(Context))) |
| 2569 | MarkDeclarationReferenced(CurrentLocation, BaseDtor); |
| 2570 | else |
| 2571 | assert(false && |
| 2572 | "DefineImplicitDestructor - missing dtor in a base class"); |
| 2573 | } |
| 2574 | } |
| 2575 | |
Fariborz Jahanian | b394bd3 | 2009-06-30 16:36:53 +0000 | [diff] [blame] | 2576 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 2577 | E = ClassDecl->field_end(); Field != E; ++Field) { |
Fariborz Jahanian | 368cc6c | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 2578 | QualType FieldType = Context.getCanonicalType((*Field)->getType()); |
| 2579 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 2580 | FieldType = Array->getElementType(); |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2581 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Fariborz Jahanian | 368cc6c | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 2582 | CXXRecordDecl *FieldClassDecl |
| 2583 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
| 2584 | if (!FieldClassDecl->hasTrivialDestructor()) { |
| 2585 | if (CXXDestructorDecl *FieldDtor = |
| 2586 | const_cast<CXXDestructorDecl*>( |
| 2587 | FieldClassDecl->getDestructor(Context))) |
| 2588 | MarkDeclarationReferenced(CurrentLocation, FieldDtor); |
| 2589 | else |
| 2590 | assert(false && |
| 2591 | "DefineImplicitDestructor - missing dtor in class of a data member"); |
| 2592 | } |
| 2593 | } |
| 2594 | } |
| 2595 | Destructor->setUsed(); |
| 2596 | } |
| 2597 | |
Fariborz Jahanian | d67364c | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 2598 | void Sema::DefineImplicitOverloadedAssign(SourceLocation CurrentLocation, |
| 2599 | CXXMethodDecl *MethodDecl) { |
| 2600 | assert((MethodDecl->isImplicit() && MethodDecl->isOverloadedOperator() && |
| 2601 | MethodDecl->getOverloadedOperator() == OO_Equal && |
| 2602 | !MethodDecl->isUsed()) && |
| 2603 | "DefineImplicitOverloadedAssign - call it for implicit assignment op"); |
| 2604 | |
| 2605 | CXXRecordDecl *ClassDecl |
| 2606 | = cast<CXXRecordDecl>(MethodDecl->getDeclContext()); |
Fariborz Jahanian | d67364c | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 2607 | |
Fariborz Jahanian | 2c313fb | 2009-06-26 16:08:57 +0000 | [diff] [blame] | 2608 | // C++[class.copy] p12 |
Fariborz Jahanian | d67364c | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 2609 | // Before the implicitly-declared copy assignment operator for a class is |
| 2610 | // implicitly defined, all implicitly-declared copy assignment operators |
| 2611 | // for its direct base classes and its nonstatic data members shall have |
| 2612 | // been implicitly defined. |
| 2613 | bool err = false; |
Fariborz Jahanian | b394bd3 | 2009-06-30 16:36:53 +0000 | [diff] [blame] | 2614 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 2615 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
Fariborz Jahanian | d67364c | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 2616 | CXXRecordDecl *BaseClassDecl |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2617 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Fariborz Jahanian | d67364c | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 2618 | if (CXXMethodDecl *BaseAssignOpMethod = |
| 2619 | getAssignOperatorMethod(MethodDecl->getParamDecl(0), BaseClassDecl)) |
| 2620 | MarkDeclarationReferenced(CurrentLocation, BaseAssignOpMethod); |
| 2621 | } |
Fariborz Jahanian | b394bd3 | 2009-06-30 16:36:53 +0000 | [diff] [blame] | 2622 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 2623 | E = ClassDecl->field_end(); Field != E; ++Field) { |
Fariborz Jahanian | d67364c | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 2624 | QualType FieldType = Context.getCanonicalType((*Field)->getType()); |
| 2625 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 2626 | FieldType = Array->getElementType(); |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2627 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Fariborz Jahanian | d67364c | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 2628 | CXXRecordDecl *FieldClassDecl |
| 2629 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
| 2630 | if (CXXMethodDecl *FieldAssignOpMethod = |
| 2631 | getAssignOperatorMethod(MethodDecl->getParamDecl(0), FieldClassDecl)) |
| 2632 | MarkDeclarationReferenced(CurrentLocation, FieldAssignOpMethod); |
Mike Stump | 90fc78e | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2633 | } else if (FieldType->isReferenceType()) { |
Fariborz Jahanian | 368cc6c | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 2634 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
Anders Carlsson | 1c0c52c | 2009-07-09 17:47:25 +0000 | [diff] [blame] | 2635 | << Context.getTagDeclType(ClassDecl) << 0 << Field->getDeclName(); |
| 2636 | Diag(Field->getLocation(), diag::note_declared_at); |
Fariborz Jahanian | d67364c | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 2637 | Diag(CurrentLocation, diag::note_first_required_here); |
| 2638 | err = true; |
Mike Stump | 90fc78e | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2639 | } else if (FieldType.isConstQualified()) { |
Fariborz Jahanian | 368cc6c | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 2640 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
Anders Carlsson | 1c0c52c | 2009-07-09 17:47:25 +0000 | [diff] [blame] | 2641 | << Context.getTagDeclType(ClassDecl) << 1 << Field->getDeclName(); |
| 2642 | Diag(Field->getLocation(), diag::note_declared_at); |
Fariborz Jahanian | d67364c | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 2643 | Diag(CurrentLocation, diag::note_first_required_here); |
| 2644 | err = true; |
| 2645 | } |
| 2646 | } |
| 2647 | if (!err) |
| 2648 | MethodDecl->setUsed(); |
| 2649 | } |
| 2650 | |
| 2651 | CXXMethodDecl * |
| 2652 | Sema::getAssignOperatorMethod(ParmVarDecl *ParmDecl, |
| 2653 | CXXRecordDecl *ClassDecl) { |
| 2654 | QualType LHSType = Context.getTypeDeclType(ClassDecl); |
| 2655 | QualType RHSType(LHSType); |
| 2656 | // If class's assignment operator argument is const/volatile qualified, |
| 2657 | // look for operator = (const/volatile B&). Otherwise, look for |
| 2658 | // operator = (B&). |
| 2659 | if (ParmDecl->getType().isConstQualified()) |
| 2660 | RHSType.addConst(); |
| 2661 | if (ParmDecl->getType().isVolatileQualified()) |
| 2662 | RHSType.addVolatile(); |
| 2663 | ExprOwningPtr<Expr> LHS(this, new (Context) DeclRefExpr(ParmDecl, |
| 2664 | LHSType, |
| 2665 | SourceLocation())); |
| 2666 | ExprOwningPtr<Expr> RHS(this, new (Context) DeclRefExpr(ParmDecl, |
| 2667 | RHSType, |
| 2668 | SourceLocation())); |
| 2669 | Expr *Args[2] = { &*LHS, &*RHS }; |
| 2670 | OverloadCandidateSet CandidateSet; |
| 2671 | AddMemberOperatorCandidates(clang::OO_Equal, SourceLocation(), Args, 2, |
| 2672 | CandidateSet); |
| 2673 | OverloadCandidateSet::iterator Best; |
| 2674 | if (BestViableFunction(CandidateSet, |
| 2675 | ClassDecl->getLocation(), Best) == OR_Success) |
| 2676 | return cast<CXXMethodDecl>(Best->Function); |
| 2677 | assert(false && |
| 2678 | "getAssignOperatorMethod - copy assignment operator method not found"); |
| 2679 | return 0; |
| 2680 | } |
| 2681 | |
Fariborz Jahanian | 599778e | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 2682 | void Sema::DefineImplicitCopyConstructor(SourceLocation CurrentLocation, |
| 2683 | CXXConstructorDecl *CopyConstructor, |
| 2684 | unsigned TypeQuals) { |
| 2685 | assert((CopyConstructor->isImplicit() && |
| 2686 | CopyConstructor->isCopyConstructor(Context, TypeQuals) && |
| 2687 | !CopyConstructor->isUsed()) && |
| 2688 | "DefineImplicitCopyConstructor - call it for implicit copy ctor"); |
| 2689 | |
| 2690 | CXXRecordDecl *ClassDecl |
| 2691 | = cast<CXXRecordDecl>(CopyConstructor->getDeclContext()); |
| 2692 | assert(ClassDecl && "DefineImplicitCopyConstructor - invalid constructor"); |
Fariborz Jahanian | f6da0dd | 2009-06-23 23:42:10 +0000 | [diff] [blame] | 2693 | // C++ [class.copy] p209 |
Fariborz Jahanian | 599778e | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 2694 | // Before the implicitly-declared copy constructor for a class is |
| 2695 | // implicitly defined, all the implicitly-declared copy constructors |
| 2696 | // for its base class and its non-static data members shall have been |
| 2697 | // implicitly defined. |
| 2698 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(); |
| 2699 | Base != ClassDecl->bases_end(); ++Base) { |
| 2700 | CXXRecordDecl *BaseClassDecl |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2701 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Fariborz Jahanian | 599778e | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 2702 | if (CXXConstructorDecl *BaseCopyCtor = |
| 2703 | BaseClassDecl->getCopyConstructor(Context, TypeQuals)) |
Fariborz Jahanian | f6da0dd | 2009-06-23 23:42:10 +0000 | [diff] [blame] | 2704 | MarkDeclarationReferenced(CurrentLocation, BaseCopyCtor); |
Fariborz Jahanian | 599778e | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 2705 | } |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2706 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 2707 | FieldEnd = ClassDecl->field_end(); |
| 2708 | Field != FieldEnd; ++Field) { |
Fariborz Jahanian | 599778e | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 2709 | QualType FieldType = Context.getCanonicalType((*Field)->getType()); |
| 2710 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 2711 | FieldType = Array->getElementType(); |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2712 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Fariborz Jahanian | 599778e | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 2713 | CXXRecordDecl *FieldClassDecl |
| 2714 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
| 2715 | if (CXXConstructorDecl *FieldCopyCtor = |
| 2716 | FieldClassDecl->getCopyConstructor(Context, TypeQuals)) |
Fariborz Jahanian | f6da0dd | 2009-06-23 23:42:10 +0000 | [diff] [blame] | 2717 | MarkDeclarationReferenced(CurrentLocation, FieldCopyCtor); |
Fariborz Jahanian | 599778e | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 2718 | } |
| 2719 | } |
| 2720 | CopyConstructor->setUsed(); |
| 2721 | } |
| 2722 | |
Anders Carlsson | 665e469 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 2723 | Sema::OwningExprResult |
| 2724 | Sema::BuildCXXConstructExpr(QualType DeclInitType, |
| 2725 | CXXConstructorDecl *Constructor, |
| 2726 | Expr **Exprs, unsigned NumExprs) { |
Anders Carlsson | bd9f51a | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 2727 | bool Elidable = false; |
| 2728 | |
| 2729 | // [class.copy]p15: |
| 2730 | // Whenever a temporary class object is copied using a copy constructor, and |
| 2731 | // this object and the copy have the same cv-unqualified type, an |
| 2732 | // implementation is permitted to treat the original and the copy as two |
| 2733 | // different ways of referring to the same object and not perform a copy at |
| 2734 | //all, even if the class copy constructor or destructor have side effects. |
| 2735 | |
| 2736 | // FIXME: Is this enough? |
| 2737 | if (Constructor->isCopyConstructor(Context) && NumExprs == 1) { |
| 2738 | Expr *E = Exprs[0]; |
| 2739 | while (CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E)) |
| 2740 | E = BE->getSubExpr(); |
| 2741 | |
| 2742 | if (isa<CallExpr>(E) || isa<CXXTemporaryObjectExpr>(E)) |
| 2743 | Elidable = true; |
| 2744 | } |
| 2745 | |
| 2746 | return BuildCXXConstructExpr(DeclInitType, Constructor, Elidable, |
| 2747 | Exprs, NumExprs); |
| 2748 | } |
| 2749 | |
Fariborz Jahanian | 1c30f26 | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 2750 | /// BuildCXXConstructExpr - Creates a complete call to a constructor, |
| 2751 | /// including handling of its default argument expressions. |
Anders Carlsson | 665e469 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 2752 | Sema::OwningExprResult |
| 2753 | Sema::BuildCXXConstructExpr(QualType DeclInitType, |
| 2754 | CXXConstructorDecl *Constructor, |
| 2755 | bool Elidable, |
| 2756 | Expr **Exprs, |
| 2757 | unsigned NumExprs) { |
Anders Carlsson | 3e03d83 | 2009-08-25 13:07:08 +0000 | [diff] [blame] | 2758 | ExprOwningPtr<CXXConstructExpr> Temp(this, |
| 2759 | CXXConstructExpr::Create(Context, |
| 2760 | DeclInitType, |
| 2761 | Constructor, |
| 2762 | Elidable, |
| 2763 | Exprs, |
| 2764 | NumExprs)); |
Anders Carlsson | ef8fd08 | 2009-08-27 05:08:22 +0000 | [diff] [blame] | 2765 | // Default arguments must be added to constructor call expression. |
Fariborz Jahanian | beb68c4 | 2009-08-05 00:26:10 +0000 | [diff] [blame] | 2766 | FunctionDecl *FDecl = cast<FunctionDecl>(Constructor); |
| 2767 | unsigned NumArgsInProto = FDecl->param_size(); |
| 2768 | for (unsigned j = NumExprs; j != NumArgsInProto; j++) { |
Anders Carlsson | 3e03d83 | 2009-08-25 13:07:08 +0000 | [diff] [blame] | 2769 | ParmVarDecl *Param = FDecl->getParamDecl(j); |
| 2770 | |
| 2771 | OwningExprResult ArgExpr = |
| 2772 | BuildCXXDefaultArgExpr(/*FIXME:*/SourceLocation(), |
| 2773 | FDecl, Param); |
| 2774 | if (ArgExpr.isInvalid()) |
| 2775 | return ExprError(); |
| 2776 | |
| 2777 | Temp->setArg(j, ArgExpr.takeAs<Expr>()); |
Fariborz Jahanian | beb68c4 | 2009-08-05 00:26:10 +0000 | [diff] [blame] | 2778 | } |
Anders Carlsson | 3e03d83 | 2009-08-25 13:07:08 +0000 | [diff] [blame] | 2779 | return move(Temp); |
Fariborz Jahanian | 1c30f26 | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 2780 | } |
| 2781 | |
Anders Carlsson | ef8fd08 | 2009-08-27 05:08:22 +0000 | [diff] [blame] | 2782 | Sema::OwningExprResult |
| 2783 | Sema::BuildCXXTemporaryObjectExpr(CXXConstructorDecl *Constructor, |
| 2784 | QualType Ty, |
| 2785 | SourceLocation TyBeginLoc, |
| 2786 | MultiExprArg Args, |
| 2787 | SourceLocation RParenLoc) { |
| 2788 | CXXTemporaryObjectExpr *E |
| 2789 | = new (Context) CXXTemporaryObjectExpr(Context, Constructor, Ty, TyBeginLoc, |
| 2790 | (Expr **)Args.get(), |
| 2791 | Args.size(), RParenLoc); |
| 2792 | |
| 2793 | ExprOwningPtr<CXXTemporaryObjectExpr> Temp(this, E); |
| 2794 | |
| 2795 | // Default arguments must be added to constructor call expression. |
| 2796 | FunctionDecl *FDecl = cast<FunctionDecl>(Constructor); |
| 2797 | unsigned NumArgsInProto = FDecl->param_size(); |
| 2798 | for (unsigned j = Args.size(); j != NumArgsInProto; j++) { |
| 2799 | ParmVarDecl *Param = FDecl->getParamDecl(j); |
| 2800 | |
| 2801 | OwningExprResult ArgExpr = BuildCXXDefaultArgExpr(TyBeginLoc, FDecl, Param); |
| 2802 | if (ArgExpr.isInvalid()) |
| 2803 | return ExprError(); |
| 2804 | |
| 2805 | Temp->setArg(j, ArgExpr.takeAs<Expr>()); |
| 2806 | } |
| 2807 | |
| 2808 | Args.release(); |
| 2809 | return move(Temp); |
| 2810 | } |
| 2811 | |
| 2812 | |
Anders Carlsson | baa8a31 | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 2813 | bool Sema::InitializeVarWithConstructor(VarDecl *VD, |
Fariborz Jahanian | 1c30f26 | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 2814 | CXXConstructorDecl *Constructor, |
| 2815 | QualType DeclInitType, |
| 2816 | Expr **Exprs, unsigned NumExprs) { |
Anders Carlsson | 665e469 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 2817 | OwningExprResult TempResult = BuildCXXConstructExpr(DeclInitType, Constructor, |
| 2818 | Exprs, NumExprs); |
Anders Carlsson | baa8a31 | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 2819 | if (TempResult.isInvalid()) |
| 2820 | return true; |
Anders Carlsson | 665e469 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 2821 | |
| 2822 | Expr *Temp = TempResult.takeAs<Expr>(); |
Douglas Gregor | cad27f6 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 2823 | MarkDeclarationReferenced(VD->getLocation(), Constructor); |
Fariborz Jahanian | 88e09cc | 2009-08-05 18:17:32 +0000 | [diff] [blame] | 2824 | Temp = MaybeCreateCXXExprWithTemporaries(Temp, /*DestroyTemps=*/true); |
Douglas Gregor | 4833ff0 | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 2825 | VD->setInit(Context, Temp); |
Anders Carlsson | baa8a31 | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 2826 | |
| 2827 | return false; |
Anders Carlsson | 05e5965 | 2009-04-16 23:50:50 +0000 | [diff] [blame] | 2828 | } |
| 2829 | |
Fariborz Jahanian | cd20811 | 2009-08-03 19:13:25 +0000 | [diff] [blame] | 2830 | void Sema::FinalizeVarWithDestructor(VarDecl *VD, QualType DeclInitType) |
Fariborz Jahanian | 368cc6c | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 2831 | { |
| 2832 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>( |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2833 | DeclInitType->getAs<RecordType>()->getDecl()); |
Fariborz Jahanian | 368cc6c | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 2834 | if (!ClassDecl->hasTrivialDestructor()) |
| 2835 | if (CXXDestructorDecl *Destructor = |
| 2836 | const_cast<CXXDestructorDecl*>(ClassDecl->getDestructor(Context))) |
Fariborz Jahanian | cd20811 | 2009-08-03 19:13:25 +0000 | [diff] [blame] | 2837 | MarkDeclarationReferenced(VD->getLocation(), Destructor); |
Fariborz Jahanian | 368cc6c | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 2838 | } |
| 2839 | |
Argiris Kirtzidis | 9e55d46 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2840 | /// AddCXXDirectInitializerToDecl - This action is called immediately after |
| 2841 | /// ActOnDeclarator, when a C++ direct initializer is present. |
| 2842 | /// e.g: "int x(1);" |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2843 | void Sema::AddCXXDirectInitializerToDecl(DeclPtrTy Dcl, |
| 2844 | SourceLocation LParenLoc, |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 2845 | MultiExprArg Exprs, |
Argiris Kirtzidis | 9e55d46 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2846 | SourceLocation *CommaLocs, |
| 2847 | SourceLocation RParenLoc) { |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 2848 | unsigned NumExprs = Exprs.size(); |
| 2849 | assert(NumExprs != 0 && Exprs.get() && "missing expressions"); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2850 | Decl *RealDecl = Dcl.getAs<Decl>(); |
Argiris Kirtzidis | 9e55d46 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2851 | |
| 2852 | // If there is no declaration, there was an error parsing it. Just ignore |
| 2853 | // the initializer. |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2854 | if (RealDecl == 0) |
Argiris Kirtzidis | 9e55d46 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2855 | return; |
Argiris Kirtzidis | 9e55d46 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2856 | |
| 2857 | VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl); |
| 2858 | if (!VDecl) { |
| 2859 | Diag(RealDecl->getLocation(), diag::err_illegal_initializer); |
| 2860 | RealDecl->setInvalidDecl(); |
| 2861 | return; |
| 2862 | } |
| 2863 | |
Douglas Gregor | 52be1ca | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 2864 | // We will represent direct-initialization similarly to copy-initialization: |
Argiris Kirtzidis | bca33bf | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 2865 | // int x(1); -as-> int x = 1; |
Argiris Kirtzidis | 9e55d46 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2866 | // ClassType x(a,b,c); -as-> ClassType x = ClassType(a,b,c); |
| 2867 | // |
| 2868 | // Clients that want to distinguish between the two forms, can check for |
| 2869 | // direct initializer using VarDecl::hasCXXDirectInitializer(). |
| 2870 | // A major benefit is that clients that don't particularly care about which |
| 2871 | // exactly form was it (like the CodeGen) can handle both cases without |
| 2872 | // special case code. |
Argiris Kirtzidis | ffcb503 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 2873 | |
Douglas Gregor | 52be1ca | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 2874 | // If either the declaration has a dependent type or if any of the expressions |
| 2875 | // is type-dependent, we represent the initialization via a ParenListExpr for |
| 2876 | // later use during template instantiation. |
| 2877 | if (VDecl->getType()->isDependentType() || |
| 2878 | Expr::hasAnyTypeDependentArguments((Expr **)Exprs.get(), Exprs.size())) { |
| 2879 | // Let clients know that initialization was done with a direct initializer. |
| 2880 | VDecl->setCXXDirectInitializer(true); |
| 2881 | |
| 2882 | // Store the initialization expressions as a ParenListExpr. |
| 2883 | unsigned NumExprs = Exprs.size(); |
| 2884 | VDecl->setInit(Context, |
| 2885 | new (Context) ParenListExpr(Context, LParenLoc, |
| 2886 | (Expr **)Exprs.release(), |
| 2887 | NumExprs, RParenLoc)); |
| 2888 | return; |
| 2889 | } |
| 2890 | |
| 2891 | |
Argiris Kirtzidis | 9e55d46 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2892 | // C++ 8.5p11: |
| 2893 | // The form of initialization (using parentheses or '=') is generally |
| 2894 | // insignificant, but does matter when the entity being initialized has a |
Argiris Kirtzidis | ffcb503 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 2895 | // class type. |
Douglas Gregor | 5870a95 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 2896 | QualType DeclInitType = VDecl->getType(); |
| 2897 | if (const ArrayType *Array = Context.getAsArrayType(DeclInitType)) |
| 2898 | DeclInitType = Array->getElementType(); |
Argiris Kirtzidis | ffcb503 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 2899 | |
Douglas Gregor | ad7d181 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 2900 | // FIXME: This isn't the right place to complete the type. |
| 2901 | if (RequireCompleteType(VDecl->getLocation(), VDecl->getType(), |
| 2902 | diag::err_typecheck_decl_incomplete_type)) { |
| 2903 | VDecl->setInvalidDecl(); |
| 2904 | return; |
| 2905 | } |
| 2906 | |
Argiris Kirtzidis | ffcb503 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 2907 | if (VDecl->getType()->isRecordType()) { |
Douglas Gregor | 5870a95 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 2908 | CXXConstructorDecl *Constructor |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 2909 | = PerformInitializationByConstructor(DeclInitType, |
| 2910 | (Expr **)Exprs.get(), NumExprs, |
Douglas Gregor | 6428e76 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 2911 | VDecl->getLocation(), |
| 2912 | SourceRange(VDecl->getLocation(), |
| 2913 | RParenLoc), |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2914 | VDecl->getDeclName(), |
Douglas Gregor | 6428e76 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 2915 | IK_Direct); |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 2916 | if (!Constructor) |
Douglas Gregor | 5870a95 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 2917 | RealDecl->setInvalidDecl(); |
Anders Carlsson | 9c7b492 | 2009-04-15 21:48:18 +0000 | [diff] [blame] | 2918 | else { |
Anders Carlsson | 9c7b492 | 2009-04-15 21:48:18 +0000 | [diff] [blame] | 2919 | VDecl->setCXXDirectInitializer(true); |
Anders Carlsson | baa8a31 | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 2920 | if (InitializeVarWithConstructor(VDecl, Constructor, DeclInitType, |
| 2921 | (Expr**)Exprs.release(), NumExprs)) |
| 2922 | RealDecl->setInvalidDecl(); |
Fariborz Jahanian | cd20811 | 2009-08-03 19:13:25 +0000 | [diff] [blame] | 2923 | FinalizeVarWithDestructor(VDecl, DeclInitType); |
Anders Carlsson | 9c7b492 | 2009-04-15 21:48:18 +0000 | [diff] [blame] | 2924 | } |
Argiris Kirtzidis | ffcb503 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 2925 | return; |
| 2926 | } |
Argiris Kirtzidis | 9e55d46 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2927 | |
Argiris Kirtzidis | bca33bf | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 2928 | if (NumExprs > 1) { |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 2929 | Diag(CommaLocs[0], diag::err_builtin_direct_init_more_than_one_arg) |
| 2930 | << SourceRange(VDecl->getLocation(), RParenLoc); |
Argiris Kirtzidis | 9e55d46 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2931 | RealDecl->setInvalidDecl(); |
| 2932 | return; |
| 2933 | } |
| 2934 | |
Argiris Kirtzidis | 9e55d46 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2935 | // Let clients know that initialization was done with a direct initializer. |
| 2936 | VDecl->setCXXDirectInitializer(true); |
Argiris Kirtzidis | bca33bf | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 2937 | |
| 2938 | assert(NumExprs == 1 && "Expected 1 expression"); |
| 2939 | // Set the init expression, handles conversions. |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 2940 | AddInitializerToDecl(Dcl, ExprArg(*this, Exprs.release()[0]), |
| 2941 | /*DirectInit=*/true); |
Argiris Kirtzidis | 9e55d46 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2942 | } |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2943 | |
Douglas Gregor | 6428e76 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 2944 | /// PerformInitializationByConstructor - Perform initialization by |
| 2945 | /// constructor (C++ [dcl.init]p14), which may occur as part of |
| 2946 | /// direct-initialization or copy-initialization. We are initializing |
| 2947 | /// an object of type @p ClassType with the given arguments @p |
| 2948 | /// Args. @p Loc is the location in the source code where the |
| 2949 | /// initializer occurs (e.g., a declaration, member initializer, |
| 2950 | /// functional cast, etc.) while @p Range covers the whole |
| 2951 | /// initialization. @p InitEntity is the entity being initialized, |
| 2952 | /// which may by the name of a declaration or a type. @p Kind is the |
| 2953 | /// kind of initialization we're performing, which affects whether |
| 2954 | /// explicit constructors will be considered. When successful, returns |
Douglas Gregor | 5870a95 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 2955 | /// the constructor that will be used to perform the initialization; |
Douglas Gregor | 6428e76 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 2956 | /// when the initialization fails, emits a diagnostic and returns |
| 2957 | /// null. |
Douglas Gregor | 5870a95 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 2958 | CXXConstructorDecl * |
Douglas Gregor | 6428e76 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 2959 | Sema::PerformInitializationByConstructor(QualType ClassType, |
| 2960 | Expr **Args, unsigned NumArgs, |
| 2961 | SourceLocation Loc, SourceRange Range, |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2962 | DeclarationName InitEntity, |
Douglas Gregor | 6428e76 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 2963 | InitializationKind Kind) { |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2964 | const RecordType *ClassRec = ClassType->getAs<RecordType>(); |
Douglas Gregor | 5870a95 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 2965 | assert(ClassRec && "Can only initialize a class type here"); |
| 2966 | |
| 2967 | // C++ [dcl.init]p14: |
| 2968 | // |
| 2969 | // If the initialization is direct-initialization, or if it is |
| 2970 | // copy-initialization where the cv-unqualified version of the |
| 2971 | // source type is the same class as, or a derived class of, the |
| 2972 | // class of the destination, constructors are considered. The |
| 2973 | // applicable constructors are enumerated (13.3.1.3), and the |
| 2974 | // best one is chosen through overload resolution (13.3). The |
| 2975 | // constructor so selected is called to initialize the object, |
| 2976 | // with the initializer expression(s) as its argument(s). If no |
| 2977 | // constructor applies, or the overload resolution is ambiguous, |
| 2978 | // the initialization is ill-formed. |
Douglas Gregor | 5870a95 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 2979 | const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(ClassRec->getDecl()); |
| 2980 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 6428e76 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 2981 | |
| 2982 | // Add constructors to the overload set. |
Douglas Gregor | b921383 | 2008-12-15 21:24:18 +0000 | [diff] [blame] | 2983 | DeclarationName ConstructorName |
| 2984 | = Context.DeclarationNames.getCXXConstructorName( |
| 2985 | Context.getCanonicalType(ClassType.getUnqualifiedType())); |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 2986 | DeclContext::lookup_const_iterator Con, ConEnd; |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2987 | for (llvm::tie(Con, ConEnd) = ClassDecl->lookup(ConstructorName); |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 2988 | Con != ConEnd; ++Con) { |
Douglas Gregor | 050cabf | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2989 | // Find the constructor (which may be a template). |
| 2990 | CXXConstructorDecl *Constructor = 0; |
| 2991 | FunctionTemplateDecl *ConstructorTmpl= dyn_cast<FunctionTemplateDecl>(*Con); |
| 2992 | if (ConstructorTmpl) |
| 2993 | Constructor |
| 2994 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 2995 | else |
| 2996 | Constructor = cast<CXXConstructorDecl>(*Con); |
| 2997 | |
Douglas Gregor | 6428e76 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 2998 | if ((Kind == IK_Direct) || |
Anders Carlsson | 9489457 | 2009-08-28 16:57:08 +0000 | [diff] [blame] | 2999 | (Kind == IK_Copy && |
| 3000 | Constructor->isConvertingConstructor(/*AllowExplicit=*/false)) || |
Douglas Gregor | 050cabf | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3001 | (Kind == IK_Default && Constructor->isDefaultConstructor())) { |
| 3002 | if (ConstructorTmpl) |
| 3003 | AddTemplateOverloadCandidate(ConstructorTmpl, false, 0, 0, |
| 3004 | Args, NumArgs, CandidateSet); |
| 3005 | else |
| 3006 | AddOverloadCandidate(Constructor, Args, NumArgs, CandidateSet); |
| 3007 | } |
Douglas Gregor | 6428e76 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 3008 | } |
| 3009 | |
Douglas Gregor | b921383 | 2008-12-15 21:24:18 +0000 | [diff] [blame] | 3010 | // FIXME: When we decide not to synthesize the implicitly-declared |
| 3011 | // constructors, we'll need to make them appear here. |
| 3012 | |
Douglas Gregor | 5870a95 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 3013 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | 9818926 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 3014 | switch (BestViableFunction(CandidateSet, Loc, Best)) { |
Douglas Gregor | 5870a95 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 3015 | case OR_Success: |
| 3016 | // We found a constructor. Return it. |
| 3017 | return cast<CXXConstructorDecl>(Best->Function); |
| 3018 | |
| 3019 | case OR_No_Viable_Function: |
Douglas Gregor | 538a4c2 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 3020 | if (InitEntity) |
| 3021 | Diag(Loc, diag::err_ovl_no_viable_function_in_init) |
Chris Lattner | 4a52611 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 3022 | << InitEntity << Range; |
Douglas Gregor | 538a4c2 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 3023 | else |
| 3024 | Diag(Loc, diag::err_ovl_no_viable_function_in_init) |
Chris Lattner | 4a52611 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 3025 | << ClassType << Range; |
Sebastian Redl | fd9f2ac | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 3026 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
Douglas Gregor | 5870a95 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 3027 | return 0; |
| 3028 | |
| 3029 | case OR_Ambiguous: |
Douglas Gregor | 538a4c2 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 3030 | if (InitEntity) |
| 3031 | Diag(Loc, diag::err_ovl_ambiguous_init) << InitEntity << Range; |
| 3032 | else |
| 3033 | Diag(Loc, diag::err_ovl_ambiguous_init) << ClassType << Range; |
Douglas Gregor | 5870a95 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 3034 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 3035 | return 0; |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3036 | |
| 3037 | case OR_Deleted: |
| 3038 | if (InitEntity) |
| 3039 | Diag(Loc, diag::err_ovl_deleted_init) |
| 3040 | << Best->Function->isDeleted() |
| 3041 | << InitEntity << Range; |
| 3042 | else |
| 3043 | Diag(Loc, diag::err_ovl_deleted_init) |
| 3044 | << Best->Function->isDeleted() |
| 3045 | << InitEntity << Range; |
| 3046 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 3047 | return 0; |
Douglas Gregor | 5870a95 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 3048 | } |
| 3049 | |
| 3050 | return 0; |
| 3051 | } |
| 3052 | |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3053 | /// CompareReferenceRelationship - Compare the two types T1 and T2 to |
| 3054 | /// determine whether they are reference-related, |
| 3055 | /// reference-compatible, reference-compatible with added |
| 3056 | /// qualification, or incompatible, for use in C++ initialization by |
| 3057 | /// reference (C++ [dcl.ref.init]p4). Neither type can be a reference |
| 3058 | /// type, and the first type (T1) is the pointee type of the reference |
| 3059 | /// type being initialized. |
| 3060 | Sema::ReferenceCompareResult |
Douglas Gregor | 2aecd1f | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3061 | Sema::CompareReferenceRelationship(QualType T1, QualType T2, |
| 3062 | bool& DerivedToBase) { |
Sebastian Redl | ce6fff0 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3063 | assert(!T1->isReferenceType() && |
| 3064 | "T1 must be the pointee type of the reference type"); |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3065 | assert(!T2->isReferenceType() && "T2 cannot be a reference type"); |
| 3066 | |
| 3067 | T1 = Context.getCanonicalType(T1); |
| 3068 | T2 = Context.getCanonicalType(T2); |
| 3069 | QualType UnqualT1 = T1.getUnqualifiedType(); |
| 3070 | QualType UnqualT2 = T2.getUnqualifiedType(); |
| 3071 | |
| 3072 | // C++ [dcl.init.ref]p4: |
Eli Friedman | d5a72f0 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 3073 | // Given types "cv1 T1" and "cv2 T2," "cv1 T1" is |
| 3074 | // reference-related to "cv2 T2" if T1 is the same type as T2, or |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3075 | // T1 is a base class of T2. |
Douglas Gregor | 2aecd1f | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3076 | if (UnqualT1 == UnqualT2) |
| 3077 | DerivedToBase = false; |
| 3078 | else if (IsDerivedFrom(UnqualT2, UnqualT1)) |
| 3079 | DerivedToBase = true; |
| 3080 | else |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3081 | return Ref_Incompatible; |
| 3082 | |
| 3083 | // At this point, we know that T1 and T2 are reference-related (at |
| 3084 | // least). |
| 3085 | |
| 3086 | // C++ [dcl.init.ref]p4: |
Eli Friedman | d5a72f0 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 3087 | // "cv1 T1" is reference-compatible with "cv2 T2" if T1 is |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3088 | // reference-related to T2 and cv1 is the same cv-qualification |
| 3089 | // as, or greater cv-qualification than, cv2. For purposes of |
| 3090 | // overload resolution, cases for which cv1 is greater |
| 3091 | // cv-qualification than cv2 are identified as |
| 3092 | // reference-compatible with added qualification (see 13.3.3.2). |
| 3093 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 3094 | return Ref_Compatible; |
| 3095 | else if (T1.isMoreQualifiedThan(T2)) |
| 3096 | return Ref_Compatible_With_Added_Qualification; |
| 3097 | else |
| 3098 | return Ref_Related; |
| 3099 | } |
| 3100 | |
| 3101 | /// CheckReferenceInit - Check the initialization of a reference |
| 3102 | /// variable with the given initializer (C++ [dcl.init.ref]). Init is |
| 3103 | /// the initializer (either a simple initializer or an initializer |
Douglas Gregor | b3dff48 | 2008-10-29 23:31:03 +0000 | [diff] [blame] | 3104 | /// list), and DeclType is the type of the declaration. When ICS is |
| 3105 | /// non-null, this routine will compute the implicit conversion |
| 3106 | /// sequence according to C++ [over.ics.ref] and will not produce any |
| 3107 | /// diagnostics; when ICS is null, it will emit diagnostics when any |
| 3108 | /// errors are found. Either way, a return value of true indicates |
| 3109 | /// that there was a failure, a return value of false indicates that |
| 3110 | /// the reference initialization succeeded. |
Douglas Gregor | a3b34bb | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3111 | /// |
| 3112 | /// When @p SuppressUserConversions, user-defined conversions are |
| 3113 | /// suppressed. |
Douglas Gregor | 6214d8a | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3114 | /// When @p AllowExplicit, we also permit explicit user-defined |
| 3115 | /// conversion functions. |
Sebastian Redl | a55834a | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 3116 | /// When @p ForceRValue, we unconditionally treat the initializer as an rvalue. |
Douglas Gregor | 2aecd1f | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3117 | bool |
Sebastian Redl | bd26196 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3118 | Sema::CheckReferenceInit(Expr *&Init, QualType DeclType, |
Douglas Gregor | 6214d8a | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3119 | bool SuppressUserConversions, |
Anders Carlsson | 8f809f9 | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 3120 | bool AllowExplicit, bool ForceRValue, |
| 3121 | ImplicitConversionSequence *ICS) { |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3122 | assert(DeclType->isReferenceType() && "Reference init needs a reference"); |
| 3123 | |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3124 | QualType T1 = DeclType->getAs<ReferenceType>()->getPointeeType(); |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3125 | QualType T2 = Init->getType(); |
| 3126 | |
Douglas Gregor | 45014fd | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3127 | // If the initializer is the address of an overloaded function, try |
| 3128 | // to resolve the overloaded function. If all goes well, T2 is the |
| 3129 | // type of the resulting function. |
Douglas Gregor | 00fe3f6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3130 | if (Context.getCanonicalType(T2) == Context.OverloadTy) { |
Douglas Gregor | 45014fd | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3131 | FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Init, DeclType, |
| 3132 | ICS != 0); |
| 3133 | if (Fn) { |
| 3134 | // Since we're performing this reference-initialization for |
| 3135 | // real, update the initializer with the resulting function. |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3136 | if (!ICS) { |
| 3137 | if (DiagnoseUseOfDecl(Fn, Init->getSourceRange().getBegin())) |
| 3138 | return true; |
| 3139 | |
Douglas Gregor | 45014fd | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3140 | FixOverloadedFunctionReference(Init, Fn); |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3141 | } |
Douglas Gregor | 45014fd | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3142 | |
| 3143 | T2 = Fn->getType(); |
| 3144 | } |
| 3145 | } |
| 3146 | |
Douglas Gregor | 2aecd1f | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3147 | // Compute some basic properties of the types and the initializer. |
Sebastian Redl | ce6fff0 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3148 | bool isRValRef = DeclType->isRValueReferenceType(); |
Douglas Gregor | 2aecd1f | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3149 | bool DerivedToBase = false; |
Sebastian Redl | a55834a | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 3150 | Expr::isLvalueResult InitLvalue = ForceRValue ? Expr::LV_InvalidExpression : |
| 3151 | Init->isLvalue(Context); |
Douglas Gregor | 2aecd1f | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3152 | ReferenceCompareResult RefRelationship |
| 3153 | = CompareReferenceRelationship(T1, T2, DerivedToBase); |
| 3154 | |
| 3155 | // Most paths end in a failed conversion. |
| 3156 | if (ICS) |
| 3157 | ICS->ConversionKind = ImplicitConversionSequence::BadConversion; |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3158 | |
| 3159 | // C++ [dcl.init.ref]p5: |
Eli Friedman | d5a72f0 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 3160 | // A reference to type "cv1 T1" is initialized by an expression |
| 3161 | // of type "cv2 T2" as follows: |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3162 | |
| 3163 | // -- If the initializer expression |
| 3164 | |
Sebastian Redl | dfc3033 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 3165 | // Rvalue references cannot bind to lvalues (N2812). |
| 3166 | // There is absolutely no situation where they can. In particular, note that |
| 3167 | // this is ill-formed, even if B has a user-defined conversion to A&&: |
| 3168 | // B b; |
| 3169 | // A&& r = b; |
| 3170 | if (isRValRef && InitLvalue == Expr::LV_Valid) { |
| 3171 | if (!ICS) |
| 3172 | Diag(Init->getSourceRange().getBegin(), diag::err_lvalue_to_rvalue_ref) |
| 3173 | << Init->getSourceRange(); |
| 3174 | return true; |
| 3175 | } |
| 3176 | |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3177 | bool BindsDirectly = false; |
Eli Friedman | d5a72f0 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 3178 | // -- is an lvalue (but is not a bit-field), and "cv1 T1" is |
| 3179 | // reference-compatible with "cv2 T2," or |
Douglas Gregor | 2aecd1f | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3180 | // |
| 3181 | // Note that the bit-field check is skipped if we are just computing |
| 3182 | // the implicit conversion sequence (C++ [over.best.ics]p2). |
Douglas Gregor | 531434b | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3183 | if (InitLvalue == Expr::LV_Valid && (ICS || !Init->getBitField()) && |
Douglas Gregor | 2aecd1f | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3184 | RefRelationship >= Ref_Compatible_With_Added_Qualification) { |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3185 | BindsDirectly = true; |
| 3186 | |
Douglas Gregor | 2aecd1f | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3187 | if (ICS) { |
| 3188 | // C++ [over.ics.ref]p1: |
| 3189 | // When a parameter of reference type binds directly (8.5.3) |
| 3190 | // to an argument expression, the implicit conversion sequence |
| 3191 | // is the identity conversion, unless the argument expression |
| 3192 | // has a type that is a derived class of the parameter type, |
| 3193 | // in which case the implicit conversion sequence is a |
| 3194 | // derived-to-base Conversion (13.3.3.1). |
| 3195 | ICS->ConversionKind = ImplicitConversionSequence::StandardConversion; |
| 3196 | ICS->Standard.First = ICK_Identity; |
| 3197 | ICS->Standard.Second = DerivedToBase? ICK_Derived_To_Base : ICK_Identity; |
| 3198 | ICS->Standard.Third = ICK_Identity; |
| 3199 | ICS->Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 3200 | ICS->Standard.ToTypePtr = T1.getAsOpaquePtr(); |
Douglas Gregor | 0e34338 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3201 | ICS->Standard.ReferenceBinding = true; |
| 3202 | ICS->Standard.DirectBinding = true; |
Sebastian Redl | dfc3033 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 3203 | ICS->Standard.RRefBinding = false; |
Sebastian Redl | d316913 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 3204 | ICS->Standard.CopyConstructor = 0; |
Douglas Gregor | 2aecd1f | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3205 | |
| 3206 | // Nothing more to do: the inaccessibility/ambiguity check for |
| 3207 | // derived-to-base conversions is suppressed when we're |
| 3208 | // computing the implicit conversion sequence (C++ |
| 3209 | // [over.best.ics]p2). |
| 3210 | return false; |
| 3211 | } else { |
| 3212 | // Perform the conversion. |
Mike Stump | e127ae3 | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3213 | // FIXME: Binding to a subobject of the lvalue is going to require more |
| 3214 | // AST annotation than this. |
Anders Carlsson | 8518694 | 2009-07-31 01:23:52 +0000 | [diff] [blame] | 3215 | ImpCastExprToType(Init, T1, CastExpr::CK_Unknown, /*isLvalue=*/true); |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3216 | } |
| 3217 | } |
| 3218 | |
| 3219 | // -- has a class type (i.e., T2 is a class type) and can be |
Eli Friedman | d5a72f0 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 3220 | // implicitly converted to an lvalue of type "cv3 T3," |
| 3221 | // where "cv1 T1" is reference-compatible with "cv3 T3" |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3222 | // 92) (this conversion is selected by enumerating the |
| 3223 | // applicable conversion functions (13.3.1.6) and choosing |
| 3224 | // the best one through overload resolution (13.3)), |
Douglas Gregor | b35c799 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3225 | if (!isRValRef && !SuppressUserConversions && T2->isRecordType() && |
| 3226 | !RequireCompleteType(SourceLocation(), T2, 0)) { |
Douglas Gregor | e6985fe | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 3227 | // FIXME: Look for conversions in base classes! |
| 3228 | CXXRecordDecl *T2RecordDecl |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3229 | = dyn_cast<CXXRecordDecl>(T2->getAs<RecordType>()->getDecl()); |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3230 | |
Douglas Gregor | e6985fe | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 3231 | OverloadCandidateSet CandidateSet; |
| 3232 | OverloadedFunctionDecl *Conversions |
| 3233 | = T2RecordDecl->getConversionFunctions(); |
| 3234 | for (OverloadedFunctionDecl::function_iterator Func |
| 3235 | = Conversions->function_begin(); |
| 3236 | Func != Conversions->function_end(); ++Func) { |
Douglas Gregor | 8c860df | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3237 | FunctionTemplateDecl *ConvTemplate |
| 3238 | = dyn_cast<FunctionTemplateDecl>(*Func); |
| 3239 | CXXConversionDecl *Conv; |
| 3240 | if (ConvTemplate) |
| 3241 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 3242 | else |
| 3243 | Conv = cast<CXXConversionDecl>(*Func); |
Sebastian Redl | 16ac38f | 2009-03-22 21:28:55 +0000 | [diff] [blame] | 3244 | |
Douglas Gregor | e6985fe | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 3245 | // If the conversion function doesn't return a reference type, |
| 3246 | // it can't be considered for this conversion. |
Sebastian Redl | ce6fff0 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3247 | if (Conv->getConversionType()->isLValueReferenceType() && |
Douglas Gregor | 8c860df | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3248 | (AllowExplicit || !Conv->isExplicit())) { |
| 3249 | if (ConvTemplate) |
| 3250 | AddTemplateConversionCandidate(ConvTemplate, Init, DeclType, |
| 3251 | CandidateSet); |
| 3252 | else |
| 3253 | AddConversionCandidate(Conv, Init, DeclType, CandidateSet); |
| 3254 | } |
Douglas Gregor | e6985fe | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 3255 | } |
| 3256 | |
| 3257 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | 9818926 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 3258 | switch (BestViableFunction(CandidateSet, Init->getLocStart(), Best)) { |
Douglas Gregor | e6985fe | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 3259 | case OR_Success: |
| 3260 | // This is a direct binding. |
| 3261 | BindsDirectly = true; |
| 3262 | |
| 3263 | if (ICS) { |
| 3264 | // C++ [over.ics.ref]p1: |
| 3265 | // |
| 3266 | // [...] If the parameter binds directly to the result of |
| 3267 | // applying a conversion function to the argument |
| 3268 | // expression, the implicit conversion sequence is a |
| 3269 | // user-defined conversion sequence (13.3.3.1.2), with the |
| 3270 | // second standard conversion sequence either an identity |
| 3271 | // conversion or, if the conversion function returns an |
| 3272 | // entity of a type that is a derived class of the parameter |
| 3273 | // type, a derived-to-base Conversion. |
| 3274 | ICS->ConversionKind = ImplicitConversionSequence::UserDefinedConversion; |
| 3275 | ICS->UserDefined.Before = Best->Conversions[0].Standard; |
| 3276 | ICS->UserDefined.After = Best->FinalConversion; |
| 3277 | ICS->UserDefined.ConversionFunction = Best->Function; |
| 3278 | assert(ICS->UserDefined.After.ReferenceBinding && |
| 3279 | ICS->UserDefined.After.DirectBinding && |
| 3280 | "Expected a direct reference binding!"); |
| 3281 | return false; |
| 3282 | } else { |
| 3283 | // Perform the conversion. |
Mike Stump | e127ae3 | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3284 | // FIXME: Binding to a subobject of the lvalue is going to require more |
| 3285 | // AST annotation than this. |
Anders Carlsson | 8518694 | 2009-07-31 01:23:52 +0000 | [diff] [blame] | 3286 | ImpCastExprToType(Init, T1, CastExpr::CK_Unknown, /*isLvalue=*/true); |
Douglas Gregor | e6985fe | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 3287 | } |
| 3288 | break; |
| 3289 | |
| 3290 | case OR_Ambiguous: |
| 3291 | assert(false && "Ambiguous reference binding conversions not implemented."); |
| 3292 | return true; |
| 3293 | |
| 3294 | case OR_No_Viable_Function: |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3295 | case OR_Deleted: |
| 3296 | // There was no suitable conversion, or we found a deleted |
| 3297 | // conversion; continue with other checks. |
Douglas Gregor | e6985fe | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 3298 | break; |
| 3299 | } |
| 3300 | } |
| 3301 | |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3302 | if (BindsDirectly) { |
| 3303 | // C++ [dcl.init.ref]p4: |
| 3304 | // [...] In all cases where the reference-related or |
| 3305 | // reference-compatible relationship of two types is used to |
| 3306 | // establish the validity of a reference binding, and T1 is a |
| 3307 | // base class of T2, a program that necessitates such a binding |
| 3308 | // is ill-formed if T1 is an inaccessible (clause 11) or |
| 3309 | // ambiguous (10.2) base class of T2. |
| 3310 | // |
| 3311 | // Note that we only check this condition when we're allowed to |
| 3312 | // complain about errors, because we should not be checking for |
| 3313 | // ambiguity (or inaccessibility) unless the reference binding |
| 3314 | // actually happens. |
Douglas Gregor | 2aecd1f | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3315 | if (DerivedToBase) |
| 3316 | return CheckDerivedToBaseConversion(T2, T1, |
| 3317 | Init->getSourceRange().getBegin(), |
| 3318 | Init->getSourceRange()); |
| 3319 | else |
| 3320 | return false; |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3321 | } |
| 3322 | |
| 3323 | // -- Otherwise, the reference shall be to a non-volatile const |
Sebastian Redl | dfc3033 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 3324 | // type (i.e., cv1 shall be const), or the reference shall be an |
| 3325 | // rvalue reference and the initializer expression shall be an rvalue. |
Sebastian Redl | ce6fff0 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3326 | if (!isRValRef && T1.getCVRQualifiers() != QualType::Const) { |
Douglas Gregor | 2aecd1f | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3327 | if (!ICS) |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3328 | Diag(Init->getSourceRange().getBegin(), |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3329 | diag::err_not_reference_to_const_init) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3330 | << T1 << (InitLvalue != Expr::LV_Valid? "temporary" : "value") |
| 3331 | << T2 << Init->getSourceRange(); |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3332 | return true; |
| 3333 | } |
| 3334 | |
| 3335 | // -- If the initializer expression is an rvalue, with T2 a |
Eli Friedman | d5a72f0 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 3336 | // class type, and "cv1 T1" is reference-compatible with |
| 3337 | // "cv2 T2," the reference is bound in one of the |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3338 | // following ways (the choice is implementation-defined): |
| 3339 | // |
| 3340 | // -- The reference is bound to the object represented by |
| 3341 | // the rvalue (see 3.10) or to a sub-object within that |
| 3342 | // object. |
| 3343 | // |
Eli Friedman | d5a72f0 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 3344 | // -- A temporary of type "cv1 T2" [sic] is created, and |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3345 | // a constructor is called to copy the entire rvalue |
| 3346 | // object into the temporary. The reference is bound to |
| 3347 | // the temporary or to a sub-object within the |
| 3348 | // temporary. |
| 3349 | // |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3350 | // The constructor that would be used to make the copy |
| 3351 | // shall be callable whether or not the copy is actually |
| 3352 | // done. |
| 3353 | // |
Sebastian Redl | dfc3033 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 3354 | // Note that C++0x [dcl.init.ref]p5 takes away this implementation |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3355 | // freedom, so we will always take the first option and never build |
| 3356 | // a temporary in this case. FIXME: We will, however, have to check |
| 3357 | // for the presence of a copy constructor in C++98/03 mode. |
| 3358 | if (InitLvalue != Expr::LV_Valid && T2->isRecordType() && |
Douglas Gregor | 2aecd1f | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3359 | RefRelationship >= Ref_Compatible_With_Added_Qualification) { |
| 3360 | if (ICS) { |
| 3361 | ICS->ConversionKind = ImplicitConversionSequence::StandardConversion; |
| 3362 | ICS->Standard.First = ICK_Identity; |
| 3363 | ICS->Standard.Second = DerivedToBase? ICK_Derived_To_Base : ICK_Identity; |
| 3364 | ICS->Standard.Third = ICK_Identity; |
| 3365 | ICS->Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 3366 | ICS->Standard.ToTypePtr = T1.getAsOpaquePtr(); |
Douglas Gregor | 0e34338 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3367 | ICS->Standard.ReferenceBinding = true; |
Sebastian Redl | dfc3033 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 3368 | ICS->Standard.DirectBinding = false; |
| 3369 | ICS->Standard.RRefBinding = isRValRef; |
Sebastian Redl | d316913 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 3370 | ICS->Standard.CopyConstructor = 0; |
Douglas Gregor | 2aecd1f | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3371 | } else { |
Mike Stump | e127ae3 | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3372 | // FIXME: Binding to a subobject of the rvalue is going to require more |
| 3373 | // AST annotation than this. |
Anders Carlsson | 8518694 | 2009-07-31 01:23:52 +0000 | [diff] [blame] | 3374 | ImpCastExprToType(Init, T1, CastExpr::CK_Unknown, /*isLvalue=*/false); |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3375 | } |
| 3376 | return false; |
| 3377 | } |
| 3378 | |
Eli Friedman | d5a72f0 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 3379 | // -- Otherwise, a temporary of type "cv1 T1" is created and |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3380 | // initialized from the initializer expression using the |
| 3381 | // rules for a non-reference copy initialization (8.5). The |
| 3382 | // reference is then bound to the temporary. If T1 is |
| 3383 | // reference-related to T2, cv1 must be the same |
| 3384 | // cv-qualification as, or greater cv-qualification than, |
| 3385 | // cv2; otherwise, the program is ill-formed. |
| 3386 | if (RefRelationship == Ref_Related) { |
| 3387 | // If cv1 == cv2 or cv1 is a greater cv-qualified than cv2, then |
| 3388 | // we would be reference-compatible or reference-compatible with |
| 3389 | // added qualification. But that wasn't the case, so the reference |
| 3390 | // initialization fails. |
Douglas Gregor | 2aecd1f | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3391 | if (!ICS) |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3392 | Diag(Init->getSourceRange().getBegin(), |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3393 | diag::err_reference_init_drops_quals) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3394 | << T1 << (InitLvalue != Expr::LV_Valid? "temporary" : "value") |
| 3395 | << T2 << Init->getSourceRange(); |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3396 | return true; |
| 3397 | } |
| 3398 | |
Douglas Gregor | b206cc4 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 3399 | // If at least one of the types is a class type, the types are not |
| 3400 | // related, and we aren't allowed any user conversions, the |
| 3401 | // reference binding fails. This case is important for breaking |
| 3402 | // recursion, since TryImplicitConversion below will attempt to |
| 3403 | // create a temporary through the use of a copy constructor. |
| 3404 | if (SuppressUserConversions && RefRelationship == Ref_Incompatible && |
| 3405 | (T1->isRecordType() || T2->isRecordType())) { |
| 3406 | if (!ICS) |
| 3407 | Diag(Init->getSourceRange().getBegin(), |
| 3408 | diag::err_typecheck_convert_incompatible) |
| 3409 | << DeclType << Init->getType() << "initializing" << Init->getSourceRange(); |
| 3410 | return true; |
| 3411 | } |
| 3412 | |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3413 | // Actually try to convert the initializer to T1. |
Douglas Gregor | 2aecd1f | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3414 | if (ICS) { |
Sebastian Redl | dfc3033 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 3415 | // C++ [over.ics.ref]p2: |
| 3416 | // |
| 3417 | // When a parameter of reference type is not bound directly to |
| 3418 | // an argument expression, the conversion sequence is the one |
| 3419 | // required to convert the argument expression to the |
| 3420 | // underlying type of the reference according to |
| 3421 | // 13.3.3.1. Conceptually, this conversion sequence corresponds |
| 3422 | // to copy-initializing a temporary of the underlying type with |
| 3423 | // the argument expression. Any difference in top-level |
| 3424 | // cv-qualification is subsumed by the initialization itself |
| 3425 | // and does not constitute a conversion. |
Anders Carlsson | 6ed4a61 | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 3426 | *ICS = TryImplicitConversion(Init, T1, SuppressUserConversions, |
| 3427 | /*AllowExplicit=*/false, |
Anders Carlsson | 8e4c169 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 3428 | /*ForceRValue=*/false, |
| 3429 | /*InOverloadResolution=*/false); |
Anders Carlsson | 6ed4a61 | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 3430 | |
Sebastian Redl | dfc3033 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 3431 | // Of course, that's still a reference binding. |
| 3432 | if (ICS->ConversionKind == ImplicitConversionSequence::StandardConversion) { |
| 3433 | ICS->Standard.ReferenceBinding = true; |
| 3434 | ICS->Standard.RRefBinding = isRValRef; |
| 3435 | } else if(ICS->ConversionKind == |
| 3436 | ImplicitConversionSequence::UserDefinedConversion) { |
| 3437 | ICS->UserDefined.After.ReferenceBinding = true; |
| 3438 | ICS->UserDefined.After.RRefBinding = isRValRef; |
| 3439 | } |
Douglas Gregor | 2aecd1f | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3440 | return ICS->ConversionKind == ImplicitConversionSequence::BadConversion; |
| 3441 | } else { |
Douglas Gregor | 6fd3557 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 3442 | return PerformImplicitConversion(Init, T1, "initializing"); |
Douglas Gregor | 2aecd1f | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3443 | } |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3444 | } |
Douglas Gregor | e60e5d3 | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3445 | |
| 3446 | /// CheckOverloadedOperatorDeclaration - Check whether the declaration |
| 3447 | /// of this overloaded operator is well-formed. If so, returns false; |
| 3448 | /// otherwise, emits appropriate diagnostics and returns true. |
| 3449 | bool Sema::CheckOverloadedOperatorDeclaration(FunctionDecl *FnDecl) { |
Douglas Gregor | 682a8cf | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 3450 | assert(FnDecl && FnDecl->isOverloadedOperator() && |
Douglas Gregor | e60e5d3 | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3451 | "Expected an overloaded operator declaration"); |
| 3452 | |
Douglas Gregor | e60e5d3 | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3453 | OverloadedOperatorKind Op = FnDecl->getOverloadedOperator(); |
| 3454 | |
| 3455 | // C++ [over.oper]p5: |
| 3456 | // The allocation and deallocation functions, operator new, |
| 3457 | // operator new[], operator delete and operator delete[], are |
| 3458 | // described completely in 3.7.3. The attributes and restrictions |
| 3459 | // found in the rest of this subclause do not apply to them unless |
| 3460 | // explicitly stated in 3.7.3. |
Mike Stump | e127ae3 | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3461 | // FIXME: Write a separate routine for checking this. For now, just allow it. |
Douglas Gregor | e60e5d3 | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3462 | if (Op == OO_New || Op == OO_Array_New || |
| 3463 | Op == OO_Delete || Op == OO_Array_Delete) |
| 3464 | return false; |
| 3465 | |
| 3466 | // C++ [over.oper]p6: |
| 3467 | // An operator function shall either be a non-static member |
| 3468 | // function or be a non-member function and have at least one |
| 3469 | // parameter whose type is a class, a reference to a class, an |
| 3470 | // enumeration, or a reference to an enumeration. |
Douglas Gregor | 682a8cf | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 3471 | if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 3472 | if (MethodDecl->isStatic()) |
| 3473 | return Diag(FnDecl->getLocation(), |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3474 | diag::err_operator_overload_static) << FnDecl->getDeclName(); |
Douglas Gregor | e60e5d3 | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3475 | } else { |
| 3476 | bool ClassOrEnumParam = false; |
Douglas Gregor | 682a8cf | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 3477 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(), |
| 3478 | ParamEnd = FnDecl->param_end(); |
| 3479 | Param != ParamEnd; ++Param) { |
| 3480 | QualType ParamType = (*Param)->getType().getNonReferenceType(); |
Eli Friedman | a73d6b1 | 2009-06-27 05:59:59 +0000 | [diff] [blame] | 3481 | if (ParamType->isDependentType() || ParamType->isRecordType() || |
| 3482 | ParamType->isEnumeralType()) { |
Douglas Gregor | e60e5d3 | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3483 | ClassOrEnumParam = true; |
| 3484 | break; |
| 3485 | } |
| 3486 | } |
| 3487 | |
Douglas Gregor | 682a8cf | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 3488 | if (!ClassOrEnumParam) |
| 3489 | return Diag(FnDecl->getLocation(), |
Chris Lattner | 10f2c2e | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 3490 | diag::err_operator_overload_needs_class_or_enum) |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3491 | << FnDecl->getDeclName(); |
Douglas Gregor | e60e5d3 | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3492 | } |
| 3493 | |
| 3494 | // C++ [over.oper]p8: |
| 3495 | // An operator function cannot have default arguments (8.3.6), |
| 3496 | // except where explicitly stated below. |
| 3497 | // |
| 3498 | // Only the function-call operator allows default arguments |
| 3499 | // (C++ [over.call]p1). |
| 3500 | if (Op != OO_Call) { |
| 3501 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 3502 | Param != FnDecl->param_end(); ++Param) { |
Douglas Gregor | 62ae25a | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 3503 | if ((*Param)->hasUnparsedDefaultArg()) |
| 3504 | return Diag((*Param)->getLocation(), |
| 3505 | diag::err_operator_overload_default_arg) |
| 3506 | << FnDecl->getDeclName(); |
| 3507 | else if (Expr *DefArg = (*Param)->getDefaultArg()) |
Douglas Gregor | 682a8cf | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 3508 | return Diag((*Param)->getLocation(), |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 3509 | diag::err_operator_overload_default_arg) |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3510 | << FnDecl->getDeclName() << DefArg->getSourceRange(); |
Douglas Gregor | e60e5d3 | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3511 | } |
| 3512 | } |
| 3513 | |
Douglas Gregor | 9c6210b | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 3514 | static const bool OperatorUses[NUM_OVERLOADED_OPERATORS][3] = { |
| 3515 | { false, false, false } |
| 3516 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 3517 | , { Unary, Binary, MemberOnly } |
| 3518 | #include "clang/Basic/OperatorKinds.def" |
| 3519 | }; |
Douglas Gregor | e60e5d3 | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3520 | |
Douglas Gregor | 9c6210b | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 3521 | bool CanBeUnaryOperator = OperatorUses[Op][0]; |
| 3522 | bool CanBeBinaryOperator = OperatorUses[Op][1]; |
| 3523 | bool MustBeMemberOperator = OperatorUses[Op][2]; |
Douglas Gregor | e60e5d3 | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3524 | |
| 3525 | // C++ [over.oper]p8: |
| 3526 | // [...] Operator functions cannot have more or fewer parameters |
| 3527 | // than the number required for the corresponding operator, as |
| 3528 | // described in the rest of this subclause. |
Douglas Gregor | 682a8cf | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 3529 | unsigned NumParams = FnDecl->getNumParams() |
| 3530 | + (isa<CXXMethodDecl>(FnDecl)? 1 : 0); |
Douglas Gregor | e60e5d3 | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3531 | if (Op != OO_Call && |
| 3532 | ((NumParams == 1 && !CanBeUnaryOperator) || |
| 3533 | (NumParams == 2 && !CanBeBinaryOperator) || |
| 3534 | (NumParams < 1) || (NumParams > 2))) { |
| 3535 | // We have the wrong number of parameters. |
Chris Lattner | bb00233 | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 3536 | unsigned ErrorKind; |
Douglas Gregor | 9c6210b | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 3537 | if (CanBeUnaryOperator && CanBeBinaryOperator) { |
Chris Lattner | bb00233 | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 3538 | ErrorKind = 2; // 2 -> unary or binary. |
Douglas Gregor | 9c6210b | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 3539 | } else if (CanBeUnaryOperator) { |
Chris Lattner | bb00233 | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 3540 | ErrorKind = 0; // 0 -> unary |
Douglas Gregor | 9c6210b | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 3541 | } else { |
Chris Lattner | a7021ee | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 3542 | assert(CanBeBinaryOperator && |
| 3543 | "All non-call overloaded operators are unary or binary!"); |
Chris Lattner | bb00233 | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 3544 | ErrorKind = 1; // 1 -> binary |
Douglas Gregor | 9c6210b | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 3545 | } |
Douglas Gregor | e60e5d3 | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3546 | |
Chris Lattner | bb00233 | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 3547 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_must_be) |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3548 | << FnDecl->getDeclName() << NumParams << ErrorKind; |
Douglas Gregor | e60e5d3 | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3549 | } |
Sebastian Redl | 39c0f6f | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 3550 | |
Douglas Gregor | 682a8cf | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 3551 | // Overloaded operators other than operator() cannot be variadic. |
| 3552 | if (Op != OO_Call && |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3553 | FnDecl->getType()->getAsFunctionProtoType()->isVariadic()) { |
Chris Lattner | 10f2c2e | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 3554 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_variadic) |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3555 | << FnDecl->getDeclName(); |
Douglas Gregor | e60e5d3 | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3556 | } |
| 3557 | |
| 3558 | // Some operators must be non-static member functions. |
Douglas Gregor | 682a8cf | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 3559 | if (MustBeMemberOperator && !isa<CXXMethodDecl>(FnDecl)) { |
| 3560 | return Diag(FnDecl->getLocation(), |
Chris Lattner | 10f2c2e | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 3561 | diag::err_operator_overload_must_be_member) |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3562 | << FnDecl->getDeclName(); |
Douglas Gregor | e60e5d3 | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3563 | } |
| 3564 | |
| 3565 | // C++ [over.inc]p1: |
| 3566 | // The user-defined function called operator++ implements the |
| 3567 | // prefix and postfix ++ operator. If this function is a member |
| 3568 | // function with no parameters, or a non-member function with one |
| 3569 | // parameter of class or enumeration type, it defines the prefix |
| 3570 | // increment operator ++ for objects of that type. If the function |
| 3571 | // is a member function with one parameter (which shall be of type |
| 3572 | // int) or a non-member function with two parameters (the second |
| 3573 | // of which shall be of type int), it defines the postfix |
| 3574 | // increment operator ++ for objects of that type. |
| 3575 | if ((Op == OO_PlusPlus || Op == OO_MinusMinus) && NumParams == 2) { |
| 3576 | ParmVarDecl *LastParam = FnDecl->getParamDecl(FnDecl->getNumParams() - 1); |
| 3577 | bool ParamIsInt = false; |
| 3578 | if (const BuiltinType *BT = LastParam->getType()->getAsBuiltinType()) |
| 3579 | ParamIsInt = BT->getKind() == BuiltinType::Int; |
| 3580 | |
Chris Lattner | a7021ee | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 3581 | if (!ParamIsInt) |
| 3582 | return Diag(LastParam->getLocation(), |
| 3583 | diag::err_operator_overload_post_incdec_must_be_int) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3584 | << LastParam->getType() << (Op == OO_MinusMinus); |
Douglas Gregor | e60e5d3 | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3585 | } |
| 3586 | |
Sebastian Redl | 39c0f6f | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 3587 | // Notify the class if it got an assignment operator. |
| 3588 | if (Op == OO_Equal) { |
| 3589 | // Would have returned earlier otherwise. |
| 3590 | assert(isa<CXXMethodDecl>(FnDecl) && |
| 3591 | "Overloaded = not member, but not filtered."); |
| 3592 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
Fariborz Jahanian | 9da58e4 | 2009-08-13 21:09:41 +0000 | [diff] [blame] | 3593 | Method->setCopyAssignment(true); |
Sebastian Redl | 39c0f6f | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 3594 | Method->getParent()->addedAssignmentOperator(Context, Method); |
| 3595 | } |
| 3596 | |
Douglas Gregor | 682a8cf | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 3597 | return false; |
Douglas Gregor | e60e5d3 | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3598 | } |
Chris Lattner | f58d52b | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 3599 | |
Douglas Gregor | d802838 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3600 | /// ActOnStartLinkageSpecification - Parsed the beginning of a C++ |
| 3601 | /// linkage specification, including the language and (if present) |
| 3602 | /// the '{'. ExternLoc is the location of the 'extern', LangLoc is |
| 3603 | /// the location of the language string literal, which is provided |
| 3604 | /// by Lang/StrSize. LBraceLoc, if valid, provides the location of |
| 3605 | /// the '{' brace. Otherwise, this linkage specification does not |
| 3606 | /// have any braces. |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3607 | Sema::DeclPtrTy Sema::ActOnStartLinkageSpecification(Scope *S, |
| 3608 | SourceLocation ExternLoc, |
| 3609 | SourceLocation LangLoc, |
| 3610 | const char *Lang, |
| 3611 | unsigned StrSize, |
| 3612 | SourceLocation LBraceLoc) { |
Chris Lattner | 9bb9abf | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 3613 | LinkageSpecDecl::LanguageIDs Language; |
| 3614 | if (strncmp(Lang, "\"C\"", StrSize) == 0) |
| 3615 | Language = LinkageSpecDecl::lang_c; |
| 3616 | else if (strncmp(Lang, "\"C++\"", StrSize) == 0) |
| 3617 | Language = LinkageSpecDecl::lang_cxx; |
| 3618 | else { |
Douglas Gregor | d802838 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3619 | Diag(LangLoc, diag::err_bad_language); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3620 | return DeclPtrTy(); |
Chris Lattner | 9bb9abf | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 3621 | } |
| 3622 | |
| 3623 | // FIXME: Add all the various semantics of linkage specifications |
| 3624 | |
Douglas Gregor | d802838 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3625 | LinkageSpecDecl *D = LinkageSpecDecl::Create(Context, CurContext, |
| 3626 | LangLoc, Language, |
| 3627 | LBraceLoc.isValid()); |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3628 | CurContext->addDecl(D); |
Douglas Gregor | d802838 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3629 | PushDeclContext(S, D); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3630 | return DeclPtrTy::make(D); |
Chris Lattner | 9bb9abf | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 3631 | } |
| 3632 | |
Douglas Gregor | d802838 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3633 | /// ActOnFinishLinkageSpecification - Completely the definition of |
| 3634 | /// the C++ linkage specification LinkageSpec. If RBraceLoc is |
| 3635 | /// valid, it's the position of the closing '}' brace in a linkage |
| 3636 | /// specification that uses braces. |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3637 | Sema::DeclPtrTy Sema::ActOnFinishLinkageSpecification(Scope *S, |
| 3638 | DeclPtrTy LinkageSpec, |
| 3639 | SourceLocation RBraceLoc) { |
Douglas Gregor | d802838 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3640 | if (LinkageSpec) |
| 3641 | PopDeclContext(); |
| 3642 | return LinkageSpec; |
Chris Lattner | f58d52b | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 3643 | } |
| 3644 | |
Douglas Gregor | 57420b4 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 3645 | /// \brief Perform semantic analysis for the variable declaration that |
| 3646 | /// occurs within a C++ catch clause, returning the newly-created |
| 3647 | /// variable. |
| 3648 | VarDecl *Sema::BuildExceptionDeclaration(Scope *S, QualType ExDeclType, |
Argiris Kirtzidis | b17120c | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 3649 | DeclaratorInfo *DInfo, |
Douglas Gregor | 57420b4 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 3650 | IdentifierInfo *Name, |
| 3651 | SourceLocation Loc, |
| 3652 | SourceRange Range) { |
| 3653 | bool Invalid = false; |
Sebastian Redl | 743c816 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 3654 | |
| 3655 | // Arrays and functions decay. |
| 3656 | if (ExDeclType->isArrayType()) |
| 3657 | ExDeclType = Context.getArrayDecayedType(ExDeclType); |
| 3658 | else if (ExDeclType->isFunctionType()) |
| 3659 | ExDeclType = Context.getPointerType(ExDeclType); |
| 3660 | |
| 3661 | // C++ 15.3p1: The exception-declaration shall not denote an incomplete type. |
| 3662 | // The exception-declaration shall not denote a pointer or reference to an |
| 3663 | // incomplete type, other than [cv] void*. |
Sebastian Redl | 8a8b351 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 3664 | // N2844 forbids rvalue references. |
Douglas Gregor | 3b7e911 | 2009-05-18 21:08:14 +0000 | [diff] [blame] | 3665 | if(!ExDeclType->isDependentType() && ExDeclType->isRValueReferenceType()) { |
Douglas Gregor | 57420b4 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 3666 | Diag(Loc, diag::err_catch_rvalue_ref) << Range; |
Sebastian Redl | 8a8b351 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 3667 | Invalid = true; |
| 3668 | } |
Douglas Gregor | 57420b4 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 3669 | |
Sebastian Redl | 743c816 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 3670 | QualType BaseType = ExDeclType; |
| 3671 | int Mode = 0; // 0 for direct type, 1 for pointer, 2 for reference |
Douglas Gregor | 46fe06e | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 3672 | unsigned DK = diag::err_catch_incomplete; |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3673 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
Sebastian Redl | 743c816 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 3674 | BaseType = Ptr->getPointeeType(); |
| 3675 | Mode = 1; |
Douglas Gregor | 46fe06e | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 3676 | DK = diag::err_catch_incomplete_ptr; |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3677 | } else if(const ReferenceType *Ref = BaseType->getAs<ReferenceType>()) { |
Sebastian Redl | 8a8b351 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 3678 | // For the purpose of error recovery, we treat rvalue refs like lvalue refs. |
Sebastian Redl | 743c816 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 3679 | BaseType = Ref->getPointeeType(); |
| 3680 | Mode = 2; |
Douglas Gregor | 46fe06e | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 3681 | DK = diag::err_catch_incomplete_ref; |
Sebastian Redl | 743c816 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 3682 | } |
Sebastian Redl | 8a8b351 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 3683 | if (!Invalid && (Mode == 0 || !BaseType->isVoidType()) && |
Douglas Gregor | 57420b4 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 3684 | !BaseType->isDependentType() && RequireCompleteType(Loc, BaseType, DK)) |
Sebastian Redl | 743c816 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 3685 | Invalid = true; |
Sebastian Redl | 743c816 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 3686 | |
Douglas Gregor | 57420b4 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 3687 | if (!Invalid && !ExDeclType->isDependentType() && |
| 3688 | RequireNonAbstractType(Loc, ExDeclType, |
| 3689 | diag::err_abstract_type_in_decl, |
| 3690 | AbstractVariableType)) |
Sebastian Redl | 5419865 | 2009-04-27 21:03:30 +0000 | [diff] [blame] | 3691 | Invalid = true; |
| 3692 | |
Douglas Gregor | 57420b4 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 3693 | // FIXME: Need to test for ability to copy-construct and destroy the |
| 3694 | // exception variable. |
| 3695 | |
Sebastian Redl | 237116b | 2008-12-22 21:35:02 +0000 | [diff] [blame] | 3696 | // FIXME: Need to check for abstract classes. |
| 3697 | |
Douglas Gregor | 57420b4 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 3698 | VarDecl *ExDecl = VarDecl::Create(Context, CurContext, Loc, |
Argiris Kirtzidis | 42556e4 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 3699 | Name, ExDeclType, DInfo, VarDecl::None); |
Douglas Gregor | 57420b4 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 3700 | |
| 3701 | if (Invalid) |
| 3702 | ExDecl->setInvalidDecl(); |
| 3703 | |
| 3704 | return ExDecl; |
| 3705 | } |
| 3706 | |
| 3707 | /// ActOnExceptionDeclarator - Parsed the exception-declarator in a C++ catch |
| 3708 | /// handler. |
| 3709 | Sema::DeclPtrTy Sema::ActOnExceptionDeclarator(Scope *S, Declarator &D) { |
Argiris Kirtzidis | b17120c | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 3710 | DeclaratorInfo *DInfo = 0; |
| 3711 | QualType ExDeclType = GetTypeForDeclarator(D, S, &DInfo); |
Douglas Gregor | 57420b4 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 3712 | |
| 3713 | bool Invalid = D.isInvalidType(); |
Sebastian Redl | 743c816 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 3714 | IdentifierInfo *II = D.getIdentifier(); |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 3715 | if (NamedDecl *PrevDecl = LookupName(S, II, LookupOrdinaryName)) { |
Sebastian Redl | 743c816 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 3716 | // The scope should be freshly made just for us. There is just no way |
| 3717 | // it contains any previous declaration. |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3718 | assert(!S->isDeclScope(DeclPtrTy::make(PrevDecl))); |
Sebastian Redl | 743c816 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 3719 | if (PrevDecl->isTemplateParameter()) { |
| 3720 | // Maybe we will complain about the shadowed template parameter. |
| 3721 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
Sebastian Redl | 743c816 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 3722 | } |
| 3723 | } |
| 3724 | |
Chris Lattner | 34c6133 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 3725 | if (D.getCXXScopeSpec().isSet() && !Invalid) { |
Sebastian Redl | 743c816 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 3726 | Diag(D.getIdentifierLoc(), diag::err_qualified_catch_declarator) |
| 3727 | << D.getCXXScopeSpec().getRange(); |
Chris Lattner | 34c6133 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 3728 | Invalid = true; |
Sebastian Redl | 743c816 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 3729 | } |
| 3730 | |
Argiris Kirtzidis | b17120c | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 3731 | VarDecl *ExDecl = BuildExceptionDeclaration(S, ExDeclType, DInfo, |
Douglas Gregor | 57420b4 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 3732 | D.getIdentifier(), |
| 3733 | D.getIdentifierLoc(), |
| 3734 | D.getDeclSpec().getSourceRange()); |
| 3735 | |
Chris Lattner | 34c6133 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 3736 | if (Invalid) |
| 3737 | ExDecl->setInvalidDecl(); |
| 3738 | |
Sebastian Redl | 743c816 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 3739 | // Add the exception declaration into this scope. |
Sebastian Redl | 743c816 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 3740 | if (II) |
Douglas Gregor | 57420b4 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 3741 | PushOnScopeChains(ExDecl, S); |
| 3742 | else |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3743 | CurContext->addDecl(ExDecl); |
Sebastian Redl | 743c816 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 3744 | |
Douglas Gregor | 2a2e040 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 3745 | ProcessDeclAttributes(S, ExDecl, D); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3746 | return DeclPtrTy::make(ExDecl); |
Sebastian Redl | 743c816 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 3747 | } |
Anders Carlsson | ed69156 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 3748 | |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3749 | Sema::DeclPtrTy Sema::ActOnStaticAssertDeclaration(SourceLocation AssertLoc, |
| 3750 | ExprArg assertexpr, |
| 3751 | ExprArg assertmessageexpr) { |
Anders Carlsson | ed69156 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 3752 | Expr *AssertExpr = (Expr *)assertexpr.get(); |
| 3753 | StringLiteral *AssertMessage = |
| 3754 | cast<StringLiteral>((Expr *)assertmessageexpr.get()); |
| 3755 | |
Anders Carlsson | 8b842c5 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 3756 | if (!AssertExpr->isTypeDependent() && !AssertExpr->isValueDependent()) { |
| 3757 | llvm::APSInt Value(32); |
| 3758 | if (!AssertExpr->isIntegerConstantExpr(Value, Context)) { |
| 3759 | Diag(AssertLoc, diag::err_static_assert_expression_is_not_constant) << |
| 3760 | AssertExpr->getSourceRange(); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3761 | return DeclPtrTy(); |
Anders Carlsson | 8b842c5 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 3762 | } |
Anders Carlsson | ed69156 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 3763 | |
Anders Carlsson | 8b842c5 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 3764 | if (Value == 0) { |
| 3765 | std::string str(AssertMessage->getStrData(), |
| 3766 | AssertMessage->getByteLength()); |
Anders Carlsson | c45057a | 2009-03-15 18:44:04 +0000 | [diff] [blame] | 3767 | Diag(AssertLoc, diag::err_static_assert_failed) |
| 3768 | << str << AssertExpr->getSourceRange(); |
Anders Carlsson | 8b842c5 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 3769 | } |
| 3770 | } |
| 3771 | |
Anders Carlsson | 0f4942b | 2009-03-15 17:35:16 +0000 | [diff] [blame] | 3772 | assertexpr.release(); |
| 3773 | assertmessageexpr.release(); |
Anders Carlsson | ed69156 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 3774 | Decl *Decl = StaticAssertDecl::Create(Context, CurContext, AssertLoc, |
| 3775 | AssertExpr, AssertMessage); |
Anders Carlsson | ed69156 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 3776 | |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3777 | CurContext->addDecl(Decl); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3778 | return DeclPtrTy::make(Decl); |
Anders Carlsson | ed69156 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 3779 | } |
Sebastian Redl | a8cecf6 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 3780 | |
John McCall | 140607b | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 3781 | Sema::DeclPtrTy Sema::ActOnFriendDecl(Scope *S, |
John McCall | 3649308 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 3782 | llvm::PointerUnion<const DeclSpec*,Declarator*> DU, |
| 3783 | bool IsDefinition) { |
John McCall | 7de1591 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 3784 | if (DU.is<Declarator*>()) |
| 3785 | return ActOnFriendFunctionDecl(S, *DU.get<Declarator*>(), IsDefinition); |
| 3786 | else |
| 3787 | return ActOnFriendTypeDecl(S, *DU.get<const DeclSpec*>(), IsDefinition); |
| 3788 | } |
| 3789 | |
| 3790 | Sema::DeclPtrTy Sema::ActOnFriendTypeDecl(Scope *S, |
| 3791 | const DeclSpec &DS, |
| 3792 | bool IsDefinition) { |
| 3793 | SourceLocation Loc = DS.getSourceRange().getBegin(); |
John McCall | 140607b | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 3794 | |
| 3795 | assert(DS.isFriendSpecified()); |
| 3796 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 3797 | |
John McCall | 7de1591 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 3798 | // Check to see if the decl spec was syntactically like "struct foo". |
| 3799 | RecordDecl *RD = NULL; |
John McCall | 140607b | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 3800 | |
John McCall | 7de1591 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 3801 | switch (DS.getTypeSpecType()) { |
| 3802 | case DeclSpec::TST_class: |
| 3803 | case DeclSpec::TST_struct: |
| 3804 | case DeclSpec::TST_union: |
| 3805 | RD = dyn_cast_or_null<CXXRecordDecl>((Decl*) DS.getTypeRep()); |
| 3806 | if (!RD) return DeclPtrTy(); |
John McCall | 140607b | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 3807 | |
John McCall | 7de1591 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 3808 | // The parser doesn't quite handle |
| 3809 | // friend class A {} |
| 3810 | // as we'd like, because it might have been the (valid) prefix of |
| 3811 | // friend class A {} foo(); |
| 3812 | // So even in C++0x mode we don't want to |
| 3813 | IsDefinition |= RD->isDefinition(); |
| 3814 | break; |
John McCall | 140607b | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 3815 | |
John McCall | 7de1591 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 3816 | default: break; |
Anders Carlsson | b56d8b3 | 2009-05-11 22:55:49 +0000 | [diff] [blame] | 3817 | } |
John McCall | 140607b | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 3818 | |
John McCall | 7de1591 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 3819 | FriendDecl::FriendUnion FU = RD; |
John McCall | 140607b | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 3820 | |
John McCall | 7de1591 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 3821 | // C++ [class.friend]p2: |
| 3822 | // An elaborated-type-specifier shall be used in a friend declaration |
| 3823 | // for a class.* |
| 3824 | // * The class-key of the elaborated-type-specifier is required. |
| 3825 | // So if we didn't get a record decl above, we're invalid in C++98 mode. |
| 3826 | if (!RD) { |
| 3827 | bool invalid = false; |
| 3828 | QualType T = ConvertDeclSpecToType(DS, Loc, invalid); |
| 3829 | if (invalid) return DeclPtrTy(); |
| 3830 | |
| 3831 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 3832 | FU = RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 3833 | |
| 3834 | // Untagged typenames are invalid prior to C++0x, but we can |
| 3835 | // suggest an easy fix which should work. |
| 3836 | if (!getLangOptions().CPlusPlus0x) { |
| 3837 | Diag(DS.getFriendSpecLoc(), diag::err_unelaborated_friend_type) |
| 3838 | << (RD->isUnion()) |
| 3839 | << CodeModificationHint::CreateInsertion(DS.getTypeSpecTypeLoc(), |
| 3840 | RD->isUnion() ? " union" : " class"); |
| 3841 | return DeclPtrTy(); |
| 3842 | } |
| 3843 | }else if (!getLangOptions().CPlusPlus0x) { |
| 3844 | Diag(DS.getFriendSpecLoc(), diag::err_unexpected_friend) |
| 3845 | << DS.getSourceRange(); |
| 3846 | return DeclPtrTy(); |
| 3847 | }else { |
| 3848 | FU = T.getTypePtr(); |
| 3849 | } |
| 3850 | } |
| 3851 | |
| 3852 | assert(FU && "should have a friend decl/type by here!"); |
| 3853 | |
| 3854 | // C++ [class.friend]p2: A class shall not be defined inside |
| 3855 | // a friend declaration. |
| 3856 | if (IsDefinition) { |
| 3857 | Diag(DS.getFriendSpecLoc(), diag::err_friend_decl_defines_class) |
| 3858 | << DS.getSourceRange(); |
| 3859 | return DeclPtrTy(); |
| 3860 | } |
| 3861 | |
| 3862 | // C++98 [class.friend]p1: A friend of a class is a function |
| 3863 | // or class that is not a member of the class . . . |
| 3864 | // But that's a silly restriction which nobody implements for |
| 3865 | // inner classes, and C++0x removes it anyway, so we only report |
| 3866 | // this (as a warning) if we're being pedantic. |
| 3867 | if (!getLangOptions().CPlusPlus0x) { |
| 3868 | assert(RD && "must have a record decl in C++98 mode"); |
| 3869 | if (RD->getDeclContext() == CurContext) |
| 3870 | Diag(DS.getFriendSpecLoc(), diag::ext_friend_inner_class); |
| 3871 | } |
| 3872 | |
| 3873 | FriendDecl *FD = FriendDecl::Create(Context, CurContext, Loc, FU, |
| 3874 | DS.getFriendSpecLoc()); |
John McCall | 73e4dbc | 2009-08-29 03:50:18 +0000 | [diff] [blame] | 3875 | FD->setAccess(AS_public); |
John McCall | 7de1591 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 3876 | CurContext->addDecl(FD); |
| 3877 | |
| 3878 | return DeclPtrTy::make(FD); |
| 3879 | } |
| 3880 | |
| 3881 | Sema::DeclPtrTy Sema::ActOnFriendFunctionDecl(Scope *S, |
| 3882 | Declarator &D, |
| 3883 | bool IsDefinition) { |
| 3884 | const DeclSpec &DS = D.getDeclSpec(); |
| 3885 | |
| 3886 | assert(DS.isFriendSpecified()); |
| 3887 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 3888 | |
| 3889 | SourceLocation Loc = D.getIdentifierLoc(); |
Argiris Kirtzidis | b17120c | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 3890 | DeclaratorInfo *DInfo = 0; |
John McCall | 7de1591 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 3891 | QualType T = GetTypeForDeclarator(D, S, &DInfo); |
John McCall | 140607b | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 3892 | |
| 3893 | // C++ [class.friend]p1 |
| 3894 | // A friend of a class is a function or class.... |
| 3895 | // Note that this sees through typedefs, which is intended. |
John McCall | 7de1591 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 3896 | // It *doesn't* see through dependent types, which is correct |
| 3897 | // according to [temp.arg.type]p3: |
| 3898 | // If a declaration acquires a function type through a |
| 3899 | // type dependent on a template-parameter and this causes |
| 3900 | // a declaration that does not use the syntactic form of a |
| 3901 | // function declarator to have a function type, the program |
| 3902 | // is ill-formed. |
John McCall | 140607b | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 3903 | if (!T->isFunctionType()) { |
| 3904 | Diag(Loc, diag::err_unexpected_friend); |
| 3905 | |
| 3906 | // It might be worthwhile to try to recover by creating an |
| 3907 | // appropriate declaration. |
| 3908 | return DeclPtrTy(); |
| 3909 | } |
| 3910 | |
| 3911 | // C++ [namespace.memdef]p3 |
| 3912 | // - If a friend declaration in a non-local class first declares a |
| 3913 | // class or function, the friend class or function is a member |
| 3914 | // of the innermost enclosing namespace. |
| 3915 | // - The name of the friend is not found by simple name lookup |
| 3916 | // until a matching declaration is provided in that namespace |
| 3917 | // scope (either before or after the class declaration granting |
| 3918 | // friendship). |
| 3919 | // - If a friend function is called, its name may be found by the |
| 3920 | // name lookup that considers functions from namespaces and |
| 3921 | // classes associated with the types of the function arguments. |
| 3922 | // - When looking for a prior declaration of a class or a function |
| 3923 | // declared as a friend, scopes outside the innermost enclosing |
| 3924 | // namespace scope are not considered. |
| 3925 | |
John McCall | 7de1591 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 3926 | CXXScopeSpec &ScopeQual = D.getCXXScopeSpec(); |
| 3927 | DeclarationName Name = GetNameForDeclarator(D); |
John McCall | 140607b | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 3928 | assert(Name); |
| 3929 | |
| 3930 | // The existing declaration we found. |
| 3931 | FunctionDecl *FD = NULL; |
| 3932 | |
| 3933 | // The context we found the declaration in, or in which we should |
| 3934 | // create the declaration. |
| 3935 | DeclContext *DC; |
| 3936 | |
| 3937 | // FIXME: handle local classes |
| 3938 | |
| 3939 | // Recover from invalid scope qualifiers as if they just weren't there. |
| 3940 | if (!ScopeQual.isInvalid() && ScopeQual.isSet()) { |
| 3941 | DC = computeDeclContext(ScopeQual); |
| 3942 | |
| 3943 | // FIXME: handle dependent contexts |
| 3944 | if (!DC) return DeclPtrTy(); |
| 3945 | |
| 3946 | Decl *Dec = LookupQualifiedNameWithType(DC, Name, T); |
| 3947 | |
| 3948 | // If searching in that context implicitly found a declaration in |
| 3949 | // a different context, treat it like it wasn't found at all. |
| 3950 | // TODO: better diagnostics for this case. Suggesting the right |
| 3951 | // qualified scope would be nice... |
| 3952 | if (!Dec || Dec->getDeclContext() != DC) { |
John McCall | 7de1591 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 3953 | D.setInvalidType(); |
John McCall | 140607b | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 3954 | Diag(Loc, diag::err_qualified_friend_not_found) << Name << T; |
| 3955 | return DeclPtrTy(); |
| 3956 | } |
| 3957 | |
| 3958 | // C++ [class.friend]p1: A friend of a class is a function or |
| 3959 | // class that is not a member of the class . . . |
| 3960 | if (DC == CurContext) |
| 3961 | Diag(DS.getFriendSpecLoc(), diag::err_friend_is_member); |
| 3962 | |
| 3963 | FD = cast<FunctionDecl>(Dec); |
| 3964 | |
| 3965 | // Otherwise walk out to the nearest namespace scope looking for matches. |
| 3966 | } else { |
| 3967 | // TODO: handle local class contexts. |
| 3968 | |
| 3969 | DC = CurContext; |
| 3970 | while (true) { |
| 3971 | // Skip class contexts. If someone can cite chapter and verse |
| 3972 | // for this behavior, that would be nice --- it's what GCC and |
| 3973 | // EDG do, and it seems like a reasonable intent, but the spec |
| 3974 | // really only says that checks for unqualified existing |
| 3975 | // declarations should stop at the nearest enclosing namespace, |
| 3976 | // not that they should only consider the nearest enclosing |
| 3977 | // namespace. |
| 3978 | while (DC->isRecord()) DC = DC->getParent(); |
| 3979 | |
| 3980 | Decl *Dec = LookupQualifiedNameWithType(DC, Name, T); |
| 3981 | |
| 3982 | // TODO: decide what we think about using declarations. |
| 3983 | if (Dec) { |
| 3984 | FD = cast<FunctionDecl>(Dec); |
| 3985 | break; |
| 3986 | } |
| 3987 | if (DC->isFileContext()) break; |
| 3988 | DC = DC->getParent(); |
| 3989 | } |
| 3990 | |
| 3991 | // C++ [class.friend]p1: A friend of a class is a function or |
| 3992 | // class that is not a member of the class . . . |
John McCall | 392245a | 2009-08-06 20:49:32 +0000 | [diff] [blame] | 3993 | // C++0x changes this for both friend types and functions. |
| 3994 | // Most C++ 98 compilers do seem to give an error here, so |
| 3995 | // we do, too. |
| 3996 | if (FD && DC == CurContext && !getLangOptions().CPlusPlus0x) |
John McCall | 140607b | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 3997 | Diag(DS.getFriendSpecLoc(), diag::err_friend_is_member); |
| 3998 | } |
| 3999 | |
John McCall | 3649308 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 4000 | bool Redeclaration = (FD != 0); |
| 4001 | |
| 4002 | // If we found a match, create a friend function declaration with |
| 4003 | // that function as the previous declaration. |
| 4004 | if (Redeclaration) { |
| 4005 | // Create it in the semantic context of the original declaration. |
| 4006 | DC = FD->getDeclContext(); |
| 4007 | |
John McCall | 140607b | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4008 | // If we didn't find something matching the type exactly, create |
| 4009 | // a declaration. This declaration should only be findable via |
| 4010 | // argument-dependent lookup. |
John McCall | 3649308 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 4011 | } else { |
John McCall | 140607b | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4012 | assert(DC->isFileContext()); |
| 4013 | |
| 4014 | // This implies that it has to be an operator or function. |
John McCall | 7de1591 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4015 | if (D.getKind() == Declarator::DK_Constructor || |
| 4016 | D.getKind() == Declarator::DK_Destructor || |
| 4017 | D.getKind() == Declarator::DK_Conversion) { |
John McCall | 140607b | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4018 | Diag(Loc, diag::err_introducing_special_friend) << |
John McCall | 7de1591 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4019 | (D.getKind() == Declarator::DK_Constructor ? 0 : |
| 4020 | D.getKind() == Declarator::DK_Destructor ? 1 : 2); |
John McCall | 140607b | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4021 | return DeclPtrTy(); |
| 4022 | } |
John McCall | 140607b | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4023 | } |
| 4024 | |
John McCall | 7de1591 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4025 | NamedDecl *ND = ActOnFunctionDeclarator(S, D, DC, T, DInfo, |
John McCall | 3649308 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 4026 | /* PrevDecl = */ FD, |
| 4027 | MultiTemplateParamsArg(*this), |
| 4028 | IsDefinition, |
| 4029 | Redeclaration); |
John McCall | 7de1591 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4030 | if (!ND) return DeclPtrTy(); |
John McCall | ea7c7ce | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 4031 | |
| 4032 | assert(cast<FunctionDecl>(ND)->getPreviousDeclaration() == FD && |
| 4033 | "lost reference to previous declaration"); |
| 4034 | |
John McCall | 7de1591 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4035 | FD = cast<FunctionDecl>(ND); |
John McCall | 3649308 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 4036 | |
John McCall | bcee927 | 2009-08-18 00:00:49 +0000 | [diff] [blame] | 4037 | assert(FD->getDeclContext() == DC); |
| 4038 | assert(FD->getLexicalDeclContext() == CurContext); |
| 4039 | |
John McCall | ea7c7ce | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 4040 | // Add the function declaration to the appropriate lookup tables, |
| 4041 | // adjusting the redeclarations list as necessary. We don't |
| 4042 | // want to do this yet if the friending class is dependent. |
| 4043 | // |
| 4044 | // Also update the scope-based lookup if the target context's |
| 4045 | // lookup context is in lexical scope. |
| 4046 | if (!CurContext->isDependentContext()) { |
| 4047 | DC = DC->getLookupContext(); |
| 4048 | DC->makeDeclVisibleInContext(FD, /* Recoverable=*/ false); |
| 4049 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 4050 | PushOnScopeChains(FD, EnclosingScope, /*AddToContext=*/ false); |
| 4051 | } |
John McCall | 7de1591 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4052 | |
| 4053 | FriendDecl *FrD = FriendDecl::Create(Context, CurContext, |
| 4054 | D.getIdentifierLoc(), FD, |
| 4055 | DS.getFriendSpecLoc()); |
John McCall | 73e4dbc | 2009-08-29 03:50:18 +0000 | [diff] [blame] | 4056 | FrD->setAccess(AS_public); |
John McCall | 7de1591 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4057 | CurContext->addDecl(FrD); |
John McCall | 140607b | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4058 | |
| 4059 | return DeclPtrTy::make(FD); |
Anders Carlsson | b56d8b3 | 2009-05-11 22:55:49 +0000 | [diff] [blame] | 4060 | } |
| 4061 | |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4062 | void Sema::SetDeclDeleted(DeclPtrTy dcl, SourceLocation DelLoc) { |
Douglas Gregor | 84164f0 | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 4063 | AdjustDeclIfTemplate(dcl); |
| 4064 | |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4065 | Decl *Dcl = dcl.getAs<Decl>(); |
Sebastian Redl | a8cecf6 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 4066 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(Dcl); |
| 4067 | if (!Fn) { |
| 4068 | Diag(DelLoc, diag::err_deleted_non_function); |
| 4069 | return; |
| 4070 | } |
| 4071 | if (const FunctionDecl *Prev = Fn->getPreviousDeclaration()) { |
| 4072 | Diag(DelLoc, diag::err_deleted_decl_not_first); |
| 4073 | Diag(Prev->getLocation(), diag::note_previous_declaration); |
| 4074 | // If the declaration wasn't the first, we delete the function anyway for |
| 4075 | // recovery. |
| 4076 | } |
| 4077 | Fn->setDeleted(); |
| 4078 | } |
Sebastian Redl | 3b1ef31 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 4079 | |
| 4080 | static void SearchForReturnInStmt(Sema &Self, Stmt *S) { |
| 4081 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); CI != E; |
| 4082 | ++CI) { |
| 4083 | Stmt *SubStmt = *CI; |
| 4084 | if (!SubStmt) |
| 4085 | continue; |
| 4086 | if (isa<ReturnStmt>(SubStmt)) |
| 4087 | Self.Diag(SubStmt->getSourceRange().getBegin(), |
| 4088 | diag::err_return_in_constructor_handler); |
| 4089 | if (!isa<Expr>(SubStmt)) |
| 4090 | SearchForReturnInStmt(Self, SubStmt); |
| 4091 | } |
| 4092 | } |
| 4093 | |
| 4094 | void Sema::DiagnoseReturnInConstructorExceptionHandler(CXXTryStmt *TryBlock) { |
| 4095 | for (unsigned I = 0, E = TryBlock->getNumHandlers(); I != E; ++I) { |
| 4096 | CXXCatchStmt *Handler = TryBlock->getHandler(I); |
| 4097 | SearchForReturnInStmt(*this, Handler); |
| 4098 | } |
| 4099 | } |
Anders Carlsson | e80e29c | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 4100 | |
| 4101 | bool Sema::CheckOverridingFunctionReturnType(const CXXMethodDecl *New, |
| 4102 | const CXXMethodDecl *Old) { |
| 4103 | QualType NewTy = New->getType()->getAsFunctionType()->getResultType(); |
| 4104 | QualType OldTy = Old->getType()->getAsFunctionType()->getResultType(); |
| 4105 | |
| 4106 | QualType CNewTy = Context.getCanonicalType(NewTy); |
| 4107 | QualType COldTy = Context.getCanonicalType(OldTy); |
| 4108 | |
| 4109 | if (CNewTy == COldTy && |
| 4110 | CNewTy.getCVRQualifiers() == COldTy.getCVRQualifiers()) |
| 4111 | return false; |
| 4112 | |
Anders Carlsson | ee7177b | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 4113 | // Check if the return types are covariant |
| 4114 | QualType NewClassTy, OldClassTy; |
| 4115 | |
| 4116 | /// Both types must be pointers or references to classes. |
| 4117 | if (PointerType *NewPT = dyn_cast<PointerType>(NewTy)) { |
| 4118 | if (PointerType *OldPT = dyn_cast<PointerType>(OldTy)) { |
| 4119 | NewClassTy = NewPT->getPointeeType(); |
| 4120 | OldClassTy = OldPT->getPointeeType(); |
| 4121 | } |
| 4122 | } else if (ReferenceType *NewRT = dyn_cast<ReferenceType>(NewTy)) { |
| 4123 | if (ReferenceType *OldRT = dyn_cast<ReferenceType>(OldTy)) { |
| 4124 | NewClassTy = NewRT->getPointeeType(); |
| 4125 | OldClassTy = OldRT->getPointeeType(); |
| 4126 | } |
| 4127 | } |
| 4128 | |
| 4129 | // The return types aren't either both pointers or references to a class type. |
| 4130 | if (NewClassTy.isNull()) { |
| 4131 | Diag(New->getLocation(), |
| 4132 | diag::err_different_return_type_for_overriding_virtual_function) |
| 4133 | << New->getDeclName() << NewTy << OldTy; |
| 4134 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 4135 | |
| 4136 | return true; |
| 4137 | } |
Anders Carlsson | e80e29c | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 4138 | |
Anders Carlsson | ee7177b | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 4139 | if (NewClassTy.getUnqualifiedType() != OldClassTy.getUnqualifiedType()) { |
| 4140 | // Check if the new class derives from the old class. |
| 4141 | if (!IsDerivedFrom(NewClassTy, OldClassTy)) { |
| 4142 | Diag(New->getLocation(), |
| 4143 | diag::err_covariant_return_not_derived) |
| 4144 | << New->getDeclName() << NewTy << OldTy; |
| 4145 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 4146 | return true; |
| 4147 | } |
| 4148 | |
| 4149 | // Check if we the conversion from derived to base is valid. |
| 4150 | if (CheckDerivedToBaseConversion(NewClassTy, OldClassTy, |
| 4151 | diag::err_covariant_return_inaccessible_base, |
| 4152 | diag::err_covariant_return_ambiguous_derived_to_base_conv, |
| 4153 | // FIXME: Should this point to the return type? |
| 4154 | New->getLocation(), SourceRange(), New->getDeclName())) { |
| 4155 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 4156 | return true; |
| 4157 | } |
| 4158 | } |
| 4159 | |
| 4160 | // The qualifiers of the return types must be the same. |
| 4161 | if (CNewTy.getCVRQualifiers() != COldTy.getCVRQualifiers()) { |
| 4162 | Diag(New->getLocation(), |
| 4163 | diag::err_covariant_return_type_different_qualifications) |
Anders Carlsson | e80e29c | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 4164 | << New->getDeclName() << NewTy << OldTy; |
Anders Carlsson | ee7177b | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 4165 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 4166 | return true; |
| 4167 | }; |
| 4168 | |
| 4169 | |
| 4170 | // The new class type must have the same or less qualifiers as the old type. |
| 4171 | if (NewClassTy.isMoreQualifiedThan(OldClassTy)) { |
| 4172 | Diag(New->getLocation(), |
| 4173 | diag::err_covariant_return_type_class_type_more_qualified) |
| 4174 | << New->getDeclName() << NewTy << OldTy; |
| 4175 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 4176 | return true; |
| 4177 | }; |
| 4178 | |
| 4179 | return false; |
Anders Carlsson | e80e29c | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 4180 | } |
Argiris Kirtzidis | 6837059 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 4181 | |
Sebastian Redl | 953d12a | 2009-07-07 20:29:57 +0000 | [diff] [blame] | 4182 | bool Sema::CheckOverridingFunctionExceptionSpec(const CXXMethodDecl *New, |
| 4183 | const CXXMethodDecl *Old) |
| 4184 | { |
| 4185 | return CheckExceptionSpecSubset(diag::err_override_exception_spec, |
| 4186 | diag::note_overridden_virtual_function, |
| 4187 | Old->getType()->getAsFunctionProtoType(), |
| 4188 | Old->getLocation(), |
| 4189 | New->getType()->getAsFunctionProtoType(), |
| 4190 | New->getLocation()); |
| 4191 | } |
| 4192 | |
Argiris Kirtzidis | 6837059 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 4193 | /// ActOnCXXEnterDeclInitializer - Invoked when we are about to parse an |
| 4194 | /// initializer for the declaration 'Dcl'. |
| 4195 | /// After this method is called, according to [C++ 3.4.1p13], if 'Dcl' is a |
| 4196 | /// static data member of class X, names should be looked up in the scope of |
| 4197 | /// class X. |
| 4198 | void Sema::ActOnCXXEnterDeclInitializer(Scope *S, DeclPtrTy Dcl) { |
Douglas Gregor | 84164f0 | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 4199 | AdjustDeclIfTemplate(Dcl); |
| 4200 | |
Argiris Kirtzidis | 6837059 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 4201 | Decl *D = Dcl.getAs<Decl>(); |
| 4202 | // If there is no declaration, there was an error parsing it. |
| 4203 | if (D == 0) |
| 4204 | return; |
| 4205 | |
| 4206 | // Check whether it is a declaration with a nested name specifier like |
| 4207 | // int foo::bar; |
| 4208 | if (!D->isOutOfLine()) |
| 4209 | return; |
| 4210 | |
| 4211 | // C++ [basic.lookup.unqual]p13 |
| 4212 | // |
| 4213 | // A name used in the definition of a static data member of class X |
| 4214 | // (after the qualified-id of the static member) is looked up as if the name |
| 4215 | // was used in a member function of X. |
| 4216 | |
| 4217 | // Change current context into the context of the initializing declaration. |
Argiris Kirtzidis | a0b19fe | 2009-06-17 23:15:40 +0000 | [diff] [blame] | 4218 | EnterDeclaratorContext(S, D->getDeclContext()); |
Argiris Kirtzidis | 6837059 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 4219 | } |
| 4220 | |
| 4221 | /// ActOnCXXExitDeclInitializer - Invoked after we are finished parsing an |
| 4222 | /// initializer for the declaration 'Dcl'. |
| 4223 | void Sema::ActOnCXXExitDeclInitializer(Scope *S, DeclPtrTy Dcl) { |
Douglas Gregor | 84164f0 | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 4224 | AdjustDeclIfTemplate(Dcl); |
| 4225 | |
Argiris Kirtzidis | 6837059 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 4226 | Decl *D = Dcl.getAs<Decl>(); |
| 4227 | // If there is no declaration, there was an error parsing it. |
| 4228 | if (D == 0) |
| 4229 | return; |
| 4230 | |
| 4231 | // Check whether it is a declaration with a nested name specifier like |
| 4232 | // int foo::bar; |
| 4233 | if (!D->isOutOfLine()) |
| 4234 | return; |
| 4235 | |
| 4236 | assert(S->getEntity() == D->getDeclContext() && "Context imbalance!"); |
Argiris Kirtzidis | a0b19fe | 2009-06-17 23:15:40 +0000 | [diff] [blame] | 4237 | ExitDeclaratorContext(S); |
Argiris Kirtzidis | 6837059 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 4238 | } |