blob: 7202d2f2f10ed94add0ab1d27c2df55937b04815 [file] [log] [blame]
Chris Lattner3d1cee32008-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"
Douglas Gregore37ac4f2008-04-13 21:30:24 +000016#include "clang/AST/ASTContext.h"
Chris Lattner3d1cee32008-04-08 05:04:30 +000017#include "clang/AST/Expr.h"
Chris Lattner8123a952008-04-10 02:22:51 +000018#include "clang/AST/StmtVisitor.h"
Chris Lattner3d1cee32008-04-08 05:04:30 +000019#include "clang/AST/Type.h"
20#include "llvm/ADT/OwningPtr.h"
Chris Lattner8123a952008-04-10 02:22:51 +000021#include "llvm/Support/Compiler.h"
Chris Lattner3d1cee32008-04-08 05:04:30 +000022
23using namespace clang;
24
Chris Lattner8123a952008-04-10 02:22:51 +000025//===----------------------------------------------------------------------===//
26// CheckDefaultArgumentVisitor
27//===----------------------------------------------------------------------===//
28
Chris Lattner9e979552008-04-12 23:52:44 +000029namespace {
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;
Chris Lattner8123a952008-04-10 02:22:51 +000040
Chris Lattner9e979552008-04-12 23:52:44 +000041 public:
42 CheckDefaultArgumentVisitor(Expr *defarg, Sema *s)
43 : DefaultArg(defarg), S(s) {}
Chris Lattner8123a952008-04-10 02:22:51 +000044
Chris Lattner9e979552008-04-12 23:52:44 +000045 bool VisitExpr(Expr *Node);
46 bool VisitDeclRefExpr(DeclRefExpr *DRE);
47 };
Chris Lattner8123a952008-04-10 02:22:51 +000048
Chris Lattner9e979552008-04-12 23:52:44 +000049 /// 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);
Chris Lattner8123a952008-04-10 02:22:51 +000056
Chris Lattner9e979552008-04-12 23:52:44 +000057 return IsInvalid;
Chris Lattner8123a952008-04-10 02:22:51 +000058 }
59
Chris Lattner9e979552008-04-12 23:52:44 +000060 /// 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 (BlockVarDecl *BlockVar = dyn_cast<BlockVarDecl>(Decl)) {
78 // C++ [dcl.fct.default]p7
79 // Local variables shall not be used in default argument
80 // expressions.
81 return S->Diag(DRE->getSourceRange().getBegin(),
82 diag::err_param_default_argument_references_local,
83 BlockVar->getName(), DefaultArg->getSourceRange());
84 }
Chris Lattner8123a952008-04-10 02:22:51 +000085
Chris Lattner9e979552008-04-12 23:52:44 +000086 // FIXME: when Clang has support for member functions, "this"
87 // will also need to be diagnosed.
88
89 return false;
90 }
Chris Lattner8123a952008-04-10 02:22:51 +000091}
92
93/// ActOnParamDefaultArgument - Check whether the default argument
94/// provided for a function parameter is well-formed. If so, attach it
95/// to the parameter declaration.
Chris Lattner3d1cee32008-04-08 05:04:30 +000096void
97Sema::ActOnParamDefaultArgument(DeclTy *param, SourceLocation EqualLoc,
98 ExprTy *defarg) {
99 ParmVarDecl *Param = (ParmVarDecl *)param;
100 llvm::OwningPtr<Expr> DefaultArg((Expr *)defarg);
101 QualType ParamType = Param->getType();
102
103 // Default arguments are only permitted in C++
104 if (!getLangOptions().CPlusPlus) {
105 Diag(EqualLoc, diag::err_param_default_argument,
106 DefaultArg->getSourceRange());
107 return;
108 }
109
110 // C++ [dcl.fct.default]p5
111 // A default argument expression is implicitly converted (clause
112 // 4) to the parameter type. The default argument expression has
113 // the same semantic constraints as the initializer expression in
114 // a declaration of a variable of the parameter type, using the
115 // copy-initialization semantics (8.5).
116 //
117 // FIXME: CheckSingleAssignmentConstraints has the wrong semantics
118 // for C++ (since we want copy-initialization, not copy-assignment),
119 // but we don't have the right semantics implemented yet. Because of
120 // this, our error message is also very poor.
121 QualType DefaultArgType = DefaultArg->getType();
122 Expr *DefaultArgPtr = DefaultArg.get();
123 AssignConvertType ConvTy = CheckSingleAssignmentConstraints(ParamType,
124 DefaultArgPtr);
125 if (DefaultArgPtr != DefaultArg.get()) {
126 DefaultArg.take();
127 DefaultArg.reset(DefaultArgPtr);
128 }
129 if (DiagnoseAssignmentResult(ConvTy, DefaultArg->getLocStart(),
130 ParamType, DefaultArgType, DefaultArg.get(),
131 "in default argument")) {
132 return;
133 }
134
135 // FIXME: C++ [dcl.fct.default]p3
136 // A default argument expression shall be specified only in the
137 // parameter-declaration-clause of a function declaration or in a
138 // template-parameter (14.1). It shall not be specified for a
139 // parameter pack. If it is specified in a
140 // parameter-declaration-clause, it shall not occur within a
141 // declarator or abstract-declarator of a parameter-declaration.
142
Chris Lattner8123a952008-04-10 02:22:51 +0000143 // Check that the default argument is well-formed
Chris Lattner9e979552008-04-12 23:52:44 +0000144 CheckDefaultArgumentVisitor DefaultArgChecker(DefaultArg.get(), this);
Chris Lattner8123a952008-04-10 02:22:51 +0000145 if (DefaultArgChecker.Visit(DefaultArg.get()))
146 return;
147
Chris Lattner3d1cee32008-04-08 05:04:30 +0000148 // Okay: add the default argument to the parameter
149 Param->setDefaultArg(DefaultArg.take());
150}
151
152// MergeCXXFunctionDecl - Merge two declarations of the same C++
153// function, once we already know that they have the same
154// type. Subroutine of MergeFunctionDecl.
155FunctionDecl *
156Sema::MergeCXXFunctionDecl(FunctionDecl *New, FunctionDecl *Old) {
157 // C++ [dcl.fct.default]p4:
158 //
159 // For non-template functions, default arguments can be added in
160 // later declarations of a function in the same
161 // scope. Declarations in different scopes have completely
162 // distinct sets of default arguments. That is, declarations in
163 // inner scopes do not acquire default arguments from
164 // declarations in outer scopes, and vice versa. In a given
165 // function declaration, all parameters subsequent to a
166 // parameter with a default argument shall have default
167 // arguments supplied in this or previous declarations. A
168 // default argument shall not be redefined by a later
169 // declaration (not even to the same value).
170 for (unsigned p = 0, NumParams = Old->getNumParams(); p < NumParams; ++p) {
171 ParmVarDecl *OldParam = Old->getParamDecl(p);
172 ParmVarDecl *NewParam = New->getParamDecl(p);
173
174 if(OldParam->getDefaultArg() && NewParam->getDefaultArg()) {
175 Diag(NewParam->getLocation(),
176 diag::err_param_default_argument_redefinition,
177 NewParam->getDefaultArg()->getSourceRange());
178 Diag(OldParam->getLocation(), diag::err_previous_definition);
179 } else if (OldParam->getDefaultArg()) {
180 // Merge the old default argument into the new parameter
181 NewParam->setDefaultArg(OldParam->getDefaultArg());
182 }
183 }
184
185 return New;
186}
187
188/// CheckCXXDefaultArguments - Verify that the default arguments for a
189/// function declaration are well-formed according to C++
190/// [dcl.fct.default].
191void Sema::CheckCXXDefaultArguments(FunctionDecl *FD) {
192 unsigned NumParams = FD->getNumParams();
193 unsigned p;
194
195 // Find first parameter with a default argument
196 for (p = 0; p < NumParams; ++p) {
197 ParmVarDecl *Param = FD->getParamDecl(p);
198 if (Param->getDefaultArg())
199 break;
200 }
201
202 // C++ [dcl.fct.default]p4:
203 // In a given function declaration, all parameters
204 // subsequent to a parameter with a default argument shall
205 // have default arguments supplied in this or previous
206 // declarations. A default argument shall not be redefined
207 // by a later declaration (not even to the same value).
208 unsigned LastMissingDefaultArg = 0;
209 for(; p < NumParams; ++p) {
210 ParmVarDecl *Param = FD->getParamDecl(p);
211 if (!Param->getDefaultArg()) {
212 if (Param->getIdentifier())
213 Diag(Param->getLocation(),
214 diag::err_param_default_argument_missing_name,
215 Param->getIdentifier()->getName());
216 else
217 Diag(Param->getLocation(),
218 diag::err_param_default_argument_missing);
219
220 LastMissingDefaultArg = p;
221 }
222 }
223
224 if (LastMissingDefaultArg > 0) {
225 // Some default arguments were missing. Clear out all of the
226 // default arguments up to (and including) the last missing
227 // default argument, so that we leave the function parameters
228 // in a semantically valid state.
229 for (p = 0; p <= LastMissingDefaultArg; ++p) {
230 ParmVarDecl *Param = FD->getParamDecl(p);
231 if (Param->getDefaultArg()) {
232 delete Param->getDefaultArg();
233 Param->setDefaultArg(0);
234 }
235 }
236 }
237}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000238
239/// ActOnBaseSpecifier - Parsed a base specifier. A base specifier is
240/// one entry in the base class list of a class specifier, for
241/// example:
242/// class foo : public bar, virtual private baz {
243/// 'public bar' and 'virtual private baz' are each base-specifiers.
244void Sema::ActOnBaseSpecifier(DeclTy *classdecl, SourceRange SpecifierRange,
245 bool Virtual, AccessSpecifier Access,
246 DeclTy *basetype, SourceLocation BaseLoc) {
247 RecordDecl *Decl = (RecordDecl*)classdecl;
248 QualType BaseType = Context.getTypeDeclType((TypeDecl*)basetype);
249
250 // Base specifiers must be record types.
251 if (!BaseType->isRecordType()) {
252 Diag(BaseLoc, diag::err_base_must_be_class, SpecifierRange);
253 return;
254 }
255
256 // C++ [class.union]p1:
257 // A union shall not be used as a base class.
258 if (BaseType->isUnionType()) {
259 Diag(BaseLoc, diag::err_union_as_base_class, SpecifierRange);
260 return;
261 }
262
263 // C++ [class.union]p1:
264 // A union shall not have base classes.
265 if (Decl->getKind() == Decl::Union) {
266 Diag(Decl->getLocation(), diag::err_base_clause_on_union,
267 SpecifierRange);
268 Decl->setInvalidDecl();
269 return;
270 }
271
272 // C++ [class.derived]p2:
273 // The class-name in a base-specifier shall not be an incompletely
274 // defined class.
275 if (BaseType->isIncompleteType()) {
276 Diag(BaseLoc, diag::err_incomplete_base_class, SpecifierRange);
277 return;
278 }
279
280 // FIXME: C++ [class.mi]p3:
281 // A class shall not be specified as a direct base class of a
282 // derived class more than once.
283
284 // FIXME: Attach base class to the record.
285}