Chris Lattner | ac7b83a | 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" |
| 15 | #include "clang/Basic/LangOptions.h" |
| 16 | #include "clang/AST/Expr.h" |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 17 | #include "clang/AST/StmtVisitor.h" |
Chris Lattner | ac7b83a | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 18 | #include "clang/AST/Type.h" |
| 19 | #include "llvm/ADT/OwningPtr.h" |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Compiler.h" |
Chris Lattner | ac7b83a | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace clang; |
| 23 | |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 24 | //===----------------------------------------------------------------------===// |
| 25 | // CheckDefaultArgumentVisitor |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | |
Chris Lattner | b1856db | 2008-04-12 23:52:44 +0000 | [diff] [blame^] | 28 | namespace { |
| 29 | /// CheckDefaultArgumentVisitor - C++ [dcl.fct.default] Traverses |
| 30 | /// the default argument of a parameter to determine whether it |
| 31 | /// contains any ill-formed subexpressions. For example, this will |
| 32 | /// diagnose the use of local variables or parameters within the |
| 33 | /// default argument expression. |
| 34 | class VISIBILITY_HIDDEN CheckDefaultArgumentVisitor |
| 35 | : public StmtVisitor<CheckDefaultArgumentVisitor, bool> |
| 36 | { |
| 37 | Expr *DefaultArg; |
| 38 | Sema *S; |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 39 | |
Chris Lattner | b1856db | 2008-04-12 23:52:44 +0000 | [diff] [blame^] | 40 | public: |
| 41 | CheckDefaultArgumentVisitor(Expr *defarg, Sema *s) |
| 42 | : DefaultArg(defarg), S(s) {} |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 43 | |
Chris Lattner | b1856db | 2008-04-12 23:52:44 +0000 | [diff] [blame^] | 44 | bool VisitExpr(Expr *Node); |
| 45 | bool VisitDeclRefExpr(DeclRefExpr *DRE); |
| 46 | }; |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 47 | |
Chris Lattner | b1856db | 2008-04-12 23:52:44 +0000 | [diff] [blame^] | 48 | /// VisitExpr - Visit all of the children of this expression. |
| 49 | bool CheckDefaultArgumentVisitor::VisitExpr(Expr *Node) { |
| 50 | bool IsInvalid = false; |
| 51 | for (Stmt::child_iterator first = Node->child_begin(), |
| 52 | last = Node->child_end(); |
| 53 | first != last; ++first) |
| 54 | IsInvalid |= Visit(*first); |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 55 | |
Chris Lattner | b1856db | 2008-04-12 23:52:44 +0000 | [diff] [blame^] | 56 | return IsInvalid; |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Chris Lattner | b1856db | 2008-04-12 23:52:44 +0000 | [diff] [blame^] | 59 | /// VisitDeclRefExpr - Visit a reference to a declaration, to |
| 60 | /// determine whether this declaration can be used in the default |
| 61 | /// argument expression. |
| 62 | bool CheckDefaultArgumentVisitor::VisitDeclRefExpr(DeclRefExpr *DRE) { |
| 63 | ValueDecl *Decl = DRE->getDecl(); |
| 64 | if (ParmVarDecl *Param = dyn_cast<ParmVarDecl>(Decl)) { |
| 65 | // C++ [dcl.fct.default]p9 |
| 66 | // Default arguments are evaluated each time the function is |
| 67 | // called. The order of evaluation of function arguments is |
| 68 | // unspecified. Consequently, parameters of a function shall not |
| 69 | // be used in default argument expressions, even if they are not |
| 70 | // evaluated. Parameters of a function declared before a default |
| 71 | // argument expression are in scope and can hide namespace and |
| 72 | // class member names. |
| 73 | return S->Diag(DRE->getSourceRange().getBegin(), |
| 74 | diag::err_param_default_argument_references_param, |
| 75 | Param->getName(), DefaultArg->getSourceRange()); |
| 76 | } else if (BlockVarDecl *BlockVar = dyn_cast<BlockVarDecl>(Decl)) { |
| 77 | // C++ [dcl.fct.default]p7 |
| 78 | // Local variables shall not be used in default argument |
| 79 | // expressions. |
| 80 | return S->Diag(DRE->getSourceRange().getBegin(), |
| 81 | diag::err_param_default_argument_references_local, |
| 82 | BlockVar->getName(), DefaultArg->getSourceRange()); |
| 83 | } |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 84 | |
Chris Lattner | b1856db | 2008-04-12 23:52:44 +0000 | [diff] [blame^] | 85 | // FIXME: when Clang has support for member functions, "this" |
| 86 | // will also need to be diagnosed. |
| 87 | |
| 88 | return false; |
| 89 | } |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /// ActOnParamDefaultArgument - Check whether the default argument |
| 93 | /// provided for a function parameter is well-formed. If so, attach it |
| 94 | /// to the parameter declaration. |
Chris Lattner | ac7b83a | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 95 | void |
| 96 | Sema::ActOnParamDefaultArgument(DeclTy *param, SourceLocation EqualLoc, |
| 97 | ExprTy *defarg) { |
| 98 | ParmVarDecl *Param = (ParmVarDecl *)param; |
| 99 | llvm::OwningPtr<Expr> DefaultArg((Expr *)defarg); |
| 100 | QualType ParamType = Param->getType(); |
| 101 | |
| 102 | // Default arguments are only permitted in C++ |
| 103 | if (!getLangOptions().CPlusPlus) { |
| 104 | Diag(EqualLoc, diag::err_param_default_argument, |
| 105 | DefaultArg->getSourceRange()); |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | // C++ [dcl.fct.default]p5 |
| 110 | // A default argument expression is implicitly converted (clause |
| 111 | // 4) to the parameter type. The default argument expression has |
| 112 | // the same semantic constraints as the initializer expression in |
| 113 | // a declaration of a variable of the parameter type, using the |
| 114 | // copy-initialization semantics (8.5). |
| 115 | // |
| 116 | // FIXME: CheckSingleAssignmentConstraints has the wrong semantics |
| 117 | // for C++ (since we want copy-initialization, not copy-assignment), |
| 118 | // but we don't have the right semantics implemented yet. Because of |
| 119 | // this, our error message is also very poor. |
| 120 | QualType DefaultArgType = DefaultArg->getType(); |
| 121 | Expr *DefaultArgPtr = DefaultArg.get(); |
| 122 | AssignConvertType ConvTy = CheckSingleAssignmentConstraints(ParamType, |
| 123 | DefaultArgPtr); |
| 124 | if (DefaultArgPtr != DefaultArg.get()) { |
| 125 | DefaultArg.take(); |
| 126 | DefaultArg.reset(DefaultArgPtr); |
| 127 | } |
| 128 | if (DiagnoseAssignmentResult(ConvTy, DefaultArg->getLocStart(), |
| 129 | ParamType, DefaultArgType, DefaultArg.get(), |
| 130 | "in default argument")) { |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | // FIXME: C++ [dcl.fct.default]p3 |
| 135 | // A default argument expression shall be specified only in the |
| 136 | // parameter-declaration-clause of a function declaration or in a |
| 137 | // template-parameter (14.1). It shall not be specified for a |
| 138 | // parameter pack. If it is specified in a |
| 139 | // parameter-declaration-clause, it shall not occur within a |
| 140 | // declarator or abstract-declarator of a parameter-declaration. |
| 141 | |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 142 | // Check that the default argument is well-formed |
Chris Lattner | b1856db | 2008-04-12 23:52:44 +0000 | [diff] [blame^] | 143 | CheckDefaultArgumentVisitor DefaultArgChecker(DefaultArg.get(), this); |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 144 | if (DefaultArgChecker.Visit(DefaultArg.get())) |
| 145 | return; |
| 146 | |
Chris Lattner | ac7b83a | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 147 | // Okay: add the default argument to the parameter |
| 148 | Param->setDefaultArg(DefaultArg.take()); |
| 149 | } |
| 150 | |
| 151 | // MergeCXXFunctionDecl - Merge two declarations of the same C++ |
| 152 | // function, once we already know that they have the same |
| 153 | // type. Subroutine of MergeFunctionDecl. |
| 154 | FunctionDecl * |
| 155 | Sema::MergeCXXFunctionDecl(FunctionDecl *New, FunctionDecl *Old) { |
| 156 | // C++ [dcl.fct.default]p4: |
| 157 | // |
| 158 | // For non-template functions, default arguments can be added in |
| 159 | // later declarations of a function in the same |
| 160 | // scope. Declarations in different scopes have completely |
| 161 | // distinct sets of default arguments. That is, declarations in |
| 162 | // inner scopes do not acquire default arguments from |
| 163 | // declarations in outer scopes, and vice versa. In a given |
| 164 | // function declaration, all parameters subsequent to a |
| 165 | // parameter with a default argument shall have default |
| 166 | // arguments supplied in this or previous declarations. A |
| 167 | // default argument shall not be redefined by a later |
| 168 | // declaration (not even to the same value). |
| 169 | for (unsigned p = 0, NumParams = Old->getNumParams(); p < NumParams; ++p) { |
| 170 | ParmVarDecl *OldParam = Old->getParamDecl(p); |
| 171 | ParmVarDecl *NewParam = New->getParamDecl(p); |
| 172 | |
| 173 | if(OldParam->getDefaultArg() && NewParam->getDefaultArg()) { |
| 174 | Diag(NewParam->getLocation(), |
| 175 | diag::err_param_default_argument_redefinition, |
| 176 | NewParam->getDefaultArg()->getSourceRange()); |
| 177 | Diag(OldParam->getLocation(), diag::err_previous_definition); |
| 178 | } else if (OldParam->getDefaultArg()) { |
| 179 | // Merge the old default argument into the new parameter |
| 180 | NewParam->setDefaultArg(OldParam->getDefaultArg()); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | return New; |
| 185 | } |
| 186 | |
| 187 | /// CheckCXXDefaultArguments - Verify that the default arguments for a |
| 188 | /// function declaration are well-formed according to C++ |
| 189 | /// [dcl.fct.default]. |
| 190 | void Sema::CheckCXXDefaultArguments(FunctionDecl *FD) { |
| 191 | unsigned NumParams = FD->getNumParams(); |
| 192 | unsigned p; |
| 193 | |
| 194 | // Find first parameter with a default argument |
| 195 | for (p = 0; p < NumParams; ++p) { |
| 196 | ParmVarDecl *Param = FD->getParamDecl(p); |
| 197 | if (Param->getDefaultArg()) |
| 198 | break; |
| 199 | } |
| 200 | |
| 201 | // C++ [dcl.fct.default]p4: |
| 202 | // In a given function declaration, all parameters |
| 203 | // subsequent to a parameter with a default argument shall |
| 204 | // have default arguments supplied in this or previous |
| 205 | // declarations. A default argument shall not be redefined |
| 206 | // by a later declaration (not even to the same value). |
| 207 | unsigned LastMissingDefaultArg = 0; |
| 208 | for(; p < NumParams; ++p) { |
| 209 | ParmVarDecl *Param = FD->getParamDecl(p); |
| 210 | if (!Param->getDefaultArg()) { |
| 211 | if (Param->getIdentifier()) |
| 212 | Diag(Param->getLocation(), |
| 213 | diag::err_param_default_argument_missing_name, |
| 214 | Param->getIdentifier()->getName()); |
| 215 | else |
| 216 | Diag(Param->getLocation(), |
| 217 | diag::err_param_default_argument_missing); |
| 218 | |
| 219 | LastMissingDefaultArg = p; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | if (LastMissingDefaultArg > 0) { |
| 224 | // Some default arguments were missing. Clear out all of the |
| 225 | // default arguments up to (and including) the last missing |
| 226 | // default argument, so that we leave the function parameters |
| 227 | // in a semantically valid state. |
| 228 | for (p = 0; p <= LastMissingDefaultArg; ++p) { |
| 229 | ParmVarDecl *Param = FD->getParamDecl(p); |
| 230 | if (Param->getDefaultArg()) { |
| 231 | delete Param->getDefaultArg(); |
| 232 | Param->setDefaultArg(0); |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | } |