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/Basic/Diagnostic.h" |
Daniel Dunbar | 12bc692 | 2008-08-11 03:27:53 +0000 | [diff] [blame] | 22 | #include "clang/Parse/DeclSpec.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(), |
| 77 | diag::err_param_default_argument_references_param, |
| 78 | Param->getName(), 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(), |
| 85 | diag::err_param_default_argument_references_local, |
| 86 | VDecl->getName(), 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(), |
| 98 | diag::err_param_default_argument_references_this, |
| 99 | ThisE->getSourceRange()); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 100 | } |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /// ActOnParamDefaultArgument - Check whether the default argument |
| 104 | /// provided for a function parameter is well-formed. If so, attach it |
| 105 | /// to the parameter declaration. |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 106 | void |
| 107 | Sema::ActOnParamDefaultArgument(DeclTy *param, SourceLocation EqualLoc, |
| 108 | ExprTy *defarg) { |
| 109 | ParmVarDecl *Param = (ParmVarDecl *)param; |
| 110 | llvm::OwningPtr<Expr> DefaultArg((Expr *)defarg); |
| 111 | QualType ParamType = Param->getType(); |
| 112 | |
| 113 | // Default arguments are only permitted in C++ |
| 114 | if (!getLangOptions().CPlusPlus) { |
| 115 | Diag(EqualLoc, diag::err_param_default_argument, |
| 116 | DefaultArg->getSourceRange()); |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | // C++ [dcl.fct.default]p5 |
| 121 | // A default argument expression is implicitly converted (clause |
| 122 | // 4) to the parameter type. The default argument expression has |
| 123 | // the same semantic constraints as the initializer expression in |
| 124 | // a declaration of a variable of the parameter type, using the |
| 125 | // copy-initialization semantics (8.5). |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 126 | Expr *DefaultArgPtr = DefaultArg.get(); |
Douglas Gregor | eb704f2 | 2008-11-04 13:57:51 +0000 | [diff] [blame] | 127 | bool DefaultInitFailed = PerformCopyInitialization(DefaultArgPtr, ParamType, |
| 128 | "in default argument"); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 129 | if (DefaultArgPtr != DefaultArg.get()) { |
| 130 | DefaultArg.take(); |
| 131 | DefaultArg.reset(DefaultArgPtr); |
| 132 | } |
Douglas Gregor | eb704f2 | 2008-11-04 13:57:51 +0000 | [diff] [blame] | 133 | if (DefaultInitFailed) { |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 134 | return; |
| 135 | } |
| 136 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 137 | // Check that the default argument is well-formed |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 138 | CheckDefaultArgumentVisitor DefaultArgChecker(DefaultArg.get(), this); |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 139 | if (DefaultArgChecker.Visit(DefaultArg.get())) |
| 140 | return; |
| 141 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 142 | // Okay: add the default argument to the parameter |
| 143 | Param->setDefaultArg(DefaultArg.take()); |
| 144 | } |
| 145 | |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 146 | /// CheckExtraCXXDefaultArguments - Check for any extra default |
| 147 | /// arguments in the declarator, which is not a function declaration |
| 148 | /// or definition and therefore is not permitted to have default |
| 149 | /// arguments. This routine should be invoked for every declarator |
| 150 | /// that is not a function declaration or definition. |
| 151 | void Sema::CheckExtraCXXDefaultArguments(Declarator &D) { |
| 152 | // C++ [dcl.fct.default]p3 |
| 153 | // A default argument expression shall be specified only in the |
| 154 | // parameter-declaration-clause of a function declaration or in a |
| 155 | // template-parameter (14.1). It shall not be specified for a |
| 156 | // parameter pack. If it is specified in a |
| 157 | // parameter-declaration-clause, it shall not occur within a |
| 158 | // declarator or abstract-declarator of a parameter-declaration. |
| 159 | for (unsigned i = 0; i < D.getNumTypeObjects(); ++i) { |
| 160 | DeclaratorChunk &chunk = D.getTypeObject(i); |
| 161 | if (chunk.Kind == DeclaratorChunk::Function) { |
| 162 | for (unsigned argIdx = 0; argIdx < chunk.Fun.NumArgs; ++argIdx) { |
| 163 | ParmVarDecl *Param = (ParmVarDecl *)chunk.Fun.ArgInfo[argIdx].Param; |
| 164 | if (Param->getDefaultArg()) { |
| 165 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc, |
| 166 | Param->getDefaultArg()->getSourceRange()); |
| 167 | Param->setDefaultArg(0); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 174 | // MergeCXXFunctionDecl - Merge two declarations of the same C++ |
| 175 | // function, once we already know that they have the same |
| 176 | // type. Subroutine of MergeFunctionDecl. |
| 177 | FunctionDecl * |
| 178 | Sema::MergeCXXFunctionDecl(FunctionDecl *New, FunctionDecl *Old) { |
| 179 | // C++ [dcl.fct.default]p4: |
| 180 | // |
| 181 | // For non-template functions, default arguments can be added in |
| 182 | // later declarations of a function in the same |
| 183 | // scope. Declarations in different scopes have completely |
| 184 | // distinct sets of default arguments. That is, declarations in |
| 185 | // inner scopes do not acquire default arguments from |
| 186 | // declarations in outer scopes, and vice versa. In a given |
| 187 | // function declaration, all parameters subsequent to a |
| 188 | // parameter with a default argument shall have default |
| 189 | // arguments supplied in this or previous declarations. A |
| 190 | // default argument shall not be redefined by a later |
| 191 | // declaration (not even to the same value). |
| 192 | for (unsigned p = 0, NumParams = Old->getNumParams(); p < NumParams; ++p) { |
| 193 | ParmVarDecl *OldParam = Old->getParamDecl(p); |
| 194 | ParmVarDecl *NewParam = New->getParamDecl(p); |
| 195 | |
| 196 | if(OldParam->getDefaultArg() && NewParam->getDefaultArg()) { |
| 197 | Diag(NewParam->getLocation(), |
| 198 | diag::err_param_default_argument_redefinition, |
| 199 | NewParam->getDefaultArg()->getSourceRange()); |
| 200 | Diag(OldParam->getLocation(), diag::err_previous_definition); |
| 201 | } else if (OldParam->getDefaultArg()) { |
| 202 | // Merge the old default argument into the new parameter |
| 203 | NewParam->setDefaultArg(OldParam->getDefaultArg()); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | return New; |
| 208 | } |
| 209 | |
| 210 | /// CheckCXXDefaultArguments - Verify that the default arguments for a |
| 211 | /// function declaration are well-formed according to C++ |
| 212 | /// [dcl.fct.default]. |
| 213 | void Sema::CheckCXXDefaultArguments(FunctionDecl *FD) { |
| 214 | unsigned NumParams = FD->getNumParams(); |
| 215 | unsigned p; |
| 216 | |
| 217 | // Find first parameter with a default argument |
| 218 | for (p = 0; p < NumParams; ++p) { |
| 219 | ParmVarDecl *Param = FD->getParamDecl(p); |
| 220 | if (Param->getDefaultArg()) |
| 221 | break; |
| 222 | } |
| 223 | |
| 224 | // C++ [dcl.fct.default]p4: |
| 225 | // In a given function declaration, all parameters |
| 226 | // subsequent to a parameter with a default argument shall |
| 227 | // have default arguments supplied in this or previous |
| 228 | // declarations. A default argument shall not be redefined |
| 229 | // by a later declaration (not even to the same value). |
| 230 | unsigned LastMissingDefaultArg = 0; |
| 231 | for(; p < NumParams; ++p) { |
| 232 | ParmVarDecl *Param = FD->getParamDecl(p); |
| 233 | if (!Param->getDefaultArg()) { |
| 234 | if (Param->getIdentifier()) |
| 235 | Diag(Param->getLocation(), |
| 236 | diag::err_param_default_argument_missing_name, |
| 237 | Param->getIdentifier()->getName()); |
| 238 | else |
| 239 | Diag(Param->getLocation(), |
| 240 | diag::err_param_default_argument_missing); |
| 241 | |
| 242 | LastMissingDefaultArg = p; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | if (LastMissingDefaultArg > 0) { |
| 247 | // Some default arguments were missing. Clear out all of the |
| 248 | // default arguments up to (and including) the last missing |
| 249 | // default argument, so that we leave the function parameters |
| 250 | // in a semantically valid state. |
| 251 | for (p = 0; p <= LastMissingDefaultArg; ++p) { |
| 252 | ParmVarDecl *Param = FD->getParamDecl(p); |
| 253 | if (Param->getDefaultArg()) { |
| 254 | delete Param->getDefaultArg(); |
| 255 | Param->setDefaultArg(0); |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 260 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 261 | /// isCurrentClassName - Determine whether the identifier II is the |
| 262 | /// name of the class type currently being defined. In the case of |
| 263 | /// nested classes, this will only return true if II is the name of |
| 264 | /// the innermost class. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 265 | bool Sema::isCurrentClassName(const IdentifierInfo &II, Scope *, |
| 266 | const CXXScopeSpec *SS) { |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 267 | CXXRecordDecl *CurDecl; |
| 268 | if (SS) { |
| 269 | DeclContext *DC = static_cast<DeclContext*>(SS->getScopeRep()); |
| 270 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(DC); |
| 271 | } else |
| 272 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(CurContext); |
| 273 | |
| 274 | if (CurDecl) |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 275 | return &II == CurDecl->getIdentifier(); |
| 276 | else |
| 277 | return false; |
| 278 | } |
| 279 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 280 | /// ActOnBaseSpecifier - Parsed a base specifier. A base specifier is |
| 281 | /// one entry in the base class list of a class specifier, for |
| 282 | /// example: |
| 283 | /// class foo : public bar, virtual private baz { |
| 284 | /// 'public bar' and 'virtual private baz' are each base-specifiers. |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 285 | Sema::BaseResult |
| 286 | Sema::ActOnBaseSpecifier(DeclTy *classdecl, SourceRange SpecifierRange, |
| 287 | bool Virtual, AccessSpecifier Access, |
| 288 | TypeTy *basetype, SourceLocation BaseLoc) { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 289 | RecordDecl *Decl = (RecordDecl*)classdecl; |
| 290 | QualType BaseType = Context.getTypeDeclType((TypeDecl*)basetype); |
| 291 | |
| 292 | // Base specifiers must be record types. |
| 293 | if (!BaseType->isRecordType()) { |
| 294 | Diag(BaseLoc, diag::err_base_must_be_class, SpecifierRange); |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 295 | return true; |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | // C++ [class.union]p1: |
| 299 | // A union shall not be used as a base class. |
| 300 | if (BaseType->isUnionType()) { |
| 301 | Diag(BaseLoc, diag::err_union_as_base_class, SpecifierRange); |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 302 | return true; |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | // C++ [class.union]p1: |
| 306 | // A union shall not have base classes. |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 307 | if (Decl->isUnion()) { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 308 | Diag(Decl->getLocation(), diag::err_base_clause_on_union, |
| 309 | SpecifierRange); |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 310 | return true; |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | // C++ [class.derived]p2: |
| 314 | // The class-name in a base-specifier shall not be an incompletely |
| 315 | // defined class. |
| 316 | if (BaseType->isIncompleteType()) { |
| 317 | Diag(BaseLoc, diag::err_incomplete_base_class, SpecifierRange); |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 318 | return true; |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Sebastian Redl | d93f0dd | 2008-11-06 15:59:35 +0000 | [diff] [blame] | 321 | // If the base class is polymorphic, the new one is, too. |
| 322 | RecordDecl *BaseDecl = BaseType->getAsRecordType()->getDecl(); |
| 323 | assert(BaseDecl && "Record type has no declaration"); |
| 324 | BaseDecl = BaseDecl->getDefinition(Context); |
| 325 | assert(BaseDecl && "Base type is not incomplete, but has no definition"); |
| 326 | if (cast<CXXRecordDecl>(BaseDecl)->isPolymorphic()) { |
| 327 | cast<CXXRecordDecl>(Decl)->setPolymorphic(true); |
| 328 | } |
| 329 | |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 330 | // Create the base specifier. |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 331 | return new CXXBaseSpecifier(SpecifierRange, Virtual, |
| 332 | BaseType->isClassType(), Access, BaseType); |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 333 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 334 | |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 335 | /// ActOnBaseSpecifiers - Attach the given base specifiers to the |
| 336 | /// class, after checking whether there are any duplicate base |
| 337 | /// classes. |
| 338 | void Sema::ActOnBaseSpecifiers(DeclTy *ClassDecl, BaseTy **Bases, |
| 339 | unsigned NumBases) { |
| 340 | if (NumBases == 0) |
| 341 | return; |
| 342 | |
| 343 | // Used to keep track of which base types we have already seen, so |
| 344 | // that we can properly diagnose redundant direct base types. Note |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 345 | // that the key is always the unqualified canonical type of the base |
| 346 | // class. |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 347 | std::map<QualType, CXXBaseSpecifier*, QualTypeOrdering> KnownBaseTypes; |
| 348 | |
| 349 | // Copy non-redundant base specifiers into permanent storage. |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 350 | CXXBaseSpecifier **BaseSpecs = (CXXBaseSpecifier **)Bases; |
| 351 | unsigned NumGoodBases = 0; |
| 352 | for (unsigned idx = 0; idx < NumBases; ++idx) { |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 353 | QualType NewBaseType |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 354 | = Context.getCanonicalType(BaseSpecs[idx]->getType()); |
| 355 | NewBaseType = NewBaseType.getUnqualifiedType(); |
| 356 | |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 357 | if (KnownBaseTypes[NewBaseType]) { |
| 358 | // C++ [class.mi]p3: |
| 359 | // A class shall not be specified as a direct base class of a |
| 360 | // derived class more than once. |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 361 | Diag(BaseSpecs[idx]->getSourceRange().getBegin(), |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 362 | diag::err_duplicate_base_class, |
| 363 | KnownBaseTypes[NewBaseType]->getType().getAsString(), |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 364 | BaseSpecs[idx]->getSourceRange()); |
| 365 | |
| 366 | // Delete the duplicate base class specifier; we're going to |
| 367 | // overwrite its pointer later. |
| 368 | delete BaseSpecs[idx]; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 369 | } else { |
| 370 | // Okay, add this new base class. |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 371 | KnownBaseTypes[NewBaseType] = BaseSpecs[idx]; |
| 372 | BaseSpecs[NumGoodBases++] = BaseSpecs[idx]; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | |
| 376 | // Attach the remaining base class specifiers to the derived class. |
| 377 | CXXRecordDecl *Decl = (CXXRecordDecl*)ClassDecl; |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 378 | Decl->setBases(BaseSpecs, NumGoodBases); |
| 379 | |
| 380 | // Delete the remaining (good) base class specifiers, since their |
| 381 | // data has been copied into the CXXRecordDecl. |
| 382 | for (unsigned idx = 0; idx < NumGoodBases; ++idx) |
| 383 | delete BaseSpecs[idx]; |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 384 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 385 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 386 | //===----------------------------------------------------------------------===// |
| 387 | // C++ class member Handling |
| 388 | //===----------------------------------------------------------------------===// |
| 389 | |
| 390 | /// ActOnStartCXXClassDef - This is called at the start of a class/struct/union |
| 391 | /// definition, when on C++. |
| 392 | void Sema::ActOnStartCXXClassDef(Scope *S, DeclTy *D, SourceLocation LBrace) { |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 393 | CXXRecordDecl *Dcl = cast<CXXRecordDecl>(static_cast<Decl *>(D)); |
| 394 | PushDeclContext(Dcl); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 395 | FieldCollector->StartClass(); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 396 | |
| 397 | if (Dcl->getIdentifier()) { |
| 398 | // C++ [class]p2: |
| 399 | // [...] The class-name is also inserted into the scope of the |
| 400 | // class itself; this is known as the injected-class-name. For |
| 401 | // purposes of access checking, the injected-class-name is treated |
| 402 | // as if it were a public member name. |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 403 | // FIXME: this should probably have its own kind of type node. |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 404 | TypedefDecl *InjectedClassName |
| 405 | = TypedefDecl::Create(Context, Dcl, LBrace, Dcl->getIdentifier(), |
| 406 | Context.getTypeDeclType(Dcl), /*PrevDecl=*/0); |
| 407 | PushOnScopeChains(InjectedClassName, S); |
| 408 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | /// ActOnCXXMemberDeclarator - This is invoked when a C++ class member |
| 412 | /// declarator is parsed. 'AS' is the access specifier, 'BW' specifies the |
| 413 | /// bitfield width if there is one and 'InitExpr' specifies the initializer if |
| 414 | /// any. 'LastInGroup' is non-null for cases where one declspec has multiple |
| 415 | /// declarators on it. |
| 416 | /// |
| 417 | /// NOTE: Because of CXXFieldDecl's inability to be chained like ScopedDecls, if |
| 418 | /// an instance field is declared, a new CXXFieldDecl is created but the method |
| 419 | /// does *not* return it; it returns LastInGroup instead. The other C++ members |
| 420 | /// (which are all ScopedDecls) are returned after appending them to |
| 421 | /// LastInGroup. |
| 422 | Sema::DeclTy * |
| 423 | Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D, |
| 424 | ExprTy *BW, ExprTy *InitExpr, |
| 425 | DeclTy *LastInGroup) { |
| 426 | const DeclSpec &DS = D.getDeclSpec(); |
| 427 | IdentifierInfo *II = D.getIdentifier(); |
| 428 | Expr *BitWidth = static_cast<Expr*>(BW); |
| 429 | Expr *Init = static_cast<Expr*>(InitExpr); |
| 430 | SourceLocation Loc = D.getIdentifierLoc(); |
| 431 | |
| 432 | // C++ 9.2p6: A member shall not be declared to have automatic storage |
| 433 | // duration (auto, register) or with the extern storage-class-specifier. |
| 434 | switch (DS.getStorageClassSpec()) { |
| 435 | case DeclSpec::SCS_unspecified: |
| 436 | case DeclSpec::SCS_typedef: |
| 437 | case DeclSpec::SCS_static: |
| 438 | // FALL THROUGH. |
| 439 | break; |
| 440 | default: |
| 441 | if (DS.getStorageClassSpecLoc().isValid()) |
| 442 | Diag(DS.getStorageClassSpecLoc(), |
| 443 | diag::err_storageclass_invalid_for_member); |
| 444 | else |
| 445 | Diag(DS.getThreadSpecLoc(), diag::err_storageclass_invalid_for_member); |
| 446 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 447 | } |
| 448 | |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 449 | bool isFunc = D.isFunctionDeclarator(); |
Argyrios Kyrtzidis | d6caa9e | 2008-10-15 20:23:22 +0000 | [diff] [blame] | 450 | if (!isFunc && |
| 451 | D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_typedef && |
| 452 | D.getNumTypeObjects() == 0) { |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 453 | // Check also for this case: |
| 454 | // |
| 455 | // typedef int f(); |
| 456 | // f a; |
| 457 | // |
| 458 | Decl *TD = static_cast<Decl *>(DS.getTypeRep()); |
| 459 | isFunc = Context.getTypeDeclType(cast<TypeDecl>(TD))->isFunctionType(); |
| 460 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 461 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 462 | bool isInstField = (DS.getStorageClassSpec() == DeclSpec::SCS_unspecified && |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 463 | !isFunc); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 464 | |
| 465 | Decl *Member; |
| 466 | bool InvalidDecl = false; |
| 467 | |
| 468 | if (isInstField) |
| 469 | Member = static_cast<Decl*>(ActOnField(S, Loc, D, BitWidth)); |
| 470 | else |
Daniel Dunbar | 914701e | 2008-08-05 16:28:08 +0000 | [diff] [blame] | 471 | Member = static_cast<Decl*>(ActOnDeclarator(S, D, LastInGroup)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 472 | |
| 473 | if (!Member) return LastInGroup; |
| 474 | |
Sanjiv Gupta | 31fc07d | 2008-10-31 09:52:39 +0000 | [diff] [blame] | 475 | assert((II || isInstField) && "No identifier for non-field ?"); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 476 | |
| 477 | // set/getAccess is not part of Decl's interface to avoid bloating it with C++ |
| 478 | // specific methods. Use a wrapper class that can be used with all C++ class |
| 479 | // member decls. |
| 480 | CXXClassMemberWrapper(Member).setAccess(AS); |
| 481 | |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 482 | // C++ [dcl.init.aggr]p1: |
| 483 | // An aggregate is an array or a class (clause 9) with [...] no |
| 484 | // private or protected non-static data members (clause 11). |
| 485 | if (isInstField && (AS == AS_private || AS == AS_protected)) |
| 486 | cast<CXXRecordDecl>(CurContext)->setAggregate(false); |
| 487 | |
Sebastian Redl | d93f0dd | 2008-11-06 15:59:35 +0000 | [diff] [blame] | 488 | if (DS.isVirtualSpecified()) { |
| 489 | if (!isFunc || DS.getStorageClassSpec() == DeclSpec::SCS_static) { |
| 490 | Diag(DS.getVirtualSpecLoc(), diag::err_virtual_non_function); |
| 491 | InvalidDecl = true; |
| 492 | } else { |
| 493 | CXXRecordDecl *CurClass = cast<CXXRecordDecl>(CurContext); |
| 494 | CurClass->setAggregate(false); |
| 495 | CurClass->setPolymorphic(true); |
| 496 | } |
| 497 | } |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 498 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 499 | if (BitWidth) { |
| 500 | // C++ 9.6p2: Only when declaring an unnamed bit-field may the |
| 501 | // constant-expression be a value equal to zero. |
| 502 | // FIXME: Check this. |
| 503 | |
| 504 | if (D.isFunctionDeclarator()) { |
| 505 | // FIXME: Emit diagnostic about only constructors taking base initializers |
| 506 | // or something similar, when constructor support is in place. |
| 507 | Diag(Loc, diag::err_not_bitfield_type, |
| 508 | II->getName(), BitWidth->getSourceRange()); |
| 509 | InvalidDecl = true; |
| 510 | |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 511 | } else if (isInstField) { |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 512 | // C++ 9.6p3: A bit-field shall have integral or enumeration type. |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 513 | if (!cast<FieldDecl>(Member)->getType()->isIntegralType()) { |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 514 | Diag(Loc, diag::err_not_integral_type_bitfield, |
| 515 | II->getName(), BitWidth->getSourceRange()); |
| 516 | InvalidDecl = true; |
| 517 | } |
| 518 | |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 519 | } else if (isa<FunctionDecl>(Member)) { |
| 520 | // A function typedef ("typedef int f(); f a;"). |
| 521 | // C++ 9.6p3: A bit-field shall have integral or enumeration type. |
| 522 | Diag(Loc, diag::err_not_integral_type_bitfield, |
| 523 | II->getName(), BitWidth->getSourceRange()); |
| 524 | InvalidDecl = true; |
| 525 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 526 | } else if (isa<TypedefDecl>(Member)) { |
| 527 | // "cannot declare 'A' to be a bit-field type" |
| 528 | Diag(Loc, diag::err_not_bitfield_type, II->getName(), |
| 529 | BitWidth->getSourceRange()); |
| 530 | InvalidDecl = true; |
| 531 | |
| 532 | } else { |
| 533 | assert(isa<CXXClassVarDecl>(Member) && |
| 534 | "Didn't we cover all member kinds?"); |
| 535 | // C++ 9.6p3: A bit-field shall not be a static member. |
| 536 | // "static member 'A' cannot be a bit-field" |
| 537 | Diag(Loc, diag::err_static_not_bitfield, II->getName(), |
| 538 | BitWidth->getSourceRange()); |
| 539 | InvalidDecl = true; |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | if (Init) { |
| 544 | // C++ 9.2p4: A member-declarator can contain a constant-initializer only |
| 545 | // if it declares a static member of const integral or const enumeration |
| 546 | // type. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 547 | if (CXXClassVarDecl *CVD = dyn_cast<CXXClassVarDecl>(Member)) { |
| 548 | // ...static member of... |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 549 | CVD->setInit(Init); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 550 | // ...const integral or const enumeration type. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 551 | if (Context.getCanonicalType(CVD->getType()).isConstQualified() && |
| 552 | CVD->getType()->isIntegralType()) { |
| 553 | // constant-initializer |
| 554 | if (CheckForConstantInitializer(Init, CVD->getType())) |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 555 | InvalidDecl = true; |
| 556 | |
| 557 | } else { |
| 558 | // not const integral. |
| 559 | Diag(Loc, diag::err_member_initialization, |
| 560 | II->getName(), Init->getSourceRange()); |
| 561 | InvalidDecl = true; |
| 562 | } |
| 563 | |
| 564 | } else { |
| 565 | // not static member. |
| 566 | Diag(Loc, diag::err_member_initialization, |
| 567 | II->getName(), Init->getSourceRange()); |
| 568 | InvalidDecl = true; |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | if (InvalidDecl) |
| 573 | Member->setInvalidDecl(); |
| 574 | |
| 575 | if (isInstField) { |
| 576 | FieldCollector->Add(cast<CXXFieldDecl>(Member)); |
| 577 | return LastInGroup; |
| 578 | } |
| 579 | return Member; |
| 580 | } |
| 581 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 582 | /// ActOnMemInitializer - Handle a C++ member initializer. |
| 583 | Sema::MemInitResult |
| 584 | Sema::ActOnMemInitializer(DeclTy *ConstructorD, |
| 585 | Scope *S, |
| 586 | IdentifierInfo *MemberOrBase, |
| 587 | SourceLocation IdLoc, |
| 588 | SourceLocation LParenLoc, |
| 589 | ExprTy **Args, unsigned NumArgs, |
| 590 | SourceLocation *CommaLocs, |
| 591 | SourceLocation RParenLoc) { |
| 592 | CXXConstructorDecl *Constructor |
| 593 | = dyn_cast<CXXConstructorDecl>((Decl*)ConstructorD); |
| 594 | if (!Constructor) { |
| 595 | // The user wrote a constructor initializer on a function that is |
| 596 | // not a C++ constructor. Ignore the error for now, because we may |
| 597 | // have more member initializers coming; we'll diagnose it just |
| 598 | // once in ActOnMemInitializers. |
| 599 | return true; |
| 600 | } |
| 601 | |
| 602 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 603 | |
| 604 | // C++ [class.base.init]p2: |
| 605 | // Names in a mem-initializer-id are looked up in the scope of the |
| 606 | // constructor’s class and, if not found in that scope, are looked |
| 607 | // up in the scope containing the constructor’s |
| 608 | // definition. [Note: if the constructor’s class contains a member |
| 609 | // with the same name as a direct or virtual base class of the |
| 610 | // class, a mem-initializer-id naming the member or base class and |
| 611 | // composed of a single identifier refers to the class member. A |
| 612 | // mem-initializer-id for the hidden base class may be specified |
| 613 | // using a qualified name. ] |
| 614 | // Look for a member, first. |
| 615 | CXXFieldDecl *Member = ClassDecl->getMember(MemberOrBase); |
| 616 | |
| 617 | // FIXME: Handle members of an anonymous union. |
| 618 | |
| 619 | if (Member) { |
| 620 | // FIXME: Perform direct initialization of the member. |
| 621 | return new CXXBaseOrMemberInitializer(Member, (Expr **)Args, NumArgs); |
| 622 | } |
| 623 | |
| 624 | // It didn't name a member, so see if it names a class. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 625 | TypeTy *BaseTy = isTypeName(*MemberOrBase, S, 0/*SS*/); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 626 | if (!BaseTy) |
| 627 | return Diag(IdLoc, diag::err_mem_init_not_member_or_class, |
| 628 | MemberOrBase->getName(), SourceRange(IdLoc, RParenLoc)); |
| 629 | |
| 630 | QualType BaseType = Context.getTypeDeclType((TypeDecl *)BaseTy); |
| 631 | if (!BaseType->isRecordType()) |
| 632 | return Diag(IdLoc, diag::err_base_init_does_not_name_class, |
| 633 | BaseType.getAsString(), SourceRange(IdLoc, RParenLoc)); |
| 634 | |
| 635 | // C++ [class.base.init]p2: |
| 636 | // [...] Unless the mem-initializer-id names a nonstatic data |
| 637 | // member of the constructor’s class or a direct or virtual base |
| 638 | // of that class, the mem-initializer is ill-formed. A |
| 639 | // mem-initializer-list can initialize a base class using any |
| 640 | // name that denotes that base class type. |
| 641 | |
| 642 | // First, check for a direct base class. |
| 643 | const CXXBaseSpecifier *DirectBaseSpec = 0; |
| 644 | for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(); |
| 645 | Base != ClassDecl->bases_end(); ++Base) { |
| 646 | if (Context.getCanonicalType(BaseType).getUnqualifiedType() == |
| 647 | Context.getCanonicalType(Base->getType()).getUnqualifiedType()) { |
| 648 | // We found a direct base of this type. That's what we're |
| 649 | // initializing. |
| 650 | DirectBaseSpec = &*Base; |
| 651 | break; |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | // Check for a virtual base class. |
| 656 | // FIXME: We might be able to short-circuit this if we know in |
| 657 | // advance that there are no virtual bases. |
| 658 | const CXXBaseSpecifier *VirtualBaseSpec = 0; |
| 659 | if (!DirectBaseSpec || !DirectBaseSpec->isVirtual()) { |
| 660 | // We haven't found a base yet; search the class hierarchy for a |
| 661 | // virtual base class. |
| 662 | BasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 663 | /*DetectVirtual=*/false); |
| 664 | if (IsDerivedFrom(Context.getTypeDeclType(ClassDecl), BaseType, Paths)) { |
| 665 | for (BasePaths::paths_iterator Path = Paths.begin(); |
| 666 | Path != Paths.end(); ++Path) { |
| 667 | if (Path->back().Base->isVirtual()) { |
| 668 | VirtualBaseSpec = Path->back().Base; |
| 669 | break; |
| 670 | } |
| 671 | } |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | // C++ [base.class.init]p2: |
| 676 | // If a mem-initializer-id is ambiguous because it designates both |
| 677 | // a direct non-virtual base class and an inherited virtual base |
| 678 | // class, the mem-initializer is ill-formed. |
| 679 | if (DirectBaseSpec && VirtualBaseSpec) |
| 680 | return Diag(IdLoc, diag::err_base_init_direct_and_virtual, |
| 681 | MemberOrBase->getName(), SourceRange(IdLoc, RParenLoc)); |
| 682 | |
| 683 | return new CXXBaseOrMemberInitializer(BaseType, (Expr **)Args, NumArgs); |
| 684 | } |
| 685 | |
| 686 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 687 | void Sema::ActOnFinishCXXMemberSpecification(Scope* S, SourceLocation RLoc, |
| 688 | DeclTy *TagDecl, |
| 689 | SourceLocation LBrac, |
| 690 | SourceLocation RBrac) { |
| 691 | ActOnFields(S, RLoc, TagDecl, |
| 692 | (DeclTy**)FieldCollector->getCurFields(), |
Daniel Dunbar | 1bfe1c2 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 693 | FieldCollector->getCurNumFields(), LBrac, RBrac, 0); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 694 | } |
| 695 | |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 696 | /// AddImplicitlyDeclaredMembersToClass - Adds any implicitly-declared |
| 697 | /// special functions, such as the default constructor, copy |
| 698 | /// constructor, or destructor, to the given C++ class (C++ |
| 699 | /// [special]p1). This routine can only be executed just before the |
| 700 | /// definition of the class is complete. |
| 701 | void Sema::AddImplicitlyDeclaredMembersToClass(CXXRecordDecl *ClassDecl) { |
| 702 | if (!ClassDecl->hasUserDeclaredConstructor()) { |
| 703 | // C++ [class.ctor]p5: |
| 704 | // A default constructor for a class X is a constructor of class X |
| 705 | // that can be called without an argument. If there is no |
| 706 | // user-declared constructor for class X, a default constructor is |
| 707 | // implicitly declared. An implicitly-declared default constructor |
| 708 | // is an inline public member of its class. |
| 709 | CXXConstructorDecl *DefaultCon = |
| 710 | CXXConstructorDecl::Create(Context, ClassDecl, |
| 711 | ClassDecl->getLocation(), |
| 712 | ClassDecl->getIdentifier(), |
| 713 | Context.getFunctionType(Context.VoidTy, |
| 714 | 0, 0, false, 0), |
| 715 | /*isExplicit=*/false, |
| 716 | /*isInline=*/true, |
| 717 | /*isImplicitlyDeclared=*/true); |
| 718 | DefaultCon->setAccess(AS_public); |
| 719 | ClassDecl->addConstructor(Context, DefaultCon); |
| 720 | } |
| 721 | |
| 722 | if (!ClassDecl->hasUserDeclaredCopyConstructor()) { |
| 723 | // C++ [class.copy]p4: |
| 724 | // If the class definition does not explicitly declare a copy |
| 725 | // constructor, one is declared implicitly. |
| 726 | |
| 727 | // C++ [class.copy]p5: |
| 728 | // The implicitly-declared copy constructor for a class X will |
| 729 | // have the form |
| 730 | // |
| 731 | // X::X(const X&) |
| 732 | // |
| 733 | // if |
| 734 | bool HasConstCopyConstructor = true; |
| 735 | |
| 736 | // -- each direct or virtual base class B of X has a copy |
| 737 | // constructor whose first parameter is of type const B& or |
| 738 | // const volatile B&, and |
| 739 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(); |
| 740 | HasConstCopyConstructor && Base != ClassDecl->bases_end(); ++Base) { |
| 741 | const CXXRecordDecl *BaseClassDecl |
| 742 | = cast<CXXRecordDecl>(Base->getType()->getAsRecordType()->getDecl()); |
| 743 | HasConstCopyConstructor |
| 744 | = BaseClassDecl->hasConstCopyConstructor(Context); |
| 745 | } |
| 746 | |
| 747 | // -- for all the nonstatic data members of X that are of a |
| 748 | // class type M (or array thereof), each such class type |
| 749 | // has a copy constructor whose first parameter is of type |
| 750 | // const M& or const volatile M&. |
| 751 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(); |
| 752 | HasConstCopyConstructor && Field != ClassDecl->field_end(); ++Field) { |
| 753 | QualType FieldType = (*Field)->getType(); |
| 754 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 755 | FieldType = Array->getElementType(); |
| 756 | if (const RecordType *FieldClassType = FieldType->getAsRecordType()) { |
| 757 | const CXXRecordDecl *FieldClassDecl |
| 758 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
| 759 | HasConstCopyConstructor |
| 760 | = FieldClassDecl->hasConstCopyConstructor(Context); |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | // Otherwise, the implicitly declared copy constructor will have |
| 765 | // the form |
| 766 | // |
| 767 | // X::X(X&) |
| 768 | QualType ArgType = Context.getTypeDeclType(ClassDecl); |
| 769 | if (HasConstCopyConstructor) |
| 770 | ArgType = ArgType.withConst(); |
| 771 | ArgType = Context.getReferenceType(ArgType); |
| 772 | |
| 773 | // An implicitly-declared copy constructor is an inline public |
| 774 | // member of its class. |
| 775 | CXXConstructorDecl *CopyConstructor |
| 776 | = CXXConstructorDecl::Create(Context, ClassDecl, |
| 777 | ClassDecl->getLocation(), |
| 778 | ClassDecl->getIdentifier(), |
| 779 | Context.getFunctionType(Context.VoidTy, |
| 780 | &ArgType, 1, |
| 781 | false, 0), |
| 782 | /*isExplicit=*/false, |
| 783 | /*isInline=*/true, |
| 784 | /*isImplicitlyDeclared=*/true); |
| 785 | CopyConstructor->setAccess(AS_public); |
| 786 | |
| 787 | // Add the parameter to the constructor. |
| 788 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyConstructor, |
| 789 | ClassDecl->getLocation(), |
| 790 | /*IdentifierInfo=*/0, |
| 791 | ArgType, VarDecl::None, 0, 0); |
| 792 | CopyConstructor->setParams(&FromParam, 1); |
| 793 | |
| 794 | ClassDecl->addConstructor(Context, CopyConstructor); |
| 795 | } |
| 796 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 797 | if (!ClassDecl->getDestructor()) { |
| 798 | // C++ [class.dtor]p2: |
| 799 | // If a class has no user-declared destructor, a destructor is |
| 800 | // declared implicitly. An implicitly-declared destructor is an |
| 801 | // inline public member of its class. |
| 802 | std::string DestructorName = "~"; |
| 803 | DestructorName += ClassDecl->getName(); |
| 804 | CXXDestructorDecl *Destructor |
| 805 | = CXXDestructorDecl::Create(Context, ClassDecl, |
| 806 | ClassDecl->getLocation(), |
| 807 | &PP.getIdentifierTable().get(DestructorName), |
| 808 | Context.getFunctionType(Context.VoidTy, |
| 809 | 0, 0, false, 0), |
| 810 | /*isInline=*/true, |
| 811 | /*isImplicitlyDeclared=*/true); |
| 812 | Destructor->setAccess(AS_public); |
| 813 | ClassDecl->setDestructor(Destructor); |
| 814 | } |
| 815 | |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 816 | // FIXME: Implicit copy assignment operator |
| 817 | } |
| 818 | |
Argyrios Kyrtzidis | 5b7f0c8 | 2008-08-09 00:39:29 +0000 | [diff] [blame] | 819 | void Sema::ActOnFinishCXXClassDef(DeclTy *D) { |
Argyrios Kyrtzidis | a4755c6 | 2008-08-09 00:58:37 +0000 | [diff] [blame] | 820 | CXXRecordDecl *Rec = cast<CXXRecordDecl>(static_cast<Decl *>(D)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 821 | FieldCollector->FinishClass(); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 822 | AddImplicitlyDeclaredMembersToClass(Rec); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 823 | PopDeclContext(); |
Argyrios Kyrtzidis | a4755c6 | 2008-08-09 00:58:37 +0000 | [diff] [blame] | 824 | |
| 825 | // Everything, including inline method definitions, have been parsed. |
| 826 | // Let the consumer know of the new TagDecl definition. |
| 827 | Consumer.HandleTagDeclDefinition(Rec); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 828 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 829 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 830 | /// CheckConstructorDeclarator - Called by ActOnDeclarator to check |
| 831 | /// the well-formednes of the constructor declarator @p D with type @p |
| 832 | /// R. If there are any errors in the declarator, this routine will |
| 833 | /// emit diagnostics and return true. Otherwise, it will return |
| 834 | /// false. Either way, the type @p R will be updated to reflect a |
| 835 | /// well-formed type for the constructor. |
| 836 | bool Sema::CheckConstructorDeclarator(Declarator &D, QualType &R, |
| 837 | FunctionDecl::StorageClass& SC) { |
| 838 | bool isVirtual = D.getDeclSpec().isVirtualSpecified(); |
| 839 | bool isInvalid = false; |
| 840 | |
| 841 | // C++ [class.ctor]p3: |
| 842 | // A constructor shall not be virtual (10.3) or static (9.4). A |
| 843 | // constructor can be invoked for a const, volatile or const |
| 844 | // volatile object. A constructor shall not be declared const, |
| 845 | // volatile, or const volatile (9.3.2). |
| 846 | if (isVirtual) { |
| 847 | Diag(D.getIdentifierLoc(), |
| 848 | diag::err_constructor_cannot_be, |
| 849 | "virtual", |
| 850 | SourceRange(D.getDeclSpec().getVirtualSpecLoc()), |
| 851 | SourceRange(D.getIdentifierLoc())); |
| 852 | isInvalid = true; |
| 853 | } |
| 854 | if (SC == FunctionDecl::Static) { |
| 855 | Diag(D.getIdentifierLoc(), |
| 856 | diag::err_constructor_cannot_be, |
| 857 | "static", |
| 858 | SourceRange(D.getDeclSpec().getStorageClassSpecLoc()), |
| 859 | SourceRange(D.getIdentifierLoc())); |
| 860 | isInvalid = true; |
| 861 | SC = FunctionDecl::None; |
| 862 | } |
| 863 | if (D.getDeclSpec().hasTypeSpecifier()) { |
| 864 | // Constructors don't have return types, but the parser will |
| 865 | // happily parse something like: |
| 866 | // |
| 867 | // class X { |
| 868 | // float X(float); |
| 869 | // }; |
| 870 | // |
| 871 | // The return type will be eliminated later. |
| 872 | Diag(D.getIdentifierLoc(), |
| 873 | diag::err_constructor_return_type, |
| 874 | SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()), |
| 875 | SourceRange(D.getIdentifierLoc())); |
| 876 | } |
| 877 | if (R->getAsFunctionTypeProto()->getTypeQuals() != 0) { |
| 878 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 879 | if (FTI.TypeQuals & QualType::Const) |
| 880 | Diag(D.getIdentifierLoc(), |
| 881 | diag::err_invalid_qualified_constructor, |
| 882 | "const", |
| 883 | SourceRange(D.getIdentifierLoc())); |
| 884 | if (FTI.TypeQuals & QualType::Volatile) |
| 885 | Diag(D.getIdentifierLoc(), |
| 886 | diag::err_invalid_qualified_constructor, |
| 887 | "volatile", |
| 888 | SourceRange(D.getIdentifierLoc())); |
| 889 | if (FTI.TypeQuals & QualType::Restrict) |
| 890 | Diag(D.getIdentifierLoc(), |
| 891 | diag::err_invalid_qualified_constructor, |
| 892 | "restrict", |
| 893 | SourceRange(D.getIdentifierLoc())); |
| 894 | } |
| 895 | |
| 896 | // Rebuild the function type "R" without any type qualifiers (in |
| 897 | // case any of the errors above fired) and with "void" as the |
| 898 | // return type, since constructors don't have return types. We |
| 899 | // *always* have to do this, because GetTypeForDeclarator will |
| 900 | // put in a result type of "int" when none was specified. |
| 901 | const FunctionTypeProto *Proto = R->getAsFunctionTypeProto(); |
| 902 | R = Context.getFunctionType(Context.VoidTy, Proto->arg_type_begin(), |
| 903 | Proto->getNumArgs(), |
| 904 | Proto->isVariadic(), |
| 905 | 0); |
| 906 | |
| 907 | return isInvalid; |
| 908 | } |
| 909 | |
| 910 | /// CheckDestructorDeclarator - Called by ActOnDeclarator to check |
| 911 | /// the well-formednes of the destructor declarator @p D with type @p |
| 912 | /// R. If there are any errors in the declarator, this routine will |
| 913 | /// emit diagnostics and return true. Otherwise, it will return |
| 914 | /// false. Either way, the type @p R will be updated to reflect a |
| 915 | /// well-formed type for the destructor. |
| 916 | bool Sema::CheckDestructorDeclarator(Declarator &D, QualType &R, |
| 917 | FunctionDecl::StorageClass& SC) { |
| 918 | bool isInvalid = false; |
| 919 | |
| 920 | // C++ [class.dtor]p1: |
| 921 | // [...] A typedef-name that names a class is a class-name |
| 922 | // (7.1.3); however, a typedef-name that names a class shall not |
| 923 | // be used as the identifier in the declarator for a destructor |
| 924 | // declaration. |
| 925 | TypeDecl *DeclaratorTypeD = (TypeDecl *)D.getDeclaratorIdType(); |
| 926 | if (const TypedefDecl *TypedefD = dyn_cast<TypedefDecl>(DeclaratorTypeD)) { |
| 927 | if (TypedefD->getIdentifier() != |
| 928 | cast<CXXRecordDecl>(CurContext)->getIdentifier()) { |
| 929 | // FIXME: This would be easier if we could just look at whether |
| 930 | // we found the injected-class-name. |
| 931 | Diag(D.getIdentifierLoc(), |
| 932 | diag::err_destructor_typedef_name, |
| 933 | TypedefD->getName()); |
| 934 | isInvalid = true; |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | // C++ [class.dtor]p2: |
| 939 | // A destructor is used to destroy objects of its class type. A |
| 940 | // destructor takes no parameters, and no return type can be |
| 941 | // specified for it (not even void). The address of a destructor |
| 942 | // shall not be taken. A destructor shall not be static. A |
| 943 | // destructor can be invoked for a const, volatile or const |
| 944 | // volatile object. A destructor shall not be declared const, |
| 945 | // volatile or const volatile (9.3.2). |
| 946 | if (SC == FunctionDecl::Static) { |
| 947 | Diag(D.getIdentifierLoc(), |
| 948 | diag::err_destructor_cannot_be, |
| 949 | "static", |
| 950 | SourceRange(D.getDeclSpec().getStorageClassSpecLoc()), |
| 951 | SourceRange(D.getIdentifierLoc())); |
| 952 | isInvalid = true; |
| 953 | SC = FunctionDecl::None; |
| 954 | } |
| 955 | if (D.getDeclSpec().hasTypeSpecifier()) { |
| 956 | // Destructors don't have return types, but the parser will |
| 957 | // happily parse something like: |
| 958 | // |
| 959 | // class X { |
| 960 | // float ~X(); |
| 961 | // }; |
| 962 | // |
| 963 | // The return type will be eliminated later. |
| 964 | Diag(D.getIdentifierLoc(), |
| 965 | diag::err_destructor_return_type, |
| 966 | SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()), |
| 967 | SourceRange(D.getIdentifierLoc())); |
| 968 | } |
| 969 | if (R->getAsFunctionTypeProto()->getTypeQuals() != 0) { |
| 970 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 971 | if (FTI.TypeQuals & QualType::Const) |
| 972 | Diag(D.getIdentifierLoc(), |
| 973 | diag::err_invalid_qualified_destructor, |
| 974 | "const", |
| 975 | SourceRange(D.getIdentifierLoc())); |
| 976 | if (FTI.TypeQuals & QualType::Volatile) |
| 977 | Diag(D.getIdentifierLoc(), |
| 978 | diag::err_invalid_qualified_destructor, |
| 979 | "volatile", |
| 980 | SourceRange(D.getIdentifierLoc())); |
| 981 | if (FTI.TypeQuals & QualType::Restrict) |
| 982 | Diag(D.getIdentifierLoc(), |
| 983 | diag::err_invalid_qualified_destructor, |
| 984 | "restrict", |
| 985 | SourceRange(D.getIdentifierLoc())); |
| 986 | } |
| 987 | |
| 988 | // Make sure we don't have any parameters. |
| 989 | if (R->getAsFunctionTypeProto()->getNumArgs() > 0) { |
| 990 | Diag(D.getIdentifierLoc(), diag::err_destructor_with_params); |
| 991 | |
| 992 | // Delete the parameters. |
| 993 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 994 | if (FTI.NumArgs) { |
| 995 | delete [] FTI.ArgInfo; |
| 996 | FTI.NumArgs = 0; |
| 997 | FTI.ArgInfo = 0; |
| 998 | } |
| 999 | } |
| 1000 | |
| 1001 | // Make sure the destructor isn't variadic. |
| 1002 | if (R->getAsFunctionTypeProto()->isVariadic()) |
| 1003 | Diag(D.getIdentifierLoc(), diag::err_destructor_variadic); |
| 1004 | |
| 1005 | // Rebuild the function type "R" without any type qualifiers or |
| 1006 | // parameters (in case any of the errors above fired) and with |
| 1007 | // "void" as the return type, since destructors don't have return |
| 1008 | // types. We *always* have to do this, because GetTypeForDeclarator |
| 1009 | // will put in a result type of "int" when none was specified. |
| 1010 | R = Context.getFunctionType(Context.VoidTy, 0, 0, false, 0); |
| 1011 | |
| 1012 | return isInvalid; |
| 1013 | } |
| 1014 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1015 | /// CheckConversionDeclarator - Called by ActOnDeclarator to check the |
| 1016 | /// well-formednes of the conversion function declarator @p D with |
| 1017 | /// type @p R. If there are any errors in the declarator, this routine |
| 1018 | /// will emit diagnostics and return true. Otherwise, it will return |
| 1019 | /// false. Either way, the type @p R will be updated to reflect a |
| 1020 | /// well-formed type for the conversion operator. |
| 1021 | bool Sema::CheckConversionDeclarator(Declarator &D, QualType &R, |
| 1022 | FunctionDecl::StorageClass& SC) { |
| 1023 | bool isInvalid = false; |
| 1024 | |
| 1025 | // C++ [class.conv.fct]p1: |
| 1026 | // Neither parameter types nor return type can be specified. The |
| 1027 | // type of a conversion function (8.3.5) is “function taking no |
| 1028 | // parameter returning conversion-type-id.” |
| 1029 | if (SC == FunctionDecl::Static) { |
| 1030 | Diag(D.getIdentifierLoc(), |
| 1031 | diag::err_conv_function_not_member, |
| 1032 | "static", |
| 1033 | SourceRange(D.getDeclSpec().getStorageClassSpecLoc()), |
| 1034 | SourceRange(D.getIdentifierLoc())); |
| 1035 | isInvalid = true; |
| 1036 | SC = FunctionDecl::None; |
| 1037 | } |
| 1038 | if (D.getDeclSpec().hasTypeSpecifier()) { |
| 1039 | // Conversion functions don't have return types, but the parser will |
| 1040 | // happily parse something like: |
| 1041 | // |
| 1042 | // class X { |
| 1043 | // float operator bool(); |
| 1044 | // }; |
| 1045 | // |
| 1046 | // The return type will be changed later anyway. |
| 1047 | Diag(D.getIdentifierLoc(), |
| 1048 | diag::err_conv_function_return_type, |
| 1049 | SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()), |
| 1050 | SourceRange(D.getIdentifierLoc())); |
| 1051 | } |
| 1052 | |
| 1053 | // Make sure we don't have any parameters. |
| 1054 | if (R->getAsFunctionTypeProto()->getNumArgs() > 0) { |
| 1055 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_params); |
| 1056 | |
| 1057 | // Delete the parameters. |
| 1058 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 1059 | if (FTI.NumArgs) { |
| 1060 | delete [] FTI.ArgInfo; |
| 1061 | FTI.NumArgs = 0; |
| 1062 | FTI.ArgInfo = 0; |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | // Make sure the conversion function isn't variadic. |
| 1067 | if (R->getAsFunctionTypeProto()->isVariadic()) |
| 1068 | Diag(D.getIdentifierLoc(), diag::err_conv_function_variadic); |
| 1069 | |
| 1070 | // C++ [class.conv.fct]p4: |
| 1071 | // The conversion-type-id shall not represent a function type nor |
| 1072 | // an array type. |
| 1073 | QualType ConvType = QualType::getFromOpaquePtr(D.getDeclaratorIdType()); |
| 1074 | if (ConvType->isArrayType()) { |
| 1075 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_array); |
| 1076 | ConvType = Context.getPointerType(ConvType); |
| 1077 | } else if (ConvType->isFunctionType()) { |
| 1078 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_function); |
| 1079 | ConvType = Context.getPointerType(ConvType); |
| 1080 | } |
| 1081 | |
| 1082 | // Rebuild the function type "R" without any parameters (in case any |
| 1083 | // of the errors above fired) and with the conversion type as the |
| 1084 | // return type. |
| 1085 | R = Context.getFunctionType(ConvType, 0, 0, false, |
| 1086 | R->getAsFunctionTypeProto()->getTypeQuals()); |
| 1087 | |
| 1088 | return isInvalid; |
| 1089 | } |
| 1090 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1091 | /// ActOnConstructorDeclarator - Called by ActOnDeclarator to complete |
| 1092 | /// the declaration of the given C++ constructor ConDecl that was |
| 1093 | /// built from declarator D. This routine is responsible for checking |
| 1094 | /// that the newly-created constructor declaration is well-formed and |
| 1095 | /// for recording it in the C++ class. Example: |
| 1096 | /// |
| 1097 | /// @code |
| 1098 | /// class X { |
| 1099 | /// X(); // X::X() will be the ConDecl. |
| 1100 | /// }; |
| 1101 | /// @endcode |
| 1102 | Sema::DeclTy *Sema::ActOnConstructorDeclarator(CXXConstructorDecl *ConDecl) { |
| 1103 | assert(ConDecl && "Expected to receive a constructor declaration"); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1104 | |
| 1105 | // Check default arguments on the constructor |
| 1106 | CheckCXXDefaultArguments(ConDecl); |
| 1107 | |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 1108 | CXXRecordDecl *ClassDecl = dyn_cast_or_null<CXXRecordDecl>(CurContext); |
| 1109 | if (!ClassDecl) { |
| 1110 | ConDecl->setInvalidDecl(); |
| 1111 | return ConDecl; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 1114 | // Make sure this constructor is an overload of the existing |
| 1115 | // constructors. |
| 1116 | OverloadedFunctionDecl::function_iterator MatchedDecl; |
| 1117 | if (!IsOverload(ConDecl, ClassDecl->getConstructors(), MatchedDecl)) { |
| 1118 | Diag(ConDecl->getLocation(), |
| 1119 | diag::err_constructor_redeclared, |
| 1120 | SourceRange(ConDecl->getLocation())); |
| 1121 | Diag((*MatchedDecl)->getLocation(), |
| 1122 | diag::err_previous_declaration, |
| 1123 | SourceRange((*MatchedDecl)->getLocation())); |
| 1124 | ConDecl->setInvalidDecl(); |
| 1125 | return ConDecl; |
| 1126 | } |
| 1127 | |
| 1128 | |
| 1129 | // C++ [class.copy]p3: |
| 1130 | // A declaration of a constructor for a class X is ill-formed if |
| 1131 | // its first parameter is of type (optionally cv-qualified) X and |
| 1132 | // either there are no other parameters or else all other |
| 1133 | // parameters have default arguments. |
| 1134 | if ((ConDecl->getNumParams() == 1) || |
| 1135 | (ConDecl->getNumParams() > 1 && |
| 1136 | ConDecl->getParamDecl(1)->getDefaultArg() != 0)) { |
| 1137 | QualType ParamType = ConDecl->getParamDecl(0)->getType(); |
| 1138 | QualType ClassTy = Context.getTagDeclType( |
| 1139 | const_cast<CXXRecordDecl*>(ConDecl->getParent())); |
| 1140 | if (Context.getCanonicalType(ParamType).getUnqualifiedType() == ClassTy) { |
| 1141 | Diag(ConDecl->getLocation(), |
| 1142 | diag::err_constructor_byvalue_arg, |
| 1143 | SourceRange(ConDecl->getParamDecl(0)->getLocation())); |
| 1144 | ConDecl->setInvalidDecl(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1145 | return ConDecl; |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 1146 | } |
| 1147 | } |
| 1148 | |
| 1149 | // Add this constructor to the set of constructors of the current |
| 1150 | // class. |
| 1151 | ClassDecl->addConstructor(Context, ConDecl); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1152 | return (DeclTy *)ConDecl; |
| 1153 | } |
| 1154 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1155 | /// ActOnDestructorDeclarator - Called by ActOnDeclarator to complete |
| 1156 | /// the declaration of the given C++ @p Destructor. This routine is |
| 1157 | /// responsible for recording the destructor in the C++ class, if |
| 1158 | /// possible. |
| 1159 | Sema::DeclTy *Sema::ActOnDestructorDeclarator(CXXDestructorDecl *Destructor) { |
| 1160 | assert(Destructor && "Expected to receive a destructor declaration"); |
| 1161 | |
| 1162 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CurContext); |
| 1163 | |
| 1164 | // Make sure we aren't redeclaring the destructor. |
| 1165 | if (CXXDestructorDecl *PrevDestructor = ClassDecl->getDestructor()) { |
| 1166 | Diag(Destructor->getLocation(), diag::err_destructor_redeclared); |
| 1167 | Diag(PrevDestructor->getLocation(), |
| 1168 | PrevDestructor->isThisDeclarationADefinition()? |
| 1169 | diag::err_previous_definition |
| 1170 | : diag::err_previous_declaration); |
| 1171 | Destructor->setInvalidDecl(); |
| 1172 | return Destructor; |
| 1173 | } |
| 1174 | |
| 1175 | ClassDecl->setDestructor(Destructor); |
| 1176 | return (DeclTy *)Destructor; |
| 1177 | } |
| 1178 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1179 | /// ActOnConversionDeclarator - Called by ActOnDeclarator to complete |
| 1180 | /// the declaration of the given C++ conversion function. This routine |
| 1181 | /// is responsible for recording the conversion function in the C++ |
| 1182 | /// class, if possible. |
| 1183 | Sema::DeclTy *Sema::ActOnConversionDeclarator(CXXConversionDecl *Conversion) { |
| 1184 | assert(Conversion && "Expected to receive a conversion function declaration"); |
| 1185 | |
| 1186 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CurContext); |
| 1187 | |
| 1188 | // Make sure we aren't redeclaring the conversion function. |
| 1189 | QualType ConvType = Context.getCanonicalType(Conversion->getConversionType()); |
| 1190 | OverloadedFunctionDecl *Conversions = ClassDecl->getConversionFunctions(); |
| 1191 | for (OverloadedFunctionDecl::function_iterator Func |
| 1192 | = Conversions->function_begin(); |
| 1193 | Func != Conversions->function_end(); ++Func) { |
| 1194 | CXXConversionDecl *OtherConv = cast<CXXConversionDecl>(*Func); |
| 1195 | if (ConvType == Context.getCanonicalType(OtherConv->getConversionType())) { |
| 1196 | Diag(Conversion->getLocation(), diag::err_conv_function_redeclared); |
| 1197 | Diag(OtherConv->getLocation(), |
| 1198 | OtherConv->isThisDeclarationADefinition()? |
| 1199 | diag::err_previous_definition |
| 1200 | : diag::err_previous_declaration); |
| 1201 | Conversion->setInvalidDecl(); |
| 1202 | return (DeclTy *)Conversion; |
| 1203 | } |
| 1204 | } |
| 1205 | |
| 1206 | // C++ [class.conv.fct]p1: |
| 1207 | // [...] A conversion function is never used to convert a |
| 1208 | // (possibly cv-qualified) object to the (possibly cv-qualified) |
| 1209 | // same object type (or a reference to it), to a (possibly |
| 1210 | // cv-qualified) base class of that type (or a reference to it), |
| 1211 | // or to (possibly cv-qualified) void. |
| 1212 | // FIXME: Suppress this warning if the conversion function ends up |
| 1213 | // being a virtual function that overrides a virtual function in a |
| 1214 | // base class. |
| 1215 | QualType ClassType |
| 1216 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
| 1217 | if (const ReferenceType *ConvTypeRef = ConvType->getAsReferenceType()) |
| 1218 | ConvType = ConvTypeRef->getPointeeType(); |
| 1219 | if (ConvType->isRecordType()) { |
| 1220 | ConvType = Context.getCanonicalType(ConvType).getUnqualifiedType(); |
| 1221 | if (ConvType == ClassType) |
| 1222 | Diag(Conversion->getLocation(), diag::warn_conv_to_self_not_used, |
| 1223 | ClassType.getAsString()); |
| 1224 | else if (IsDerivedFrom(ClassType, ConvType)) |
| 1225 | Diag(Conversion->getLocation(), diag::warn_conv_to_base_not_used, |
| 1226 | ClassType.getAsString(), |
| 1227 | ConvType.getAsString()); |
| 1228 | } else if (ConvType->isVoidType()) { |
| 1229 | Diag(Conversion->getLocation(), diag::warn_conv_to_void_not_used, |
| 1230 | ClassType.getAsString(), ConvType.getAsString()); |
| 1231 | } |
| 1232 | |
| 1233 | ClassDecl->addConversionFunction(Context, Conversion); |
| 1234 | |
| 1235 | return (DeclTy *)Conversion; |
| 1236 | } |
| 1237 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 1238 | //===----------------------------------------------------------------------===// |
| 1239 | // Namespace Handling |
| 1240 | //===----------------------------------------------------------------------===// |
| 1241 | |
| 1242 | /// ActOnStartNamespaceDef - This is called at the start of a namespace |
| 1243 | /// definition. |
| 1244 | Sema::DeclTy *Sema::ActOnStartNamespaceDef(Scope *NamespcScope, |
| 1245 | SourceLocation IdentLoc, |
| 1246 | IdentifierInfo *II, |
| 1247 | SourceLocation LBrace) { |
| 1248 | NamespaceDecl *Namespc = |
| 1249 | NamespaceDecl::Create(Context, CurContext, IdentLoc, II); |
| 1250 | Namespc->setLBracLoc(LBrace); |
| 1251 | |
| 1252 | Scope *DeclRegionScope = NamespcScope->getParent(); |
| 1253 | |
| 1254 | if (II) { |
| 1255 | // C++ [namespace.def]p2: |
| 1256 | // The identifier in an original-namespace-definition shall not have been |
| 1257 | // previously defined in the declarative region in which the |
| 1258 | // original-namespace-definition appears. The identifier in an |
| 1259 | // original-namespace-definition is the name of the namespace. Subsequently |
| 1260 | // in that declarative region, it is treated as an original-namespace-name. |
| 1261 | |
| 1262 | Decl *PrevDecl = |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1263 | LookupDecl(II, Decl::IDNS_Tag | Decl::IDNS_Ordinary, DeclRegionScope, 0, |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 1264 | /*enableLazyBuiltinCreation=*/false); |
| 1265 | |
Argyrios Kyrtzidis | 2fac626 | 2008-09-10 02:11:07 +0000 | [diff] [blame] | 1266 | if (PrevDecl && isDeclInScope(PrevDecl, CurContext, DeclRegionScope)) { |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 1267 | if (NamespaceDecl *OrigNS = dyn_cast<NamespaceDecl>(PrevDecl)) { |
| 1268 | // This is an extended namespace definition. |
| 1269 | // Attach this namespace decl to the chain of extended namespace |
| 1270 | // definitions. |
| 1271 | NamespaceDecl *NextNS = OrigNS; |
| 1272 | while (NextNS->getNextNamespace()) |
| 1273 | NextNS = NextNS->getNextNamespace(); |
| 1274 | |
| 1275 | NextNS->setNextNamespace(Namespc); |
| 1276 | Namespc->setOriginalNamespace(OrigNS); |
| 1277 | |
| 1278 | // We won't add this decl to the current scope. We want the namespace |
| 1279 | // name to return the original namespace decl during a name lookup. |
| 1280 | } else { |
| 1281 | // This is an invalid name redefinition. |
| 1282 | Diag(Namespc->getLocation(), diag::err_redefinition_different_kind, |
| 1283 | Namespc->getName()); |
| 1284 | Diag(PrevDecl->getLocation(), diag::err_previous_definition); |
| 1285 | Namespc->setInvalidDecl(); |
| 1286 | // Continue on to push Namespc as current DeclContext and return it. |
| 1287 | } |
| 1288 | } else { |
| 1289 | // This namespace name is declared for the first time. |
| 1290 | PushOnScopeChains(Namespc, DeclRegionScope); |
| 1291 | } |
| 1292 | } |
| 1293 | else { |
| 1294 | // FIXME: Handle anonymous namespaces |
| 1295 | } |
| 1296 | |
| 1297 | // Although we could have an invalid decl (i.e. the namespace name is a |
| 1298 | // redefinition), push it as current DeclContext and try to continue parsing. |
| 1299 | PushDeclContext(Namespc->getOriginalNamespace()); |
| 1300 | return Namespc; |
| 1301 | } |
| 1302 | |
| 1303 | /// ActOnFinishNamespaceDef - This callback is called after a namespace is |
| 1304 | /// exited. Decl is the DeclTy returned by ActOnStartNamespaceDef. |
| 1305 | void Sema::ActOnFinishNamespaceDef(DeclTy *D, SourceLocation RBrace) { |
| 1306 | Decl *Dcl = static_cast<Decl *>(D); |
| 1307 | NamespaceDecl *Namespc = dyn_cast_or_null<NamespaceDecl>(Dcl); |
| 1308 | assert(Namespc && "Invalid parameter, expected NamespaceDecl"); |
| 1309 | Namespc->setRBracLoc(RBrace); |
| 1310 | PopDeclContext(); |
| 1311 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1312 | |
| 1313 | |
| 1314 | /// AddCXXDirectInitializerToDecl - This action is called immediately after |
| 1315 | /// ActOnDeclarator, when a C++ direct initializer is present. |
| 1316 | /// e.g: "int x(1);" |
| 1317 | void Sema::AddCXXDirectInitializerToDecl(DeclTy *Dcl, SourceLocation LParenLoc, |
| 1318 | ExprTy **ExprTys, unsigned NumExprs, |
| 1319 | SourceLocation *CommaLocs, |
| 1320 | SourceLocation RParenLoc) { |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1321 | assert(NumExprs != 0 && ExprTys && "missing expressions"); |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 1322 | Decl *RealDecl = static_cast<Decl *>(Dcl); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1323 | |
| 1324 | // If there is no declaration, there was an error parsing it. Just ignore |
| 1325 | // the initializer. |
| 1326 | if (RealDecl == 0) { |
Ted Kremenek | 15f6139 | 2008-10-06 20:35:04 +0000 | [diff] [blame] | 1327 | for (unsigned i = 0; i != NumExprs; ++i) |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1328 | delete static_cast<Expr *>(ExprTys[i]); |
| 1329 | return; |
| 1330 | } |
| 1331 | |
| 1332 | VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl); |
| 1333 | if (!VDecl) { |
| 1334 | Diag(RealDecl->getLocation(), diag::err_illegal_initializer); |
| 1335 | RealDecl->setInvalidDecl(); |
| 1336 | return; |
| 1337 | } |
| 1338 | |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 1339 | // We will treat direct-initialization as a copy-initialization: |
| 1340 | // int x(1); -as-> int x = 1; |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1341 | // ClassType x(a,b,c); -as-> ClassType x = ClassType(a,b,c); |
| 1342 | // |
| 1343 | // Clients that want to distinguish between the two forms, can check for |
| 1344 | // direct initializer using VarDecl::hasCXXDirectInitializer(). |
| 1345 | // A major benefit is that clients that don't particularly care about which |
| 1346 | // exactly form was it (like the CodeGen) can handle both cases without |
| 1347 | // special case code. |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 1348 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1349 | // C++ 8.5p11: |
| 1350 | // The form of initialization (using parentheses or '=') is generally |
| 1351 | // insignificant, but does matter when the entity being initialized has a |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 1352 | // class type. |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 1353 | QualType DeclInitType = VDecl->getType(); |
| 1354 | if (const ArrayType *Array = Context.getAsArrayType(DeclInitType)) |
| 1355 | DeclInitType = Array->getElementType(); |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 1356 | |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 1357 | if (VDecl->getType()->isRecordType()) { |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 1358 | CXXConstructorDecl *Constructor |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 1359 | = PerformInitializationByConstructor(DeclInitType, |
| 1360 | (Expr **)ExprTys, NumExprs, |
| 1361 | VDecl->getLocation(), |
| 1362 | SourceRange(VDecl->getLocation(), |
| 1363 | RParenLoc), |
| 1364 | VDecl->getName(), |
| 1365 | IK_Direct); |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 1366 | if (!Constructor) { |
| 1367 | RealDecl->setInvalidDecl(); |
| 1368 | } |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 1369 | |
| 1370 | // Let clients know that initialization was done with a direct |
| 1371 | // initializer. |
| 1372 | VDecl->setCXXDirectInitializer(true); |
| 1373 | |
| 1374 | // FIXME: Add ExprTys and Constructor to the RealDecl as part of |
| 1375 | // the initializer. |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 1376 | return; |
| 1377 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1378 | |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 1379 | if (NumExprs > 1) { |
| 1380 | Diag(CommaLocs[0], diag::err_builtin_direct_init_more_than_one_arg, |
| 1381 | SourceRange(VDecl->getLocation(), RParenLoc)); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1382 | RealDecl->setInvalidDecl(); |
| 1383 | return; |
| 1384 | } |
| 1385 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1386 | // Let clients know that initialization was done with a direct initializer. |
| 1387 | VDecl->setCXXDirectInitializer(true); |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 1388 | |
| 1389 | assert(NumExprs == 1 && "Expected 1 expression"); |
| 1390 | // Set the init expression, handles conversions. |
| 1391 | AddInitializerToDecl(Dcl, ExprTys[0]); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1392 | } |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1393 | |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 1394 | /// PerformInitializationByConstructor - Perform initialization by |
| 1395 | /// constructor (C++ [dcl.init]p14), which may occur as part of |
| 1396 | /// direct-initialization or copy-initialization. We are initializing |
| 1397 | /// an object of type @p ClassType with the given arguments @p |
| 1398 | /// Args. @p Loc is the location in the source code where the |
| 1399 | /// initializer occurs (e.g., a declaration, member initializer, |
| 1400 | /// functional cast, etc.) while @p Range covers the whole |
| 1401 | /// initialization. @p InitEntity is the entity being initialized, |
| 1402 | /// which may by the name of a declaration or a type. @p Kind is the |
| 1403 | /// kind of initialization we're performing, which affects whether |
| 1404 | /// explicit constructors will be considered. When successful, returns |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 1405 | /// the constructor that will be used to perform the initialization; |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 1406 | /// when the initialization fails, emits a diagnostic and returns |
| 1407 | /// null. |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 1408 | CXXConstructorDecl * |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 1409 | Sema::PerformInitializationByConstructor(QualType ClassType, |
| 1410 | Expr **Args, unsigned NumArgs, |
| 1411 | SourceLocation Loc, SourceRange Range, |
| 1412 | std::string InitEntity, |
| 1413 | InitializationKind Kind) { |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 1414 | const RecordType *ClassRec = ClassType->getAsRecordType(); |
| 1415 | assert(ClassRec && "Can only initialize a class type here"); |
| 1416 | |
| 1417 | // C++ [dcl.init]p14: |
| 1418 | // |
| 1419 | // If the initialization is direct-initialization, or if it is |
| 1420 | // copy-initialization where the cv-unqualified version of the |
| 1421 | // source type is the same class as, or a derived class of, the |
| 1422 | // class of the destination, constructors are considered. The |
| 1423 | // applicable constructors are enumerated (13.3.1.3), and the |
| 1424 | // best one is chosen through overload resolution (13.3). The |
| 1425 | // constructor so selected is called to initialize the object, |
| 1426 | // with the initializer expression(s) as its argument(s). If no |
| 1427 | // constructor applies, or the overload resolution is ambiguous, |
| 1428 | // the initialization is ill-formed. |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 1429 | const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(ClassRec->getDecl()); |
| 1430 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 1431 | |
| 1432 | // Add constructors to the overload set. |
| 1433 | OverloadedFunctionDecl *Constructors |
| 1434 | = const_cast<OverloadedFunctionDecl *>(ClassDecl->getConstructors()); |
| 1435 | for (OverloadedFunctionDecl::function_iterator Con |
| 1436 | = Constructors->function_begin(); |
| 1437 | Con != Constructors->function_end(); ++Con) { |
| 1438 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con); |
| 1439 | if ((Kind == IK_Direct) || |
| 1440 | (Kind == IK_Copy && Constructor->isConvertingConstructor()) || |
| 1441 | (Kind == IK_Default && Constructor->isDefaultConstructor())) |
| 1442 | AddOverloadCandidate(Constructor, Args, NumArgs, CandidateSet); |
| 1443 | } |
| 1444 | |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 1445 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 1446 | switch (BestViableFunction(CandidateSet, Best)) { |
| 1447 | case OR_Success: |
| 1448 | // We found a constructor. Return it. |
| 1449 | return cast<CXXConstructorDecl>(Best->Function); |
| 1450 | |
| 1451 | case OR_No_Viable_Function: |
| 1452 | if (CandidateSet.empty()) |
| 1453 | Diag(Loc, diag::err_ovl_no_viable_function_in_init, |
| 1454 | InitEntity, Range); |
| 1455 | else { |
| 1456 | Diag(Loc, diag::err_ovl_no_viable_function_in_init_with_cands, |
| 1457 | InitEntity, Range); |
| 1458 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 1459 | } |
| 1460 | return 0; |
| 1461 | |
| 1462 | case OR_Ambiguous: |
| 1463 | Diag(Loc, diag::err_ovl_ambiguous_init, |
| 1464 | InitEntity, Range); |
| 1465 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 1466 | return 0; |
| 1467 | } |
| 1468 | |
| 1469 | return 0; |
| 1470 | } |
| 1471 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1472 | /// CompareReferenceRelationship - Compare the two types T1 and T2 to |
| 1473 | /// determine whether they are reference-related, |
| 1474 | /// reference-compatible, reference-compatible with added |
| 1475 | /// qualification, or incompatible, for use in C++ initialization by |
| 1476 | /// reference (C++ [dcl.ref.init]p4). Neither type can be a reference |
| 1477 | /// type, and the first type (T1) is the pointee type of the reference |
| 1478 | /// type being initialized. |
| 1479 | Sema::ReferenceCompareResult |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1480 | Sema::CompareReferenceRelationship(QualType T1, QualType T2, |
| 1481 | bool& DerivedToBase) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1482 | assert(!T1->isReferenceType() && "T1 must be the pointee type of the reference type"); |
| 1483 | assert(!T2->isReferenceType() && "T2 cannot be a reference type"); |
| 1484 | |
| 1485 | T1 = Context.getCanonicalType(T1); |
| 1486 | T2 = Context.getCanonicalType(T2); |
| 1487 | QualType UnqualT1 = T1.getUnqualifiedType(); |
| 1488 | QualType UnqualT2 = T2.getUnqualifiedType(); |
| 1489 | |
| 1490 | // C++ [dcl.init.ref]p4: |
| 1491 | // Given types “cv1 T1” and “cv2 T2,” “cv1 T1” is |
| 1492 | // reference-related to “cv2 T2” if T1 is the same type as T2, or |
| 1493 | // T1 is a base class of T2. |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1494 | if (UnqualT1 == UnqualT2) |
| 1495 | DerivedToBase = false; |
| 1496 | else if (IsDerivedFrom(UnqualT2, UnqualT1)) |
| 1497 | DerivedToBase = true; |
| 1498 | else |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1499 | return Ref_Incompatible; |
| 1500 | |
| 1501 | // At this point, we know that T1 and T2 are reference-related (at |
| 1502 | // least). |
| 1503 | |
| 1504 | // C++ [dcl.init.ref]p4: |
| 1505 | // "cv1 T1” is reference-compatible with “cv2 T2” if T1 is |
| 1506 | // reference-related to T2 and cv1 is the same cv-qualification |
| 1507 | // as, or greater cv-qualification than, cv2. For purposes of |
| 1508 | // overload resolution, cases for which cv1 is greater |
| 1509 | // cv-qualification than cv2 are identified as |
| 1510 | // reference-compatible with added qualification (see 13.3.3.2). |
| 1511 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 1512 | return Ref_Compatible; |
| 1513 | else if (T1.isMoreQualifiedThan(T2)) |
| 1514 | return Ref_Compatible_With_Added_Qualification; |
| 1515 | else |
| 1516 | return Ref_Related; |
| 1517 | } |
| 1518 | |
| 1519 | /// CheckReferenceInit - Check the initialization of a reference |
| 1520 | /// variable with the given initializer (C++ [dcl.init.ref]). Init is |
| 1521 | /// the initializer (either a simple initializer or an initializer |
Douglas Gregor | 3205a78 | 2008-10-29 23:31:03 +0000 | [diff] [blame] | 1522 | /// list), and DeclType is the type of the declaration. When ICS is |
| 1523 | /// non-null, this routine will compute the implicit conversion |
| 1524 | /// sequence according to C++ [over.ics.ref] and will not produce any |
| 1525 | /// diagnostics; when ICS is null, it will emit diagnostics when any |
| 1526 | /// errors are found. Either way, a return value of true indicates |
| 1527 | /// that there was a failure, a return value of false indicates that |
| 1528 | /// the reference initialization succeeded. |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1529 | /// |
| 1530 | /// When @p SuppressUserConversions, user-defined conversions are |
| 1531 | /// suppressed. |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1532 | bool |
| 1533 | Sema::CheckReferenceInit(Expr *&Init, QualType &DeclType, |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1534 | ImplicitConversionSequence *ICS, |
| 1535 | bool SuppressUserConversions) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1536 | assert(DeclType->isReferenceType() && "Reference init needs a reference"); |
| 1537 | |
| 1538 | QualType T1 = DeclType->getAsReferenceType()->getPointeeType(); |
| 1539 | QualType T2 = Init->getType(); |
| 1540 | |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1541 | // Compute some basic properties of the types and the initializer. |
| 1542 | bool DerivedToBase = false; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1543 | Expr::isLvalueResult InitLvalue = Init->isLvalue(Context); |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1544 | ReferenceCompareResult RefRelationship |
| 1545 | = CompareReferenceRelationship(T1, T2, DerivedToBase); |
| 1546 | |
| 1547 | // Most paths end in a failed conversion. |
| 1548 | if (ICS) |
| 1549 | ICS->ConversionKind = ImplicitConversionSequence::BadConversion; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1550 | |
| 1551 | // C++ [dcl.init.ref]p5: |
| 1552 | // A reference to type “cv1 T1” is initialized by an expression |
| 1553 | // of type “cv2 T2” as follows: |
| 1554 | |
| 1555 | // -- If the initializer expression |
| 1556 | |
| 1557 | bool BindsDirectly = false; |
| 1558 | // -- is an lvalue (but is not a bit-field), and “cv1 T1” is |
| 1559 | // reference-compatible with “cv2 T2,” or |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1560 | // |
| 1561 | // Note that the bit-field check is skipped if we are just computing |
| 1562 | // the implicit conversion sequence (C++ [over.best.ics]p2). |
| 1563 | if (InitLvalue == Expr::LV_Valid && (ICS || !Init->isBitField()) && |
| 1564 | RefRelationship >= Ref_Compatible_With_Added_Qualification) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1565 | BindsDirectly = true; |
| 1566 | |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1567 | if (ICS) { |
| 1568 | // C++ [over.ics.ref]p1: |
| 1569 | // When a parameter of reference type binds directly (8.5.3) |
| 1570 | // to an argument expression, the implicit conversion sequence |
| 1571 | // is the identity conversion, unless the argument expression |
| 1572 | // has a type that is a derived class of the parameter type, |
| 1573 | // in which case the implicit conversion sequence is a |
| 1574 | // derived-to-base Conversion (13.3.3.1). |
| 1575 | ICS->ConversionKind = ImplicitConversionSequence::StandardConversion; |
| 1576 | ICS->Standard.First = ICK_Identity; |
| 1577 | ICS->Standard.Second = DerivedToBase? ICK_Derived_To_Base : ICK_Identity; |
| 1578 | ICS->Standard.Third = ICK_Identity; |
| 1579 | ICS->Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 1580 | ICS->Standard.ToTypePtr = T1.getAsOpaquePtr(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1581 | ICS->Standard.ReferenceBinding = true; |
| 1582 | ICS->Standard.DirectBinding = true; |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1583 | |
| 1584 | // Nothing more to do: the inaccessibility/ambiguity check for |
| 1585 | // derived-to-base conversions is suppressed when we're |
| 1586 | // computing the implicit conversion sequence (C++ |
| 1587 | // [over.best.ics]p2). |
| 1588 | return false; |
| 1589 | } else { |
| 1590 | // Perform the conversion. |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1591 | // FIXME: Binding to a subobject of the lvalue is going to require |
| 1592 | // more AST annotation than this. |
| 1593 | ImpCastExprToType(Init, T1); |
| 1594 | } |
| 1595 | } |
| 1596 | |
| 1597 | // -- has a class type (i.e., T2 is a class type) and can be |
| 1598 | // implicitly converted to an lvalue of type “cv3 T3,” |
| 1599 | // where “cv1 T1” is reference-compatible with “cv3 T3” |
| 1600 | // 92) (this conversion is selected by enumerating the |
| 1601 | // applicable conversion functions (13.3.1.6) and choosing |
| 1602 | // the best one through overload resolution (13.3)), |
| 1603 | // FIXME: Implement this second bullet, once we have conversion |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1604 | // functions. Also remember C++ [over.ics.ref]p1, second part. |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1605 | |
| 1606 | if (BindsDirectly) { |
| 1607 | // C++ [dcl.init.ref]p4: |
| 1608 | // [...] In all cases where the reference-related or |
| 1609 | // reference-compatible relationship of two types is used to |
| 1610 | // establish the validity of a reference binding, and T1 is a |
| 1611 | // base class of T2, a program that necessitates such a binding |
| 1612 | // is ill-formed if T1 is an inaccessible (clause 11) or |
| 1613 | // ambiguous (10.2) base class of T2. |
| 1614 | // |
| 1615 | // Note that we only check this condition when we're allowed to |
| 1616 | // complain about errors, because we should not be checking for |
| 1617 | // ambiguity (or inaccessibility) unless the reference binding |
| 1618 | // actually happens. |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1619 | if (DerivedToBase) |
| 1620 | return CheckDerivedToBaseConversion(T2, T1, |
| 1621 | Init->getSourceRange().getBegin(), |
| 1622 | Init->getSourceRange()); |
| 1623 | else |
| 1624 | return false; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1625 | } |
| 1626 | |
| 1627 | // -- Otherwise, the reference shall be to a non-volatile const |
| 1628 | // type (i.e., cv1 shall be const). |
| 1629 | if (T1.getCVRQualifiers() != QualType::Const) { |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1630 | if (!ICS) |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1631 | Diag(Init->getSourceRange().getBegin(), |
| 1632 | diag::err_not_reference_to_const_init, |
| 1633 | T1.getAsString(), |
| 1634 | InitLvalue != Expr::LV_Valid? "temporary" : "value", |
| 1635 | T2.getAsString(), Init->getSourceRange()); |
| 1636 | return true; |
| 1637 | } |
| 1638 | |
| 1639 | // -- If the initializer expression is an rvalue, with T2 a |
| 1640 | // class type, and “cv1 T1” is reference-compatible with |
| 1641 | // “cv2 T2,” the reference is bound in one of the |
| 1642 | // following ways (the choice is implementation-defined): |
| 1643 | // |
| 1644 | // -- The reference is bound to the object represented by |
| 1645 | // the rvalue (see 3.10) or to a sub-object within that |
| 1646 | // object. |
| 1647 | // |
| 1648 | // -- A temporary of type “cv1 T2” [sic] is created, and |
| 1649 | // a constructor is called to copy the entire rvalue |
| 1650 | // object into the temporary. The reference is bound to |
| 1651 | // the temporary or to a sub-object within the |
| 1652 | // temporary. |
| 1653 | // |
| 1654 | // |
| 1655 | // The constructor that would be used to make the copy |
| 1656 | // shall be callable whether or not the copy is actually |
| 1657 | // done. |
| 1658 | // |
| 1659 | // Note that C++0x [dcl.ref.init]p5 takes away this implementation |
| 1660 | // freedom, so we will always take the first option and never build |
| 1661 | // a temporary in this case. FIXME: We will, however, have to check |
| 1662 | // for the presence of a copy constructor in C++98/03 mode. |
| 1663 | if (InitLvalue != Expr::LV_Valid && T2->isRecordType() && |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1664 | RefRelationship >= Ref_Compatible_With_Added_Qualification) { |
| 1665 | if (ICS) { |
| 1666 | ICS->ConversionKind = ImplicitConversionSequence::StandardConversion; |
| 1667 | ICS->Standard.First = ICK_Identity; |
| 1668 | ICS->Standard.Second = DerivedToBase? ICK_Derived_To_Base : ICK_Identity; |
| 1669 | ICS->Standard.Third = ICK_Identity; |
| 1670 | ICS->Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 1671 | ICS->Standard.ToTypePtr = T1.getAsOpaquePtr(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1672 | ICS->Standard.ReferenceBinding = true; |
| 1673 | ICS->Standard.DirectBinding = false; |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1674 | } else { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1675 | // FIXME: Binding to a subobject of the rvalue is going to require |
| 1676 | // more AST annotation than this. |
| 1677 | ImpCastExprToType(Init, T1); |
| 1678 | } |
| 1679 | return false; |
| 1680 | } |
| 1681 | |
| 1682 | // -- Otherwise, a temporary of type “cv1 T1” is created and |
| 1683 | // initialized from the initializer expression using the |
| 1684 | // rules for a non-reference copy initialization (8.5). The |
| 1685 | // reference is then bound to the temporary. If T1 is |
| 1686 | // reference-related to T2, cv1 must be the same |
| 1687 | // cv-qualification as, or greater cv-qualification than, |
| 1688 | // cv2; otherwise, the program is ill-formed. |
| 1689 | if (RefRelationship == Ref_Related) { |
| 1690 | // If cv1 == cv2 or cv1 is a greater cv-qualified than cv2, then |
| 1691 | // we would be reference-compatible or reference-compatible with |
| 1692 | // added qualification. But that wasn't the case, so the reference |
| 1693 | // initialization fails. |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1694 | if (!ICS) |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1695 | Diag(Init->getSourceRange().getBegin(), |
| 1696 | diag::err_reference_init_drops_quals, |
| 1697 | T1.getAsString(), |
| 1698 | InitLvalue != Expr::LV_Valid? "temporary" : "value", |
| 1699 | T2.getAsString(), Init->getSourceRange()); |
| 1700 | return true; |
| 1701 | } |
| 1702 | |
| 1703 | // Actually try to convert the initializer to T1. |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1704 | if (ICS) { |
| 1705 | /// C++ [over.ics.ref]p2: |
| 1706 | /// |
| 1707 | /// When a parameter of reference type is not bound directly to |
| 1708 | /// an argument expression, the conversion sequence is the one |
| 1709 | /// required to convert the argument expression to the |
| 1710 | /// underlying type of the reference according to |
| 1711 | /// 13.3.3.1. Conceptually, this conversion sequence corresponds |
| 1712 | /// to copy-initializing a temporary of the underlying type with |
| 1713 | /// the argument expression. Any difference in top-level |
| 1714 | /// cv-qualification is subsumed by the initialization itself |
| 1715 | /// and does not constitute a conversion. |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1716 | *ICS = TryImplicitConversion(Init, T1, SuppressUserConversions); |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1717 | return ICS->ConversionKind == ImplicitConversionSequence::BadConversion; |
| 1718 | } else { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1719 | return PerformImplicitConversion(Init, T1); |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1720 | } |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1721 | } |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 1722 | |
| 1723 | /// CheckOverloadedOperatorDeclaration - Check whether the declaration |
| 1724 | /// of this overloaded operator is well-formed. If so, returns false; |
| 1725 | /// otherwise, emits appropriate diagnostics and returns true. |
| 1726 | bool Sema::CheckOverloadedOperatorDeclaration(FunctionDecl *FnDecl) { |
| 1727 | assert(FnDecl && FnDecl->getOverloadedOperator() != OO_None && |
| 1728 | "Expected an overloaded operator declaration"); |
| 1729 | |
| 1730 | bool IsInvalid = false; |
| 1731 | |
| 1732 | OverloadedOperatorKind Op = FnDecl->getOverloadedOperator(); |
| 1733 | |
| 1734 | // C++ [over.oper]p5: |
| 1735 | // The allocation and deallocation functions, operator new, |
| 1736 | // operator new[], operator delete and operator delete[], are |
| 1737 | // described completely in 3.7.3. The attributes and restrictions |
| 1738 | // found in the rest of this subclause do not apply to them unless |
| 1739 | // explicitly stated in 3.7.3. |
| 1740 | // FIXME: Write a separate routine for checking this. For now, just |
| 1741 | // allow it. |
| 1742 | if (Op == OO_New || Op == OO_Array_New || |
| 1743 | Op == OO_Delete || Op == OO_Array_Delete) |
| 1744 | return false; |
| 1745 | |
| 1746 | // C++ [over.oper]p6: |
| 1747 | // An operator function shall either be a non-static member |
| 1748 | // function or be a non-member function and have at least one |
| 1749 | // parameter whose type is a class, a reference to a class, an |
| 1750 | // enumeration, or a reference to an enumeration. |
| 1751 | CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(FnDecl); |
| 1752 | if (MethodDecl) { |
| 1753 | if (MethodDecl->isStatic()) { |
| 1754 | Diag(FnDecl->getLocation(), |
| 1755 | diag::err_operator_overload_static, |
| 1756 | FnDecl->getName(), |
| 1757 | SourceRange(FnDecl->getLocation())); |
| 1758 | IsInvalid = true; |
| 1759 | |
| 1760 | // Pretend this isn't a member function; it'll supress |
| 1761 | // additional, unnecessary error messages. |
| 1762 | MethodDecl = 0; |
| 1763 | } |
| 1764 | } else { |
| 1765 | bool ClassOrEnumParam = false; |
| 1766 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 1767 | Param != FnDecl->param_end(); ++Param) { |
| 1768 | QualType ParamType = (*Param)->getType(); |
| 1769 | if (const ReferenceType *RefType = ParamType->getAsReferenceType()) |
| 1770 | ParamType = RefType->getPointeeType(); |
| 1771 | if (ParamType->isRecordType() || ParamType->isEnumeralType()) { |
| 1772 | ClassOrEnumParam = true; |
| 1773 | break; |
| 1774 | } |
| 1775 | } |
| 1776 | |
| 1777 | if (!ClassOrEnumParam) { |
| 1778 | Diag(FnDecl->getLocation(), |
| 1779 | diag::err_operator_overload_needs_class_or_enum, |
| 1780 | FnDecl->getName(), |
| 1781 | SourceRange(FnDecl->getLocation())); |
| 1782 | IsInvalid = true; |
| 1783 | } |
| 1784 | } |
| 1785 | |
| 1786 | // C++ [over.oper]p8: |
| 1787 | // An operator function cannot have default arguments (8.3.6), |
| 1788 | // except where explicitly stated below. |
| 1789 | // |
| 1790 | // Only the function-call operator allows default arguments |
| 1791 | // (C++ [over.call]p1). |
| 1792 | if (Op != OO_Call) { |
| 1793 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 1794 | Param != FnDecl->param_end(); ++Param) { |
| 1795 | if (Expr *DefArg = (*Param)->getDefaultArg()) { |
| 1796 | Diag((*Param)->getLocation(), |
| 1797 | diag::err_operator_overload_default_arg, |
| 1798 | DefArg->getSourceRange()); |
| 1799 | IsInvalid = true; |
| 1800 | } |
| 1801 | } |
| 1802 | } |
| 1803 | |
| 1804 | bool CanBeUnaryOperator = false; |
| 1805 | bool CanBeBinaryOperator = false; |
| 1806 | bool MustBeMemberOperator = false; |
| 1807 | |
| 1808 | switch (Op) { |
| 1809 | case OO_New: |
| 1810 | case OO_Delete: |
| 1811 | case OO_Array_New: |
| 1812 | case OO_Array_Delete: |
| 1813 | assert(false && "Operators new, new[], delete, and delete[] handled above"); |
| 1814 | return true; |
| 1815 | |
| 1816 | // Unary-only operators |
| 1817 | case OO_Arrow: |
| 1818 | MustBeMemberOperator = true; |
| 1819 | // Fall through |
| 1820 | |
| 1821 | case OO_Tilde: |
| 1822 | case OO_Exclaim: |
| 1823 | CanBeUnaryOperator = true; |
| 1824 | break; |
| 1825 | |
| 1826 | // Binary-only operators |
| 1827 | case OO_Equal: |
| 1828 | case OO_Subscript: |
| 1829 | MustBeMemberOperator = true; |
| 1830 | // Fall through |
| 1831 | |
| 1832 | case OO_Slash: |
| 1833 | case OO_Percent: |
| 1834 | case OO_Caret: |
| 1835 | case OO_Pipe: |
| 1836 | case OO_Less: |
| 1837 | case OO_Greater: |
| 1838 | case OO_PlusEqual: |
| 1839 | case OO_MinusEqual: |
| 1840 | case OO_StarEqual: |
| 1841 | case OO_SlashEqual: |
| 1842 | case OO_PercentEqual: |
| 1843 | case OO_CaretEqual: |
| 1844 | case OO_AmpEqual: |
| 1845 | case OO_PipeEqual: |
| 1846 | case OO_LessLess: |
| 1847 | case OO_GreaterGreater: |
| 1848 | case OO_LessLessEqual: |
| 1849 | case OO_GreaterGreaterEqual: |
| 1850 | case OO_EqualEqual: |
| 1851 | case OO_ExclaimEqual: |
| 1852 | case OO_LessEqual: |
| 1853 | case OO_GreaterEqual: |
| 1854 | case OO_AmpAmp: |
| 1855 | case OO_PipePipe: |
| 1856 | case OO_Comma: |
| 1857 | CanBeBinaryOperator = true; |
| 1858 | break; |
| 1859 | |
| 1860 | // Unary or binary operators |
| 1861 | case OO_Amp: |
| 1862 | case OO_Plus: |
| 1863 | case OO_Minus: |
| 1864 | case OO_Star: |
| 1865 | case OO_PlusPlus: |
| 1866 | case OO_MinusMinus: |
| 1867 | case OO_ArrowStar: |
| 1868 | CanBeUnaryOperator = true; |
| 1869 | CanBeBinaryOperator = true; |
| 1870 | break; |
| 1871 | |
| 1872 | case OO_Call: |
| 1873 | MustBeMemberOperator = true; |
| 1874 | break; |
| 1875 | |
| 1876 | case OO_None: |
| 1877 | case NUM_OVERLOADED_OPERATORS: |
| 1878 | assert(false && "Not an overloaded operator!"); |
| 1879 | return true; |
| 1880 | } |
| 1881 | |
| 1882 | // C++ [over.oper]p8: |
| 1883 | // [...] Operator functions cannot have more or fewer parameters |
| 1884 | // than the number required for the corresponding operator, as |
| 1885 | // described in the rest of this subclause. |
| 1886 | unsigned NumParams = FnDecl->getNumParams() + (MethodDecl? 1 : 0); |
| 1887 | if (Op != OO_Call && |
| 1888 | ((NumParams == 1 && !CanBeUnaryOperator) || |
| 1889 | (NumParams == 2 && !CanBeBinaryOperator) || |
| 1890 | (NumParams < 1) || (NumParams > 2))) { |
| 1891 | // We have the wrong number of parameters. |
| 1892 | std::string NumParamsStr = (llvm::APSInt(32) = NumParams).toString(10); |
| 1893 | std::string NumParamsPlural; |
| 1894 | if (NumParams != 1) |
| 1895 | NumParamsPlural = "s"; |
| 1896 | |
| 1897 | diag::kind DK; |
| 1898 | |
| 1899 | if (CanBeUnaryOperator && CanBeBinaryOperator) |
| 1900 | DK = diag::err_operator_overload_must_be_unary_or_binary; |
| 1901 | else if (CanBeUnaryOperator) |
| 1902 | DK = diag::err_operator_overload_must_be_unary; |
| 1903 | else if (CanBeBinaryOperator) |
| 1904 | DK = diag::err_operator_overload_must_be_binary; |
| 1905 | else |
| 1906 | assert(false && "All non-call overloaded operators are unary or binary!"); |
| 1907 | |
| 1908 | Diag(FnDecl->getLocation(), DK, |
| 1909 | FnDecl->getName(), NumParamsStr, NumParamsPlural, |
| 1910 | SourceRange(FnDecl->getLocation())); |
| 1911 | IsInvalid = true; |
| 1912 | } |
| 1913 | |
| 1914 | // Overloaded operators cannot be variadic. |
| 1915 | if (FnDecl->getType()->getAsFunctionTypeProto()->isVariadic()) { |
| 1916 | Diag(FnDecl->getLocation(), |
| 1917 | diag::err_operator_overload_variadic, |
| 1918 | SourceRange(FnDecl->getLocation())); |
| 1919 | IsInvalid = true; |
| 1920 | } |
| 1921 | |
| 1922 | // Some operators must be non-static member functions. |
| 1923 | if (MustBeMemberOperator && !MethodDecl) { |
| 1924 | Diag(FnDecl->getLocation(), |
| 1925 | diag::err_operator_overload_must_be_member, |
| 1926 | FnDecl->getName(), |
| 1927 | SourceRange(FnDecl->getLocation())); |
| 1928 | IsInvalid = true; |
| 1929 | } |
| 1930 | |
| 1931 | // C++ [over.inc]p1: |
| 1932 | // The user-defined function called operator++ implements the |
| 1933 | // prefix and postfix ++ operator. If this function is a member |
| 1934 | // function with no parameters, or a non-member function with one |
| 1935 | // parameter of class or enumeration type, it defines the prefix |
| 1936 | // increment operator ++ for objects of that type. If the function |
| 1937 | // is a member function with one parameter (which shall be of type |
| 1938 | // int) or a non-member function with two parameters (the second |
| 1939 | // of which shall be of type int), it defines the postfix |
| 1940 | // increment operator ++ for objects of that type. |
| 1941 | if ((Op == OO_PlusPlus || Op == OO_MinusMinus) && NumParams == 2) { |
| 1942 | ParmVarDecl *LastParam = FnDecl->getParamDecl(FnDecl->getNumParams() - 1); |
| 1943 | bool ParamIsInt = false; |
| 1944 | if (const BuiltinType *BT = LastParam->getType()->getAsBuiltinType()) |
| 1945 | ParamIsInt = BT->getKind() == BuiltinType::Int; |
| 1946 | |
| 1947 | if (!ParamIsInt) { |
| 1948 | Diag(LastParam->getLocation(), |
| 1949 | diag::err_operator_overload_post_incdec_must_be_int, |
| 1950 | MethodDecl? std::string() : std::string("second "), |
| 1951 | (Op == OO_PlusPlus)? std::string("increment") |
| 1952 | : std::string("decrement"), |
| 1953 | Context.getCanonicalType(LastParam->getType()).getAsString(), |
| 1954 | SourceRange(FnDecl->getLocation())); |
| 1955 | IsInvalid = true; |
| 1956 | } |
| 1957 | } |
| 1958 | |
| 1959 | return IsInvalid; |
| 1960 | } |