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