Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 1 | //===------ SemaDeclCXX.cpp - Semantic Analysis for C++ Declarations ------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for C++ declarations. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 15 | #include "SemaInherit.h" |
Argyrios Kyrtzidis | a4755c6 | 2008-08-09 00:58:37 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTConsumer.h" |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 18 | #include "clang/AST/TypeOrdering.h" |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 19 | #include "clang/AST/StmtVisitor.h" |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 20 | #include "clang/Lex/Preprocessor.h" |
Daniel Dunbar | 12bc692 | 2008-08-11 03:27:53 +0000 | [diff] [blame] | 21 | #include "clang/Parse/DeclSpec.h" |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Compiler.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 24 | #include <algorithm> // for std::equal |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 25 | #include <map> |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace clang; |
| 28 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 29 | //===----------------------------------------------------------------------===// |
| 30 | // CheckDefaultArgumentVisitor |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 33 | namespace { |
| 34 | /// CheckDefaultArgumentVisitor - C++ [dcl.fct.default] Traverses |
| 35 | /// the default argument of a parameter to determine whether it |
| 36 | /// contains any ill-formed subexpressions. For example, this will |
| 37 | /// diagnose the use of local variables or parameters within the |
| 38 | /// default argument expression. |
| 39 | class VISIBILITY_HIDDEN CheckDefaultArgumentVisitor |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 40 | : public StmtVisitor<CheckDefaultArgumentVisitor, bool> { |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 41 | Expr *DefaultArg; |
| 42 | Sema *S; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 43 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 44 | public: |
| 45 | CheckDefaultArgumentVisitor(Expr *defarg, Sema *s) |
| 46 | : DefaultArg(defarg), S(s) {} |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 48 | bool VisitExpr(Expr *Node); |
| 49 | bool VisitDeclRefExpr(DeclRefExpr *DRE); |
Douglas Gregor | 796da18 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 50 | bool VisitCXXThisExpr(CXXThisExpr *ThisE); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 51 | }; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 52 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 53 | /// VisitExpr - Visit all of the children of this expression. |
| 54 | bool CheckDefaultArgumentVisitor::VisitExpr(Expr *Node) { |
| 55 | bool IsInvalid = false; |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 56 | for (Stmt::child_iterator I = Node->child_begin(), |
| 57 | E = Node->child_end(); I != E; ++I) |
| 58 | IsInvalid |= Visit(*I); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 59 | return IsInvalid; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 62 | /// VisitDeclRefExpr - Visit a reference to a declaration, to |
| 63 | /// determine whether this declaration can be used in the default |
| 64 | /// argument expression. |
| 65 | bool CheckDefaultArgumentVisitor::VisitDeclRefExpr(DeclRefExpr *DRE) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 66 | NamedDecl *Decl = DRE->getDecl(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 67 | if (ParmVarDecl *Param = dyn_cast<ParmVarDecl>(Decl)) { |
| 68 | // C++ [dcl.fct.default]p9 |
| 69 | // Default arguments are evaluated each time the function is |
| 70 | // called. The order of evaluation of function arguments is |
| 71 | // unspecified. Consequently, parameters of a function shall not |
| 72 | // be used in default argument expressions, even if they are not |
| 73 | // evaluated. Parameters of a function declared before a default |
| 74 | // argument expression are in scope and can hide namespace and |
| 75 | // class member names. |
| 76 | return S->Diag(DRE->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 77 | diag::err_param_default_argument_references_param) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 78 | << Param->getDeclName() << DefaultArg->getSourceRange(); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 79 | } else if (VarDecl *VDecl = dyn_cast<VarDecl>(Decl)) { |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 80 | // C++ [dcl.fct.default]p7 |
| 81 | // Local variables shall not be used in default argument |
| 82 | // expressions. |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 83 | if (VDecl->isBlockVarDecl()) |
| 84 | return S->Diag(DRE->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 85 | diag::err_param_default_argument_references_local) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 86 | << VDecl->getDeclName() << DefaultArg->getSourceRange(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 87 | } |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 88 | |
Douglas Gregor | 3996f23 | 2008-11-04 13:41:56 +0000 | [diff] [blame] | 89 | return false; |
| 90 | } |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 91 | |
Douglas Gregor | 796da18 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 92 | /// VisitCXXThisExpr - Visit a C++ "this" expression. |
| 93 | bool CheckDefaultArgumentVisitor::VisitCXXThisExpr(CXXThisExpr *ThisE) { |
| 94 | // C++ [dcl.fct.default]p8: |
| 95 | // The keyword this shall not be used in a default argument of a |
| 96 | // member function. |
| 97 | return S->Diag(ThisE->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 98 | diag::err_param_default_argument_references_this) |
| 99 | << ThisE->getSourceRange(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 100 | } |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /// ActOnParamDefaultArgument - Check whether the default argument |
| 104 | /// provided for a function parameter is well-formed. If so, attach it |
| 105 | /// to the parameter declaration. |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 106 | void |
| 107 | Sema::ActOnParamDefaultArgument(DeclTy *param, SourceLocation EqualLoc, |
| 108 | ExprTy *defarg) { |
| 109 | ParmVarDecl *Param = (ParmVarDecl *)param; |
| 110 | llvm::OwningPtr<Expr> DefaultArg((Expr *)defarg); |
| 111 | QualType ParamType = Param->getType(); |
| 112 | |
| 113 | // Default arguments are only permitted in C++ |
| 114 | if (!getLangOptions().CPlusPlus) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 115 | Diag(EqualLoc, diag::err_param_default_argument) |
| 116 | << DefaultArg->getSourceRange(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 117 | Param->setInvalidDecl(); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 118 | return; |
| 119 | } |
| 120 | |
| 121 | // C++ [dcl.fct.default]p5 |
| 122 | // A default argument expression is implicitly converted (clause |
| 123 | // 4) to the parameter type. The default argument expression has |
| 124 | // the same semantic constraints as the initializer expression in |
| 125 | // a declaration of a variable of the parameter type, using the |
| 126 | // copy-initialization semantics (8.5). |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 127 | Expr *DefaultArgPtr = DefaultArg.get(); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 128 | bool DefaultInitFailed = CheckInitializerTypes(DefaultArgPtr, ParamType, |
| 129 | EqualLoc, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 130 | Param->getDeclName(), |
| 131 | /*DirectInit=*/false); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 132 | if (DefaultArgPtr != DefaultArg.get()) { |
| 133 | DefaultArg.take(); |
| 134 | DefaultArg.reset(DefaultArgPtr); |
| 135 | } |
Douglas Gregor | eb704f2 | 2008-11-04 13:57:51 +0000 | [diff] [blame] | 136 | if (DefaultInitFailed) { |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 137 | return; |
| 138 | } |
| 139 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 140 | // Check that the default argument is well-formed |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 141 | CheckDefaultArgumentVisitor DefaultArgChecker(DefaultArg.get(), this); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 142 | if (DefaultArgChecker.Visit(DefaultArg.get())) { |
| 143 | Param->setInvalidDecl(); |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 144 | return; |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 145 | } |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 146 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 147 | // Okay: add the default argument to the parameter |
| 148 | Param->setDefaultArg(DefaultArg.take()); |
| 149 | } |
| 150 | |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 151 | /// ActOnParamUnparsedDefaultArgument - We've seen a default |
| 152 | /// argument for a function parameter, but we can't parse it yet |
| 153 | /// because we're inside a class definition. Note that this default |
| 154 | /// argument will be parsed later. |
| 155 | void Sema::ActOnParamUnparsedDefaultArgument(DeclTy *param, |
| 156 | SourceLocation EqualLoc) { |
| 157 | ParmVarDecl *Param = (ParmVarDecl*)param; |
| 158 | if (Param) |
| 159 | Param->setUnparsedDefaultArg(); |
| 160 | } |
| 161 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 162 | /// ActOnParamDefaultArgumentError - Parsing or semantic analysis of |
| 163 | /// the default argument for the parameter param failed. |
| 164 | void Sema::ActOnParamDefaultArgumentError(DeclTy *param) { |
| 165 | ((ParmVarDecl*)param)->setInvalidDecl(); |
| 166 | } |
| 167 | |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 168 | /// CheckExtraCXXDefaultArguments - Check for any extra default |
| 169 | /// arguments in the declarator, which is not a function declaration |
| 170 | /// or definition and therefore is not permitted to have default |
| 171 | /// arguments. This routine should be invoked for every declarator |
| 172 | /// that is not a function declaration or definition. |
| 173 | void Sema::CheckExtraCXXDefaultArguments(Declarator &D) { |
| 174 | // C++ [dcl.fct.default]p3 |
| 175 | // A default argument expression shall be specified only in the |
| 176 | // parameter-declaration-clause of a function declaration or in a |
| 177 | // template-parameter (14.1). It shall not be specified for a |
| 178 | // parameter pack. If it is specified in a |
| 179 | // parameter-declaration-clause, it shall not occur within a |
| 180 | // declarator or abstract-declarator of a parameter-declaration. |
| 181 | for (unsigned i = 0; i < D.getNumTypeObjects(); ++i) { |
| 182 | DeclaratorChunk &chunk = D.getTypeObject(i); |
| 183 | if (chunk.Kind == DeclaratorChunk::Function) { |
| 184 | for (unsigned argIdx = 0; argIdx < chunk.Fun.NumArgs; ++argIdx) { |
| 185 | ParmVarDecl *Param = (ParmVarDecl *)chunk.Fun.ArgInfo[argIdx].Param; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 186 | if (Param->hasUnparsedDefaultArg()) { |
| 187 | CachedTokens *Toks = chunk.Fun.ArgInfo[argIdx].DefaultArgTokens; |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 188 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc) |
| 189 | << SourceRange((*Toks)[1].getLocation(), Toks->back().getLocation()); |
| 190 | delete Toks; |
| 191 | chunk.Fun.ArgInfo[argIdx].DefaultArgTokens = 0; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 192 | } else if (Param->getDefaultArg()) { |
| 193 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc) |
| 194 | << Param->getDefaultArg()->getSourceRange(); |
| 195 | Param->setDefaultArg(0); |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 202 | // MergeCXXFunctionDecl - Merge two declarations of the same C++ |
| 203 | // function, once we already know that they have the same |
| 204 | // type. Subroutine of MergeFunctionDecl. |
| 205 | FunctionDecl * |
| 206 | Sema::MergeCXXFunctionDecl(FunctionDecl *New, FunctionDecl *Old) { |
| 207 | // C++ [dcl.fct.default]p4: |
| 208 | // |
| 209 | // For non-template functions, default arguments can be added in |
| 210 | // later declarations of a function in the same |
| 211 | // scope. Declarations in different scopes have completely |
| 212 | // distinct sets of default arguments. That is, declarations in |
| 213 | // inner scopes do not acquire default arguments from |
| 214 | // declarations in outer scopes, and vice versa. In a given |
| 215 | // function declaration, all parameters subsequent to a |
| 216 | // parameter with a default argument shall have default |
| 217 | // arguments supplied in this or previous declarations. A |
| 218 | // default argument shall not be redefined by a later |
| 219 | // declaration (not even to the same value). |
| 220 | for (unsigned p = 0, NumParams = Old->getNumParams(); p < NumParams; ++p) { |
| 221 | ParmVarDecl *OldParam = Old->getParamDecl(p); |
| 222 | ParmVarDecl *NewParam = New->getParamDecl(p); |
| 223 | |
| 224 | if(OldParam->getDefaultArg() && NewParam->getDefaultArg()) { |
| 225 | Diag(NewParam->getLocation(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 226 | diag::err_param_default_argument_redefinition) |
| 227 | << NewParam->getDefaultArg()->getSourceRange(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 228 | Diag(OldParam->getLocation(), diag::note_previous_definition); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 229 | } else if (OldParam->getDefaultArg()) { |
| 230 | // Merge the old default argument into the new parameter |
| 231 | NewParam->setDefaultArg(OldParam->getDefaultArg()); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | return New; |
| 236 | } |
| 237 | |
| 238 | /// CheckCXXDefaultArguments - Verify that the default arguments for a |
| 239 | /// function declaration are well-formed according to C++ |
| 240 | /// [dcl.fct.default]. |
| 241 | void Sema::CheckCXXDefaultArguments(FunctionDecl *FD) { |
| 242 | unsigned NumParams = FD->getNumParams(); |
| 243 | unsigned p; |
| 244 | |
| 245 | // Find first parameter with a default argument |
| 246 | for (p = 0; p < NumParams; ++p) { |
| 247 | ParmVarDecl *Param = FD->getParamDecl(p); |
| 248 | if (Param->getDefaultArg()) |
| 249 | break; |
| 250 | } |
| 251 | |
| 252 | // C++ [dcl.fct.default]p4: |
| 253 | // In a given function declaration, all parameters |
| 254 | // subsequent to a parameter with a default argument shall |
| 255 | // have default arguments supplied in this or previous |
| 256 | // declarations. A default argument shall not be redefined |
| 257 | // by a later declaration (not even to the same value). |
| 258 | unsigned LastMissingDefaultArg = 0; |
| 259 | for(; p < NumParams; ++p) { |
| 260 | ParmVarDecl *Param = FD->getParamDecl(p); |
| 261 | if (!Param->getDefaultArg()) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 262 | if (Param->isInvalidDecl()) |
| 263 | /* We already complained about this parameter. */; |
| 264 | else if (Param->getIdentifier()) |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 265 | Diag(Param->getLocation(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 266 | diag::err_param_default_argument_missing_name) |
Chris Lattner | 43b628c | 2008-11-19 07:32:16 +0000 | [diff] [blame] | 267 | << Param->getIdentifier(); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 268 | else |
| 269 | Diag(Param->getLocation(), |
| 270 | diag::err_param_default_argument_missing); |
| 271 | |
| 272 | LastMissingDefaultArg = p; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | if (LastMissingDefaultArg > 0) { |
| 277 | // Some default arguments were missing. Clear out all of the |
| 278 | // default arguments up to (and including) the last missing |
| 279 | // default argument, so that we leave the function parameters |
| 280 | // in a semantically valid state. |
| 281 | for (p = 0; p <= LastMissingDefaultArg; ++p) { |
| 282 | ParmVarDecl *Param = FD->getParamDecl(p); |
| 283 | if (Param->getDefaultArg()) { |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 284 | if (!Param->hasUnparsedDefaultArg()) |
| 285 | Param->getDefaultArg()->Destroy(Context); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 286 | Param->setDefaultArg(0); |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 291 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 292 | /// isCurrentClassName - Determine whether the identifier II is the |
| 293 | /// name of the class type currently being defined. In the case of |
| 294 | /// nested classes, this will only return true if II is the name of |
| 295 | /// the innermost class. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 296 | bool Sema::isCurrentClassName(const IdentifierInfo &II, Scope *, |
| 297 | const CXXScopeSpec *SS) { |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 298 | CXXRecordDecl *CurDecl; |
| 299 | if (SS) { |
| 300 | DeclContext *DC = static_cast<DeclContext*>(SS->getScopeRep()); |
| 301 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(DC); |
| 302 | } else |
| 303 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(CurContext); |
| 304 | |
| 305 | if (CurDecl) |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 306 | return &II == CurDecl->getIdentifier(); |
| 307 | else |
| 308 | return false; |
| 309 | } |
| 310 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 311 | /// ActOnBaseSpecifier - Parsed a base specifier. A base specifier is |
| 312 | /// one entry in the base class list of a class specifier, for |
| 313 | /// example: |
| 314 | /// class foo : public bar, virtual private baz { |
| 315 | /// 'public bar' and 'virtual private baz' are each base-specifiers. |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 316 | Sema::BaseResult |
| 317 | Sema::ActOnBaseSpecifier(DeclTy *classdecl, SourceRange SpecifierRange, |
| 318 | bool Virtual, AccessSpecifier Access, |
| 319 | TypeTy *basetype, SourceLocation BaseLoc) { |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 320 | CXXRecordDecl *Decl = (CXXRecordDecl*)classdecl; |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 321 | QualType BaseType = Context.getTypeDeclType((TypeDecl*)basetype); |
| 322 | |
| 323 | // Base specifiers must be record types. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 324 | if (!BaseType->isRecordType()) |
| 325 | return Diag(BaseLoc, diag::err_base_must_be_class) << SpecifierRange; |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 326 | |
| 327 | // C++ [class.union]p1: |
| 328 | // A union shall not be used as a base class. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 329 | if (BaseType->isUnionType()) |
| 330 | return Diag(BaseLoc, diag::err_union_as_base_class) << SpecifierRange; |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 331 | |
| 332 | // C++ [class.union]p1: |
| 333 | // A union shall not have base classes. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 334 | if (Decl->isUnion()) |
| 335 | return Diag(Decl->getLocation(), diag::err_base_clause_on_union) |
| 336 | << SpecifierRange; |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 337 | |
| 338 | // C++ [class.derived]p2: |
| 339 | // The class-name in a base-specifier shall not be an incompletely |
| 340 | // defined class. |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 341 | if (DiagnoseIncompleteType(BaseLoc, BaseType, diag::err_incomplete_base_class, |
| 342 | SpecifierRange)) |
| 343 | return true; |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 344 | |
Sebastian Redl | d93f0dd | 2008-11-06 15:59:35 +0000 | [diff] [blame] | 345 | // If the base class is polymorphic, the new one is, too. |
| 346 | RecordDecl *BaseDecl = BaseType->getAsRecordType()->getDecl(); |
| 347 | assert(BaseDecl && "Record type has no declaration"); |
| 348 | BaseDecl = BaseDecl->getDefinition(Context); |
| 349 | assert(BaseDecl && "Base type is not incomplete, but has no definition"); |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 350 | if (cast<CXXRecordDecl>(BaseDecl)->isPolymorphic()) |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 351 | Decl->setPolymorphic(true); |
| 352 | |
| 353 | // C++ [dcl.init.aggr]p1: |
| 354 | // An aggregate is [...] a class with [...] no base classes [...]. |
| 355 | Decl->setAggregate(false); |
| 356 | Decl->setPOD(false); |
Sebastian Redl | d93f0dd | 2008-11-06 15:59:35 +0000 | [diff] [blame] | 357 | |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 358 | // Create the base specifier. |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 359 | return new CXXBaseSpecifier(SpecifierRange, Virtual, |
| 360 | BaseType->isClassType(), Access, BaseType); |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 361 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 362 | |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 363 | /// ActOnBaseSpecifiers - Attach the given base specifiers to the |
| 364 | /// class, after checking whether there are any duplicate base |
| 365 | /// classes. |
| 366 | void Sema::ActOnBaseSpecifiers(DeclTy *ClassDecl, BaseTy **Bases, |
| 367 | unsigned NumBases) { |
| 368 | if (NumBases == 0) |
| 369 | return; |
| 370 | |
| 371 | // Used to keep track of which base types we have already seen, so |
| 372 | // that we can properly diagnose redundant direct base types. Note |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 373 | // that the key is always the unqualified canonical type of the base |
| 374 | // class. |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 375 | std::map<QualType, CXXBaseSpecifier*, QualTypeOrdering> KnownBaseTypes; |
| 376 | |
| 377 | // Copy non-redundant base specifiers into permanent storage. |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 378 | CXXBaseSpecifier **BaseSpecs = (CXXBaseSpecifier **)Bases; |
| 379 | unsigned NumGoodBases = 0; |
| 380 | for (unsigned idx = 0; idx < NumBases; ++idx) { |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 381 | QualType NewBaseType |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 382 | = Context.getCanonicalType(BaseSpecs[idx]->getType()); |
| 383 | NewBaseType = NewBaseType.getUnqualifiedType(); |
| 384 | |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 385 | if (KnownBaseTypes[NewBaseType]) { |
| 386 | // C++ [class.mi]p3: |
| 387 | // A class shall not be specified as a direct base class of a |
| 388 | // derived class more than once. |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 389 | Diag(BaseSpecs[idx]->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 390 | diag::err_duplicate_base_class) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 391 | << KnownBaseTypes[NewBaseType]->getType() |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 392 | << BaseSpecs[idx]->getSourceRange(); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 393 | |
| 394 | // Delete the duplicate base class specifier; we're going to |
| 395 | // overwrite its pointer later. |
| 396 | delete BaseSpecs[idx]; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 397 | } else { |
| 398 | // Okay, add this new base class. |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 399 | KnownBaseTypes[NewBaseType] = BaseSpecs[idx]; |
| 400 | BaseSpecs[NumGoodBases++] = BaseSpecs[idx]; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | |
| 404 | // Attach the remaining base class specifiers to the derived class. |
| 405 | CXXRecordDecl *Decl = (CXXRecordDecl*)ClassDecl; |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 406 | Decl->setBases(BaseSpecs, NumGoodBases); |
| 407 | |
| 408 | // Delete the remaining (good) base class specifiers, since their |
| 409 | // data has been copied into the CXXRecordDecl. |
| 410 | for (unsigned idx = 0; idx < NumGoodBases; ++idx) |
| 411 | delete BaseSpecs[idx]; |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 412 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 413 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 414 | //===----------------------------------------------------------------------===// |
| 415 | // C++ class member Handling |
| 416 | //===----------------------------------------------------------------------===// |
| 417 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 418 | /// ActOnCXXMemberDeclarator - This is invoked when a C++ class member |
| 419 | /// declarator is parsed. 'AS' is the access specifier, 'BW' specifies the |
| 420 | /// bitfield width if there is one and 'InitExpr' specifies the initializer if |
| 421 | /// any. 'LastInGroup' is non-null for cases where one declspec has multiple |
| 422 | /// declarators on it. |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 423 | Sema::DeclTy * |
| 424 | Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D, |
| 425 | ExprTy *BW, ExprTy *InitExpr, |
| 426 | DeclTy *LastInGroup) { |
| 427 | const DeclSpec &DS = D.getDeclSpec(); |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 428 | DeclarationName Name = GetNameForDeclarator(D); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 429 | Expr *BitWidth = static_cast<Expr*>(BW); |
| 430 | Expr *Init = static_cast<Expr*>(InitExpr); |
| 431 | SourceLocation Loc = D.getIdentifierLoc(); |
| 432 | |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 433 | bool isFunc = D.isFunctionDeclarator(); |
| 434 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 435 | // C++ 9.2p6: A member shall not be declared to have automatic storage |
| 436 | // duration (auto, register) or with the extern storage-class-specifier. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 437 | // C++ 7.1.1p8: The mutable specifier can be applied only to names of class |
| 438 | // data members and cannot be applied to names declared const or static, |
| 439 | // and cannot be applied to reference members. |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 440 | switch (DS.getStorageClassSpec()) { |
| 441 | case DeclSpec::SCS_unspecified: |
| 442 | case DeclSpec::SCS_typedef: |
| 443 | case DeclSpec::SCS_static: |
| 444 | // FALL THROUGH. |
| 445 | break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 446 | case DeclSpec::SCS_mutable: |
| 447 | if (isFunc) { |
| 448 | if (DS.getStorageClassSpecLoc().isValid()) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 449 | Diag(DS.getStorageClassSpecLoc(), diag::err_mutable_function); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 450 | else |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 451 | Diag(DS.getThreadSpecLoc(), diag::err_mutable_function); |
| 452 | |
Sebastian Redl | a11f42f | 2008-11-17 23:24:37 +0000 | [diff] [blame] | 453 | // FIXME: It would be nicer if the keyword was ignored only for this |
| 454 | // declarator. Otherwise we could get follow-up errors. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 455 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 456 | } else { |
| 457 | QualType T = GetTypeForDeclarator(D, S); |
| 458 | diag::kind err = static_cast<diag::kind>(0); |
| 459 | if (T->isReferenceType()) |
| 460 | err = diag::err_mutable_reference; |
| 461 | else if (T.isConstQualified()) |
| 462 | err = diag::err_mutable_const; |
| 463 | if (err != 0) { |
| 464 | if (DS.getStorageClassSpecLoc().isValid()) |
| 465 | Diag(DS.getStorageClassSpecLoc(), err); |
| 466 | else |
| 467 | Diag(DS.getThreadSpecLoc(), err); |
Sebastian Redl | a11f42f | 2008-11-17 23:24:37 +0000 | [diff] [blame] | 468 | // FIXME: It would be nicer if the keyword was ignored only for this |
| 469 | // declarator. Otherwise we could get follow-up errors. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 470 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 471 | } |
| 472 | } |
| 473 | break; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 474 | default: |
| 475 | if (DS.getStorageClassSpecLoc().isValid()) |
| 476 | Diag(DS.getStorageClassSpecLoc(), |
| 477 | diag::err_storageclass_invalid_for_member); |
| 478 | else |
| 479 | Diag(DS.getThreadSpecLoc(), diag::err_storageclass_invalid_for_member); |
| 480 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 481 | } |
| 482 | |
Argyrios Kyrtzidis | d6caa9e | 2008-10-15 20:23:22 +0000 | [diff] [blame] | 483 | if (!isFunc && |
| 484 | D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_typedef && |
| 485 | D.getNumTypeObjects() == 0) { |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 486 | // Check also for this case: |
| 487 | // |
| 488 | // typedef int f(); |
| 489 | // f a; |
| 490 | // |
| 491 | Decl *TD = static_cast<Decl *>(DS.getTypeRep()); |
| 492 | isFunc = Context.getTypeDeclType(cast<TypeDecl>(TD))->isFunctionType(); |
| 493 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 494 | |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 495 | bool isInstField = ((DS.getStorageClassSpec() == DeclSpec::SCS_unspecified || |
| 496 | DS.getStorageClassSpec() == DeclSpec::SCS_mutable) && |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 497 | !isFunc); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 498 | |
| 499 | Decl *Member; |
| 500 | bool InvalidDecl = false; |
| 501 | |
| 502 | if (isInstField) |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 503 | Member = static_cast<Decl*>(ActOnField(S, cast<CXXRecordDecl>(CurContext), |
| 504 | Loc, D, BitWidth)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 505 | else |
Daniel Dunbar | 914701e | 2008-08-05 16:28:08 +0000 | [diff] [blame] | 506 | Member = static_cast<Decl*>(ActOnDeclarator(S, D, LastInGroup)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 507 | |
| 508 | if (!Member) return LastInGroup; |
| 509 | |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 510 | assert((Name || isInstField) && "No identifier for non-field ?"); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 511 | |
| 512 | // set/getAccess is not part of Decl's interface to avoid bloating it with C++ |
| 513 | // specific methods. Use a wrapper class that can be used with all C++ class |
| 514 | // member decls. |
| 515 | CXXClassMemberWrapper(Member).setAccess(AS); |
| 516 | |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 517 | // C++ [dcl.init.aggr]p1: |
| 518 | // An aggregate is an array or a class (clause 9) with [...] no |
| 519 | // private or protected non-static data members (clause 11). |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 520 | // A POD must be an aggregate. |
| 521 | if (isInstField && (AS == AS_private || AS == AS_protected)) { |
| 522 | CXXRecordDecl *Record = cast<CXXRecordDecl>(CurContext); |
| 523 | Record->setAggregate(false); |
| 524 | Record->setPOD(false); |
| 525 | } |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 526 | |
Sebastian Redl | d93f0dd | 2008-11-06 15:59:35 +0000 | [diff] [blame] | 527 | if (DS.isVirtualSpecified()) { |
| 528 | if (!isFunc || DS.getStorageClassSpec() == DeclSpec::SCS_static) { |
| 529 | Diag(DS.getVirtualSpecLoc(), diag::err_virtual_non_function); |
| 530 | InvalidDecl = true; |
| 531 | } else { |
Sebastian Redl | 9ba73ad | 2009-01-09 19:57:06 +0000 | [diff] [blame] | 532 | cast<CXXMethodDecl>(Member)->setVirtual(); |
Sebastian Redl | d93f0dd | 2008-11-06 15:59:35 +0000 | [diff] [blame] | 533 | CXXRecordDecl *CurClass = cast<CXXRecordDecl>(CurContext); |
| 534 | CurClass->setAggregate(false); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 535 | CurClass->setPOD(false); |
Sebastian Redl | d93f0dd | 2008-11-06 15:59:35 +0000 | [diff] [blame] | 536 | CurClass->setPolymorphic(true); |
| 537 | } |
| 538 | } |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 539 | |
Sebastian Redl | 9ba73ad | 2009-01-09 19:57:06 +0000 | [diff] [blame] | 540 | // FIXME: The above definition of virtual is not sufficient. A function is |
| 541 | // also virtual if it overrides an already virtual function. This is important |
| 542 | // to do here because it decides the validity of a pure specifier. |
| 543 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 544 | if (BitWidth) { |
| 545 | // C++ 9.6p2: Only when declaring an unnamed bit-field may the |
| 546 | // constant-expression be a value equal to zero. |
| 547 | // FIXME: Check this. |
| 548 | |
| 549 | if (D.isFunctionDeclarator()) { |
| 550 | // FIXME: Emit diagnostic about only constructors taking base initializers |
| 551 | // or something similar, when constructor support is in place. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 552 | Diag(Loc, diag::err_not_bitfield_type) |
Anders Carlsson | a75023d | 2008-12-06 20:05:35 +0000 | [diff] [blame] | 553 | << Name << BitWidth->getSourceRange(); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 554 | InvalidDecl = true; |
| 555 | |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 556 | } else if (isInstField) { |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 557 | // C++ 9.6p3: A bit-field shall have integral or enumeration type. |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 558 | if (!cast<FieldDecl>(Member)->getType()->isIntegralType()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 559 | Diag(Loc, diag::err_not_integral_type_bitfield) |
Anders Carlsson | a75023d | 2008-12-06 20:05:35 +0000 | [diff] [blame] | 560 | << Name << BitWidth->getSourceRange(); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 561 | InvalidDecl = true; |
| 562 | } |
| 563 | |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 564 | } else if (isa<FunctionDecl>(Member)) { |
| 565 | // A function typedef ("typedef int f(); f a;"). |
| 566 | // C++ 9.6p3: A bit-field shall have integral or enumeration type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 567 | Diag(Loc, diag::err_not_integral_type_bitfield) |
Anders Carlsson | a75023d | 2008-12-06 20:05:35 +0000 | [diff] [blame] | 568 | << Name << BitWidth->getSourceRange(); |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 569 | InvalidDecl = true; |
| 570 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 571 | } else if (isa<TypedefDecl>(Member)) { |
| 572 | // "cannot declare 'A' to be a bit-field type" |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 573 | Diag(Loc, diag::err_not_bitfield_type) |
Anders Carlsson | a75023d | 2008-12-06 20:05:35 +0000 | [diff] [blame] | 574 | << Name << BitWidth->getSourceRange(); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 575 | InvalidDecl = true; |
| 576 | |
| 577 | } else { |
| 578 | assert(isa<CXXClassVarDecl>(Member) && |
| 579 | "Didn't we cover all member kinds?"); |
| 580 | // C++ 9.6p3: A bit-field shall not be a static member. |
| 581 | // "static member 'A' cannot be a bit-field" |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 582 | Diag(Loc, diag::err_static_not_bitfield) |
Anders Carlsson | a75023d | 2008-12-06 20:05:35 +0000 | [diff] [blame] | 583 | << Name << BitWidth->getSourceRange(); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 584 | InvalidDecl = true; |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | if (Init) { |
| 589 | // C++ 9.2p4: A member-declarator can contain a constant-initializer only |
| 590 | // if it declares a static member of const integral or const enumeration |
| 591 | // type. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 592 | if (CXXClassVarDecl *CVD = dyn_cast<CXXClassVarDecl>(Member)) { |
| 593 | // ...static member of... |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 594 | CVD->setInit(Init); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 595 | // ...const integral or const enumeration type. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 596 | if (Context.getCanonicalType(CVD->getType()).isConstQualified() && |
| 597 | CVD->getType()->isIntegralType()) { |
| 598 | // constant-initializer |
| 599 | if (CheckForConstantInitializer(Init, CVD->getType())) |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 600 | InvalidDecl = true; |
| 601 | |
| 602 | } else { |
| 603 | // not const integral. |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 604 | Diag(Loc, diag::err_member_initialization) |
Anders Carlsson | a75023d | 2008-12-06 20:05:35 +0000 | [diff] [blame] | 605 | << Name << Init->getSourceRange(); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 606 | InvalidDecl = true; |
| 607 | } |
| 608 | |
| 609 | } else { |
Sebastian Redl | 9ba73ad | 2009-01-09 19:57:06 +0000 | [diff] [blame] | 610 | // not static member. perhaps virtual function? |
| 611 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Member)) { |
Sebastian Redl | c9b580a | 2009-01-09 22:29:03 +0000 | [diff] [blame] | 612 | // With declarators parsed the way they are, the parser cannot |
| 613 | // distinguish between a normal initializer and a pure-specifier. |
| 614 | // Thus this grotesque test. |
Sebastian Redl | 9ba73ad | 2009-01-09 19:57:06 +0000 | [diff] [blame] | 615 | IntegerLiteral *IL; |
| 616 | if ((IL = dyn_cast<IntegerLiteral>(Init)) && IL->getValue() == 0 && |
| 617 | Context.getCanonicalType(IL->getType()) == Context.IntTy) { |
| 618 | if (MD->isVirtual()) |
| 619 | MD->setPure(); |
| 620 | else { |
| 621 | Diag(Loc, diag::err_non_virtual_pure) |
| 622 | << Name << Init->getSourceRange(); |
| 623 | InvalidDecl = true; |
| 624 | } |
| 625 | } else { |
| 626 | Diag(Loc, diag::err_member_function_initialization) |
| 627 | << Name << Init->getSourceRange(); |
| 628 | InvalidDecl = true; |
| 629 | } |
| 630 | } else { |
| 631 | Diag(Loc, diag::err_member_initialization) |
| 632 | << Name << Init->getSourceRange(); |
| 633 | InvalidDecl = true; |
| 634 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 635 | } |
| 636 | } |
| 637 | |
| 638 | if (InvalidDecl) |
| 639 | Member->setInvalidDecl(); |
| 640 | |
| 641 | if (isInstField) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 642 | FieldCollector->Add(cast<FieldDecl>(Member)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 643 | return LastInGroup; |
| 644 | } |
| 645 | return Member; |
| 646 | } |
| 647 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 648 | /// ActOnMemInitializer - Handle a C++ member initializer. |
| 649 | Sema::MemInitResult |
| 650 | Sema::ActOnMemInitializer(DeclTy *ConstructorD, |
| 651 | Scope *S, |
| 652 | IdentifierInfo *MemberOrBase, |
| 653 | SourceLocation IdLoc, |
| 654 | SourceLocation LParenLoc, |
| 655 | ExprTy **Args, unsigned NumArgs, |
| 656 | SourceLocation *CommaLocs, |
| 657 | SourceLocation RParenLoc) { |
| 658 | CXXConstructorDecl *Constructor |
| 659 | = dyn_cast<CXXConstructorDecl>((Decl*)ConstructorD); |
| 660 | if (!Constructor) { |
| 661 | // The user wrote a constructor initializer on a function that is |
| 662 | // not a C++ constructor. Ignore the error for now, because we may |
| 663 | // have more member initializers coming; we'll diagnose it just |
| 664 | // once in ActOnMemInitializers. |
| 665 | return true; |
| 666 | } |
| 667 | |
| 668 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 669 | |
| 670 | // C++ [class.base.init]p2: |
| 671 | // Names in a mem-initializer-id are looked up in the scope of the |
| 672 | // constructor’s class and, if not found in that scope, are looked |
| 673 | // up in the scope containing the constructor’s |
| 674 | // definition. [Note: if the constructor’s class contains a member |
| 675 | // with the same name as a direct or virtual base class of the |
| 676 | // class, a mem-initializer-id naming the member or base class and |
| 677 | // composed of a single identifier refers to the class member. A |
| 678 | // mem-initializer-id for the hidden base class may be specified |
| 679 | // using a qualified name. ] |
| 680 | // Look for a member, first. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 681 | FieldDecl *Member = 0; |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 682 | DeclContext::lookup_result Result = ClassDecl->lookup(MemberOrBase); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 683 | if (Result.first != Result.second) |
| 684 | Member = dyn_cast<FieldDecl>(*Result.first); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 685 | |
| 686 | // FIXME: Handle members of an anonymous union. |
| 687 | |
| 688 | if (Member) { |
| 689 | // FIXME: Perform direct initialization of the member. |
| 690 | return new CXXBaseOrMemberInitializer(Member, (Expr **)Args, NumArgs); |
| 691 | } |
| 692 | |
| 693 | // It didn't name a member, so see if it names a class. |
Douglas Gregor | b696ea3 | 2009-02-04 17:00:24 +0000 | [diff] [blame] | 694 | TypeTy *BaseTy = getTypeName(*MemberOrBase, IdLoc, S, 0/*SS*/); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 695 | if (!BaseTy) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 696 | return Diag(IdLoc, diag::err_mem_init_not_member_or_class) |
| 697 | << MemberOrBase << SourceRange(IdLoc, RParenLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 698 | |
| 699 | QualType BaseType = Context.getTypeDeclType((TypeDecl *)BaseTy); |
| 700 | if (!BaseType->isRecordType()) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 701 | return Diag(IdLoc, diag::err_base_init_does_not_name_class) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 702 | << BaseType << SourceRange(IdLoc, RParenLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 703 | |
| 704 | // C++ [class.base.init]p2: |
| 705 | // [...] Unless the mem-initializer-id names a nonstatic data |
| 706 | // member of the constructor’s class or a direct or virtual base |
| 707 | // of that class, the mem-initializer is ill-formed. A |
| 708 | // mem-initializer-list can initialize a base class using any |
| 709 | // name that denotes that base class type. |
| 710 | |
| 711 | // First, check for a direct base class. |
| 712 | const CXXBaseSpecifier *DirectBaseSpec = 0; |
| 713 | for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(); |
| 714 | Base != ClassDecl->bases_end(); ++Base) { |
| 715 | if (Context.getCanonicalType(BaseType).getUnqualifiedType() == |
| 716 | Context.getCanonicalType(Base->getType()).getUnqualifiedType()) { |
| 717 | // We found a direct base of this type. That's what we're |
| 718 | // initializing. |
| 719 | DirectBaseSpec = &*Base; |
| 720 | break; |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | // Check for a virtual base class. |
| 725 | // FIXME: We might be able to short-circuit this if we know in |
| 726 | // advance that there are no virtual bases. |
| 727 | const CXXBaseSpecifier *VirtualBaseSpec = 0; |
| 728 | if (!DirectBaseSpec || !DirectBaseSpec->isVirtual()) { |
| 729 | // We haven't found a base yet; search the class hierarchy for a |
| 730 | // virtual base class. |
| 731 | BasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 732 | /*DetectVirtual=*/false); |
| 733 | if (IsDerivedFrom(Context.getTypeDeclType(ClassDecl), BaseType, Paths)) { |
| 734 | for (BasePaths::paths_iterator Path = Paths.begin(); |
| 735 | Path != Paths.end(); ++Path) { |
| 736 | if (Path->back().Base->isVirtual()) { |
| 737 | VirtualBaseSpec = Path->back().Base; |
| 738 | break; |
| 739 | } |
| 740 | } |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | // C++ [base.class.init]p2: |
| 745 | // If a mem-initializer-id is ambiguous because it designates both |
| 746 | // a direct non-virtual base class and an inherited virtual base |
| 747 | // class, the mem-initializer is ill-formed. |
| 748 | if (DirectBaseSpec && VirtualBaseSpec) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 749 | return Diag(IdLoc, diag::err_base_init_direct_and_virtual) |
| 750 | << MemberOrBase << SourceRange(IdLoc, RParenLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 751 | |
| 752 | return new CXXBaseOrMemberInitializer(BaseType, (Expr **)Args, NumArgs); |
| 753 | } |
| 754 | |
| 755 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 756 | void Sema::ActOnFinishCXXMemberSpecification(Scope* S, SourceLocation RLoc, |
| 757 | DeclTy *TagDecl, |
| 758 | SourceLocation LBrac, |
| 759 | SourceLocation RBrac) { |
| 760 | ActOnFields(S, RLoc, TagDecl, |
| 761 | (DeclTy**)FieldCollector->getCurFields(), |
Daniel Dunbar | 1bfe1c2 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 762 | FieldCollector->getCurNumFields(), LBrac, RBrac, 0); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 763 | AddImplicitlyDeclaredMembersToClass(cast<CXXRecordDecl>((Decl*)TagDecl)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 764 | } |
| 765 | |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 766 | /// AddImplicitlyDeclaredMembersToClass - Adds any implicitly-declared |
| 767 | /// special functions, such as the default constructor, copy |
| 768 | /// constructor, or destructor, to the given C++ class (C++ |
| 769 | /// [special]p1). This routine can only be executed just before the |
| 770 | /// definition of the class is complete. |
| 771 | void Sema::AddImplicitlyDeclaredMembersToClass(CXXRecordDecl *ClassDecl) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 772 | QualType ClassType = Context.getTypeDeclType(ClassDecl); |
| 773 | ClassType = Context.getCanonicalType(ClassType); |
| 774 | |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 775 | if (!ClassDecl->hasUserDeclaredConstructor()) { |
| 776 | // C++ [class.ctor]p5: |
| 777 | // A default constructor for a class X is a constructor of class X |
| 778 | // that can be called without an argument. If there is no |
| 779 | // user-declared constructor for class X, a default constructor is |
| 780 | // implicitly declared. An implicitly-declared default constructor |
| 781 | // is an inline public member of its class. |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 782 | DeclarationName Name |
| 783 | = Context.DeclarationNames.getCXXConstructorName(ClassType); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 784 | CXXConstructorDecl *DefaultCon = |
| 785 | CXXConstructorDecl::Create(Context, ClassDecl, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 786 | ClassDecl->getLocation(), Name, |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 787 | Context.getFunctionType(Context.VoidTy, |
| 788 | 0, 0, false, 0), |
| 789 | /*isExplicit=*/false, |
| 790 | /*isInline=*/true, |
| 791 | /*isImplicitlyDeclared=*/true); |
| 792 | DefaultCon->setAccess(AS_public); |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 793 | DefaultCon->setImplicit(); |
Douglas Gregor | 482b77d | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 794 | ClassDecl->addDecl(DefaultCon); |
Douglas Gregor | 9e7d9de | 2008-12-15 21:24:18 +0000 | [diff] [blame] | 795 | |
| 796 | // Notify the class that we've added a constructor. |
| 797 | ClassDecl->addedConstructor(Context, DefaultCon); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | if (!ClassDecl->hasUserDeclaredCopyConstructor()) { |
| 801 | // C++ [class.copy]p4: |
| 802 | // If the class definition does not explicitly declare a copy |
| 803 | // constructor, one is declared implicitly. |
| 804 | |
| 805 | // C++ [class.copy]p5: |
| 806 | // The implicitly-declared copy constructor for a class X will |
| 807 | // have the form |
| 808 | // |
| 809 | // X::X(const X&) |
| 810 | // |
| 811 | // if |
| 812 | bool HasConstCopyConstructor = true; |
| 813 | |
| 814 | // -- each direct or virtual base class B of X has a copy |
| 815 | // constructor whose first parameter is of type const B& or |
| 816 | // const volatile B&, and |
| 817 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(); |
| 818 | HasConstCopyConstructor && Base != ClassDecl->bases_end(); ++Base) { |
| 819 | const CXXRecordDecl *BaseClassDecl |
| 820 | = cast<CXXRecordDecl>(Base->getType()->getAsRecordType()->getDecl()); |
| 821 | HasConstCopyConstructor |
| 822 | = BaseClassDecl->hasConstCopyConstructor(Context); |
| 823 | } |
| 824 | |
| 825 | // -- for all the nonstatic data members of X that are of a |
| 826 | // class type M (or array thereof), each such class type |
| 827 | // has a copy constructor whose first parameter is of type |
| 828 | // const M& or const volatile M&. |
| 829 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(); |
| 830 | HasConstCopyConstructor && Field != ClassDecl->field_end(); ++Field) { |
| 831 | QualType FieldType = (*Field)->getType(); |
| 832 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 833 | FieldType = Array->getElementType(); |
| 834 | if (const RecordType *FieldClassType = FieldType->getAsRecordType()) { |
| 835 | const CXXRecordDecl *FieldClassDecl |
| 836 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
| 837 | HasConstCopyConstructor |
| 838 | = FieldClassDecl->hasConstCopyConstructor(Context); |
| 839 | } |
| 840 | } |
| 841 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 842 | // Otherwise, the implicitly declared copy constructor will have |
| 843 | // the form |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 844 | // |
| 845 | // X::X(X&) |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 846 | QualType ArgType = ClassType; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 847 | if (HasConstCopyConstructor) |
| 848 | ArgType = ArgType.withConst(); |
| 849 | ArgType = Context.getReferenceType(ArgType); |
| 850 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 851 | // An implicitly-declared copy constructor is an inline public |
| 852 | // member of its class. |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 853 | DeclarationName Name |
| 854 | = Context.DeclarationNames.getCXXConstructorName(ClassType); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 855 | CXXConstructorDecl *CopyConstructor |
| 856 | = CXXConstructorDecl::Create(Context, ClassDecl, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 857 | ClassDecl->getLocation(), Name, |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 858 | Context.getFunctionType(Context.VoidTy, |
| 859 | &ArgType, 1, |
| 860 | false, 0), |
| 861 | /*isExplicit=*/false, |
| 862 | /*isInline=*/true, |
| 863 | /*isImplicitlyDeclared=*/true); |
| 864 | CopyConstructor->setAccess(AS_public); |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 865 | CopyConstructor->setImplicit(); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 866 | |
| 867 | // Add the parameter to the constructor. |
| 868 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyConstructor, |
| 869 | ClassDecl->getLocation(), |
| 870 | /*IdentifierInfo=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 871 | ArgType, VarDecl::None, 0); |
Ted Kremenek | fc76761 | 2009-01-14 00:42:25 +0000 | [diff] [blame] | 872 | CopyConstructor->setParams(Context, &FromParam, 1); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 873 | |
Douglas Gregor | 9e7d9de | 2008-12-15 21:24:18 +0000 | [diff] [blame] | 874 | ClassDecl->addedConstructor(Context, CopyConstructor); |
Douglas Gregor | 482b77d | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 875 | ClassDecl->addDecl(CopyConstructor); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 876 | } |
| 877 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 878 | if (!ClassDecl->hasUserDeclaredCopyAssignment()) { |
| 879 | // Note: The following rules are largely analoguous to the copy |
| 880 | // constructor rules. Note that virtual bases are not taken into account |
| 881 | // for determining the argument type of the operator. Note also that |
| 882 | // operators taking an object instead of a reference are allowed. |
| 883 | // |
| 884 | // C++ [class.copy]p10: |
| 885 | // If the class definition does not explicitly declare a copy |
| 886 | // assignment operator, one is declared implicitly. |
| 887 | // The implicitly-defined copy assignment operator for a class X |
| 888 | // will have the form |
| 889 | // |
| 890 | // X& X::operator=(const X&) |
| 891 | // |
| 892 | // if |
| 893 | bool HasConstCopyAssignment = true; |
| 894 | |
| 895 | // -- each direct base class B of X has a copy assignment operator |
| 896 | // whose parameter is of type const B&, const volatile B& or B, |
| 897 | // and |
| 898 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(); |
| 899 | HasConstCopyAssignment && Base != ClassDecl->bases_end(); ++Base) { |
| 900 | const CXXRecordDecl *BaseClassDecl |
| 901 | = cast<CXXRecordDecl>(Base->getType()->getAsRecordType()->getDecl()); |
| 902 | HasConstCopyAssignment = BaseClassDecl->hasConstCopyAssignment(Context); |
| 903 | } |
| 904 | |
| 905 | // -- for all the nonstatic data members of X that are of a class |
| 906 | // type M (or array thereof), each such class type has a copy |
| 907 | // assignment operator whose parameter is of type const M&, |
| 908 | // const volatile M& or M. |
| 909 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(); |
| 910 | HasConstCopyAssignment && Field != ClassDecl->field_end(); ++Field) { |
| 911 | QualType FieldType = (*Field)->getType(); |
| 912 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 913 | FieldType = Array->getElementType(); |
| 914 | if (const RecordType *FieldClassType = FieldType->getAsRecordType()) { |
| 915 | const CXXRecordDecl *FieldClassDecl |
| 916 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
| 917 | HasConstCopyAssignment |
| 918 | = FieldClassDecl->hasConstCopyAssignment(Context); |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | // Otherwise, the implicitly declared copy assignment operator will |
| 923 | // have the form |
| 924 | // |
| 925 | // X& X::operator=(X&) |
| 926 | QualType ArgType = ClassType; |
| 927 | QualType RetType = Context.getReferenceType(ArgType); |
| 928 | if (HasConstCopyAssignment) |
| 929 | ArgType = ArgType.withConst(); |
| 930 | ArgType = Context.getReferenceType(ArgType); |
| 931 | |
| 932 | // An implicitly-declared copy assignment operator is an inline public |
| 933 | // member of its class. |
| 934 | DeclarationName Name = |
| 935 | Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 936 | CXXMethodDecl *CopyAssignment = |
| 937 | CXXMethodDecl::Create(Context, ClassDecl, ClassDecl->getLocation(), Name, |
| 938 | Context.getFunctionType(RetType, &ArgType, 1, |
| 939 | false, 0), |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 940 | /*isStatic=*/false, /*isInline=*/true); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 941 | CopyAssignment->setAccess(AS_public); |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 942 | CopyAssignment->setImplicit(); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 943 | |
| 944 | // Add the parameter to the operator. |
| 945 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyAssignment, |
| 946 | ClassDecl->getLocation(), |
| 947 | /*IdentifierInfo=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 948 | ArgType, VarDecl::None, 0); |
Ted Kremenek | fc76761 | 2009-01-14 00:42:25 +0000 | [diff] [blame] | 949 | CopyAssignment->setParams(Context, &FromParam, 1); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 950 | |
| 951 | // Don't call addedAssignmentOperator. There is no way to distinguish an |
| 952 | // implicit from an explicit assignment operator. |
Douglas Gregor | 482b77d | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 953 | ClassDecl->addDecl(CopyAssignment); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 954 | } |
| 955 | |
Douglas Gregor | 9e7d9de | 2008-12-15 21:24:18 +0000 | [diff] [blame] | 956 | if (!ClassDecl->hasUserDeclaredDestructor()) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 957 | // C++ [class.dtor]p2: |
| 958 | // If a class has no user-declared destructor, a destructor is |
| 959 | // declared implicitly. An implicitly-declared destructor is an |
| 960 | // inline public member of its class. |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 961 | DeclarationName Name |
| 962 | = Context.DeclarationNames.getCXXDestructorName(ClassType); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 963 | CXXDestructorDecl *Destructor |
| 964 | = CXXDestructorDecl::Create(Context, ClassDecl, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 965 | ClassDecl->getLocation(), Name, |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 966 | Context.getFunctionType(Context.VoidTy, |
| 967 | 0, 0, false, 0), |
| 968 | /*isInline=*/true, |
| 969 | /*isImplicitlyDeclared=*/true); |
| 970 | Destructor->setAccess(AS_public); |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 971 | Destructor->setImplicit(); |
Douglas Gregor | 482b77d | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 972 | ClassDecl->addDecl(Destructor); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 973 | } |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 974 | } |
| 975 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 976 | /// ActOnStartDelayedCXXMethodDeclaration - We have completed |
| 977 | /// parsing a top-level (non-nested) C++ class, and we are now |
| 978 | /// parsing those parts of the given Method declaration that could |
| 979 | /// not be parsed earlier (C++ [class.mem]p2), such as default |
| 980 | /// arguments. This action should enter the scope of the given |
| 981 | /// Method declaration as if we had just parsed the qualified method |
| 982 | /// name. However, it should not bring the parameters into scope; |
| 983 | /// that will be performed by ActOnDelayedCXXMethodParameter. |
| 984 | void Sema::ActOnStartDelayedCXXMethodDeclaration(Scope *S, DeclTy *Method) { |
| 985 | CXXScopeSpec SS; |
| 986 | SS.setScopeRep(((FunctionDecl*)Method)->getDeclContext()); |
| 987 | ActOnCXXEnterDeclaratorScope(S, SS); |
| 988 | } |
| 989 | |
| 990 | /// ActOnDelayedCXXMethodParameter - We've already started a delayed |
| 991 | /// C++ method declaration. We're (re-)introducing the given |
| 992 | /// function parameter into scope for use in parsing later parts of |
| 993 | /// the method declaration. For example, we could see an |
| 994 | /// ActOnParamDefaultArgument event for this parameter. |
| 995 | void Sema::ActOnDelayedCXXMethodParameter(Scope *S, DeclTy *ParamD) { |
| 996 | ParmVarDecl *Param = (ParmVarDecl*)ParamD; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 997 | |
| 998 | // If this parameter has an unparsed default argument, clear it out |
| 999 | // to make way for the parsed default argument. |
| 1000 | if (Param->hasUnparsedDefaultArg()) |
| 1001 | Param->setDefaultArg(0); |
| 1002 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1003 | S->AddDecl(Param); |
| 1004 | if (Param->getDeclName()) |
| 1005 | IdResolver.AddDecl(Param); |
| 1006 | } |
| 1007 | |
| 1008 | /// ActOnFinishDelayedCXXMethodDeclaration - We have finished |
| 1009 | /// processing the delayed method declaration for Method. The method |
| 1010 | /// declaration is now considered finished. There may be a separate |
| 1011 | /// ActOnStartOfFunctionDef action later (not necessarily |
| 1012 | /// immediately!) for this method, if it was also defined inside the |
| 1013 | /// class body. |
| 1014 | void Sema::ActOnFinishDelayedCXXMethodDeclaration(Scope *S, DeclTy *MethodD) { |
| 1015 | FunctionDecl *Method = (FunctionDecl*)MethodD; |
| 1016 | CXXScopeSpec SS; |
| 1017 | SS.setScopeRep(Method->getDeclContext()); |
| 1018 | ActOnCXXExitDeclaratorScope(S, SS); |
| 1019 | |
| 1020 | // Now that we have our default arguments, check the constructor |
| 1021 | // again. It could produce additional diagnostics or affect whether |
| 1022 | // the class has implicitly-declared destructors, among other |
| 1023 | // things. |
| 1024 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Method)) { |
| 1025 | if (CheckConstructor(Constructor)) |
| 1026 | Constructor->setInvalidDecl(); |
| 1027 | } |
| 1028 | |
| 1029 | // Check the default arguments, which we may have added. |
| 1030 | if (!Method->isInvalidDecl()) |
| 1031 | CheckCXXDefaultArguments(Method); |
| 1032 | } |
| 1033 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1034 | /// CheckConstructorDeclarator - Called by ActOnDeclarator to check |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1035 | /// the well-formedness of the constructor declarator @p D with type @p |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1036 | /// R. If there are any errors in the declarator, this routine will |
| 1037 | /// emit diagnostics and return true. Otherwise, it will return |
| 1038 | /// false. Either way, the type @p R will be updated to reflect a |
| 1039 | /// well-formed type for the constructor. |
| 1040 | bool Sema::CheckConstructorDeclarator(Declarator &D, QualType &R, |
| 1041 | FunctionDecl::StorageClass& SC) { |
| 1042 | bool isVirtual = D.getDeclSpec().isVirtualSpecified(); |
| 1043 | bool isInvalid = false; |
| 1044 | |
| 1045 | // C++ [class.ctor]p3: |
| 1046 | // A constructor shall not be virtual (10.3) or static (9.4). A |
| 1047 | // constructor can be invoked for a const, volatile or const |
| 1048 | // volatile object. A constructor shall not be declared const, |
| 1049 | // volatile, or const volatile (9.3.2). |
| 1050 | if (isVirtual) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1051 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 1052 | << "virtual" << SourceRange(D.getDeclSpec().getVirtualSpecLoc()) |
| 1053 | << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1054 | isInvalid = true; |
| 1055 | } |
| 1056 | if (SC == FunctionDecl::Static) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1057 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 1058 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 1059 | << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1060 | isInvalid = true; |
| 1061 | SC = FunctionDecl::None; |
| 1062 | } |
| 1063 | if (D.getDeclSpec().hasTypeSpecifier()) { |
| 1064 | // Constructors don't have return types, but the parser will |
| 1065 | // happily parse something like: |
| 1066 | // |
| 1067 | // class X { |
| 1068 | // float X(float); |
| 1069 | // }; |
| 1070 | // |
| 1071 | // The return type will be eliminated later. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1072 | Diag(D.getIdentifierLoc(), diag::err_constructor_return_type) |
| 1073 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 1074 | << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1075 | } |
| 1076 | if (R->getAsFunctionTypeProto()->getTypeQuals() != 0) { |
| 1077 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 1078 | if (FTI.TypeQuals & QualType::Const) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1079 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 1080 | << "const" << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1081 | if (FTI.TypeQuals & QualType::Volatile) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1082 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 1083 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1084 | if (FTI.TypeQuals & QualType::Restrict) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1085 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 1086 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | // Rebuild the function type "R" without any type qualifiers (in |
| 1090 | // case any of the errors above fired) and with "void" as the |
| 1091 | // return type, since constructors don't have return types. We |
| 1092 | // *always* have to do this, because GetTypeForDeclarator will |
| 1093 | // put in a result type of "int" when none was specified. |
| 1094 | const FunctionTypeProto *Proto = R->getAsFunctionTypeProto(); |
| 1095 | R = Context.getFunctionType(Context.VoidTy, Proto->arg_type_begin(), |
| 1096 | Proto->getNumArgs(), |
| 1097 | Proto->isVariadic(), |
| 1098 | 0); |
| 1099 | |
| 1100 | return isInvalid; |
| 1101 | } |
| 1102 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1103 | /// CheckConstructor - Checks a fully-formed constructor for |
| 1104 | /// well-formedness, issuing any diagnostics required. Returns true if |
| 1105 | /// the constructor declarator is invalid. |
| 1106 | bool Sema::CheckConstructor(CXXConstructorDecl *Constructor) { |
| 1107 | if (Constructor->isInvalidDecl()) |
| 1108 | return true; |
| 1109 | |
| 1110 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Constructor->getDeclContext()); |
| 1111 | bool Invalid = false; |
| 1112 | |
| 1113 | // C++ [class.copy]p3: |
| 1114 | // A declaration of a constructor for a class X is ill-formed if |
| 1115 | // its first parameter is of type (optionally cv-qualified) X and |
| 1116 | // either there are no other parameters or else all other |
| 1117 | // parameters have default arguments. |
| 1118 | if ((Constructor->getNumParams() == 1) || |
| 1119 | (Constructor->getNumParams() > 1 && |
| 1120 | Constructor->getParamDecl(1)->getDefaultArg() != 0)) { |
| 1121 | QualType ParamType = Constructor->getParamDecl(0)->getType(); |
| 1122 | QualType ClassTy = Context.getTagDeclType(ClassDecl); |
| 1123 | if (Context.getCanonicalType(ParamType).getUnqualifiedType() == ClassTy) { |
| 1124 | Diag(Constructor->getLocation(), diag::err_constructor_byvalue_arg) |
| 1125 | << SourceRange(Constructor->getParamDecl(0)->getLocation()); |
| 1126 | Invalid = true; |
| 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | // Notify the class that we've added a constructor. |
| 1131 | ClassDecl->addedConstructor(Context, Constructor); |
| 1132 | |
| 1133 | return Invalid; |
| 1134 | } |
| 1135 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1136 | /// CheckDestructorDeclarator - Called by ActOnDeclarator to check |
| 1137 | /// the well-formednes of the destructor declarator @p D with type @p |
| 1138 | /// R. If there are any errors in the declarator, this routine will |
| 1139 | /// emit diagnostics and return true. Otherwise, it will return |
| 1140 | /// false. Either way, the type @p R will be updated to reflect a |
| 1141 | /// well-formed type for the destructor. |
| 1142 | bool Sema::CheckDestructorDeclarator(Declarator &D, QualType &R, |
| 1143 | FunctionDecl::StorageClass& SC) { |
| 1144 | bool isInvalid = false; |
| 1145 | |
| 1146 | // C++ [class.dtor]p1: |
| 1147 | // [...] A typedef-name that names a class is a class-name |
| 1148 | // (7.1.3); however, a typedef-name that names a class shall not |
| 1149 | // be used as the identifier in the declarator for a destructor |
| 1150 | // declaration. |
| 1151 | TypeDecl *DeclaratorTypeD = (TypeDecl *)D.getDeclaratorIdType(); |
| 1152 | if (const TypedefDecl *TypedefD = dyn_cast<TypedefDecl>(DeclaratorTypeD)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1153 | Diag(D.getIdentifierLoc(), diag::err_destructor_typedef_name) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1154 | << TypedefD->getDeclName(); |
Douglas Gregor | 55c6095 | 2008-11-10 14:41:22 +0000 | [diff] [blame] | 1155 | isInvalid = true; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | // C++ [class.dtor]p2: |
| 1159 | // A destructor is used to destroy objects of its class type. A |
| 1160 | // destructor takes no parameters, and no return type can be |
| 1161 | // specified for it (not even void). The address of a destructor |
| 1162 | // shall not be taken. A destructor shall not be static. A |
| 1163 | // destructor can be invoked for a const, volatile or const |
| 1164 | // volatile object. A destructor shall not be declared const, |
| 1165 | // volatile or const volatile (9.3.2). |
| 1166 | if (SC == FunctionDecl::Static) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1167 | Diag(D.getIdentifierLoc(), diag::err_destructor_cannot_be) |
| 1168 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 1169 | << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1170 | isInvalid = true; |
| 1171 | SC = FunctionDecl::None; |
| 1172 | } |
| 1173 | if (D.getDeclSpec().hasTypeSpecifier()) { |
| 1174 | // Destructors don't have return types, but the parser will |
| 1175 | // happily parse something like: |
| 1176 | // |
| 1177 | // class X { |
| 1178 | // float ~X(); |
| 1179 | // }; |
| 1180 | // |
| 1181 | // The return type will be eliminated later. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1182 | Diag(D.getIdentifierLoc(), diag::err_destructor_return_type) |
| 1183 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 1184 | << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1185 | } |
| 1186 | if (R->getAsFunctionTypeProto()->getTypeQuals() != 0) { |
| 1187 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 1188 | if (FTI.TypeQuals & QualType::Const) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1189 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 1190 | << "const" << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1191 | if (FTI.TypeQuals & QualType::Volatile) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1192 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 1193 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1194 | if (FTI.TypeQuals & QualType::Restrict) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1195 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 1196 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | // Make sure we don't have any parameters. |
| 1200 | if (R->getAsFunctionTypeProto()->getNumArgs() > 0) { |
| 1201 | Diag(D.getIdentifierLoc(), diag::err_destructor_with_params); |
| 1202 | |
| 1203 | // Delete the parameters. |
Chris Lattner | 1833a83 | 2009-01-20 21:06:38 +0000 | [diff] [blame] | 1204 | D.getTypeObject(0).Fun.freeArgs(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1205 | } |
| 1206 | |
| 1207 | // Make sure the destructor isn't variadic. |
| 1208 | if (R->getAsFunctionTypeProto()->isVariadic()) |
| 1209 | Diag(D.getIdentifierLoc(), diag::err_destructor_variadic); |
| 1210 | |
| 1211 | // Rebuild the function type "R" without any type qualifiers or |
| 1212 | // parameters (in case any of the errors above fired) and with |
| 1213 | // "void" as the return type, since destructors don't have return |
| 1214 | // types. We *always* have to do this, because GetTypeForDeclarator |
| 1215 | // will put in a result type of "int" when none was specified. |
| 1216 | R = Context.getFunctionType(Context.VoidTy, 0, 0, false, 0); |
| 1217 | |
| 1218 | return isInvalid; |
| 1219 | } |
| 1220 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1221 | /// CheckConversionDeclarator - Called by ActOnDeclarator to check the |
| 1222 | /// well-formednes of the conversion function declarator @p D with |
| 1223 | /// type @p R. If there are any errors in the declarator, this routine |
| 1224 | /// will emit diagnostics and return true. Otherwise, it will return |
| 1225 | /// false. Either way, the type @p R will be updated to reflect a |
| 1226 | /// well-formed type for the conversion operator. |
| 1227 | bool Sema::CheckConversionDeclarator(Declarator &D, QualType &R, |
| 1228 | FunctionDecl::StorageClass& SC) { |
| 1229 | bool isInvalid = false; |
| 1230 | |
| 1231 | // C++ [class.conv.fct]p1: |
| 1232 | // Neither parameter types nor return type can be specified. The |
| 1233 | // type of a conversion function (8.3.5) is “function taking no |
| 1234 | // parameter returning conversion-type-id.” |
| 1235 | if (SC == FunctionDecl::Static) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1236 | Diag(D.getIdentifierLoc(), diag::err_conv_function_not_member) |
| 1237 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 1238 | << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1239 | isInvalid = true; |
| 1240 | SC = FunctionDecl::None; |
| 1241 | } |
| 1242 | if (D.getDeclSpec().hasTypeSpecifier()) { |
| 1243 | // Conversion functions don't have return types, but the parser will |
| 1244 | // happily parse something like: |
| 1245 | // |
| 1246 | // class X { |
| 1247 | // float operator bool(); |
| 1248 | // }; |
| 1249 | // |
| 1250 | // The return type will be changed later anyway. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1251 | Diag(D.getIdentifierLoc(), diag::err_conv_function_return_type) |
| 1252 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 1253 | << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1254 | } |
| 1255 | |
| 1256 | // Make sure we don't have any parameters. |
| 1257 | if (R->getAsFunctionTypeProto()->getNumArgs() > 0) { |
| 1258 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_params); |
| 1259 | |
| 1260 | // Delete the parameters. |
Chris Lattner | 1833a83 | 2009-01-20 21:06:38 +0000 | [diff] [blame] | 1261 | D.getTypeObject(0).Fun.freeArgs(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
| 1264 | // Make sure the conversion function isn't variadic. |
| 1265 | if (R->getAsFunctionTypeProto()->isVariadic()) |
| 1266 | Diag(D.getIdentifierLoc(), diag::err_conv_function_variadic); |
| 1267 | |
| 1268 | // C++ [class.conv.fct]p4: |
| 1269 | // The conversion-type-id shall not represent a function type nor |
| 1270 | // an array type. |
| 1271 | QualType ConvType = QualType::getFromOpaquePtr(D.getDeclaratorIdType()); |
| 1272 | if (ConvType->isArrayType()) { |
| 1273 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_array); |
| 1274 | ConvType = Context.getPointerType(ConvType); |
| 1275 | } else if (ConvType->isFunctionType()) { |
| 1276 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_function); |
| 1277 | ConvType = Context.getPointerType(ConvType); |
| 1278 | } |
| 1279 | |
| 1280 | // Rebuild the function type "R" without any parameters (in case any |
| 1281 | // of the errors above fired) and with the conversion type as the |
| 1282 | // return type. |
| 1283 | R = Context.getFunctionType(ConvType, 0, 0, false, |
| 1284 | R->getAsFunctionTypeProto()->getTypeQuals()); |
| 1285 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1286 | // C++0x explicit conversion operators. |
| 1287 | if (D.getDeclSpec().isExplicitSpecified() && !getLangOptions().CPlusPlus0x) |
| 1288 | Diag(D.getDeclSpec().getExplicitSpecLoc(), |
| 1289 | diag::warn_explicit_conversion_functions) |
| 1290 | << SourceRange(D.getDeclSpec().getExplicitSpecLoc()); |
| 1291 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1292 | return isInvalid; |
| 1293 | } |
| 1294 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1295 | /// ActOnConversionDeclarator - Called by ActOnDeclarator to complete |
| 1296 | /// the declaration of the given C++ conversion function. This routine |
| 1297 | /// is responsible for recording the conversion function in the C++ |
| 1298 | /// class, if possible. |
| 1299 | Sema::DeclTy *Sema::ActOnConversionDeclarator(CXXConversionDecl *Conversion) { |
| 1300 | assert(Conversion && "Expected to receive a conversion function declaration"); |
| 1301 | |
Douglas Gregor | 9d35097 | 2008-12-12 08:25:50 +0000 | [diff] [blame] | 1302 | // Set the lexical context of this conversion function |
| 1303 | Conversion->setLexicalDeclContext(CurContext); |
| 1304 | |
| 1305 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Conversion->getDeclContext()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1306 | |
| 1307 | // Make sure we aren't redeclaring the conversion function. |
| 1308 | QualType ConvType = Context.getCanonicalType(Conversion->getConversionType()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1309 | |
| 1310 | // C++ [class.conv.fct]p1: |
| 1311 | // [...] A conversion function is never used to convert a |
| 1312 | // (possibly cv-qualified) object to the (possibly cv-qualified) |
| 1313 | // same object type (or a reference to it), to a (possibly |
| 1314 | // cv-qualified) base class of that type (or a reference to it), |
| 1315 | // or to (possibly cv-qualified) void. |
| 1316 | // FIXME: Suppress this warning if the conversion function ends up |
| 1317 | // being a virtual function that overrides a virtual function in a |
| 1318 | // base class. |
| 1319 | QualType ClassType |
| 1320 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
| 1321 | if (const ReferenceType *ConvTypeRef = ConvType->getAsReferenceType()) |
| 1322 | ConvType = ConvTypeRef->getPointeeType(); |
| 1323 | if (ConvType->isRecordType()) { |
| 1324 | ConvType = Context.getCanonicalType(ConvType).getUnqualifiedType(); |
| 1325 | if (ConvType == ClassType) |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 1326 | Diag(Conversion->getLocation(), diag::warn_conv_to_self_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 1327 | << ClassType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1328 | else if (IsDerivedFrom(ClassType, ConvType)) |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 1329 | Diag(Conversion->getLocation(), diag::warn_conv_to_base_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 1330 | << ClassType << ConvType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1331 | } else if (ConvType->isVoidType()) { |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 1332 | Diag(Conversion->getLocation(), diag::warn_conv_to_void_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 1333 | << ClassType << ConvType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1334 | } |
| 1335 | |
Douglas Gregor | 70316a0 | 2008-12-26 15:00:45 +0000 | [diff] [blame] | 1336 | if (Conversion->getPreviousDeclaration()) { |
| 1337 | OverloadedFunctionDecl *Conversions = ClassDecl->getConversionFunctions(); |
| 1338 | for (OverloadedFunctionDecl::function_iterator |
| 1339 | Conv = Conversions->function_begin(), |
| 1340 | ConvEnd = Conversions->function_end(); |
| 1341 | Conv != ConvEnd; ++Conv) { |
| 1342 | if (*Conv == Conversion->getPreviousDeclaration()) { |
| 1343 | *Conv = Conversion; |
| 1344 | return (DeclTy *)Conversion; |
| 1345 | } |
| 1346 | } |
| 1347 | assert(Conversion->isInvalidDecl() && "Conversion should not get here."); |
| 1348 | } else |
| 1349 | ClassDecl->addConversionFunction(Context, Conversion); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1350 | |
| 1351 | return (DeclTy *)Conversion; |
| 1352 | } |
| 1353 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 1354 | //===----------------------------------------------------------------------===// |
| 1355 | // Namespace Handling |
| 1356 | //===----------------------------------------------------------------------===// |
| 1357 | |
| 1358 | /// ActOnStartNamespaceDef - This is called at the start of a namespace |
| 1359 | /// definition. |
| 1360 | Sema::DeclTy *Sema::ActOnStartNamespaceDef(Scope *NamespcScope, |
| 1361 | SourceLocation IdentLoc, |
| 1362 | IdentifierInfo *II, |
| 1363 | SourceLocation LBrace) { |
| 1364 | NamespaceDecl *Namespc = |
| 1365 | NamespaceDecl::Create(Context, CurContext, IdentLoc, II); |
| 1366 | Namespc->setLBracLoc(LBrace); |
| 1367 | |
| 1368 | Scope *DeclRegionScope = NamespcScope->getParent(); |
| 1369 | |
| 1370 | if (II) { |
| 1371 | // C++ [namespace.def]p2: |
| 1372 | // The identifier in an original-namespace-definition shall not have been |
| 1373 | // previously defined in the declarative region in which the |
| 1374 | // original-namespace-definition appears. The identifier in an |
| 1375 | // original-namespace-definition is the name of the namespace. Subsequently |
| 1376 | // in that declarative region, it is treated as an original-namespace-name. |
| 1377 | |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1378 | Decl *PrevDecl = LookupName(DeclRegionScope, II, LookupOrdinaryName, |
| 1379 | true); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1380 | |
| 1381 | if (NamespaceDecl *OrigNS = dyn_cast_or_null<NamespaceDecl>(PrevDecl)) { |
| 1382 | // This is an extended namespace definition. |
| 1383 | // Attach this namespace decl to the chain of extended namespace |
| 1384 | // definitions. |
| 1385 | OrigNS->setNextNamespace(Namespc); |
| 1386 | Namespc->setOriginalNamespace(OrigNS->getOriginalNamespace()); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 1387 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1388 | // Remove the previous declaration from the scope. |
| 1389 | if (DeclRegionScope->isDeclScope(OrigNS)) { |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 1390 | IdResolver.RemoveDecl(OrigNS); |
| 1391 | DeclRegionScope->RemoveDecl(OrigNS); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 1392 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1393 | } else if (PrevDecl) { |
| 1394 | // This is an invalid name redefinition. |
| 1395 | Diag(Namespc->getLocation(), diag::err_redefinition_different_kind) |
| 1396 | << Namespc->getDeclName(); |
| 1397 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 1398 | Namespc->setInvalidDecl(); |
| 1399 | // Continue on to push Namespc as current DeclContext and return it. |
| 1400 | } |
| 1401 | |
| 1402 | PushOnScopeChains(Namespc, DeclRegionScope); |
| 1403 | } else { |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 1404 | // FIXME: Handle anonymous namespaces |
| 1405 | } |
| 1406 | |
| 1407 | // Although we could have an invalid decl (i.e. the namespace name is a |
| 1408 | // redefinition), push it as current DeclContext and try to continue parsing. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1409 | // FIXME: We should be able to push Namespc here, so that the |
| 1410 | // each DeclContext for the namespace has the declarations |
| 1411 | // that showed up in that particular namespace definition. |
| 1412 | PushDeclContext(NamespcScope, Namespc); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 1413 | return Namespc; |
| 1414 | } |
| 1415 | |
| 1416 | /// ActOnFinishNamespaceDef - This callback is called after a namespace is |
| 1417 | /// exited. Decl is the DeclTy returned by ActOnStartNamespaceDef. |
| 1418 | void Sema::ActOnFinishNamespaceDef(DeclTy *D, SourceLocation RBrace) { |
| 1419 | Decl *Dcl = static_cast<Decl *>(D); |
| 1420 | NamespaceDecl *Namespc = dyn_cast_or_null<NamespaceDecl>(Dcl); |
| 1421 | assert(Namespc && "Invalid parameter, expected NamespaceDecl"); |
| 1422 | Namespc->setRBracLoc(RBrace); |
| 1423 | PopDeclContext(); |
| 1424 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1425 | |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1426 | Sema::DeclTy *Sema::ActOnUsingDirective(Scope *S, |
| 1427 | SourceLocation UsingLoc, |
| 1428 | SourceLocation NamespcLoc, |
| 1429 | const CXXScopeSpec &SS, |
| 1430 | SourceLocation IdentLoc, |
| 1431 | IdentifierInfo *NamespcName, |
| 1432 | AttributeList *AttrList) { |
| 1433 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
| 1434 | assert(NamespcName && "Invalid NamespcName."); |
| 1435 | assert(IdentLoc.isValid() && "Invalid NamespceName location."); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1436 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1437 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1438 | UsingDirectiveDecl *UDir = 0; |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1439 | |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1440 | // Lookup namespace name. |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1441 | LookupResult R = LookupParsedName(S, &SS, NamespcName, |
| 1442 | LookupNamespaceName, false); |
| 1443 | if (R.isAmbiguous()) { |
| 1444 | DiagnoseAmbiguousLookup(R, NamespcName, IdentLoc); |
| 1445 | return 0; |
| 1446 | } |
| 1447 | if (Decl *NS = R) { |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1448 | assert(isa<NamespaceDecl>(NS) && "expected namespace decl"); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1449 | // C++ [namespace.udir]p1: |
| 1450 | // A using-directive specifies that the names in the nominated |
| 1451 | // namespace can be used in the scope in which the |
| 1452 | // using-directive appears after the using-directive. During |
| 1453 | // unqualified name lookup (3.4.1), the names appear as if they |
| 1454 | // were declared in the nearest enclosing namespace which |
| 1455 | // contains both the using-directive and the nominated |
| 1456 | // namespace. [Note: in this context, “contains” means “contains |
| 1457 | // directly or indirectly”. ] |
| 1458 | |
| 1459 | // Find enclosing context containing both using-directive and |
| 1460 | // nominated namespace. |
| 1461 | DeclContext *CommonAncestor = cast<DeclContext>(NS); |
| 1462 | while (CommonAncestor && !CommonAncestor->Encloses(CurContext)) |
| 1463 | CommonAncestor = CommonAncestor->getParent(); |
| 1464 | |
| 1465 | UDir = UsingDirectiveDecl::Create(Context, CurContext, UsingLoc, |
| 1466 | NamespcLoc, IdentLoc, |
| 1467 | cast<NamespaceDecl>(NS), |
| 1468 | CommonAncestor); |
| 1469 | PushUsingDirective(S, UDir); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1470 | } else { |
Chris Lattner | ead013e | 2009-01-06 07:24:29 +0000 | [diff] [blame] | 1471 | Diag(IdentLoc, diag::err_expected_namespace_name) << SS.getRange(); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1472 | } |
| 1473 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1474 | // FIXME: We ignore attributes for now. |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1475 | delete AttrList; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1476 | return UDir; |
| 1477 | } |
| 1478 | |
| 1479 | void Sema::PushUsingDirective(Scope *S, UsingDirectiveDecl *UDir) { |
| 1480 | // If scope has associated entity, then using directive is at namespace |
| 1481 | // or translation unit scope. We add UsingDirectiveDecls, into |
| 1482 | // it's lookup structure. |
| 1483 | if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity())) |
| 1484 | Ctx->addDecl(UDir); |
| 1485 | else |
| 1486 | // Otherwise it is block-sope. using-directives will affect lookup |
| 1487 | // only to the end of scope. |
| 1488 | S->PushUsingDirective(UDir); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1489 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1490 | |
| 1491 | /// AddCXXDirectInitializerToDecl - This action is called immediately after |
| 1492 | /// ActOnDeclarator, when a C++ direct initializer is present. |
| 1493 | /// e.g: "int x(1);" |
| 1494 | void Sema::AddCXXDirectInitializerToDecl(DeclTy *Dcl, SourceLocation LParenLoc, |
| 1495 | ExprTy **ExprTys, unsigned NumExprs, |
| 1496 | SourceLocation *CommaLocs, |
| 1497 | SourceLocation RParenLoc) { |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1498 | assert(NumExprs != 0 && ExprTys && "missing expressions"); |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 1499 | Decl *RealDecl = static_cast<Decl *>(Dcl); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1500 | |
| 1501 | // If there is no declaration, there was an error parsing it. Just ignore |
| 1502 | // the initializer. |
| 1503 | if (RealDecl == 0) { |
Ted Kremenek | 15f6139 | 2008-10-06 20:35:04 +0000 | [diff] [blame] | 1504 | for (unsigned i = 0; i != NumExprs; ++i) |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1505 | delete static_cast<Expr *>(ExprTys[i]); |
| 1506 | return; |
| 1507 | } |
| 1508 | |
| 1509 | VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl); |
| 1510 | if (!VDecl) { |
| 1511 | Diag(RealDecl->getLocation(), diag::err_illegal_initializer); |
| 1512 | RealDecl->setInvalidDecl(); |
| 1513 | return; |
| 1514 | } |
| 1515 | |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 1516 | // We will treat direct-initialization as a copy-initialization: |
| 1517 | // int x(1); -as-> int x = 1; |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1518 | // ClassType x(a,b,c); -as-> ClassType x = ClassType(a,b,c); |
| 1519 | // |
| 1520 | // Clients that want to distinguish between the two forms, can check for |
| 1521 | // direct initializer using VarDecl::hasCXXDirectInitializer(). |
| 1522 | // A major benefit is that clients that don't particularly care about which |
| 1523 | // exactly form was it (like the CodeGen) can handle both cases without |
| 1524 | // special case code. |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 1525 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1526 | // C++ 8.5p11: |
| 1527 | // The form of initialization (using parentheses or '=') is generally |
| 1528 | // insignificant, but does matter when the entity being initialized has a |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 1529 | // class type. |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 1530 | QualType DeclInitType = VDecl->getType(); |
| 1531 | if (const ArrayType *Array = Context.getAsArrayType(DeclInitType)) |
| 1532 | DeclInitType = Array->getElementType(); |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 1533 | |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 1534 | if (VDecl->getType()->isRecordType()) { |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 1535 | CXXConstructorDecl *Constructor |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 1536 | = PerformInitializationByConstructor(DeclInitType, |
| 1537 | (Expr **)ExprTys, NumExprs, |
| 1538 | VDecl->getLocation(), |
| 1539 | SourceRange(VDecl->getLocation(), |
| 1540 | RParenLoc), |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1541 | VDecl->getDeclName(), |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 1542 | IK_Direct); |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 1543 | if (!Constructor) { |
| 1544 | RealDecl->setInvalidDecl(); |
| 1545 | } |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 1546 | |
| 1547 | // Let clients know that initialization was done with a direct |
| 1548 | // initializer. |
| 1549 | VDecl->setCXXDirectInitializer(true); |
| 1550 | |
| 1551 | // FIXME: Add ExprTys and Constructor to the RealDecl as part of |
| 1552 | // the initializer. |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 1553 | return; |
| 1554 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1555 | |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 1556 | if (NumExprs > 1) { |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1557 | Diag(CommaLocs[0], diag::err_builtin_direct_init_more_than_one_arg) |
| 1558 | << SourceRange(VDecl->getLocation(), RParenLoc); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1559 | RealDecl->setInvalidDecl(); |
| 1560 | return; |
| 1561 | } |
| 1562 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1563 | // Let clients know that initialization was done with a direct initializer. |
| 1564 | VDecl->setCXXDirectInitializer(true); |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 1565 | |
| 1566 | assert(NumExprs == 1 && "Expected 1 expression"); |
| 1567 | // Set the init expression, handles conversions. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1568 | AddInitializerToDecl(Dcl, ExprArg(*this, ExprTys[0]), /*DirectInit=*/true); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1569 | } |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1570 | |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 1571 | /// PerformInitializationByConstructor - Perform initialization by |
| 1572 | /// constructor (C++ [dcl.init]p14), which may occur as part of |
| 1573 | /// direct-initialization or copy-initialization. We are initializing |
| 1574 | /// an object of type @p ClassType with the given arguments @p |
| 1575 | /// Args. @p Loc is the location in the source code where the |
| 1576 | /// initializer occurs (e.g., a declaration, member initializer, |
| 1577 | /// functional cast, etc.) while @p Range covers the whole |
| 1578 | /// initialization. @p InitEntity is the entity being initialized, |
| 1579 | /// which may by the name of a declaration or a type. @p Kind is the |
| 1580 | /// kind of initialization we're performing, which affects whether |
| 1581 | /// explicit constructors will be considered. When successful, returns |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 1582 | /// the constructor that will be used to perform the initialization; |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 1583 | /// when the initialization fails, emits a diagnostic and returns |
| 1584 | /// null. |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 1585 | CXXConstructorDecl * |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 1586 | Sema::PerformInitializationByConstructor(QualType ClassType, |
| 1587 | Expr **Args, unsigned NumArgs, |
| 1588 | SourceLocation Loc, SourceRange Range, |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1589 | DeclarationName InitEntity, |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 1590 | InitializationKind Kind) { |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 1591 | const RecordType *ClassRec = ClassType->getAsRecordType(); |
| 1592 | assert(ClassRec && "Can only initialize a class type here"); |
| 1593 | |
| 1594 | // C++ [dcl.init]p14: |
| 1595 | // |
| 1596 | // If the initialization is direct-initialization, or if it is |
| 1597 | // copy-initialization where the cv-unqualified version of the |
| 1598 | // source type is the same class as, or a derived class of, the |
| 1599 | // class of the destination, constructors are considered. The |
| 1600 | // applicable constructors are enumerated (13.3.1.3), and the |
| 1601 | // best one is chosen through overload resolution (13.3). The |
| 1602 | // constructor so selected is called to initialize the object, |
| 1603 | // with the initializer expression(s) as its argument(s). If no |
| 1604 | // constructor applies, or the overload resolution is ambiguous, |
| 1605 | // the initialization is ill-formed. |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 1606 | const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(ClassRec->getDecl()); |
| 1607 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 1608 | |
| 1609 | // Add constructors to the overload set. |
Douglas Gregor | 9e7d9de | 2008-12-15 21:24:18 +0000 | [diff] [blame] | 1610 | DeclarationName ConstructorName |
| 1611 | = Context.DeclarationNames.getCXXConstructorName( |
| 1612 | Context.getCanonicalType(ClassType.getUnqualifiedType())); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1613 | DeclContext::lookup_const_iterator Con, ConEnd; |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 1614 | for (llvm::tie(Con, ConEnd) = ClassDecl->lookup(ConstructorName); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1615 | Con != ConEnd; ++Con) { |
| 1616 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con); |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 1617 | if ((Kind == IK_Direct) || |
| 1618 | (Kind == IK_Copy && Constructor->isConvertingConstructor()) || |
| 1619 | (Kind == IK_Default && Constructor->isDefaultConstructor())) |
| 1620 | AddOverloadCandidate(Constructor, Args, NumArgs, CandidateSet); |
| 1621 | } |
| 1622 | |
Douglas Gregor | 9e7d9de | 2008-12-15 21:24:18 +0000 | [diff] [blame] | 1623 | // FIXME: When we decide not to synthesize the implicitly-declared |
| 1624 | // constructors, we'll need to make them appear here. |
| 1625 | |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 1626 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 1627 | switch (BestViableFunction(CandidateSet, Best)) { |
| 1628 | case OR_Success: |
| 1629 | // We found a constructor. Return it. |
| 1630 | return cast<CXXConstructorDecl>(Best->Function); |
| 1631 | |
| 1632 | case OR_No_Viable_Function: |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 1633 | if (InitEntity) |
| 1634 | Diag(Loc, diag::err_ovl_no_viable_function_in_init) |
| 1635 | << InitEntity << (unsigned)CandidateSet.size() << Range; |
| 1636 | else |
| 1637 | Diag(Loc, diag::err_ovl_no_viable_function_in_init) |
| 1638 | << ClassType << (unsigned)CandidateSet.size() << Range; |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 1639 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 1640 | return 0; |
| 1641 | |
| 1642 | case OR_Ambiguous: |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 1643 | if (InitEntity) |
| 1644 | Diag(Loc, diag::err_ovl_ambiguous_init) << InitEntity << Range; |
| 1645 | else |
| 1646 | Diag(Loc, diag::err_ovl_ambiguous_init) << ClassType << Range; |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 1647 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 1648 | return 0; |
| 1649 | } |
| 1650 | |
| 1651 | return 0; |
| 1652 | } |
| 1653 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1654 | /// CompareReferenceRelationship - Compare the two types T1 and T2 to |
| 1655 | /// determine whether they are reference-related, |
| 1656 | /// reference-compatible, reference-compatible with added |
| 1657 | /// qualification, or incompatible, for use in C++ initialization by |
| 1658 | /// reference (C++ [dcl.ref.init]p4). Neither type can be a reference |
| 1659 | /// type, and the first type (T1) is the pointee type of the reference |
| 1660 | /// type being initialized. |
| 1661 | Sema::ReferenceCompareResult |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1662 | Sema::CompareReferenceRelationship(QualType T1, QualType T2, |
| 1663 | bool& DerivedToBase) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1664 | assert(!T1->isReferenceType() && "T1 must be the pointee type of the reference type"); |
| 1665 | assert(!T2->isReferenceType() && "T2 cannot be a reference type"); |
| 1666 | |
| 1667 | T1 = Context.getCanonicalType(T1); |
| 1668 | T2 = Context.getCanonicalType(T2); |
| 1669 | QualType UnqualT1 = T1.getUnqualifiedType(); |
| 1670 | QualType UnqualT2 = T2.getUnqualifiedType(); |
| 1671 | |
| 1672 | // C++ [dcl.init.ref]p4: |
| 1673 | // Given types “cv1 T1” and “cv2 T2,” “cv1 T1” is |
| 1674 | // reference-related to “cv2 T2” if T1 is the same type as T2, or |
| 1675 | // T1 is a base class of T2. |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1676 | if (UnqualT1 == UnqualT2) |
| 1677 | DerivedToBase = false; |
| 1678 | else if (IsDerivedFrom(UnqualT2, UnqualT1)) |
| 1679 | DerivedToBase = true; |
| 1680 | else |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1681 | return Ref_Incompatible; |
| 1682 | |
| 1683 | // At this point, we know that T1 and T2 are reference-related (at |
| 1684 | // least). |
| 1685 | |
| 1686 | // C++ [dcl.init.ref]p4: |
| 1687 | // "cv1 T1” is reference-compatible with “cv2 T2” if T1 is |
| 1688 | // reference-related to T2 and cv1 is the same cv-qualification |
| 1689 | // as, or greater cv-qualification than, cv2. For purposes of |
| 1690 | // overload resolution, cases for which cv1 is greater |
| 1691 | // cv-qualification than cv2 are identified as |
| 1692 | // reference-compatible with added qualification (see 13.3.3.2). |
| 1693 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 1694 | return Ref_Compatible; |
| 1695 | else if (T1.isMoreQualifiedThan(T2)) |
| 1696 | return Ref_Compatible_With_Added_Qualification; |
| 1697 | else |
| 1698 | return Ref_Related; |
| 1699 | } |
| 1700 | |
| 1701 | /// CheckReferenceInit - Check the initialization of a reference |
| 1702 | /// variable with the given initializer (C++ [dcl.init.ref]). Init is |
| 1703 | /// the initializer (either a simple initializer or an initializer |
Douglas Gregor | 3205a78 | 2008-10-29 23:31:03 +0000 | [diff] [blame] | 1704 | /// list), and DeclType is the type of the declaration. When ICS is |
| 1705 | /// non-null, this routine will compute the implicit conversion |
| 1706 | /// sequence according to C++ [over.ics.ref] and will not produce any |
| 1707 | /// diagnostics; when ICS is null, it will emit diagnostics when any |
| 1708 | /// errors are found. Either way, a return value of true indicates |
| 1709 | /// that there was a failure, a return value of false indicates that |
| 1710 | /// the reference initialization succeeded. |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1711 | /// |
| 1712 | /// When @p SuppressUserConversions, user-defined conversions are |
| 1713 | /// suppressed. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1714 | /// When @p AllowExplicit, we also permit explicit user-defined |
| 1715 | /// conversion functions. |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1716 | bool |
| 1717 | Sema::CheckReferenceInit(Expr *&Init, QualType &DeclType, |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1718 | ImplicitConversionSequence *ICS, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1719 | bool SuppressUserConversions, |
| 1720 | bool AllowExplicit) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1721 | assert(DeclType->isReferenceType() && "Reference init needs a reference"); |
| 1722 | |
| 1723 | QualType T1 = DeclType->getAsReferenceType()->getPointeeType(); |
| 1724 | QualType T2 = Init->getType(); |
| 1725 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 1726 | // If the initializer is the address of an overloaded function, try |
| 1727 | // to resolve the overloaded function. If all goes well, T2 is the |
| 1728 | // type of the resulting function. |
| 1729 | if (T2->isOverloadType()) { |
| 1730 | FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Init, DeclType, |
| 1731 | ICS != 0); |
| 1732 | if (Fn) { |
| 1733 | // Since we're performing this reference-initialization for |
| 1734 | // real, update the initializer with the resulting function. |
| 1735 | if (!ICS) |
| 1736 | FixOverloadedFunctionReference(Init, Fn); |
| 1737 | |
| 1738 | T2 = Fn->getType(); |
| 1739 | } |
| 1740 | } |
| 1741 | |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1742 | // Compute some basic properties of the types and the initializer. |
| 1743 | bool DerivedToBase = false; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1744 | Expr::isLvalueResult InitLvalue = Init->isLvalue(Context); |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1745 | ReferenceCompareResult RefRelationship |
| 1746 | = CompareReferenceRelationship(T1, T2, DerivedToBase); |
| 1747 | |
| 1748 | // Most paths end in a failed conversion. |
| 1749 | if (ICS) |
| 1750 | ICS->ConversionKind = ImplicitConversionSequence::BadConversion; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1751 | |
| 1752 | // C++ [dcl.init.ref]p5: |
| 1753 | // A reference to type “cv1 T1” is initialized by an expression |
| 1754 | // of type “cv2 T2” as follows: |
| 1755 | |
| 1756 | // -- If the initializer expression |
| 1757 | |
| 1758 | bool BindsDirectly = false; |
| 1759 | // -- is an lvalue (but is not a bit-field), and “cv1 T1” is |
| 1760 | // reference-compatible with “cv2 T2,” or |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1761 | // |
| 1762 | // Note that the bit-field check is skipped if we are just computing |
| 1763 | // the implicit conversion sequence (C++ [over.best.ics]p2). |
| 1764 | if (InitLvalue == Expr::LV_Valid && (ICS || !Init->isBitField()) && |
| 1765 | RefRelationship >= Ref_Compatible_With_Added_Qualification) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1766 | BindsDirectly = true; |
| 1767 | |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1768 | if (ICS) { |
| 1769 | // C++ [over.ics.ref]p1: |
| 1770 | // When a parameter of reference type binds directly (8.5.3) |
| 1771 | // to an argument expression, the implicit conversion sequence |
| 1772 | // is the identity conversion, unless the argument expression |
| 1773 | // has a type that is a derived class of the parameter type, |
| 1774 | // in which case the implicit conversion sequence is a |
| 1775 | // derived-to-base Conversion (13.3.3.1). |
| 1776 | ICS->ConversionKind = ImplicitConversionSequence::StandardConversion; |
| 1777 | ICS->Standard.First = ICK_Identity; |
| 1778 | ICS->Standard.Second = DerivedToBase? ICK_Derived_To_Base : ICK_Identity; |
| 1779 | ICS->Standard.Third = ICK_Identity; |
| 1780 | ICS->Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 1781 | ICS->Standard.ToTypePtr = T1.getAsOpaquePtr(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1782 | ICS->Standard.ReferenceBinding = true; |
| 1783 | ICS->Standard.DirectBinding = true; |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1784 | |
| 1785 | // Nothing more to do: the inaccessibility/ambiguity check for |
| 1786 | // derived-to-base conversions is suppressed when we're |
| 1787 | // computing the implicit conversion sequence (C++ |
| 1788 | // [over.best.ics]p2). |
| 1789 | return false; |
| 1790 | } else { |
| 1791 | // Perform the conversion. |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1792 | // FIXME: Binding to a subobject of the lvalue is going to require |
| 1793 | // more AST annotation than this. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1794 | ImpCastExprToType(Init, T1, /*isLvalue=*/true); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1795 | } |
| 1796 | } |
| 1797 | |
| 1798 | // -- has a class type (i.e., T2 is a class type) and can be |
| 1799 | // implicitly converted to an lvalue of type “cv3 T3,” |
| 1800 | // where “cv1 T1” is reference-compatible with “cv3 T3” |
| 1801 | // 92) (this conversion is selected by enumerating the |
| 1802 | // applicable conversion functions (13.3.1.6) and choosing |
| 1803 | // the best one through overload resolution (13.3)), |
Douglas Gregor | cb9b977 | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 1804 | if (!SuppressUserConversions && T2->isRecordType()) { |
| 1805 | // FIXME: Look for conversions in base classes! |
| 1806 | CXXRecordDecl *T2RecordDecl |
| 1807 | = dyn_cast<CXXRecordDecl>(T2->getAsRecordType()->getDecl()); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1808 | |
Douglas Gregor | cb9b977 | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 1809 | OverloadCandidateSet CandidateSet; |
| 1810 | OverloadedFunctionDecl *Conversions |
| 1811 | = T2RecordDecl->getConversionFunctions(); |
| 1812 | for (OverloadedFunctionDecl::function_iterator Func |
| 1813 | = Conversions->function_begin(); |
| 1814 | Func != Conversions->function_end(); ++Func) { |
| 1815 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(*Func); |
| 1816 | |
| 1817 | // If the conversion function doesn't return a reference type, |
| 1818 | // it can't be considered for this conversion. |
| 1819 | // FIXME: This will change when we support rvalue references. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1820 | if (Conv->getConversionType()->isReferenceType() && |
| 1821 | (AllowExplicit || !Conv->isExplicit())) |
Douglas Gregor | cb9b977 | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 1822 | AddConversionCandidate(Conv, Init, DeclType, CandidateSet); |
| 1823 | } |
| 1824 | |
| 1825 | OverloadCandidateSet::iterator Best; |
| 1826 | switch (BestViableFunction(CandidateSet, Best)) { |
| 1827 | case OR_Success: |
| 1828 | // This is a direct binding. |
| 1829 | BindsDirectly = true; |
| 1830 | |
| 1831 | if (ICS) { |
| 1832 | // C++ [over.ics.ref]p1: |
| 1833 | // |
| 1834 | // [...] If the parameter binds directly to the result of |
| 1835 | // applying a conversion function to the argument |
| 1836 | // expression, the implicit conversion sequence is a |
| 1837 | // user-defined conversion sequence (13.3.3.1.2), with the |
| 1838 | // second standard conversion sequence either an identity |
| 1839 | // conversion or, if the conversion function returns an |
| 1840 | // entity of a type that is a derived class of the parameter |
| 1841 | // type, a derived-to-base Conversion. |
| 1842 | ICS->ConversionKind = ImplicitConversionSequence::UserDefinedConversion; |
| 1843 | ICS->UserDefined.Before = Best->Conversions[0].Standard; |
| 1844 | ICS->UserDefined.After = Best->FinalConversion; |
| 1845 | ICS->UserDefined.ConversionFunction = Best->Function; |
| 1846 | assert(ICS->UserDefined.After.ReferenceBinding && |
| 1847 | ICS->UserDefined.After.DirectBinding && |
| 1848 | "Expected a direct reference binding!"); |
| 1849 | return false; |
| 1850 | } else { |
| 1851 | // Perform the conversion. |
| 1852 | // FIXME: Binding to a subobject of the lvalue is going to require |
| 1853 | // more AST annotation than this. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1854 | ImpCastExprToType(Init, T1, /*isLvalue=*/true); |
Douglas Gregor | cb9b977 | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 1855 | } |
| 1856 | break; |
| 1857 | |
| 1858 | case OR_Ambiguous: |
| 1859 | assert(false && "Ambiguous reference binding conversions not implemented."); |
| 1860 | return true; |
| 1861 | |
| 1862 | case OR_No_Viable_Function: |
| 1863 | // There was no suitable conversion; continue with other checks. |
| 1864 | break; |
| 1865 | } |
| 1866 | } |
| 1867 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1868 | if (BindsDirectly) { |
| 1869 | // C++ [dcl.init.ref]p4: |
| 1870 | // [...] In all cases where the reference-related or |
| 1871 | // reference-compatible relationship of two types is used to |
| 1872 | // establish the validity of a reference binding, and T1 is a |
| 1873 | // base class of T2, a program that necessitates such a binding |
| 1874 | // is ill-formed if T1 is an inaccessible (clause 11) or |
| 1875 | // ambiguous (10.2) base class of T2. |
| 1876 | // |
| 1877 | // Note that we only check this condition when we're allowed to |
| 1878 | // complain about errors, because we should not be checking for |
| 1879 | // ambiguity (or inaccessibility) unless the reference binding |
| 1880 | // actually happens. |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1881 | if (DerivedToBase) |
| 1882 | return CheckDerivedToBaseConversion(T2, T1, |
| 1883 | Init->getSourceRange().getBegin(), |
| 1884 | Init->getSourceRange()); |
| 1885 | else |
| 1886 | return false; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1887 | } |
| 1888 | |
| 1889 | // -- Otherwise, the reference shall be to a non-volatile const |
| 1890 | // type (i.e., cv1 shall be const). |
| 1891 | if (T1.getCVRQualifiers() != QualType::Const) { |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1892 | if (!ICS) |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1893 | Diag(Init->getSourceRange().getBegin(), |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 1894 | diag::err_not_reference_to_const_init) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 1895 | << T1 << (InitLvalue != Expr::LV_Valid? "temporary" : "value") |
| 1896 | << T2 << Init->getSourceRange(); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1897 | return true; |
| 1898 | } |
| 1899 | |
| 1900 | // -- If the initializer expression is an rvalue, with T2 a |
| 1901 | // class type, and “cv1 T1” is reference-compatible with |
| 1902 | // “cv2 T2,” the reference is bound in one of the |
| 1903 | // following ways (the choice is implementation-defined): |
| 1904 | // |
| 1905 | // -- The reference is bound to the object represented by |
| 1906 | // the rvalue (see 3.10) or to a sub-object within that |
| 1907 | // object. |
| 1908 | // |
| 1909 | // -- A temporary of type “cv1 T2” [sic] is created, and |
| 1910 | // a constructor is called to copy the entire rvalue |
| 1911 | // object into the temporary. The reference is bound to |
| 1912 | // the temporary or to a sub-object within the |
| 1913 | // temporary. |
| 1914 | // |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1915 | // The constructor that would be used to make the copy |
| 1916 | // shall be callable whether or not the copy is actually |
| 1917 | // done. |
| 1918 | // |
| 1919 | // Note that C++0x [dcl.ref.init]p5 takes away this implementation |
| 1920 | // freedom, so we will always take the first option and never build |
| 1921 | // a temporary in this case. FIXME: We will, however, have to check |
| 1922 | // for the presence of a copy constructor in C++98/03 mode. |
| 1923 | if (InitLvalue != Expr::LV_Valid && T2->isRecordType() && |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1924 | RefRelationship >= Ref_Compatible_With_Added_Qualification) { |
| 1925 | if (ICS) { |
| 1926 | ICS->ConversionKind = ImplicitConversionSequence::StandardConversion; |
| 1927 | ICS->Standard.First = ICK_Identity; |
| 1928 | ICS->Standard.Second = DerivedToBase? ICK_Derived_To_Base : ICK_Identity; |
| 1929 | ICS->Standard.Third = ICK_Identity; |
| 1930 | ICS->Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 1931 | ICS->Standard.ToTypePtr = T1.getAsOpaquePtr(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1932 | ICS->Standard.ReferenceBinding = true; |
| 1933 | ICS->Standard.DirectBinding = false; |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1934 | } else { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1935 | // FIXME: Binding to a subobject of the rvalue is going to require |
| 1936 | // more AST annotation than this. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1937 | ImpCastExprToType(Init, T1, /*isLvalue=*/true); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1938 | } |
| 1939 | return false; |
| 1940 | } |
| 1941 | |
| 1942 | // -- Otherwise, a temporary of type “cv1 T1” is created and |
| 1943 | // initialized from the initializer expression using the |
| 1944 | // rules for a non-reference copy initialization (8.5). The |
| 1945 | // reference is then bound to the temporary. If T1 is |
| 1946 | // reference-related to T2, cv1 must be the same |
| 1947 | // cv-qualification as, or greater cv-qualification than, |
| 1948 | // cv2; otherwise, the program is ill-formed. |
| 1949 | if (RefRelationship == Ref_Related) { |
| 1950 | // If cv1 == cv2 or cv1 is a greater cv-qualified than cv2, then |
| 1951 | // we would be reference-compatible or reference-compatible with |
| 1952 | // added qualification. But that wasn't the case, so the reference |
| 1953 | // initialization fails. |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1954 | if (!ICS) |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1955 | Diag(Init->getSourceRange().getBegin(), |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 1956 | diag::err_reference_init_drops_quals) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 1957 | << T1 << (InitLvalue != Expr::LV_Valid? "temporary" : "value") |
| 1958 | << T2 << Init->getSourceRange(); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1959 | return true; |
| 1960 | } |
| 1961 | |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1962 | // If at least one of the types is a class type, the types are not |
| 1963 | // related, and we aren't allowed any user conversions, the |
| 1964 | // reference binding fails. This case is important for breaking |
| 1965 | // recursion, since TryImplicitConversion below will attempt to |
| 1966 | // create a temporary through the use of a copy constructor. |
| 1967 | if (SuppressUserConversions && RefRelationship == Ref_Incompatible && |
| 1968 | (T1->isRecordType() || T2->isRecordType())) { |
| 1969 | if (!ICS) |
| 1970 | Diag(Init->getSourceRange().getBegin(), |
| 1971 | diag::err_typecheck_convert_incompatible) |
| 1972 | << DeclType << Init->getType() << "initializing" << Init->getSourceRange(); |
| 1973 | return true; |
| 1974 | } |
| 1975 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1976 | // Actually try to convert the initializer to T1. |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1977 | if (ICS) { |
| 1978 | /// C++ [over.ics.ref]p2: |
| 1979 | /// |
| 1980 | /// When a parameter of reference type is not bound directly to |
| 1981 | /// an argument expression, the conversion sequence is the one |
| 1982 | /// required to convert the argument expression to the |
| 1983 | /// underlying type of the reference according to |
| 1984 | /// 13.3.3.1. Conceptually, this conversion sequence corresponds |
| 1985 | /// to copy-initializing a temporary of the underlying type with |
| 1986 | /// the argument expression. Any difference in top-level |
| 1987 | /// cv-qualification is subsumed by the initialization itself |
| 1988 | /// and does not constitute a conversion. |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1989 | *ICS = TryImplicitConversion(Init, T1, SuppressUserConversions); |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1990 | return ICS->ConversionKind == ImplicitConversionSequence::BadConversion; |
| 1991 | } else { |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1992 | return PerformImplicitConversion(Init, T1, "initializing"); |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1993 | } |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1994 | } |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 1995 | |
| 1996 | /// CheckOverloadedOperatorDeclaration - Check whether the declaration |
| 1997 | /// of this overloaded operator is well-formed. If so, returns false; |
| 1998 | /// otherwise, emits appropriate diagnostics and returns true. |
| 1999 | bool Sema::CheckOverloadedOperatorDeclaration(FunctionDecl *FnDecl) { |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 2000 | assert(FnDecl && FnDecl->isOverloadedOperator() && |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 2001 | "Expected an overloaded operator declaration"); |
| 2002 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 2003 | OverloadedOperatorKind Op = FnDecl->getOverloadedOperator(); |
| 2004 | |
| 2005 | // C++ [over.oper]p5: |
| 2006 | // The allocation and deallocation functions, operator new, |
| 2007 | // operator new[], operator delete and operator delete[], are |
| 2008 | // described completely in 3.7.3. The attributes and restrictions |
| 2009 | // found in the rest of this subclause do not apply to them unless |
| 2010 | // explicitly stated in 3.7.3. |
| 2011 | // FIXME: Write a separate routine for checking this. For now, just |
| 2012 | // allow it. |
| 2013 | if (Op == OO_New || Op == OO_Array_New || |
| 2014 | Op == OO_Delete || Op == OO_Array_Delete) |
| 2015 | return false; |
| 2016 | |
| 2017 | // C++ [over.oper]p6: |
| 2018 | // An operator function shall either be a non-static member |
| 2019 | // function or be a non-member function and have at least one |
| 2020 | // parameter whose type is a class, a reference to a class, an |
| 2021 | // enumeration, or a reference to an enumeration. |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 2022 | if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 2023 | if (MethodDecl->isStatic()) |
| 2024 | return Diag(FnDecl->getLocation(), |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2025 | diag::err_operator_overload_static) << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 2026 | } else { |
| 2027 | bool ClassOrEnumParam = false; |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 2028 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(), |
| 2029 | ParamEnd = FnDecl->param_end(); |
| 2030 | Param != ParamEnd; ++Param) { |
| 2031 | QualType ParamType = (*Param)->getType().getNonReferenceType(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 2032 | if (ParamType->isRecordType() || ParamType->isEnumeralType()) { |
| 2033 | ClassOrEnumParam = true; |
| 2034 | break; |
| 2035 | } |
| 2036 | } |
| 2037 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 2038 | if (!ClassOrEnumParam) |
| 2039 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 2040 | diag::err_operator_overload_needs_class_or_enum) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2041 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 2042 | } |
| 2043 | |
| 2044 | // C++ [over.oper]p8: |
| 2045 | // An operator function cannot have default arguments (8.3.6), |
| 2046 | // except where explicitly stated below. |
| 2047 | // |
| 2048 | // Only the function-call operator allows default arguments |
| 2049 | // (C++ [over.call]p1). |
| 2050 | if (Op != OO_Call) { |
| 2051 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 2052 | Param != FnDecl->param_end(); ++Param) { |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2053 | if ((*Param)->hasUnparsedDefaultArg()) |
| 2054 | return Diag((*Param)->getLocation(), |
| 2055 | diag::err_operator_overload_default_arg) |
| 2056 | << FnDecl->getDeclName(); |
| 2057 | else if (Expr *DefArg = (*Param)->getDefaultArg()) |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 2058 | return Diag((*Param)->getLocation(), |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 2059 | diag::err_operator_overload_default_arg) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2060 | << FnDecl->getDeclName() << DefArg->getSourceRange(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 2061 | } |
| 2062 | } |
| 2063 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 2064 | static const bool OperatorUses[NUM_OVERLOADED_OPERATORS][3] = { |
| 2065 | { false, false, false } |
| 2066 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 2067 | , { Unary, Binary, MemberOnly } |
| 2068 | #include "clang/Basic/OperatorKinds.def" |
| 2069 | }; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 2070 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 2071 | bool CanBeUnaryOperator = OperatorUses[Op][0]; |
| 2072 | bool CanBeBinaryOperator = OperatorUses[Op][1]; |
| 2073 | bool MustBeMemberOperator = OperatorUses[Op][2]; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 2074 | |
| 2075 | // C++ [over.oper]p8: |
| 2076 | // [...] Operator functions cannot have more or fewer parameters |
| 2077 | // than the number required for the corresponding operator, as |
| 2078 | // described in the rest of this subclause. |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 2079 | unsigned NumParams = FnDecl->getNumParams() |
| 2080 | + (isa<CXXMethodDecl>(FnDecl)? 1 : 0); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 2081 | if (Op != OO_Call && |
| 2082 | ((NumParams == 1 && !CanBeUnaryOperator) || |
| 2083 | (NumParams == 2 && !CanBeBinaryOperator) || |
| 2084 | (NumParams < 1) || (NumParams > 2))) { |
| 2085 | // We have the wrong number of parameters. |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 2086 | unsigned ErrorKind; |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 2087 | if (CanBeUnaryOperator && CanBeBinaryOperator) { |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 2088 | ErrorKind = 2; // 2 -> unary or binary. |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 2089 | } else if (CanBeUnaryOperator) { |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 2090 | ErrorKind = 0; // 0 -> unary |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 2091 | } else { |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 2092 | assert(CanBeBinaryOperator && |
| 2093 | "All non-call overloaded operators are unary or binary!"); |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 2094 | ErrorKind = 1; // 1 -> binary |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 2095 | } |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 2096 | |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 2097 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_must_be) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2098 | << FnDecl->getDeclName() << NumParams << ErrorKind; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 2099 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2100 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 2101 | // Overloaded operators other than operator() cannot be variadic. |
| 2102 | if (Op != OO_Call && |
| 2103 | FnDecl->getType()->getAsFunctionTypeProto()->isVariadic()) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 2104 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_variadic) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2105 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 2106 | } |
| 2107 | |
| 2108 | // Some operators must be non-static member functions. |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 2109 | if (MustBeMemberOperator && !isa<CXXMethodDecl>(FnDecl)) { |
| 2110 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 2111 | diag::err_operator_overload_must_be_member) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2112 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 2113 | } |
| 2114 | |
| 2115 | // C++ [over.inc]p1: |
| 2116 | // The user-defined function called operator++ implements the |
| 2117 | // prefix and postfix ++ operator. If this function is a member |
| 2118 | // function with no parameters, or a non-member function with one |
| 2119 | // parameter of class or enumeration type, it defines the prefix |
| 2120 | // increment operator ++ for objects of that type. If the function |
| 2121 | // is a member function with one parameter (which shall be of type |
| 2122 | // int) or a non-member function with two parameters (the second |
| 2123 | // of which shall be of type int), it defines the postfix |
| 2124 | // increment operator ++ for objects of that type. |
| 2125 | if ((Op == OO_PlusPlus || Op == OO_MinusMinus) && NumParams == 2) { |
| 2126 | ParmVarDecl *LastParam = FnDecl->getParamDecl(FnDecl->getNumParams() - 1); |
| 2127 | bool ParamIsInt = false; |
| 2128 | if (const BuiltinType *BT = LastParam->getType()->getAsBuiltinType()) |
| 2129 | ParamIsInt = BT->getKind() == BuiltinType::Int; |
| 2130 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 2131 | if (!ParamIsInt) |
| 2132 | return Diag(LastParam->getLocation(), |
| 2133 | diag::err_operator_overload_post_incdec_must_be_int) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2134 | << LastParam->getType() << (Op == OO_MinusMinus); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 2135 | } |
| 2136 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2137 | // Notify the class if it got an assignment operator. |
| 2138 | if (Op == OO_Equal) { |
| 2139 | // Would have returned earlier otherwise. |
| 2140 | assert(isa<CXXMethodDecl>(FnDecl) && |
| 2141 | "Overloaded = not member, but not filtered."); |
| 2142 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
| 2143 | Method->getParent()->addedAssignmentOperator(Context, Method); |
| 2144 | } |
| 2145 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 2146 | return false; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 2147 | } |
Chris Lattner | 5a003a4 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 2148 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 2149 | /// ActOnStartLinkageSpecification - Parsed the beginning of a C++ |
| 2150 | /// linkage specification, including the language and (if present) |
| 2151 | /// the '{'. ExternLoc is the location of the 'extern', LangLoc is |
| 2152 | /// the location of the language string literal, which is provided |
| 2153 | /// by Lang/StrSize. LBraceLoc, if valid, provides the location of |
| 2154 | /// the '{' brace. Otherwise, this linkage specification does not |
| 2155 | /// have any braces. |
| 2156 | Sema::DeclTy *Sema::ActOnStartLinkageSpecification(Scope *S, |
| 2157 | SourceLocation ExternLoc, |
| 2158 | SourceLocation LangLoc, |
| 2159 | const char *Lang, |
| 2160 | unsigned StrSize, |
| 2161 | SourceLocation LBraceLoc) { |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 2162 | LinkageSpecDecl::LanguageIDs Language; |
| 2163 | if (strncmp(Lang, "\"C\"", StrSize) == 0) |
| 2164 | Language = LinkageSpecDecl::lang_c; |
| 2165 | else if (strncmp(Lang, "\"C++\"", StrSize) == 0) |
| 2166 | Language = LinkageSpecDecl::lang_cxx; |
| 2167 | else { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 2168 | Diag(LangLoc, diag::err_bad_language); |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 2169 | return 0; |
| 2170 | } |
| 2171 | |
| 2172 | // FIXME: Add all the various semantics of linkage specifications |
| 2173 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 2174 | LinkageSpecDecl *D = LinkageSpecDecl::Create(Context, CurContext, |
| 2175 | LangLoc, Language, |
| 2176 | LBraceLoc.isValid()); |
Douglas Gregor | 482b77d | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 2177 | CurContext->addDecl(D); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 2178 | PushDeclContext(S, D); |
| 2179 | return D; |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 2180 | } |
| 2181 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 2182 | /// ActOnFinishLinkageSpecification - Completely the definition of |
| 2183 | /// the C++ linkage specification LinkageSpec. If RBraceLoc is |
| 2184 | /// valid, it's the position of the closing '}' brace in a linkage |
| 2185 | /// specification that uses braces. |
| 2186 | Sema::DeclTy *Sema::ActOnFinishLinkageSpecification(Scope *S, |
| 2187 | DeclTy *LinkageSpec, |
| 2188 | SourceLocation RBraceLoc) { |
| 2189 | if (LinkageSpec) |
| 2190 | PopDeclContext(); |
| 2191 | return LinkageSpec; |
Chris Lattner | 5a003a4 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 2192 | } |
| 2193 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 2194 | /// ActOnExceptionDeclarator - Parsed the exception-declarator in a C++ catch |
| 2195 | /// handler. |
| 2196 | Sema::DeclTy *Sema::ActOnExceptionDeclarator(Scope *S, Declarator &D) |
| 2197 | { |
| 2198 | QualType ExDeclType = GetTypeForDeclarator(D, S); |
| 2199 | SourceLocation Begin = D.getDeclSpec().getSourceRange().getBegin(); |
| 2200 | |
| 2201 | bool Invalid = false; |
| 2202 | |
| 2203 | // Arrays and functions decay. |
| 2204 | if (ExDeclType->isArrayType()) |
| 2205 | ExDeclType = Context.getArrayDecayedType(ExDeclType); |
| 2206 | else if (ExDeclType->isFunctionType()) |
| 2207 | ExDeclType = Context.getPointerType(ExDeclType); |
| 2208 | |
| 2209 | // C++ 15.3p1: The exception-declaration shall not denote an incomplete type. |
| 2210 | // The exception-declaration shall not denote a pointer or reference to an |
| 2211 | // incomplete type, other than [cv] void*. |
| 2212 | QualType BaseType = ExDeclType; |
| 2213 | int Mode = 0; // 0 for direct type, 1 for pointer, 2 for reference |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2214 | unsigned DK = diag::err_catch_incomplete; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 2215 | if (const PointerType *Ptr = BaseType->getAsPointerType()) { |
| 2216 | BaseType = Ptr->getPointeeType(); |
| 2217 | Mode = 1; |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2218 | DK = diag::err_catch_incomplete_ptr; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 2219 | } else if(const ReferenceType *Ref = BaseType->getAsReferenceType()) { |
| 2220 | BaseType = Ref->getPointeeType(); |
| 2221 | Mode = 2; |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2222 | DK = diag::err_catch_incomplete_ref; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 2223 | } |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2224 | if ((Mode == 0 || !BaseType->isVoidType()) && |
| 2225 | DiagnoseIncompleteType(Begin, BaseType, DK)) |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 2226 | Invalid = true; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 2227 | |
Sebastian Redl | 8351da0 | 2008-12-22 21:35:02 +0000 | [diff] [blame] | 2228 | // FIXME: Need to test for ability to copy-construct and destroy the |
| 2229 | // exception variable. |
| 2230 | // FIXME: Need to check for abstract classes. |
| 2231 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 2232 | IdentifierInfo *II = D.getIdentifier(); |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 2233 | if (Decl *PrevDecl = LookupName(S, II, LookupOrdinaryName)) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 2234 | // The scope should be freshly made just for us. There is just no way |
| 2235 | // it contains any previous declaration. |
| 2236 | assert(!S->isDeclScope(PrevDecl)); |
| 2237 | if (PrevDecl->isTemplateParameter()) { |
| 2238 | // Maybe we will complain about the shadowed template parameter. |
| 2239 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
| 2240 | |
| 2241 | } |
| 2242 | } |
| 2243 | |
| 2244 | VarDecl *ExDecl = VarDecl::Create(Context, CurContext, D.getIdentifierLoc(), |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2245 | II, ExDeclType, VarDecl::None, Begin); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 2246 | if (D.getInvalidType() || Invalid) |
| 2247 | ExDecl->setInvalidDecl(); |
| 2248 | |
| 2249 | if (D.getCXXScopeSpec().isSet()) { |
| 2250 | Diag(D.getIdentifierLoc(), diag::err_qualified_catch_declarator) |
| 2251 | << D.getCXXScopeSpec().getRange(); |
| 2252 | ExDecl->setInvalidDecl(); |
| 2253 | } |
| 2254 | |
| 2255 | // Add the exception declaration into this scope. |
| 2256 | S->AddDecl(ExDecl); |
| 2257 | if (II) |
| 2258 | IdResolver.AddDecl(ExDecl); |
| 2259 | |
| 2260 | ProcessDeclAttributes(ExDecl, D); |
| 2261 | return ExDecl; |
| 2262 | } |