SemaDeclCXX.cpp revision 248a753f6b670692523c99afaeb8fe98f7ae3ca7
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" 15#include "clang/Basic/LangOptions.h" 16#include "clang/AST/ASTContext.h" 17#include "clang/AST/Expr.h" 18#include "clang/AST/StmtVisitor.h" 19#include "clang/AST/Type.h" 20#include "llvm/ADT/OwningPtr.h" 21#include "llvm/Support/Compiler.h" 22 23using namespace clang; 24 25//===----------------------------------------------------------------------===// 26// CheckDefaultArgumentVisitor 27//===----------------------------------------------------------------------===// 28 29namespace { 30 /// CheckDefaultArgumentVisitor - C++ [dcl.fct.default] Traverses 31 /// the default argument of a parameter to determine whether it 32 /// contains any ill-formed subexpressions. For example, this will 33 /// diagnose the use of local variables or parameters within the 34 /// default argument expression. 35 class VISIBILITY_HIDDEN CheckDefaultArgumentVisitor 36 : public StmtVisitor<CheckDefaultArgumentVisitor, bool> 37 { 38 Expr *DefaultArg; 39 Sema *S; 40 41 public: 42 CheckDefaultArgumentVisitor(Expr *defarg, Sema *s) 43 : DefaultArg(defarg), S(s) {} 44 45 bool VisitExpr(Expr *Node); 46 bool VisitDeclRefExpr(DeclRefExpr *DRE); 47 }; 48 49 /// VisitExpr - Visit all of the children of this expression. 50 bool CheckDefaultArgumentVisitor::VisitExpr(Expr *Node) { 51 bool IsInvalid = false; 52 for (Stmt::child_iterator first = Node->child_begin(), 53 last = Node->child_end(); 54 first != last; ++first) 55 IsInvalid |= Visit(*first); 56 57 return IsInvalid; 58 } 59 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) { 64 ValueDecl *Decl = DRE->getDecl(); 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()); 77 } else if (VarDecl *VDecl = dyn_cast<VarDecl>(Decl)) { 78 // C++ [dcl.fct.default]p7 79 // Local variables shall not be used in default argument 80 // expressions. 81 if (VDecl->isBlockVarDecl()) 82 return S->Diag(DRE->getSourceRange().getBegin(), 83 diag::err_param_default_argument_references_local, 84 VDecl->getName(), DefaultArg->getSourceRange()); 85 } 86 87 // FIXME: when Clang has support for member functions, "this" 88 // will also need to be diagnosed. 89 90 return false; 91 } 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. 97void 98Sema::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 136 // FIXME: C++ [dcl.fct.default]p3 137 // A default argument expression shall be specified only in the 138 // parameter-declaration-clause of a function declaration or in a 139 // template-parameter (14.1). It shall not be specified for a 140 // parameter pack. If it is specified in a 141 // parameter-declaration-clause, it shall not occur within a 142 // declarator or abstract-declarator of a parameter-declaration. 143 144 // Check that the default argument is well-formed 145 CheckDefaultArgumentVisitor DefaultArgChecker(DefaultArg.get(), this); 146 if (DefaultArgChecker.Visit(DefaultArg.get())) 147 return; 148 149 // Okay: add the default argument to the parameter 150 Param->setDefaultArg(DefaultArg.take()); 151} 152 153// MergeCXXFunctionDecl - Merge two declarations of the same C++ 154// function, once we already know that they have the same 155// type. Subroutine of MergeFunctionDecl. 156FunctionDecl * 157Sema::MergeCXXFunctionDecl(FunctionDecl *New, FunctionDecl *Old) { 158 // C++ [dcl.fct.default]p4: 159 // 160 // For non-template functions, default arguments can be added in 161 // later declarations of a function in the same 162 // scope. Declarations in different scopes have completely 163 // distinct sets of default arguments. That is, declarations in 164 // inner scopes do not acquire default arguments from 165 // declarations in outer scopes, and vice versa. In a given 166 // function declaration, all parameters subsequent to a 167 // parameter with a default argument shall have default 168 // arguments supplied in this or previous declarations. A 169 // default argument shall not be redefined by a later 170 // declaration (not even to the same value). 171 for (unsigned p = 0, NumParams = Old->getNumParams(); p < NumParams; ++p) { 172 ParmVarDecl *OldParam = Old->getParamDecl(p); 173 ParmVarDecl *NewParam = New->getParamDecl(p); 174 175 if(OldParam->getDefaultArg() && NewParam->getDefaultArg()) { 176 Diag(NewParam->getLocation(), 177 diag::err_param_default_argument_redefinition, 178 NewParam->getDefaultArg()->getSourceRange()); 179 Diag(OldParam->getLocation(), diag::err_previous_definition); 180 } else if (OldParam->getDefaultArg()) { 181 // Merge the old default argument into the new parameter 182 NewParam->setDefaultArg(OldParam->getDefaultArg()); 183 } 184 } 185 186 return New; 187} 188 189/// CheckCXXDefaultArguments - Verify that the default arguments for a 190/// function declaration are well-formed according to C++ 191/// [dcl.fct.default]. 192void Sema::CheckCXXDefaultArguments(FunctionDecl *FD) { 193 unsigned NumParams = FD->getNumParams(); 194 unsigned p; 195 196 // Find first parameter with a default argument 197 for (p = 0; p < NumParams; ++p) { 198 ParmVarDecl *Param = FD->getParamDecl(p); 199 if (Param->getDefaultArg()) 200 break; 201 } 202 203 // C++ [dcl.fct.default]p4: 204 // In a given function declaration, all parameters 205 // subsequent to a parameter with a default argument shall 206 // have default arguments supplied in this or previous 207 // declarations. A default argument shall not be redefined 208 // by a later declaration (not even to the same value). 209 unsigned LastMissingDefaultArg = 0; 210 for(; p < NumParams; ++p) { 211 ParmVarDecl *Param = FD->getParamDecl(p); 212 if (!Param->getDefaultArg()) { 213 if (Param->getIdentifier()) 214 Diag(Param->getLocation(), 215 diag::err_param_default_argument_missing_name, 216 Param->getIdentifier()->getName()); 217 else 218 Diag(Param->getLocation(), 219 diag::err_param_default_argument_missing); 220 221 LastMissingDefaultArg = p; 222 } 223 } 224 225 if (LastMissingDefaultArg > 0) { 226 // Some default arguments were missing. Clear out all of the 227 // default arguments up to (and including) the last missing 228 // default argument, so that we leave the function parameters 229 // in a semantically valid state. 230 for (p = 0; p <= LastMissingDefaultArg; ++p) { 231 ParmVarDecl *Param = FD->getParamDecl(p); 232 if (Param->getDefaultArg()) { 233 delete Param->getDefaultArg(); 234 Param->setDefaultArg(0); 235 } 236 } 237 } 238} 239 240/// ActOnBaseSpecifier - Parsed a base specifier. A base specifier is 241/// one entry in the base class list of a class specifier, for 242/// example: 243/// class foo : public bar, virtual private baz { 244/// 'public bar' and 'virtual private baz' are each base-specifiers. 245void Sema::ActOnBaseSpecifier(DeclTy *classdecl, SourceRange SpecifierRange, 246 bool Virtual, AccessSpecifier Access, 247 DeclTy *basetype, SourceLocation BaseLoc) { 248 RecordDecl *Decl = (RecordDecl*)classdecl; 249 QualType BaseType = Context.getTypeDeclType((TypeDecl*)basetype); 250 251 // Base specifiers must be record types. 252 if (!BaseType->isRecordType()) { 253 Diag(BaseLoc, diag::err_base_must_be_class, SpecifierRange); 254 return; 255 } 256 257 // C++ [class.union]p1: 258 // A union shall not be used as a base class. 259 if (BaseType->isUnionType()) { 260 Diag(BaseLoc, diag::err_union_as_base_class, SpecifierRange); 261 return; 262 } 263 264 // C++ [class.union]p1: 265 // A union shall not have base classes. 266 if (Decl->getKind() == Decl::Union) { 267 Diag(Decl->getLocation(), diag::err_base_clause_on_union, 268 SpecifierRange); 269 Decl->setInvalidDecl(); 270 return; 271 } 272 273 // C++ [class.derived]p2: 274 // The class-name in a base-specifier shall not be an incompletely 275 // defined class. 276 if (BaseType->isIncompleteType()) { 277 Diag(BaseLoc, diag::err_incomplete_base_class, SpecifierRange); 278 return; 279 } 280 281 // FIXME: C++ [class.mi]p3: 282 // A class shall not be specified as a direct base class of a 283 // derived class more than once. 284 285 // FIXME: Attach base class to the record. 286} 287