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