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 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 260 | /// isCurrentClassName - Determine whether the identifier II is the |
| 261 | /// name of the class type currently being defined. In the case of |
| 262 | /// nested classes, this will only return true if II is the name of |
| 263 | /// the innermost class. |
| 264 | bool Sema::isCurrentClassName(const IdentifierInfo &II, Scope *) { |
| 265 | if (CXXRecordDecl *CurDecl = dyn_cast_or_null<CXXRecordDecl>(CurContext)) |
| 266 | return &II == CurDecl->getIdentifier(); |
| 267 | else |
| 268 | return false; |
| 269 | } |
| 270 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 271 | /// ActOnBaseSpecifier - Parsed a base specifier. A base specifier is |
| 272 | /// one entry in the base class list of a class specifier, for |
| 273 | /// example: |
| 274 | /// class foo : public bar, virtual private baz { |
| 275 | /// 'public bar' and 'virtual private baz' are each base-specifiers. |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 276 | Sema::BaseResult |
| 277 | Sema::ActOnBaseSpecifier(DeclTy *classdecl, SourceRange SpecifierRange, |
| 278 | bool Virtual, AccessSpecifier Access, |
| 279 | TypeTy *basetype, SourceLocation BaseLoc) { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 280 | RecordDecl *Decl = (RecordDecl*)classdecl; |
| 281 | QualType BaseType = Context.getTypeDeclType((TypeDecl*)basetype); |
| 282 | |
| 283 | // Base specifiers must be record types. |
| 284 | if (!BaseType->isRecordType()) { |
| 285 | Diag(BaseLoc, diag::err_base_must_be_class, SpecifierRange); |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 286 | return true; |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | // C++ [class.union]p1: |
| 290 | // A union shall not be used as a base class. |
| 291 | if (BaseType->isUnionType()) { |
| 292 | Diag(BaseLoc, diag::err_union_as_base_class, SpecifierRange); |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 293 | return true; |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | // C++ [class.union]p1: |
| 297 | // A union shall not have base classes. |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 298 | if (Decl->isUnion()) { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 299 | Diag(Decl->getLocation(), diag::err_base_clause_on_union, |
| 300 | SpecifierRange); |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 301 | return true; |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | // C++ [class.derived]p2: |
| 305 | // The class-name in a base-specifier shall not be an incompletely |
| 306 | // defined class. |
| 307 | if (BaseType->isIncompleteType()) { |
| 308 | Diag(BaseLoc, diag::err_incomplete_base_class, SpecifierRange); |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 309 | return true; |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 312 | // Create the base specifier. |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 313 | return new CXXBaseSpecifier(SpecifierRange, Virtual, |
| 314 | BaseType->isClassType(), Access, BaseType); |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 315 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 316 | |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 317 | /// ActOnBaseSpecifiers - Attach the given base specifiers to the |
| 318 | /// class, after checking whether there are any duplicate base |
| 319 | /// classes. |
| 320 | void Sema::ActOnBaseSpecifiers(DeclTy *ClassDecl, BaseTy **Bases, |
| 321 | unsigned NumBases) { |
| 322 | if (NumBases == 0) |
| 323 | return; |
| 324 | |
| 325 | // Used to keep track of which base types we have already seen, so |
| 326 | // that we can properly diagnose redundant direct base types. Note |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 327 | // that the key is always the unqualified canonical type of the base |
| 328 | // class. |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 329 | std::map<QualType, CXXBaseSpecifier*, QualTypeOrdering> KnownBaseTypes; |
| 330 | |
| 331 | // Copy non-redundant base specifiers into permanent storage. |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 332 | CXXBaseSpecifier **BaseSpecs = (CXXBaseSpecifier **)Bases; |
| 333 | unsigned NumGoodBases = 0; |
| 334 | for (unsigned idx = 0; idx < NumBases; ++idx) { |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 335 | QualType NewBaseType |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 336 | = Context.getCanonicalType(BaseSpecs[idx]->getType()); |
| 337 | NewBaseType = NewBaseType.getUnqualifiedType(); |
| 338 | |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 339 | if (KnownBaseTypes[NewBaseType]) { |
| 340 | // C++ [class.mi]p3: |
| 341 | // A class shall not be specified as a direct base class of a |
| 342 | // derived class more than once. |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 343 | Diag(BaseSpecs[idx]->getSourceRange().getBegin(), |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 344 | diag::err_duplicate_base_class, |
| 345 | KnownBaseTypes[NewBaseType]->getType().getAsString(), |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 346 | BaseSpecs[idx]->getSourceRange()); |
| 347 | |
| 348 | // Delete the duplicate base class specifier; we're going to |
| 349 | // overwrite its pointer later. |
| 350 | delete BaseSpecs[idx]; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 351 | } else { |
| 352 | // Okay, add this new base class. |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 353 | KnownBaseTypes[NewBaseType] = BaseSpecs[idx]; |
| 354 | BaseSpecs[NumGoodBases++] = BaseSpecs[idx]; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | |
| 358 | // Attach the remaining base class specifiers to the derived class. |
| 359 | CXXRecordDecl *Decl = (CXXRecordDecl*)ClassDecl; |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 360 | Decl->setBases(BaseSpecs, NumGoodBases); |
| 361 | |
| 362 | // Delete the remaining (good) base class specifiers, since their |
| 363 | // data has been copied into the CXXRecordDecl. |
| 364 | for (unsigned idx = 0; idx < NumGoodBases; ++idx) |
| 365 | delete BaseSpecs[idx]; |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 366 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 367 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 368 | //===----------------------------------------------------------------------===// |
| 369 | // C++ class member Handling |
| 370 | //===----------------------------------------------------------------------===// |
| 371 | |
| 372 | /// ActOnStartCXXClassDef - This is called at the start of a class/struct/union |
| 373 | /// definition, when on C++. |
| 374 | void Sema::ActOnStartCXXClassDef(Scope *S, DeclTy *D, SourceLocation LBrace) { |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 375 | CXXRecordDecl *Dcl = cast<CXXRecordDecl>(static_cast<Decl *>(D)); |
| 376 | PushDeclContext(Dcl); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 377 | FieldCollector->StartClass(); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 378 | |
| 379 | if (Dcl->getIdentifier()) { |
| 380 | // C++ [class]p2: |
| 381 | // [...] The class-name is also inserted into the scope of the |
| 382 | // class itself; this is known as the injected-class-name. For |
| 383 | // purposes of access checking, the injected-class-name is treated |
| 384 | // as if it were a public member name. |
| 385 | TypedefDecl *InjectedClassName |
| 386 | = TypedefDecl::Create(Context, Dcl, LBrace, Dcl->getIdentifier(), |
| 387 | Context.getTypeDeclType(Dcl), /*PrevDecl=*/0); |
| 388 | PushOnScopeChains(InjectedClassName, S); |
| 389 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | /// ActOnCXXMemberDeclarator - This is invoked when a C++ class member |
| 393 | /// declarator is parsed. 'AS' is the access specifier, 'BW' specifies the |
| 394 | /// bitfield width if there is one and 'InitExpr' specifies the initializer if |
| 395 | /// any. 'LastInGroup' is non-null for cases where one declspec has multiple |
| 396 | /// declarators on it. |
| 397 | /// |
| 398 | /// NOTE: Because of CXXFieldDecl's inability to be chained like ScopedDecls, if |
| 399 | /// an instance field is declared, a new CXXFieldDecl is created but the method |
| 400 | /// does *not* return it; it returns LastInGroup instead. The other C++ members |
| 401 | /// (which are all ScopedDecls) are returned after appending them to |
| 402 | /// LastInGroup. |
| 403 | Sema::DeclTy * |
| 404 | Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D, |
| 405 | ExprTy *BW, ExprTy *InitExpr, |
| 406 | DeclTy *LastInGroup) { |
| 407 | const DeclSpec &DS = D.getDeclSpec(); |
| 408 | IdentifierInfo *II = D.getIdentifier(); |
| 409 | Expr *BitWidth = static_cast<Expr*>(BW); |
| 410 | Expr *Init = static_cast<Expr*>(InitExpr); |
| 411 | SourceLocation Loc = D.getIdentifierLoc(); |
| 412 | |
| 413 | // C++ 9.2p6: A member shall not be declared to have automatic storage |
| 414 | // duration (auto, register) or with the extern storage-class-specifier. |
| 415 | switch (DS.getStorageClassSpec()) { |
| 416 | case DeclSpec::SCS_unspecified: |
| 417 | case DeclSpec::SCS_typedef: |
| 418 | case DeclSpec::SCS_static: |
| 419 | // FALL THROUGH. |
| 420 | break; |
| 421 | default: |
| 422 | if (DS.getStorageClassSpecLoc().isValid()) |
| 423 | Diag(DS.getStorageClassSpecLoc(), |
| 424 | diag::err_storageclass_invalid_for_member); |
| 425 | else |
| 426 | Diag(DS.getThreadSpecLoc(), diag::err_storageclass_invalid_for_member); |
| 427 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 428 | } |
| 429 | |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 430 | bool isFunc = D.isFunctionDeclarator(); |
Argyrios Kyrtzidis | d6caa9e | 2008-10-15 20:23:22 +0000 | [diff] [blame] | 431 | if (!isFunc && |
| 432 | D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_typedef && |
| 433 | D.getNumTypeObjects() == 0) { |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 434 | // Check also for this case: |
| 435 | // |
| 436 | // typedef int f(); |
| 437 | // f a; |
| 438 | // |
| 439 | Decl *TD = static_cast<Decl *>(DS.getTypeRep()); |
| 440 | isFunc = Context.getTypeDeclType(cast<TypeDecl>(TD))->isFunctionType(); |
| 441 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 442 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 443 | bool isInstField = (DS.getStorageClassSpec() == DeclSpec::SCS_unspecified && |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 444 | !isFunc); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 445 | |
| 446 | Decl *Member; |
| 447 | bool InvalidDecl = false; |
| 448 | |
| 449 | if (isInstField) |
| 450 | Member = static_cast<Decl*>(ActOnField(S, Loc, D, BitWidth)); |
| 451 | else |
Daniel Dunbar | 914701e | 2008-08-05 16:28:08 +0000 | [diff] [blame] | 452 | Member = static_cast<Decl*>(ActOnDeclarator(S, D, LastInGroup)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 453 | |
| 454 | if (!Member) return LastInGroup; |
| 455 | |
Sanjiv Gupta | 31fc07d | 2008-10-31 09:52:39 +0000 | [diff] [blame] | 456 | assert((II || isInstField) && "No identifier for non-field ?"); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 457 | |
| 458 | // set/getAccess is not part of Decl's interface to avoid bloating it with C++ |
| 459 | // specific methods. Use a wrapper class that can be used with all C++ class |
| 460 | // member decls. |
| 461 | CXXClassMemberWrapper(Member).setAccess(AS); |
| 462 | |
| 463 | if (BitWidth) { |
| 464 | // C++ 9.6p2: Only when declaring an unnamed bit-field may the |
| 465 | // constant-expression be a value equal to zero. |
| 466 | // FIXME: Check this. |
| 467 | |
| 468 | if (D.isFunctionDeclarator()) { |
| 469 | // FIXME: Emit diagnostic about only constructors taking base initializers |
| 470 | // or something similar, when constructor support is in place. |
| 471 | Diag(Loc, diag::err_not_bitfield_type, |
| 472 | II->getName(), BitWidth->getSourceRange()); |
| 473 | InvalidDecl = true; |
| 474 | |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 475 | } else if (isInstField) { |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 476 | // C++ 9.6p3: A bit-field shall have integral or enumeration type. |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 477 | if (!cast<FieldDecl>(Member)->getType()->isIntegralType()) { |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 478 | Diag(Loc, diag::err_not_integral_type_bitfield, |
| 479 | II->getName(), BitWidth->getSourceRange()); |
| 480 | InvalidDecl = true; |
| 481 | } |
| 482 | |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 483 | } else if (isa<FunctionDecl>(Member)) { |
| 484 | // A function typedef ("typedef int f(); f a;"). |
| 485 | // C++ 9.6p3: A bit-field shall have integral or enumeration type. |
| 486 | Diag(Loc, diag::err_not_integral_type_bitfield, |
| 487 | II->getName(), BitWidth->getSourceRange()); |
| 488 | InvalidDecl = true; |
| 489 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 490 | } else if (isa<TypedefDecl>(Member)) { |
| 491 | // "cannot declare 'A' to be a bit-field type" |
| 492 | Diag(Loc, diag::err_not_bitfield_type, II->getName(), |
| 493 | BitWidth->getSourceRange()); |
| 494 | InvalidDecl = true; |
| 495 | |
| 496 | } else { |
| 497 | assert(isa<CXXClassVarDecl>(Member) && |
| 498 | "Didn't we cover all member kinds?"); |
| 499 | // C++ 9.6p3: A bit-field shall not be a static member. |
| 500 | // "static member 'A' cannot be a bit-field" |
| 501 | Diag(Loc, diag::err_static_not_bitfield, II->getName(), |
| 502 | BitWidth->getSourceRange()); |
| 503 | InvalidDecl = true; |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | if (Init) { |
| 508 | // C++ 9.2p4: A member-declarator can contain a constant-initializer only |
| 509 | // if it declares a static member of const integral or const enumeration |
| 510 | // type. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 511 | if (CXXClassVarDecl *CVD = dyn_cast<CXXClassVarDecl>(Member)) { |
| 512 | // ...static member of... |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 513 | CVD->setInit(Init); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 514 | // ...const integral or const enumeration type. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 515 | if (Context.getCanonicalType(CVD->getType()).isConstQualified() && |
| 516 | CVD->getType()->isIntegralType()) { |
| 517 | // constant-initializer |
| 518 | if (CheckForConstantInitializer(Init, CVD->getType())) |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 519 | InvalidDecl = true; |
| 520 | |
| 521 | } else { |
| 522 | // not const integral. |
| 523 | Diag(Loc, diag::err_member_initialization, |
| 524 | II->getName(), Init->getSourceRange()); |
| 525 | InvalidDecl = true; |
| 526 | } |
| 527 | |
| 528 | } else { |
| 529 | // not static member. |
| 530 | Diag(Loc, diag::err_member_initialization, |
| 531 | II->getName(), Init->getSourceRange()); |
| 532 | InvalidDecl = true; |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | if (InvalidDecl) |
| 537 | Member->setInvalidDecl(); |
| 538 | |
| 539 | if (isInstField) { |
| 540 | FieldCollector->Add(cast<CXXFieldDecl>(Member)); |
| 541 | return LastInGroup; |
| 542 | } |
| 543 | return Member; |
| 544 | } |
| 545 | |
| 546 | void Sema::ActOnFinishCXXMemberSpecification(Scope* S, SourceLocation RLoc, |
| 547 | DeclTy *TagDecl, |
| 548 | SourceLocation LBrac, |
| 549 | SourceLocation RBrac) { |
| 550 | ActOnFields(S, RLoc, TagDecl, |
| 551 | (DeclTy**)FieldCollector->getCurFields(), |
Daniel Dunbar | 1bfe1c2 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 552 | FieldCollector->getCurNumFields(), LBrac, RBrac, 0); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 553 | } |
| 554 | |
Argyrios Kyrtzidis | 5b7f0c8 | 2008-08-09 00:39:29 +0000 | [diff] [blame] | 555 | void Sema::ActOnFinishCXXClassDef(DeclTy *D) { |
Argyrios Kyrtzidis | a4755c6 | 2008-08-09 00:58:37 +0000 | [diff] [blame] | 556 | CXXRecordDecl *Rec = cast<CXXRecordDecl>(static_cast<Decl *>(D)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 557 | FieldCollector->FinishClass(); |
| 558 | PopDeclContext(); |
Argyrios Kyrtzidis | a4755c6 | 2008-08-09 00:58:37 +0000 | [diff] [blame] | 559 | |
| 560 | // Everything, including inline method definitions, have been parsed. |
| 561 | // Let the consumer know of the new TagDecl definition. |
| 562 | Consumer.HandleTagDeclDefinition(Rec); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 563 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 564 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 565 | /// ActOnConstructorDeclarator - Called by ActOnDeclarator to complete |
| 566 | /// the declaration of the given C++ constructor ConDecl that was |
| 567 | /// built from declarator D. This routine is responsible for checking |
| 568 | /// that the newly-created constructor declaration is well-formed and |
| 569 | /// for recording it in the C++ class. Example: |
| 570 | /// |
| 571 | /// @code |
| 572 | /// class X { |
| 573 | /// X(); // X::X() will be the ConDecl. |
| 574 | /// }; |
| 575 | /// @endcode |
| 576 | Sema::DeclTy *Sema::ActOnConstructorDeclarator(CXXConstructorDecl *ConDecl) { |
| 577 | assert(ConDecl && "Expected to receive a constructor declaration"); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 578 | |
| 579 | // Check default arguments on the constructor |
| 580 | CheckCXXDefaultArguments(ConDecl); |
| 581 | |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame^] | 582 | CXXRecordDecl *ClassDecl = dyn_cast_or_null<CXXRecordDecl>(CurContext); |
| 583 | if (!ClassDecl) { |
| 584 | ConDecl->setInvalidDecl(); |
| 585 | return ConDecl; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 586 | } |
| 587 | |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame^] | 588 | // Make sure this constructor is an overload of the existing |
| 589 | // constructors. |
| 590 | OverloadedFunctionDecl::function_iterator MatchedDecl; |
| 591 | if (!IsOverload(ConDecl, ClassDecl->getConstructors(), MatchedDecl)) { |
| 592 | Diag(ConDecl->getLocation(), |
| 593 | diag::err_constructor_redeclared, |
| 594 | SourceRange(ConDecl->getLocation())); |
| 595 | Diag((*MatchedDecl)->getLocation(), |
| 596 | diag::err_previous_declaration, |
| 597 | SourceRange((*MatchedDecl)->getLocation())); |
| 598 | ConDecl->setInvalidDecl(); |
| 599 | return ConDecl; |
| 600 | } |
| 601 | |
| 602 | |
| 603 | // C++ [class.copy]p3: |
| 604 | // A declaration of a constructor for a class X is ill-formed if |
| 605 | // its first parameter is of type (optionally cv-qualified) X and |
| 606 | // either there are no other parameters or else all other |
| 607 | // parameters have default arguments. |
| 608 | if ((ConDecl->getNumParams() == 1) || |
| 609 | (ConDecl->getNumParams() > 1 && |
| 610 | ConDecl->getParamDecl(1)->getDefaultArg() != 0)) { |
| 611 | QualType ParamType = ConDecl->getParamDecl(0)->getType(); |
| 612 | QualType ClassTy = Context.getTagDeclType( |
| 613 | const_cast<CXXRecordDecl*>(ConDecl->getParent())); |
| 614 | if (Context.getCanonicalType(ParamType).getUnqualifiedType() == ClassTy) { |
| 615 | Diag(ConDecl->getLocation(), |
| 616 | diag::err_constructor_byvalue_arg, |
| 617 | SourceRange(ConDecl->getParamDecl(0)->getLocation())); |
| 618 | ConDecl->setInvalidDecl(); |
| 619 | return 0; |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | // Add this constructor to the set of constructors of the current |
| 624 | // class. |
| 625 | ClassDecl->addConstructor(Context, ConDecl); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 626 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 627 | return (DeclTy *)ConDecl; |
| 628 | } |
| 629 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 630 | //===----------------------------------------------------------------------===// |
| 631 | // Namespace Handling |
| 632 | //===----------------------------------------------------------------------===// |
| 633 | |
| 634 | /// ActOnStartNamespaceDef - This is called at the start of a namespace |
| 635 | /// definition. |
| 636 | Sema::DeclTy *Sema::ActOnStartNamespaceDef(Scope *NamespcScope, |
| 637 | SourceLocation IdentLoc, |
| 638 | IdentifierInfo *II, |
| 639 | SourceLocation LBrace) { |
| 640 | NamespaceDecl *Namespc = |
| 641 | NamespaceDecl::Create(Context, CurContext, IdentLoc, II); |
| 642 | Namespc->setLBracLoc(LBrace); |
| 643 | |
| 644 | Scope *DeclRegionScope = NamespcScope->getParent(); |
| 645 | |
| 646 | if (II) { |
| 647 | // C++ [namespace.def]p2: |
| 648 | // The identifier in an original-namespace-definition shall not have been |
| 649 | // previously defined in the declarative region in which the |
| 650 | // original-namespace-definition appears. The identifier in an |
| 651 | // original-namespace-definition is the name of the namespace. Subsequently |
| 652 | // in that declarative region, it is treated as an original-namespace-name. |
| 653 | |
| 654 | Decl *PrevDecl = |
Argyrios Kyrtzidis | 154d8e2 | 2008-10-14 18:28:48 +0000 | [diff] [blame] | 655 | LookupDecl(II, Decl::IDNS_Tag | Decl::IDNS_Ordinary, DeclRegionScope, |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 656 | /*enableLazyBuiltinCreation=*/false); |
| 657 | |
Argyrios Kyrtzidis | 2fac626 | 2008-09-10 02:11:07 +0000 | [diff] [blame] | 658 | if (PrevDecl && isDeclInScope(PrevDecl, CurContext, DeclRegionScope)) { |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 659 | if (NamespaceDecl *OrigNS = dyn_cast<NamespaceDecl>(PrevDecl)) { |
| 660 | // This is an extended namespace definition. |
| 661 | // Attach this namespace decl to the chain of extended namespace |
| 662 | // definitions. |
| 663 | NamespaceDecl *NextNS = OrigNS; |
| 664 | while (NextNS->getNextNamespace()) |
| 665 | NextNS = NextNS->getNextNamespace(); |
| 666 | |
| 667 | NextNS->setNextNamespace(Namespc); |
| 668 | Namespc->setOriginalNamespace(OrigNS); |
| 669 | |
| 670 | // We won't add this decl to the current scope. We want the namespace |
| 671 | // name to return the original namespace decl during a name lookup. |
| 672 | } else { |
| 673 | // This is an invalid name redefinition. |
| 674 | Diag(Namespc->getLocation(), diag::err_redefinition_different_kind, |
| 675 | Namespc->getName()); |
| 676 | Diag(PrevDecl->getLocation(), diag::err_previous_definition); |
| 677 | Namespc->setInvalidDecl(); |
| 678 | // Continue on to push Namespc as current DeclContext and return it. |
| 679 | } |
| 680 | } else { |
| 681 | // This namespace name is declared for the first time. |
| 682 | PushOnScopeChains(Namespc, DeclRegionScope); |
| 683 | } |
| 684 | } |
| 685 | else { |
| 686 | // FIXME: Handle anonymous namespaces |
| 687 | } |
| 688 | |
| 689 | // Although we could have an invalid decl (i.e. the namespace name is a |
| 690 | // redefinition), push it as current DeclContext and try to continue parsing. |
| 691 | PushDeclContext(Namespc->getOriginalNamespace()); |
| 692 | return Namespc; |
| 693 | } |
| 694 | |
| 695 | /// ActOnFinishNamespaceDef - This callback is called after a namespace is |
| 696 | /// exited. Decl is the DeclTy returned by ActOnStartNamespaceDef. |
| 697 | void Sema::ActOnFinishNamespaceDef(DeclTy *D, SourceLocation RBrace) { |
| 698 | Decl *Dcl = static_cast<Decl *>(D); |
| 699 | NamespaceDecl *Namespc = dyn_cast_or_null<NamespaceDecl>(Dcl); |
| 700 | assert(Namespc && "Invalid parameter, expected NamespaceDecl"); |
| 701 | Namespc->setRBracLoc(RBrace); |
| 702 | PopDeclContext(); |
| 703 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 704 | |
| 705 | |
| 706 | /// AddCXXDirectInitializerToDecl - This action is called immediately after |
| 707 | /// ActOnDeclarator, when a C++ direct initializer is present. |
| 708 | /// e.g: "int x(1);" |
| 709 | void Sema::AddCXXDirectInitializerToDecl(DeclTy *Dcl, SourceLocation LParenLoc, |
| 710 | ExprTy **ExprTys, unsigned NumExprs, |
| 711 | SourceLocation *CommaLocs, |
| 712 | SourceLocation RParenLoc) { |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 713 | assert(NumExprs != 0 && ExprTys && "missing expressions"); |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 714 | Decl *RealDecl = static_cast<Decl *>(Dcl); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 715 | |
| 716 | // If there is no declaration, there was an error parsing it. Just ignore |
| 717 | // the initializer. |
| 718 | if (RealDecl == 0) { |
Ted Kremenek | 15f6139 | 2008-10-06 20:35:04 +0000 | [diff] [blame] | 719 | for (unsigned i = 0; i != NumExprs; ++i) |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 720 | delete static_cast<Expr *>(ExprTys[i]); |
| 721 | return; |
| 722 | } |
| 723 | |
| 724 | VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl); |
| 725 | if (!VDecl) { |
| 726 | Diag(RealDecl->getLocation(), diag::err_illegal_initializer); |
| 727 | RealDecl->setInvalidDecl(); |
| 728 | return; |
| 729 | } |
| 730 | |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 731 | // We will treat direct-initialization as a copy-initialization: |
| 732 | // int x(1); -as-> int x = 1; |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 733 | // ClassType x(a,b,c); -as-> ClassType x = ClassType(a,b,c); |
| 734 | // |
| 735 | // Clients that want to distinguish between the two forms, can check for |
| 736 | // direct initializer using VarDecl::hasCXXDirectInitializer(). |
| 737 | // A major benefit is that clients that don't particularly care about which |
| 738 | // exactly form was it (like the CodeGen) can handle both cases without |
| 739 | // special case code. |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 740 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 741 | // C++ 8.5p11: |
| 742 | // The form of initialization (using parentheses or '=') is generally |
| 743 | // insignificant, but does matter when the entity being initialized has a |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 744 | // class type. |
| 745 | |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 746 | if (VDecl->getType()->isRecordType()) { |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 747 | // FIXME: When constructors for class types are supported, determine how |
| 748 | // exactly semantic checking will be done for direct initializers. |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 749 | unsigned DiagID = PP.getDiagnostics().getCustomDiagID(Diagnostic::Error, |
| 750 | "initialization for class types is not handled yet"); |
| 751 | Diag(VDecl->getLocation(), DiagID); |
| 752 | RealDecl->setInvalidDecl(); |
| 753 | return; |
| 754 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 755 | |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 756 | if (NumExprs > 1) { |
| 757 | Diag(CommaLocs[0], diag::err_builtin_direct_init_more_than_one_arg, |
| 758 | SourceRange(VDecl->getLocation(), RParenLoc)); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 759 | RealDecl->setInvalidDecl(); |
| 760 | return; |
| 761 | } |
| 762 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 763 | // Let clients know that initialization was done with a direct initializer. |
| 764 | VDecl->setCXXDirectInitializer(true); |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 765 | |
| 766 | assert(NumExprs == 1 && "Expected 1 expression"); |
| 767 | // Set the init expression, handles conversions. |
| 768 | AddInitializerToDecl(Dcl, ExprTys[0]); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 769 | } |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 770 | |
| 771 | /// CompareReferenceRelationship - Compare the two types T1 and T2 to |
| 772 | /// determine whether they are reference-related, |
| 773 | /// reference-compatible, reference-compatible with added |
| 774 | /// qualification, or incompatible, for use in C++ initialization by |
| 775 | /// reference (C++ [dcl.ref.init]p4). Neither type can be a reference |
| 776 | /// type, and the first type (T1) is the pointee type of the reference |
| 777 | /// type being initialized. |
| 778 | Sema::ReferenceCompareResult |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 779 | Sema::CompareReferenceRelationship(QualType T1, QualType T2, |
| 780 | bool& DerivedToBase) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 781 | assert(!T1->isReferenceType() && "T1 must be the pointee type of the reference type"); |
| 782 | assert(!T2->isReferenceType() && "T2 cannot be a reference type"); |
| 783 | |
| 784 | T1 = Context.getCanonicalType(T1); |
| 785 | T2 = Context.getCanonicalType(T2); |
| 786 | QualType UnqualT1 = T1.getUnqualifiedType(); |
| 787 | QualType UnqualT2 = T2.getUnqualifiedType(); |
| 788 | |
| 789 | // C++ [dcl.init.ref]p4: |
| 790 | // Given types “cv1 T1” and “cv2 T2,” “cv1 T1” is |
| 791 | // reference-related to “cv2 T2” if T1 is the same type as T2, or |
| 792 | // T1 is a base class of T2. |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 793 | if (UnqualT1 == UnqualT2) |
| 794 | DerivedToBase = false; |
| 795 | else if (IsDerivedFrom(UnqualT2, UnqualT1)) |
| 796 | DerivedToBase = true; |
| 797 | else |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 798 | return Ref_Incompatible; |
| 799 | |
| 800 | // At this point, we know that T1 and T2 are reference-related (at |
| 801 | // least). |
| 802 | |
| 803 | // C++ [dcl.init.ref]p4: |
| 804 | // "cv1 T1” is reference-compatible with “cv2 T2” if T1 is |
| 805 | // reference-related to T2 and cv1 is the same cv-qualification |
| 806 | // as, or greater cv-qualification than, cv2. For purposes of |
| 807 | // overload resolution, cases for which cv1 is greater |
| 808 | // cv-qualification than cv2 are identified as |
| 809 | // reference-compatible with added qualification (see 13.3.3.2). |
| 810 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 811 | return Ref_Compatible; |
| 812 | else if (T1.isMoreQualifiedThan(T2)) |
| 813 | return Ref_Compatible_With_Added_Qualification; |
| 814 | else |
| 815 | return Ref_Related; |
| 816 | } |
| 817 | |
| 818 | /// CheckReferenceInit - Check the initialization of a reference |
| 819 | /// variable with the given initializer (C++ [dcl.init.ref]). Init is |
| 820 | /// the initializer (either a simple initializer or an initializer |
Douglas Gregor | 3205a78 | 2008-10-29 23:31:03 +0000 | [diff] [blame] | 821 | /// list), and DeclType is the type of the declaration. When ICS is |
| 822 | /// non-null, this routine will compute the implicit conversion |
| 823 | /// sequence according to C++ [over.ics.ref] and will not produce any |
| 824 | /// diagnostics; when ICS is null, it will emit diagnostics when any |
| 825 | /// errors are found. Either way, a return value of true indicates |
| 826 | /// that there was a failure, a return value of false indicates that |
| 827 | /// the reference initialization succeeded. |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 828 | bool |
| 829 | Sema::CheckReferenceInit(Expr *&Init, QualType &DeclType, |
| 830 | ImplicitConversionSequence *ICS) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 831 | assert(DeclType->isReferenceType() && "Reference init needs a reference"); |
| 832 | |
| 833 | QualType T1 = DeclType->getAsReferenceType()->getPointeeType(); |
| 834 | QualType T2 = Init->getType(); |
| 835 | |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 836 | // Compute some basic properties of the types and the initializer. |
| 837 | bool DerivedToBase = false; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 838 | Expr::isLvalueResult InitLvalue = Init->isLvalue(Context); |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 839 | ReferenceCompareResult RefRelationship |
| 840 | = CompareReferenceRelationship(T1, T2, DerivedToBase); |
| 841 | |
| 842 | // Most paths end in a failed conversion. |
| 843 | if (ICS) |
| 844 | ICS->ConversionKind = ImplicitConversionSequence::BadConversion; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 845 | |
| 846 | // C++ [dcl.init.ref]p5: |
| 847 | // A reference to type “cv1 T1” is initialized by an expression |
| 848 | // of type “cv2 T2” as follows: |
| 849 | |
| 850 | // -- If the initializer expression |
| 851 | |
| 852 | bool BindsDirectly = false; |
| 853 | // -- is an lvalue (but is not a bit-field), and “cv1 T1” is |
| 854 | // reference-compatible with “cv2 T2,” or |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 855 | // |
| 856 | // Note that the bit-field check is skipped if we are just computing |
| 857 | // the implicit conversion sequence (C++ [over.best.ics]p2). |
| 858 | if (InitLvalue == Expr::LV_Valid && (ICS || !Init->isBitField()) && |
| 859 | RefRelationship >= Ref_Compatible_With_Added_Qualification) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 860 | BindsDirectly = true; |
| 861 | |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 862 | if (ICS) { |
| 863 | // C++ [over.ics.ref]p1: |
| 864 | // When a parameter of reference type binds directly (8.5.3) |
| 865 | // to an argument expression, the implicit conversion sequence |
| 866 | // is the identity conversion, unless the argument expression |
| 867 | // has a type that is a derived class of the parameter type, |
| 868 | // in which case the implicit conversion sequence is a |
| 869 | // derived-to-base Conversion (13.3.3.1). |
| 870 | ICS->ConversionKind = ImplicitConversionSequence::StandardConversion; |
| 871 | ICS->Standard.First = ICK_Identity; |
| 872 | ICS->Standard.Second = DerivedToBase? ICK_Derived_To_Base : ICK_Identity; |
| 873 | ICS->Standard.Third = ICK_Identity; |
| 874 | ICS->Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 875 | ICS->Standard.ToTypePtr = T1.getAsOpaquePtr(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 876 | ICS->Standard.ReferenceBinding = true; |
| 877 | ICS->Standard.DirectBinding = true; |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 878 | |
| 879 | // Nothing more to do: the inaccessibility/ambiguity check for |
| 880 | // derived-to-base conversions is suppressed when we're |
| 881 | // computing the implicit conversion sequence (C++ |
| 882 | // [over.best.ics]p2). |
| 883 | return false; |
| 884 | } else { |
| 885 | // Perform the conversion. |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 886 | // FIXME: Binding to a subobject of the lvalue is going to require |
| 887 | // more AST annotation than this. |
| 888 | ImpCastExprToType(Init, T1); |
| 889 | } |
| 890 | } |
| 891 | |
| 892 | // -- has a class type (i.e., T2 is a class type) and can be |
| 893 | // implicitly converted to an lvalue of type “cv3 T3,” |
| 894 | // where “cv1 T1” is reference-compatible with “cv3 T3” |
| 895 | // 92) (this conversion is selected by enumerating the |
| 896 | // applicable conversion functions (13.3.1.6) and choosing |
| 897 | // the best one through overload resolution (13.3)), |
| 898 | // FIXME: Implement this second bullet, once we have conversion |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 899 | // functions. Also remember C++ [over.ics.ref]p1, second part. |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 900 | |
| 901 | if (BindsDirectly) { |
| 902 | // C++ [dcl.init.ref]p4: |
| 903 | // [...] In all cases where the reference-related or |
| 904 | // reference-compatible relationship of two types is used to |
| 905 | // establish the validity of a reference binding, and T1 is a |
| 906 | // base class of T2, a program that necessitates such a binding |
| 907 | // is ill-formed if T1 is an inaccessible (clause 11) or |
| 908 | // ambiguous (10.2) base class of T2. |
| 909 | // |
| 910 | // Note that we only check this condition when we're allowed to |
| 911 | // complain about errors, because we should not be checking for |
| 912 | // ambiguity (or inaccessibility) unless the reference binding |
| 913 | // actually happens. |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 914 | if (DerivedToBase) |
| 915 | return CheckDerivedToBaseConversion(T2, T1, |
| 916 | Init->getSourceRange().getBegin(), |
| 917 | Init->getSourceRange()); |
| 918 | else |
| 919 | return false; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | // -- Otherwise, the reference shall be to a non-volatile const |
| 923 | // type (i.e., cv1 shall be const). |
| 924 | if (T1.getCVRQualifiers() != QualType::Const) { |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 925 | if (!ICS) |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 926 | Diag(Init->getSourceRange().getBegin(), |
| 927 | diag::err_not_reference_to_const_init, |
| 928 | T1.getAsString(), |
| 929 | InitLvalue != Expr::LV_Valid? "temporary" : "value", |
| 930 | T2.getAsString(), Init->getSourceRange()); |
| 931 | return true; |
| 932 | } |
| 933 | |
| 934 | // -- If the initializer expression is an rvalue, with T2 a |
| 935 | // class type, and “cv1 T1” is reference-compatible with |
| 936 | // “cv2 T2,” the reference is bound in one of the |
| 937 | // following ways (the choice is implementation-defined): |
| 938 | // |
| 939 | // -- The reference is bound to the object represented by |
| 940 | // the rvalue (see 3.10) or to a sub-object within that |
| 941 | // object. |
| 942 | // |
| 943 | // -- A temporary of type “cv1 T2” [sic] is created, and |
| 944 | // a constructor is called to copy the entire rvalue |
| 945 | // object into the temporary. The reference is bound to |
| 946 | // the temporary or to a sub-object within the |
| 947 | // temporary. |
| 948 | // |
| 949 | // |
| 950 | // The constructor that would be used to make the copy |
| 951 | // shall be callable whether or not the copy is actually |
| 952 | // done. |
| 953 | // |
| 954 | // Note that C++0x [dcl.ref.init]p5 takes away this implementation |
| 955 | // freedom, so we will always take the first option and never build |
| 956 | // a temporary in this case. FIXME: We will, however, have to check |
| 957 | // for the presence of a copy constructor in C++98/03 mode. |
| 958 | if (InitLvalue != Expr::LV_Valid && T2->isRecordType() && |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 959 | RefRelationship >= Ref_Compatible_With_Added_Qualification) { |
| 960 | if (ICS) { |
| 961 | ICS->ConversionKind = ImplicitConversionSequence::StandardConversion; |
| 962 | ICS->Standard.First = ICK_Identity; |
| 963 | ICS->Standard.Second = DerivedToBase? ICK_Derived_To_Base : ICK_Identity; |
| 964 | ICS->Standard.Third = ICK_Identity; |
| 965 | ICS->Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 966 | ICS->Standard.ToTypePtr = T1.getAsOpaquePtr(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 967 | ICS->Standard.ReferenceBinding = true; |
| 968 | ICS->Standard.DirectBinding = false; |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 969 | } else { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 970 | // FIXME: Binding to a subobject of the rvalue is going to require |
| 971 | // more AST annotation than this. |
| 972 | ImpCastExprToType(Init, T1); |
| 973 | } |
| 974 | return false; |
| 975 | } |
| 976 | |
| 977 | // -- Otherwise, a temporary of type “cv1 T1” is created and |
| 978 | // initialized from the initializer expression using the |
| 979 | // rules for a non-reference copy initialization (8.5). The |
| 980 | // reference is then bound to the temporary. If T1 is |
| 981 | // reference-related to T2, cv1 must be the same |
| 982 | // cv-qualification as, or greater cv-qualification than, |
| 983 | // cv2; otherwise, the program is ill-formed. |
| 984 | if (RefRelationship == Ref_Related) { |
| 985 | // If cv1 == cv2 or cv1 is a greater cv-qualified than cv2, then |
| 986 | // we would be reference-compatible or reference-compatible with |
| 987 | // added qualification. But that wasn't the case, so the reference |
| 988 | // initialization fails. |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 989 | if (!ICS) |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 990 | Diag(Init->getSourceRange().getBegin(), |
| 991 | diag::err_reference_init_drops_quals, |
| 992 | T1.getAsString(), |
| 993 | InitLvalue != Expr::LV_Valid? "temporary" : "value", |
| 994 | T2.getAsString(), Init->getSourceRange()); |
| 995 | return true; |
| 996 | } |
| 997 | |
| 998 | // Actually try to convert the initializer to T1. |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 999 | if (ICS) { |
| 1000 | /// C++ [over.ics.ref]p2: |
| 1001 | /// |
| 1002 | /// When a parameter of reference type is not bound directly to |
| 1003 | /// an argument expression, the conversion sequence is the one |
| 1004 | /// required to convert the argument expression to the |
| 1005 | /// underlying type of the reference according to |
| 1006 | /// 13.3.3.1. Conceptually, this conversion sequence corresponds |
| 1007 | /// to copy-initializing a temporary of the underlying type with |
| 1008 | /// the argument expression. Any difference in top-level |
| 1009 | /// cv-qualification is subsumed by the initialization itself |
| 1010 | /// and does not constitute a conversion. |
| 1011 | *ICS = TryImplicitConversion(Init, T1); |
| 1012 | return ICS->ConversionKind == ImplicitConversionSequence::BadConversion; |
| 1013 | } else { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1014 | return PerformImplicitConversion(Init, T1); |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1015 | } |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1016 | } |