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