Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 1 | //===------ SemaDeclCXX.cpp - Semantic Analysis for C++ Declarations ------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for C++ declarations. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
Argyrios Kyrtzidis | a4755c6 | 2008-08-09 00:58:37 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTConsumer.h" |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 17 | #include "clang/AST/TypeOrdering.h" |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 18 | #include "clang/AST/StmtVisitor.h" |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 19 | #include "clang/Lex/Preprocessor.h" |
Daniel Dunbar | 12bc692 | 2008-08-11 03:27:53 +0000 | [diff] [blame] | 20 | #include "clang/Basic/Diagnostic.h" |
Daniel Dunbar | 12bc692 | 2008-08-11 03:27:53 +0000 | [diff] [blame] | 21 | #include "clang/Parse/DeclSpec.h" |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Compiler.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 23 | #include <algorithm> // for std::equal |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 24 | #include <map> |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace clang; |
| 27 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 28 | //===----------------------------------------------------------------------===// |
| 29 | // CheckDefaultArgumentVisitor |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 32 | namespace { |
| 33 | /// CheckDefaultArgumentVisitor - C++ [dcl.fct.default] Traverses |
| 34 | /// the default argument of a parameter to determine whether it |
| 35 | /// contains any ill-formed subexpressions. For example, this will |
| 36 | /// diagnose the use of local variables or parameters within the |
| 37 | /// default argument expression. |
| 38 | class VISIBILITY_HIDDEN CheckDefaultArgumentVisitor |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 39 | : public StmtVisitor<CheckDefaultArgumentVisitor, bool> { |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 40 | Expr *DefaultArg; |
| 41 | Sema *S; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 42 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 43 | public: |
| 44 | CheckDefaultArgumentVisitor(Expr *defarg, Sema *s) |
| 45 | : DefaultArg(defarg), S(s) {} |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 46 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 47 | bool VisitExpr(Expr *Node); |
| 48 | bool VisitDeclRefExpr(DeclRefExpr *DRE); |
Douglas Gregor | 3996f23 | 2008-11-04 13:41:56 +0000 | [diff] [blame^] | 49 | bool VisitPredefinedExpr(PredefinedExpr *PE); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 50 | }; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 51 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 52 | /// VisitExpr - Visit all of the children of this expression. |
| 53 | bool CheckDefaultArgumentVisitor::VisitExpr(Expr *Node) { |
| 54 | bool IsInvalid = false; |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 55 | for (Stmt::child_iterator I = Node->child_begin(), |
| 56 | E = Node->child_end(); I != E; ++I) |
| 57 | IsInvalid |= Visit(*I); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 58 | return IsInvalid; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 61 | /// VisitDeclRefExpr - Visit a reference to a declaration, to |
| 62 | /// determine whether this declaration can be used in the default |
| 63 | /// argument expression. |
| 64 | bool CheckDefaultArgumentVisitor::VisitDeclRefExpr(DeclRefExpr *DRE) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 65 | NamedDecl *Decl = DRE->getDecl(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 66 | if (ParmVarDecl *Param = dyn_cast<ParmVarDecl>(Decl)) { |
| 67 | // C++ [dcl.fct.default]p9 |
| 68 | // Default arguments are evaluated each time the function is |
| 69 | // called. The order of evaluation of function arguments is |
| 70 | // unspecified. Consequently, parameters of a function shall not |
| 71 | // be used in default argument expressions, even if they are not |
| 72 | // evaluated. Parameters of a function declared before a default |
| 73 | // argument expression are in scope and can hide namespace and |
| 74 | // class member names. |
| 75 | return S->Diag(DRE->getSourceRange().getBegin(), |
| 76 | diag::err_param_default_argument_references_param, |
| 77 | Param->getName(), DefaultArg->getSourceRange()); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 78 | } else if (VarDecl *VDecl = dyn_cast<VarDecl>(Decl)) { |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 79 | // C++ [dcl.fct.default]p7 |
| 80 | // Local variables shall not be used in default argument |
| 81 | // expressions. |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 82 | if (VDecl->isBlockVarDecl()) |
| 83 | return S->Diag(DRE->getSourceRange().getBegin(), |
| 84 | diag::err_param_default_argument_references_local, |
| 85 | VDecl->getName(), DefaultArg->getSourceRange()); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 86 | } |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 87 | |
Douglas Gregor | 3996f23 | 2008-11-04 13:41:56 +0000 | [diff] [blame^] | 88 | return false; |
| 89 | } |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 90 | |
Douglas Gregor | 3996f23 | 2008-11-04 13:41:56 +0000 | [diff] [blame^] | 91 | /// VisitPredefinedExpr - Visit a predefined expression, which could |
| 92 | /// refer to "this". |
| 93 | bool CheckDefaultArgumentVisitor::VisitPredefinedExpr(PredefinedExpr *PE) { |
| 94 | if (PE->getIdentType() == PredefinedExpr::CXXThis) { |
| 95 | // C++ [dcl.fct.default]p8: |
| 96 | // The keyword this shall not be used in a default argument of a |
| 97 | // member function. |
| 98 | return S->Diag(PE->getSourceRange().getBegin(), |
| 99 | diag::err_param_default_argument_references_this, |
| 100 | PE->getSourceRange()); |
| 101 | } |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 102 | return false; |
| 103 | } |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /// ActOnParamDefaultArgument - Check whether the default argument |
| 107 | /// provided for a function parameter is well-formed. If so, attach it |
| 108 | /// to the parameter declaration. |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 109 | void |
| 110 | Sema::ActOnParamDefaultArgument(DeclTy *param, SourceLocation EqualLoc, |
| 111 | ExprTy *defarg) { |
| 112 | ParmVarDecl *Param = (ParmVarDecl *)param; |
| 113 | llvm::OwningPtr<Expr> DefaultArg((Expr *)defarg); |
| 114 | QualType ParamType = Param->getType(); |
| 115 | |
| 116 | // Default arguments are only permitted in C++ |
| 117 | if (!getLangOptions().CPlusPlus) { |
| 118 | Diag(EqualLoc, diag::err_param_default_argument, |
| 119 | DefaultArg->getSourceRange()); |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | // C++ [dcl.fct.default]p5 |
| 124 | // A default argument expression is implicitly converted (clause |
| 125 | // 4) to the parameter type. The default argument expression has |
| 126 | // the same semantic constraints as the initializer expression in |
| 127 | // a declaration of a variable of the parameter type, using the |
| 128 | // copy-initialization semantics (8.5). |
| 129 | // |
| 130 | // FIXME: CheckSingleAssignmentConstraints has the wrong semantics |
| 131 | // for C++ (since we want copy-initialization, not copy-assignment), |
| 132 | // but we don't have the right semantics implemented yet. Because of |
| 133 | // this, our error message is also very poor. |
| 134 | QualType DefaultArgType = DefaultArg->getType(); |
| 135 | Expr *DefaultArgPtr = DefaultArg.get(); |
| 136 | AssignConvertType ConvTy = CheckSingleAssignmentConstraints(ParamType, |
| 137 | DefaultArgPtr); |
| 138 | if (DefaultArgPtr != DefaultArg.get()) { |
| 139 | DefaultArg.take(); |
| 140 | DefaultArg.reset(DefaultArgPtr); |
| 141 | } |
| 142 | if (DiagnoseAssignmentResult(ConvTy, DefaultArg->getLocStart(), |
| 143 | ParamType, DefaultArgType, DefaultArg.get(), |
| 144 | "in default argument")) { |
| 145 | return; |
| 146 | } |
| 147 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 148 | // Check that the default argument is well-formed |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 149 | CheckDefaultArgumentVisitor DefaultArgChecker(DefaultArg.get(), this); |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 150 | if (DefaultArgChecker.Visit(DefaultArg.get())) |
| 151 | return; |
| 152 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 153 | // Okay: add the default argument to the parameter |
| 154 | Param->setDefaultArg(DefaultArg.take()); |
| 155 | } |
| 156 | |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 157 | /// CheckExtraCXXDefaultArguments - Check for any extra default |
| 158 | /// arguments in the declarator, which is not a function declaration |
| 159 | /// or definition and therefore is not permitted to have default |
| 160 | /// arguments. This routine should be invoked for every declarator |
| 161 | /// that is not a function declaration or definition. |
| 162 | void Sema::CheckExtraCXXDefaultArguments(Declarator &D) { |
| 163 | // C++ [dcl.fct.default]p3 |
| 164 | // A default argument expression shall be specified only in the |
| 165 | // parameter-declaration-clause of a function declaration or in a |
| 166 | // template-parameter (14.1). It shall not be specified for a |
| 167 | // parameter pack. If it is specified in a |
| 168 | // parameter-declaration-clause, it shall not occur within a |
| 169 | // declarator or abstract-declarator of a parameter-declaration. |
| 170 | for (unsigned i = 0; i < D.getNumTypeObjects(); ++i) { |
| 171 | DeclaratorChunk &chunk = D.getTypeObject(i); |
| 172 | if (chunk.Kind == DeclaratorChunk::Function) { |
| 173 | for (unsigned argIdx = 0; argIdx < chunk.Fun.NumArgs; ++argIdx) { |
| 174 | ParmVarDecl *Param = (ParmVarDecl *)chunk.Fun.ArgInfo[argIdx].Param; |
| 175 | if (Param->getDefaultArg()) { |
| 176 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc, |
| 177 | Param->getDefaultArg()->getSourceRange()); |
| 178 | Param->setDefaultArg(0); |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 185 | // MergeCXXFunctionDecl - Merge two declarations of the same C++ |
| 186 | // function, once we already know that they have the same |
| 187 | // type. Subroutine of MergeFunctionDecl. |
| 188 | FunctionDecl * |
| 189 | Sema::MergeCXXFunctionDecl(FunctionDecl *New, FunctionDecl *Old) { |
| 190 | // C++ [dcl.fct.default]p4: |
| 191 | // |
| 192 | // For non-template functions, default arguments can be added in |
| 193 | // later declarations of a function in the same |
| 194 | // scope. Declarations in different scopes have completely |
| 195 | // distinct sets of default arguments. That is, declarations in |
| 196 | // inner scopes do not acquire default arguments from |
| 197 | // declarations in outer scopes, and vice versa. In a given |
| 198 | // function declaration, all parameters subsequent to a |
| 199 | // parameter with a default argument shall have default |
| 200 | // arguments supplied in this or previous declarations. A |
| 201 | // default argument shall not be redefined by a later |
| 202 | // declaration (not even to the same value). |
| 203 | for (unsigned p = 0, NumParams = Old->getNumParams(); p < NumParams; ++p) { |
| 204 | ParmVarDecl *OldParam = Old->getParamDecl(p); |
| 205 | ParmVarDecl *NewParam = New->getParamDecl(p); |
| 206 | |
| 207 | if(OldParam->getDefaultArg() && NewParam->getDefaultArg()) { |
| 208 | Diag(NewParam->getLocation(), |
| 209 | diag::err_param_default_argument_redefinition, |
| 210 | NewParam->getDefaultArg()->getSourceRange()); |
| 211 | Diag(OldParam->getLocation(), diag::err_previous_definition); |
| 212 | } else if (OldParam->getDefaultArg()) { |
| 213 | // Merge the old default argument into the new parameter |
| 214 | NewParam->setDefaultArg(OldParam->getDefaultArg()); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | return New; |
| 219 | } |
| 220 | |
| 221 | /// CheckCXXDefaultArguments - Verify that the default arguments for a |
| 222 | /// function declaration are well-formed according to C++ |
| 223 | /// [dcl.fct.default]. |
| 224 | void Sema::CheckCXXDefaultArguments(FunctionDecl *FD) { |
| 225 | unsigned NumParams = FD->getNumParams(); |
| 226 | unsigned p; |
| 227 | |
| 228 | // Find first parameter with a default argument |
| 229 | for (p = 0; p < NumParams; ++p) { |
| 230 | ParmVarDecl *Param = FD->getParamDecl(p); |
| 231 | if (Param->getDefaultArg()) |
| 232 | break; |
| 233 | } |
| 234 | |
| 235 | // C++ [dcl.fct.default]p4: |
| 236 | // In a given function declaration, all parameters |
| 237 | // subsequent to a parameter with a default argument shall |
| 238 | // have default arguments supplied in this or previous |
| 239 | // declarations. A default argument shall not be redefined |
| 240 | // by a later declaration (not even to the same value). |
| 241 | unsigned LastMissingDefaultArg = 0; |
| 242 | for(; p < NumParams; ++p) { |
| 243 | ParmVarDecl *Param = FD->getParamDecl(p); |
| 244 | if (!Param->getDefaultArg()) { |
| 245 | if (Param->getIdentifier()) |
| 246 | Diag(Param->getLocation(), |
| 247 | diag::err_param_default_argument_missing_name, |
| 248 | Param->getIdentifier()->getName()); |
| 249 | else |
| 250 | Diag(Param->getLocation(), |
| 251 | diag::err_param_default_argument_missing); |
| 252 | |
| 253 | LastMissingDefaultArg = p; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | if (LastMissingDefaultArg > 0) { |
| 258 | // Some default arguments were missing. Clear out all of the |
| 259 | // default arguments up to (and including) the last missing |
| 260 | // default argument, so that we leave the function parameters |
| 261 | // in a semantically valid state. |
| 262 | for (p = 0; p <= LastMissingDefaultArg; ++p) { |
| 263 | ParmVarDecl *Param = FD->getParamDecl(p); |
| 264 | if (Param->getDefaultArg()) { |
| 265 | delete Param->getDefaultArg(); |
| 266 | Param->setDefaultArg(0); |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 271 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 272 | /// isCurrentClassName - Determine whether the identifier II is the |
| 273 | /// name of the class type currently being defined. In the case of |
| 274 | /// nested classes, this will only return true if II is the name of |
| 275 | /// the innermost class. |
| 276 | bool Sema::isCurrentClassName(const IdentifierInfo &II, Scope *) { |
| 277 | if (CXXRecordDecl *CurDecl = dyn_cast_or_null<CXXRecordDecl>(CurContext)) |
| 278 | return &II == CurDecl->getIdentifier(); |
| 279 | else |
| 280 | return false; |
| 281 | } |
| 282 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 283 | /// ActOnBaseSpecifier - Parsed a base specifier. A base specifier is |
| 284 | /// one entry in the base class list of a class specifier, for |
| 285 | /// example: |
| 286 | /// class foo : public bar, virtual private baz { |
| 287 | /// 'public bar' and 'virtual private baz' are each base-specifiers. |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 288 | Sema::BaseResult |
| 289 | Sema::ActOnBaseSpecifier(DeclTy *classdecl, SourceRange SpecifierRange, |
| 290 | bool Virtual, AccessSpecifier Access, |
| 291 | TypeTy *basetype, SourceLocation BaseLoc) { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 292 | RecordDecl *Decl = (RecordDecl*)classdecl; |
| 293 | QualType BaseType = Context.getTypeDeclType((TypeDecl*)basetype); |
| 294 | |
| 295 | // Base specifiers must be record types. |
| 296 | if (!BaseType->isRecordType()) { |
| 297 | Diag(BaseLoc, diag::err_base_must_be_class, SpecifierRange); |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 298 | return true; |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | // C++ [class.union]p1: |
| 302 | // A union shall not be used as a base class. |
| 303 | if (BaseType->isUnionType()) { |
| 304 | Diag(BaseLoc, diag::err_union_as_base_class, SpecifierRange); |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 305 | return true; |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | // C++ [class.union]p1: |
| 309 | // A union shall not have base classes. |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 310 | if (Decl->isUnion()) { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 311 | Diag(Decl->getLocation(), diag::err_base_clause_on_union, |
| 312 | SpecifierRange); |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 313 | return true; |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | // C++ [class.derived]p2: |
| 317 | // The class-name in a base-specifier shall not be an incompletely |
| 318 | // defined class. |
| 319 | if (BaseType->isIncompleteType()) { |
| 320 | Diag(BaseLoc, diag::err_incomplete_base_class, SpecifierRange); |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 321 | return true; |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 324 | // Create the base specifier. |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 325 | return new CXXBaseSpecifier(SpecifierRange, Virtual, |
| 326 | BaseType->isClassType(), Access, BaseType); |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 327 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 328 | |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 329 | /// ActOnBaseSpecifiers - Attach the given base specifiers to the |
| 330 | /// class, after checking whether there are any duplicate base |
| 331 | /// classes. |
| 332 | void Sema::ActOnBaseSpecifiers(DeclTy *ClassDecl, BaseTy **Bases, |
| 333 | unsigned NumBases) { |
| 334 | if (NumBases == 0) |
| 335 | return; |
| 336 | |
| 337 | // Used to keep track of which base types we have already seen, so |
| 338 | // that we can properly diagnose redundant direct base types. Note |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 339 | // that the key is always the unqualified canonical type of the base |
| 340 | // class. |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 341 | std::map<QualType, CXXBaseSpecifier*, QualTypeOrdering> KnownBaseTypes; |
| 342 | |
| 343 | // Copy non-redundant base specifiers into permanent storage. |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 344 | CXXBaseSpecifier **BaseSpecs = (CXXBaseSpecifier **)Bases; |
| 345 | unsigned NumGoodBases = 0; |
| 346 | for (unsigned idx = 0; idx < NumBases; ++idx) { |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 347 | QualType NewBaseType |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 348 | = Context.getCanonicalType(BaseSpecs[idx]->getType()); |
| 349 | NewBaseType = NewBaseType.getUnqualifiedType(); |
| 350 | |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 351 | if (KnownBaseTypes[NewBaseType]) { |
| 352 | // C++ [class.mi]p3: |
| 353 | // A class shall not be specified as a direct base class of a |
| 354 | // derived class more than once. |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 355 | Diag(BaseSpecs[idx]->getSourceRange().getBegin(), |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 356 | diag::err_duplicate_base_class, |
| 357 | KnownBaseTypes[NewBaseType]->getType().getAsString(), |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 358 | BaseSpecs[idx]->getSourceRange()); |
| 359 | |
| 360 | // Delete the duplicate base class specifier; we're going to |
| 361 | // overwrite its pointer later. |
| 362 | delete BaseSpecs[idx]; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 363 | } else { |
| 364 | // Okay, add this new base class. |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 365 | KnownBaseTypes[NewBaseType] = BaseSpecs[idx]; |
| 366 | BaseSpecs[NumGoodBases++] = BaseSpecs[idx]; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | |
| 370 | // Attach the remaining base class specifiers to the derived class. |
| 371 | CXXRecordDecl *Decl = (CXXRecordDecl*)ClassDecl; |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 372 | Decl->setBases(BaseSpecs, NumGoodBases); |
| 373 | |
| 374 | // Delete the remaining (good) base class specifiers, since their |
| 375 | // data has been copied into the CXXRecordDecl. |
| 376 | for (unsigned idx = 0; idx < NumGoodBases; ++idx) |
| 377 | delete BaseSpecs[idx]; |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 378 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 379 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 380 | //===----------------------------------------------------------------------===// |
| 381 | // C++ class member Handling |
| 382 | //===----------------------------------------------------------------------===// |
| 383 | |
| 384 | /// ActOnStartCXXClassDef - This is called at the start of a class/struct/union |
| 385 | /// definition, when on C++. |
| 386 | void Sema::ActOnStartCXXClassDef(Scope *S, DeclTy *D, SourceLocation LBrace) { |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 387 | CXXRecordDecl *Dcl = cast<CXXRecordDecl>(static_cast<Decl *>(D)); |
| 388 | PushDeclContext(Dcl); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 389 | FieldCollector->StartClass(); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 390 | |
| 391 | if (Dcl->getIdentifier()) { |
| 392 | // C++ [class]p2: |
| 393 | // [...] The class-name is also inserted into the scope of the |
| 394 | // class itself; this is known as the injected-class-name. For |
| 395 | // purposes of access checking, the injected-class-name is treated |
| 396 | // as if it were a public member name. |
| 397 | TypedefDecl *InjectedClassName |
| 398 | = TypedefDecl::Create(Context, Dcl, LBrace, Dcl->getIdentifier(), |
| 399 | Context.getTypeDeclType(Dcl), /*PrevDecl=*/0); |
| 400 | PushOnScopeChains(InjectedClassName, S); |
| 401 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | /// ActOnCXXMemberDeclarator - This is invoked when a C++ class member |
| 405 | /// declarator is parsed. 'AS' is the access specifier, 'BW' specifies the |
| 406 | /// bitfield width if there is one and 'InitExpr' specifies the initializer if |
| 407 | /// any. 'LastInGroup' is non-null for cases where one declspec has multiple |
| 408 | /// declarators on it. |
| 409 | /// |
| 410 | /// NOTE: Because of CXXFieldDecl's inability to be chained like ScopedDecls, if |
| 411 | /// an instance field is declared, a new CXXFieldDecl is created but the method |
| 412 | /// does *not* return it; it returns LastInGroup instead. The other C++ members |
| 413 | /// (which are all ScopedDecls) are returned after appending them to |
| 414 | /// LastInGroup. |
| 415 | Sema::DeclTy * |
| 416 | Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D, |
| 417 | ExprTy *BW, ExprTy *InitExpr, |
| 418 | DeclTy *LastInGroup) { |
| 419 | const DeclSpec &DS = D.getDeclSpec(); |
| 420 | IdentifierInfo *II = D.getIdentifier(); |
| 421 | Expr *BitWidth = static_cast<Expr*>(BW); |
| 422 | Expr *Init = static_cast<Expr*>(InitExpr); |
| 423 | SourceLocation Loc = D.getIdentifierLoc(); |
| 424 | |
| 425 | // C++ 9.2p6: A member shall not be declared to have automatic storage |
| 426 | // duration (auto, register) or with the extern storage-class-specifier. |
| 427 | switch (DS.getStorageClassSpec()) { |
| 428 | case DeclSpec::SCS_unspecified: |
| 429 | case DeclSpec::SCS_typedef: |
| 430 | case DeclSpec::SCS_static: |
| 431 | // FALL THROUGH. |
| 432 | break; |
| 433 | default: |
| 434 | if (DS.getStorageClassSpecLoc().isValid()) |
| 435 | Diag(DS.getStorageClassSpecLoc(), |
| 436 | diag::err_storageclass_invalid_for_member); |
| 437 | else |
| 438 | Diag(DS.getThreadSpecLoc(), diag::err_storageclass_invalid_for_member); |
| 439 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 440 | } |
| 441 | |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 442 | bool isFunc = D.isFunctionDeclarator(); |
Argyrios Kyrtzidis | d6caa9e | 2008-10-15 20:23:22 +0000 | [diff] [blame] | 443 | if (!isFunc && |
| 444 | D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_typedef && |
| 445 | D.getNumTypeObjects() == 0) { |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 446 | // Check also for this case: |
| 447 | // |
| 448 | // typedef int f(); |
| 449 | // f a; |
| 450 | // |
| 451 | Decl *TD = static_cast<Decl *>(DS.getTypeRep()); |
| 452 | isFunc = Context.getTypeDeclType(cast<TypeDecl>(TD))->isFunctionType(); |
| 453 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 454 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 455 | bool isInstField = (DS.getStorageClassSpec() == DeclSpec::SCS_unspecified && |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 456 | !isFunc); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 457 | |
| 458 | Decl *Member; |
| 459 | bool InvalidDecl = false; |
| 460 | |
| 461 | if (isInstField) |
| 462 | Member = static_cast<Decl*>(ActOnField(S, Loc, D, BitWidth)); |
| 463 | else |
Daniel Dunbar | 914701e | 2008-08-05 16:28:08 +0000 | [diff] [blame] | 464 | Member = static_cast<Decl*>(ActOnDeclarator(S, D, LastInGroup)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 465 | |
| 466 | if (!Member) return LastInGroup; |
| 467 | |
Sanjiv Gupta | 31fc07d | 2008-10-31 09:52:39 +0000 | [diff] [blame] | 468 | assert((II || isInstField) && "No identifier for non-field ?"); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 469 | |
| 470 | // set/getAccess is not part of Decl's interface to avoid bloating it with C++ |
| 471 | // specific methods. Use a wrapper class that can be used with all C++ class |
| 472 | // member decls. |
| 473 | CXXClassMemberWrapper(Member).setAccess(AS); |
| 474 | |
| 475 | if (BitWidth) { |
| 476 | // C++ 9.6p2: Only when declaring an unnamed bit-field may the |
| 477 | // constant-expression be a value equal to zero. |
| 478 | // FIXME: Check this. |
| 479 | |
| 480 | if (D.isFunctionDeclarator()) { |
| 481 | // FIXME: Emit diagnostic about only constructors taking base initializers |
| 482 | // or something similar, when constructor support is in place. |
| 483 | Diag(Loc, diag::err_not_bitfield_type, |
| 484 | II->getName(), BitWidth->getSourceRange()); |
| 485 | InvalidDecl = true; |
| 486 | |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 487 | } else if (isInstField) { |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 488 | // C++ 9.6p3: A bit-field shall have integral or enumeration type. |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 489 | if (!cast<FieldDecl>(Member)->getType()->isIntegralType()) { |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 490 | Diag(Loc, diag::err_not_integral_type_bitfield, |
| 491 | II->getName(), BitWidth->getSourceRange()); |
| 492 | InvalidDecl = true; |
| 493 | } |
| 494 | |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 495 | } else if (isa<FunctionDecl>(Member)) { |
| 496 | // A function typedef ("typedef int f(); f a;"). |
| 497 | // C++ 9.6p3: A bit-field shall have integral or enumeration type. |
| 498 | Diag(Loc, diag::err_not_integral_type_bitfield, |
| 499 | II->getName(), BitWidth->getSourceRange()); |
| 500 | InvalidDecl = true; |
| 501 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 502 | } else if (isa<TypedefDecl>(Member)) { |
| 503 | // "cannot declare 'A' to be a bit-field type" |
| 504 | Diag(Loc, diag::err_not_bitfield_type, II->getName(), |
| 505 | BitWidth->getSourceRange()); |
| 506 | InvalidDecl = true; |
| 507 | |
| 508 | } else { |
| 509 | assert(isa<CXXClassVarDecl>(Member) && |
| 510 | "Didn't we cover all member kinds?"); |
| 511 | // C++ 9.6p3: A bit-field shall not be a static member. |
| 512 | // "static member 'A' cannot be a bit-field" |
| 513 | Diag(Loc, diag::err_static_not_bitfield, II->getName(), |
| 514 | BitWidth->getSourceRange()); |
| 515 | InvalidDecl = true; |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | if (Init) { |
| 520 | // C++ 9.2p4: A member-declarator can contain a constant-initializer only |
| 521 | // if it declares a static member of const integral or const enumeration |
| 522 | // type. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 523 | if (CXXClassVarDecl *CVD = dyn_cast<CXXClassVarDecl>(Member)) { |
| 524 | // ...static member of... |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 525 | CVD->setInit(Init); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 526 | // ...const integral or const enumeration type. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 527 | if (Context.getCanonicalType(CVD->getType()).isConstQualified() && |
| 528 | CVD->getType()->isIntegralType()) { |
| 529 | // constant-initializer |
| 530 | if (CheckForConstantInitializer(Init, CVD->getType())) |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 531 | InvalidDecl = true; |
| 532 | |
| 533 | } else { |
| 534 | // not const integral. |
| 535 | Diag(Loc, diag::err_member_initialization, |
| 536 | II->getName(), Init->getSourceRange()); |
| 537 | InvalidDecl = true; |
| 538 | } |
| 539 | |
| 540 | } else { |
| 541 | // not static member. |
| 542 | Diag(Loc, diag::err_member_initialization, |
| 543 | II->getName(), Init->getSourceRange()); |
| 544 | InvalidDecl = true; |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | if (InvalidDecl) |
| 549 | Member->setInvalidDecl(); |
| 550 | |
| 551 | if (isInstField) { |
| 552 | FieldCollector->Add(cast<CXXFieldDecl>(Member)); |
| 553 | return LastInGroup; |
| 554 | } |
| 555 | return Member; |
| 556 | } |
| 557 | |
| 558 | void Sema::ActOnFinishCXXMemberSpecification(Scope* S, SourceLocation RLoc, |
| 559 | DeclTy *TagDecl, |
| 560 | SourceLocation LBrac, |
| 561 | SourceLocation RBrac) { |
| 562 | ActOnFields(S, RLoc, TagDecl, |
| 563 | (DeclTy**)FieldCollector->getCurFields(), |
Daniel Dunbar | 1bfe1c2 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 564 | FieldCollector->getCurNumFields(), LBrac, RBrac, 0); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 567 | /// AddImplicitlyDeclaredMembersToClass - Adds any implicitly-declared |
| 568 | /// special functions, such as the default constructor, copy |
| 569 | /// constructor, or destructor, to the given C++ class (C++ |
| 570 | /// [special]p1). This routine can only be executed just before the |
| 571 | /// definition of the class is complete. |
| 572 | void Sema::AddImplicitlyDeclaredMembersToClass(CXXRecordDecl *ClassDecl) { |
| 573 | if (!ClassDecl->hasUserDeclaredConstructor()) { |
| 574 | // C++ [class.ctor]p5: |
| 575 | // A default constructor for a class X is a constructor of class X |
| 576 | // that can be called without an argument. If there is no |
| 577 | // user-declared constructor for class X, a default constructor is |
| 578 | // implicitly declared. An implicitly-declared default constructor |
| 579 | // is an inline public member of its class. |
| 580 | CXXConstructorDecl *DefaultCon = |
| 581 | CXXConstructorDecl::Create(Context, ClassDecl, |
| 582 | ClassDecl->getLocation(), |
| 583 | ClassDecl->getIdentifier(), |
| 584 | Context.getFunctionType(Context.VoidTy, |
| 585 | 0, 0, false, 0), |
| 586 | /*isExplicit=*/false, |
| 587 | /*isInline=*/true, |
| 588 | /*isImplicitlyDeclared=*/true); |
| 589 | DefaultCon->setAccess(AS_public); |
| 590 | ClassDecl->addConstructor(Context, DefaultCon); |
| 591 | } |
| 592 | |
| 593 | if (!ClassDecl->hasUserDeclaredCopyConstructor()) { |
| 594 | // C++ [class.copy]p4: |
| 595 | // If the class definition does not explicitly declare a copy |
| 596 | // constructor, one is declared implicitly. |
| 597 | |
| 598 | // C++ [class.copy]p5: |
| 599 | // The implicitly-declared copy constructor for a class X will |
| 600 | // have the form |
| 601 | // |
| 602 | // X::X(const X&) |
| 603 | // |
| 604 | // if |
| 605 | bool HasConstCopyConstructor = true; |
| 606 | |
| 607 | // -- each direct or virtual base class B of X has a copy |
| 608 | // constructor whose first parameter is of type const B& or |
| 609 | // const volatile B&, and |
| 610 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(); |
| 611 | HasConstCopyConstructor && Base != ClassDecl->bases_end(); ++Base) { |
| 612 | const CXXRecordDecl *BaseClassDecl |
| 613 | = cast<CXXRecordDecl>(Base->getType()->getAsRecordType()->getDecl()); |
| 614 | HasConstCopyConstructor |
| 615 | = BaseClassDecl->hasConstCopyConstructor(Context); |
| 616 | } |
| 617 | |
| 618 | // -- for all the nonstatic data members of X that are of a |
| 619 | // class type M (or array thereof), each such class type |
| 620 | // has a copy constructor whose first parameter is of type |
| 621 | // const M& or const volatile M&. |
| 622 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(); |
| 623 | HasConstCopyConstructor && Field != ClassDecl->field_end(); ++Field) { |
| 624 | QualType FieldType = (*Field)->getType(); |
| 625 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 626 | FieldType = Array->getElementType(); |
| 627 | if (const RecordType *FieldClassType = FieldType->getAsRecordType()) { |
| 628 | const CXXRecordDecl *FieldClassDecl |
| 629 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
| 630 | HasConstCopyConstructor |
| 631 | = FieldClassDecl->hasConstCopyConstructor(Context); |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | // Otherwise, the implicitly declared copy constructor will have |
| 636 | // the form |
| 637 | // |
| 638 | // X::X(X&) |
| 639 | QualType ArgType = Context.getTypeDeclType(ClassDecl); |
| 640 | if (HasConstCopyConstructor) |
| 641 | ArgType = ArgType.withConst(); |
| 642 | ArgType = Context.getReferenceType(ArgType); |
| 643 | |
| 644 | // An implicitly-declared copy constructor is an inline public |
| 645 | // member of its class. |
| 646 | CXXConstructorDecl *CopyConstructor |
| 647 | = CXXConstructorDecl::Create(Context, ClassDecl, |
| 648 | ClassDecl->getLocation(), |
| 649 | ClassDecl->getIdentifier(), |
| 650 | Context.getFunctionType(Context.VoidTy, |
| 651 | &ArgType, 1, |
| 652 | false, 0), |
| 653 | /*isExplicit=*/false, |
| 654 | /*isInline=*/true, |
| 655 | /*isImplicitlyDeclared=*/true); |
| 656 | CopyConstructor->setAccess(AS_public); |
| 657 | |
| 658 | // Add the parameter to the constructor. |
| 659 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyConstructor, |
| 660 | ClassDecl->getLocation(), |
| 661 | /*IdentifierInfo=*/0, |
| 662 | ArgType, VarDecl::None, 0, 0); |
| 663 | CopyConstructor->setParams(&FromParam, 1); |
| 664 | |
| 665 | ClassDecl->addConstructor(Context, CopyConstructor); |
| 666 | } |
| 667 | |
| 668 | // FIXME: Implicit destructor |
| 669 | // FIXME: Implicit copy assignment operator |
| 670 | } |
| 671 | |
Argyrios Kyrtzidis | 5b7f0c8 | 2008-08-09 00:39:29 +0000 | [diff] [blame] | 672 | void Sema::ActOnFinishCXXClassDef(DeclTy *D) { |
Argyrios Kyrtzidis | a4755c6 | 2008-08-09 00:58:37 +0000 | [diff] [blame] | 673 | CXXRecordDecl *Rec = cast<CXXRecordDecl>(static_cast<Decl *>(D)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 674 | FieldCollector->FinishClass(); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 675 | AddImplicitlyDeclaredMembersToClass(Rec); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 676 | PopDeclContext(); |
Argyrios Kyrtzidis | a4755c6 | 2008-08-09 00:58:37 +0000 | [diff] [blame] | 677 | |
| 678 | // Everything, including inline method definitions, have been parsed. |
| 679 | // Let the consumer know of the new TagDecl definition. |
| 680 | Consumer.HandleTagDeclDefinition(Rec); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 681 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 682 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 683 | /// ActOnConstructorDeclarator - Called by ActOnDeclarator to complete |
| 684 | /// the declaration of the given C++ constructor ConDecl that was |
| 685 | /// built from declarator D. This routine is responsible for checking |
| 686 | /// that the newly-created constructor declaration is well-formed and |
| 687 | /// for recording it in the C++ class. Example: |
| 688 | /// |
| 689 | /// @code |
| 690 | /// class X { |
| 691 | /// X(); // X::X() will be the ConDecl. |
| 692 | /// }; |
| 693 | /// @endcode |
| 694 | Sema::DeclTy *Sema::ActOnConstructorDeclarator(CXXConstructorDecl *ConDecl) { |
| 695 | assert(ConDecl && "Expected to receive a constructor declaration"); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 696 | |
| 697 | // Check default arguments on the constructor |
| 698 | CheckCXXDefaultArguments(ConDecl); |
| 699 | |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 700 | CXXRecordDecl *ClassDecl = dyn_cast_or_null<CXXRecordDecl>(CurContext); |
| 701 | if (!ClassDecl) { |
| 702 | ConDecl->setInvalidDecl(); |
| 703 | return ConDecl; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 704 | } |
| 705 | |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 706 | // Make sure this constructor is an overload of the existing |
| 707 | // constructors. |
| 708 | OverloadedFunctionDecl::function_iterator MatchedDecl; |
| 709 | if (!IsOverload(ConDecl, ClassDecl->getConstructors(), MatchedDecl)) { |
| 710 | Diag(ConDecl->getLocation(), |
| 711 | diag::err_constructor_redeclared, |
| 712 | SourceRange(ConDecl->getLocation())); |
| 713 | Diag((*MatchedDecl)->getLocation(), |
| 714 | diag::err_previous_declaration, |
| 715 | SourceRange((*MatchedDecl)->getLocation())); |
| 716 | ConDecl->setInvalidDecl(); |
| 717 | return ConDecl; |
| 718 | } |
| 719 | |
| 720 | |
| 721 | // C++ [class.copy]p3: |
| 722 | // A declaration of a constructor for a class X is ill-formed if |
| 723 | // its first parameter is of type (optionally cv-qualified) X and |
| 724 | // either there are no other parameters or else all other |
| 725 | // parameters have default arguments. |
| 726 | if ((ConDecl->getNumParams() == 1) || |
| 727 | (ConDecl->getNumParams() > 1 && |
| 728 | ConDecl->getParamDecl(1)->getDefaultArg() != 0)) { |
| 729 | QualType ParamType = ConDecl->getParamDecl(0)->getType(); |
| 730 | QualType ClassTy = Context.getTagDeclType( |
| 731 | const_cast<CXXRecordDecl*>(ConDecl->getParent())); |
| 732 | if (Context.getCanonicalType(ParamType).getUnqualifiedType() == ClassTy) { |
| 733 | Diag(ConDecl->getLocation(), |
| 734 | diag::err_constructor_byvalue_arg, |
| 735 | SourceRange(ConDecl->getParamDecl(0)->getLocation())); |
| 736 | ConDecl->setInvalidDecl(); |
| 737 | return 0; |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | // Add this constructor to the set of constructors of the current |
| 742 | // class. |
| 743 | ClassDecl->addConstructor(Context, ConDecl); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 744 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 745 | return (DeclTy *)ConDecl; |
| 746 | } |
| 747 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 748 | //===----------------------------------------------------------------------===// |
| 749 | // Namespace Handling |
| 750 | //===----------------------------------------------------------------------===// |
| 751 | |
| 752 | /// ActOnStartNamespaceDef - This is called at the start of a namespace |
| 753 | /// definition. |
| 754 | Sema::DeclTy *Sema::ActOnStartNamespaceDef(Scope *NamespcScope, |
| 755 | SourceLocation IdentLoc, |
| 756 | IdentifierInfo *II, |
| 757 | SourceLocation LBrace) { |
| 758 | NamespaceDecl *Namespc = |
| 759 | NamespaceDecl::Create(Context, CurContext, IdentLoc, II); |
| 760 | Namespc->setLBracLoc(LBrace); |
| 761 | |
| 762 | Scope *DeclRegionScope = NamespcScope->getParent(); |
| 763 | |
| 764 | if (II) { |
| 765 | // C++ [namespace.def]p2: |
| 766 | // The identifier in an original-namespace-definition shall not have been |
| 767 | // previously defined in the declarative region in which the |
| 768 | // original-namespace-definition appears. The identifier in an |
| 769 | // original-namespace-definition is the name of the namespace. Subsequently |
| 770 | // in that declarative region, it is treated as an original-namespace-name. |
| 771 | |
| 772 | Decl *PrevDecl = |
Argyrios Kyrtzidis | 154d8e2 | 2008-10-14 18:28:48 +0000 | [diff] [blame] | 773 | LookupDecl(II, Decl::IDNS_Tag | Decl::IDNS_Ordinary, DeclRegionScope, |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 774 | /*enableLazyBuiltinCreation=*/false); |
| 775 | |
Argyrios Kyrtzidis | 2fac626 | 2008-09-10 02:11:07 +0000 | [diff] [blame] | 776 | if (PrevDecl && isDeclInScope(PrevDecl, CurContext, DeclRegionScope)) { |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 777 | if (NamespaceDecl *OrigNS = dyn_cast<NamespaceDecl>(PrevDecl)) { |
| 778 | // This is an extended namespace definition. |
| 779 | // Attach this namespace decl to the chain of extended namespace |
| 780 | // definitions. |
| 781 | NamespaceDecl *NextNS = OrigNS; |
| 782 | while (NextNS->getNextNamespace()) |
| 783 | NextNS = NextNS->getNextNamespace(); |
| 784 | |
| 785 | NextNS->setNextNamespace(Namespc); |
| 786 | Namespc->setOriginalNamespace(OrigNS); |
| 787 | |
| 788 | // We won't add this decl to the current scope. We want the namespace |
| 789 | // name to return the original namespace decl during a name lookup. |
| 790 | } else { |
| 791 | // This is an invalid name redefinition. |
| 792 | Diag(Namespc->getLocation(), diag::err_redefinition_different_kind, |
| 793 | Namespc->getName()); |
| 794 | Diag(PrevDecl->getLocation(), diag::err_previous_definition); |
| 795 | Namespc->setInvalidDecl(); |
| 796 | // Continue on to push Namespc as current DeclContext and return it. |
| 797 | } |
| 798 | } else { |
| 799 | // This namespace name is declared for the first time. |
| 800 | PushOnScopeChains(Namespc, DeclRegionScope); |
| 801 | } |
| 802 | } |
| 803 | else { |
| 804 | // FIXME: Handle anonymous namespaces |
| 805 | } |
| 806 | |
| 807 | // Although we could have an invalid decl (i.e. the namespace name is a |
| 808 | // redefinition), push it as current DeclContext and try to continue parsing. |
| 809 | PushDeclContext(Namespc->getOriginalNamespace()); |
| 810 | return Namespc; |
| 811 | } |
| 812 | |
| 813 | /// ActOnFinishNamespaceDef - This callback is called after a namespace is |
| 814 | /// exited. Decl is the DeclTy returned by ActOnStartNamespaceDef. |
| 815 | void Sema::ActOnFinishNamespaceDef(DeclTy *D, SourceLocation RBrace) { |
| 816 | Decl *Dcl = static_cast<Decl *>(D); |
| 817 | NamespaceDecl *Namespc = dyn_cast_or_null<NamespaceDecl>(Dcl); |
| 818 | assert(Namespc && "Invalid parameter, expected NamespaceDecl"); |
| 819 | Namespc->setRBracLoc(RBrace); |
| 820 | PopDeclContext(); |
| 821 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 822 | |
| 823 | |
| 824 | /// AddCXXDirectInitializerToDecl - This action is called immediately after |
| 825 | /// ActOnDeclarator, when a C++ direct initializer is present. |
| 826 | /// e.g: "int x(1);" |
| 827 | void Sema::AddCXXDirectInitializerToDecl(DeclTy *Dcl, SourceLocation LParenLoc, |
| 828 | ExprTy **ExprTys, unsigned NumExprs, |
| 829 | SourceLocation *CommaLocs, |
| 830 | SourceLocation RParenLoc) { |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 831 | assert(NumExprs != 0 && ExprTys && "missing expressions"); |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 832 | Decl *RealDecl = static_cast<Decl *>(Dcl); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 833 | |
| 834 | // If there is no declaration, there was an error parsing it. Just ignore |
| 835 | // the initializer. |
| 836 | if (RealDecl == 0) { |
Ted Kremenek | 15f6139 | 2008-10-06 20:35:04 +0000 | [diff] [blame] | 837 | for (unsigned i = 0; i != NumExprs; ++i) |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 838 | delete static_cast<Expr *>(ExprTys[i]); |
| 839 | return; |
| 840 | } |
| 841 | |
| 842 | VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl); |
| 843 | if (!VDecl) { |
| 844 | Diag(RealDecl->getLocation(), diag::err_illegal_initializer); |
| 845 | RealDecl->setInvalidDecl(); |
| 846 | return; |
| 847 | } |
| 848 | |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 849 | // We will treat direct-initialization as a copy-initialization: |
| 850 | // int x(1); -as-> int x = 1; |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 851 | // ClassType x(a,b,c); -as-> ClassType x = ClassType(a,b,c); |
| 852 | // |
| 853 | // Clients that want to distinguish between the two forms, can check for |
| 854 | // direct initializer using VarDecl::hasCXXDirectInitializer(). |
| 855 | // A major benefit is that clients that don't particularly care about which |
| 856 | // exactly form was it (like the CodeGen) can handle both cases without |
| 857 | // special case code. |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 858 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 859 | // C++ 8.5p11: |
| 860 | // The form of initialization (using parentheses or '=') is generally |
| 861 | // insignificant, but does matter when the entity being initialized has a |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 862 | // class type. |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 863 | QualType DeclInitType = VDecl->getType(); |
| 864 | if (const ArrayType *Array = Context.getAsArrayType(DeclInitType)) |
| 865 | DeclInitType = Array->getElementType(); |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 866 | |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 867 | if (VDecl->getType()->isRecordType()) { |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 868 | CXXConstructorDecl *Constructor |
| 869 | = PerformDirectInitForClassType(DeclInitType, (Expr **)ExprTys, NumExprs, |
| 870 | VDecl->getLocation(), |
| 871 | SourceRange(VDecl->getLocation(), |
| 872 | RParenLoc), |
| 873 | VDecl->getName(), |
| 874 | /*HasInitializer=*/true); |
| 875 | if (!Constructor) { |
| 876 | RealDecl->setInvalidDecl(); |
| 877 | } |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 878 | return; |
| 879 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 880 | |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 881 | if (NumExprs > 1) { |
| 882 | Diag(CommaLocs[0], diag::err_builtin_direct_init_more_than_one_arg, |
| 883 | SourceRange(VDecl->getLocation(), RParenLoc)); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 884 | RealDecl->setInvalidDecl(); |
| 885 | return; |
| 886 | } |
| 887 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 888 | // Let clients know that initialization was done with a direct initializer. |
| 889 | VDecl->setCXXDirectInitializer(true); |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 890 | |
| 891 | assert(NumExprs == 1 && "Expected 1 expression"); |
| 892 | // Set the init expression, handles conversions. |
| 893 | AddInitializerToDecl(Dcl, ExprTys[0]); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 894 | } |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 895 | |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 896 | /// PerformDirectInitForClassType - Perform direct-initialization (C++ |
| 897 | /// [dcl.init]) for a value of the given class type with the given set |
| 898 | /// of arguments (@p Args). @p Loc is the location in the source code |
| 899 | /// where the initializer occurs (e.g., a declaration, member |
| 900 | /// initializer, functional cast, etc.) while @p Range covers the |
| 901 | /// whole initialization. @p HasInitializer is true if the initializer |
| 902 | /// was actually written in the source code. When successful, returns |
| 903 | /// the constructor that will be used to perform the initialization; |
| 904 | /// when the initialization fails, emits a diagnostic and returns null. |
| 905 | CXXConstructorDecl * |
| 906 | Sema::PerformDirectInitForClassType(QualType ClassType, |
| 907 | Expr **Args, unsigned NumArgs, |
| 908 | SourceLocation Loc, SourceRange Range, |
| 909 | std::string InitEntity, |
| 910 | bool HasInitializer) { |
| 911 | const RecordType *ClassRec = ClassType->getAsRecordType(); |
| 912 | assert(ClassRec && "Can only initialize a class type here"); |
| 913 | |
| 914 | // C++ [dcl.init]p14: |
| 915 | // |
| 916 | // If the initialization is direct-initialization, or if it is |
| 917 | // copy-initialization where the cv-unqualified version of the |
| 918 | // source type is the same class as, or a derived class of, the |
| 919 | // class of the destination, constructors are considered. The |
| 920 | // applicable constructors are enumerated (13.3.1.3), and the |
| 921 | // best one is chosen through overload resolution (13.3). The |
| 922 | // constructor so selected is called to initialize the object, |
| 923 | // with the initializer expression(s) as its argument(s). If no |
| 924 | // constructor applies, or the overload resolution is ambiguous, |
| 925 | // the initialization is ill-formed. |
| 926 | // |
| 927 | // FIXME: We don't check cv-qualifiers on the class type, because we |
| 928 | // don't yet keep track of whether a class type is a POD class type |
| 929 | // (or a "trivial" class type, as is used in C++0x). |
| 930 | const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(ClassRec->getDecl()); |
| 931 | OverloadCandidateSet CandidateSet; |
| 932 | OverloadCandidateSet::iterator Best; |
| 933 | AddOverloadCandidates(ClassDecl->getConstructors(), Args, NumArgs, |
| 934 | CandidateSet); |
| 935 | switch (BestViableFunction(CandidateSet, Best)) { |
| 936 | case OR_Success: |
| 937 | // We found a constructor. Return it. |
| 938 | return cast<CXXConstructorDecl>(Best->Function); |
| 939 | |
| 940 | case OR_No_Viable_Function: |
| 941 | if (CandidateSet.empty()) |
| 942 | Diag(Loc, diag::err_ovl_no_viable_function_in_init, |
| 943 | InitEntity, Range); |
| 944 | else { |
| 945 | Diag(Loc, diag::err_ovl_no_viable_function_in_init_with_cands, |
| 946 | InitEntity, Range); |
| 947 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 948 | } |
| 949 | return 0; |
| 950 | |
| 951 | case OR_Ambiguous: |
| 952 | Diag(Loc, diag::err_ovl_ambiguous_init, |
| 953 | InitEntity, Range); |
| 954 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 955 | return 0; |
| 956 | } |
| 957 | |
| 958 | return 0; |
| 959 | } |
| 960 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 961 | /// CompareReferenceRelationship - Compare the two types T1 and T2 to |
| 962 | /// determine whether they are reference-related, |
| 963 | /// reference-compatible, reference-compatible with added |
| 964 | /// qualification, or incompatible, for use in C++ initialization by |
| 965 | /// reference (C++ [dcl.ref.init]p4). Neither type can be a reference |
| 966 | /// type, and the first type (T1) is the pointee type of the reference |
| 967 | /// type being initialized. |
| 968 | Sema::ReferenceCompareResult |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 969 | Sema::CompareReferenceRelationship(QualType T1, QualType T2, |
| 970 | bool& DerivedToBase) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 971 | assert(!T1->isReferenceType() && "T1 must be the pointee type of the reference type"); |
| 972 | assert(!T2->isReferenceType() && "T2 cannot be a reference type"); |
| 973 | |
| 974 | T1 = Context.getCanonicalType(T1); |
| 975 | T2 = Context.getCanonicalType(T2); |
| 976 | QualType UnqualT1 = T1.getUnqualifiedType(); |
| 977 | QualType UnqualT2 = T2.getUnqualifiedType(); |
| 978 | |
| 979 | // C++ [dcl.init.ref]p4: |
| 980 | // Given types “cv1 T1” and “cv2 T2,” “cv1 T1” is |
| 981 | // reference-related to “cv2 T2” if T1 is the same type as T2, or |
| 982 | // T1 is a base class of T2. |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 983 | if (UnqualT1 == UnqualT2) |
| 984 | DerivedToBase = false; |
| 985 | else if (IsDerivedFrom(UnqualT2, UnqualT1)) |
| 986 | DerivedToBase = true; |
| 987 | else |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 988 | return Ref_Incompatible; |
| 989 | |
| 990 | // At this point, we know that T1 and T2 are reference-related (at |
| 991 | // least). |
| 992 | |
| 993 | // C++ [dcl.init.ref]p4: |
| 994 | // "cv1 T1” is reference-compatible with “cv2 T2” if T1 is |
| 995 | // reference-related to T2 and cv1 is the same cv-qualification |
| 996 | // as, or greater cv-qualification than, cv2. For purposes of |
| 997 | // overload resolution, cases for which cv1 is greater |
| 998 | // cv-qualification than cv2 are identified as |
| 999 | // reference-compatible with added qualification (see 13.3.3.2). |
| 1000 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 1001 | return Ref_Compatible; |
| 1002 | else if (T1.isMoreQualifiedThan(T2)) |
| 1003 | return Ref_Compatible_With_Added_Qualification; |
| 1004 | else |
| 1005 | return Ref_Related; |
| 1006 | } |
| 1007 | |
| 1008 | /// CheckReferenceInit - Check the initialization of a reference |
| 1009 | /// variable with the given initializer (C++ [dcl.init.ref]). Init is |
| 1010 | /// the initializer (either a simple initializer or an initializer |
Douglas Gregor | 3205a78 | 2008-10-29 23:31:03 +0000 | [diff] [blame] | 1011 | /// list), and DeclType is the type of the declaration. When ICS is |
| 1012 | /// non-null, this routine will compute the implicit conversion |
| 1013 | /// sequence according to C++ [over.ics.ref] and will not produce any |
| 1014 | /// diagnostics; when ICS is null, it will emit diagnostics when any |
| 1015 | /// errors are found. Either way, a return value of true indicates |
| 1016 | /// that there was a failure, a return value of false indicates that |
| 1017 | /// the reference initialization succeeded. |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1018 | /// |
| 1019 | /// When @p SuppressUserConversions, user-defined conversions are |
| 1020 | /// suppressed. |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1021 | bool |
| 1022 | Sema::CheckReferenceInit(Expr *&Init, QualType &DeclType, |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1023 | ImplicitConversionSequence *ICS, |
| 1024 | bool SuppressUserConversions) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1025 | assert(DeclType->isReferenceType() && "Reference init needs a reference"); |
| 1026 | |
| 1027 | QualType T1 = DeclType->getAsReferenceType()->getPointeeType(); |
| 1028 | QualType T2 = Init->getType(); |
| 1029 | |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1030 | // Compute some basic properties of the types and the initializer. |
| 1031 | bool DerivedToBase = false; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1032 | Expr::isLvalueResult InitLvalue = Init->isLvalue(Context); |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1033 | ReferenceCompareResult RefRelationship |
| 1034 | = CompareReferenceRelationship(T1, T2, DerivedToBase); |
| 1035 | |
| 1036 | // Most paths end in a failed conversion. |
| 1037 | if (ICS) |
| 1038 | ICS->ConversionKind = ImplicitConversionSequence::BadConversion; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1039 | |
| 1040 | // C++ [dcl.init.ref]p5: |
| 1041 | // A reference to type “cv1 T1” is initialized by an expression |
| 1042 | // of type “cv2 T2” as follows: |
| 1043 | |
| 1044 | // -- If the initializer expression |
| 1045 | |
| 1046 | bool BindsDirectly = false; |
| 1047 | // -- is an lvalue (but is not a bit-field), and “cv1 T1” is |
| 1048 | // reference-compatible with “cv2 T2,” or |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1049 | // |
| 1050 | // Note that the bit-field check is skipped if we are just computing |
| 1051 | // the implicit conversion sequence (C++ [over.best.ics]p2). |
| 1052 | if (InitLvalue == Expr::LV_Valid && (ICS || !Init->isBitField()) && |
| 1053 | RefRelationship >= Ref_Compatible_With_Added_Qualification) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1054 | BindsDirectly = true; |
| 1055 | |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1056 | if (ICS) { |
| 1057 | // C++ [over.ics.ref]p1: |
| 1058 | // When a parameter of reference type binds directly (8.5.3) |
| 1059 | // to an argument expression, the implicit conversion sequence |
| 1060 | // is the identity conversion, unless the argument expression |
| 1061 | // has a type that is a derived class of the parameter type, |
| 1062 | // in which case the implicit conversion sequence is a |
| 1063 | // derived-to-base Conversion (13.3.3.1). |
| 1064 | ICS->ConversionKind = ImplicitConversionSequence::StandardConversion; |
| 1065 | ICS->Standard.First = ICK_Identity; |
| 1066 | ICS->Standard.Second = DerivedToBase? ICK_Derived_To_Base : ICK_Identity; |
| 1067 | ICS->Standard.Third = ICK_Identity; |
| 1068 | ICS->Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 1069 | ICS->Standard.ToTypePtr = T1.getAsOpaquePtr(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1070 | ICS->Standard.ReferenceBinding = true; |
| 1071 | ICS->Standard.DirectBinding = true; |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1072 | |
| 1073 | // Nothing more to do: the inaccessibility/ambiguity check for |
| 1074 | // derived-to-base conversions is suppressed when we're |
| 1075 | // computing the implicit conversion sequence (C++ |
| 1076 | // [over.best.ics]p2). |
| 1077 | return false; |
| 1078 | } else { |
| 1079 | // Perform the conversion. |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1080 | // FIXME: Binding to a subobject of the lvalue is going to require |
| 1081 | // more AST annotation than this. |
| 1082 | ImpCastExprToType(Init, T1); |
| 1083 | } |
| 1084 | } |
| 1085 | |
| 1086 | // -- has a class type (i.e., T2 is a class type) and can be |
| 1087 | // implicitly converted to an lvalue of type “cv3 T3,” |
| 1088 | // where “cv1 T1” is reference-compatible with “cv3 T3” |
| 1089 | // 92) (this conversion is selected by enumerating the |
| 1090 | // applicable conversion functions (13.3.1.6) and choosing |
| 1091 | // the best one through overload resolution (13.3)), |
| 1092 | // FIXME: Implement this second bullet, once we have conversion |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1093 | // functions. Also remember C++ [over.ics.ref]p1, second part. |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1094 | |
| 1095 | if (BindsDirectly) { |
| 1096 | // C++ [dcl.init.ref]p4: |
| 1097 | // [...] In all cases where the reference-related or |
| 1098 | // reference-compatible relationship of two types is used to |
| 1099 | // establish the validity of a reference binding, and T1 is a |
| 1100 | // base class of T2, a program that necessitates such a binding |
| 1101 | // is ill-formed if T1 is an inaccessible (clause 11) or |
| 1102 | // ambiguous (10.2) base class of T2. |
| 1103 | // |
| 1104 | // Note that we only check this condition when we're allowed to |
| 1105 | // complain about errors, because we should not be checking for |
| 1106 | // ambiguity (or inaccessibility) unless the reference binding |
| 1107 | // actually happens. |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1108 | if (DerivedToBase) |
| 1109 | return CheckDerivedToBaseConversion(T2, T1, |
| 1110 | Init->getSourceRange().getBegin(), |
| 1111 | Init->getSourceRange()); |
| 1112 | else |
| 1113 | return false; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
| 1116 | // -- Otherwise, the reference shall be to a non-volatile const |
| 1117 | // type (i.e., cv1 shall be const). |
| 1118 | if (T1.getCVRQualifiers() != QualType::Const) { |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1119 | if (!ICS) |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1120 | Diag(Init->getSourceRange().getBegin(), |
| 1121 | diag::err_not_reference_to_const_init, |
| 1122 | T1.getAsString(), |
| 1123 | InitLvalue != Expr::LV_Valid? "temporary" : "value", |
| 1124 | T2.getAsString(), Init->getSourceRange()); |
| 1125 | return true; |
| 1126 | } |
| 1127 | |
| 1128 | // -- If the initializer expression is an rvalue, with T2 a |
| 1129 | // class type, and “cv1 T1” is reference-compatible with |
| 1130 | // “cv2 T2,” the reference is bound in one of the |
| 1131 | // following ways (the choice is implementation-defined): |
| 1132 | // |
| 1133 | // -- The reference is bound to the object represented by |
| 1134 | // the rvalue (see 3.10) or to a sub-object within that |
| 1135 | // object. |
| 1136 | // |
| 1137 | // -- A temporary of type “cv1 T2” [sic] is created, and |
| 1138 | // a constructor is called to copy the entire rvalue |
| 1139 | // object into the temporary. The reference is bound to |
| 1140 | // the temporary or to a sub-object within the |
| 1141 | // temporary. |
| 1142 | // |
| 1143 | // |
| 1144 | // The constructor that would be used to make the copy |
| 1145 | // shall be callable whether or not the copy is actually |
| 1146 | // done. |
| 1147 | // |
| 1148 | // Note that C++0x [dcl.ref.init]p5 takes away this implementation |
| 1149 | // freedom, so we will always take the first option and never build |
| 1150 | // a temporary in this case. FIXME: We will, however, have to check |
| 1151 | // for the presence of a copy constructor in C++98/03 mode. |
| 1152 | if (InitLvalue != Expr::LV_Valid && T2->isRecordType() && |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1153 | RefRelationship >= Ref_Compatible_With_Added_Qualification) { |
| 1154 | if (ICS) { |
| 1155 | ICS->ConversionKind = ImplicitConversionSequence::StandardConversion; |
| 1156 | ICS->Standard.First = ICK_Identity; |
| 1157 | ICS->Standard.Second = DerivedToBase? ICK_Derived_To_Base : ICK_Identity; |
| 1158 | ICS->Standard.Third = ICK_Identity; |
| 1159 | ICS->Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 1160 | ICS->Standard.ToTypePtr = T1.getAsOpaquePtr(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1161 | ICS->Standard.ReferenceBinding = true; |
| 1162 | ICS->Standard.DirectBinding = false; |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1163 | } else { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1164 | // FIXME: Binding to a subobject of the rvalue is going to require |
| 1165 | // more AST annotation than this. |
| 1166 | ImpCastExprToType(Init, T1); |
| 1167 | } |
| 1168 | return false; |
| 1169 | } |
| 1170 | |
| 1171 | // -- Otherwise, a temporary of type “cv1 T1” is created and |
| 1172 | // initialized from the initializer expression using the |
| 1173 | // rules for a non-reference copy initialization (8.5). The |
| 1174 | // reference is then bound to the temporary. If T1 is |
| 1175 | // reference-related to T2, cv1 must be the same |
| 1176 | // cv-qualification as, or greater cv-qualification than, |
| 1177 | // cv2; otherwise, the program is ill-formed. |
| 1178 | if (RefRelationship == Ref_Related) { |
| 1179 | // If cv1 == cv2 or cv1 is a greater cv-qualified than cv2, then |
| 1180 | // we would be reference-compatible or reference-compatible with |
| 1181 | // added qualification. But that wasn't the case, so the reference |
| 1182 | // initialization fails. |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1183 | if (!ICS) |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1184 | Diag(Init->getSourceRange().getBegin(), |
| 1185 | diag::err_reference_init_drops_quals, |
| 1186 | T1.getAsString(), |
| 1187 | InitLvalue != Expr::LV_Valid? "temporary" : "value", |
| 1188 | T2.getAsString(), Init->getSourceRange()); |
| 1189 | return true; |
| 1190 | } |
| 1191 | |
| 1192 | // Actually try to convert the initializer to T1. |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1193 | if (ICS) { |
| 1194 | /// C++ [over.ics.ref]p2: |
| 1195 | /// |
| 1196 | /// When a parameter of reference type is not bound directly to |
| 1197 | /// an argument expression, the conversion sequence is the one |
| 1198 | /// required to convert the argument expression to the |
| 1199 | /// underlying type of the reference according to |
| 1200 | /// 13.3.3.1. Conceptually, this conversion sequence corresponds |
| 1201 | /// to copy-initializing a temporary of the underlying type with |
| 1202 | /// the argument expression. Any difference in top-level |
| 1203 | /// cv-qualification is subsumed by the initialization itself |
| 1204 | /// and does not constitute a conversion. |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1205 | *ICS = TryImplicitConversion(Init, T1, SuppressUserConversions); |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1206 | return ICS->ConversionKind == ImplicitConversionSequence::BadConversion; |
| 1207 | } else { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1208 | return PerformImplicitConversion(Init, T1); |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1209 | } |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1210 | } |