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