blob: e7bf7bff4b4f0de086ac137323443bf6d61bf5a5 [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"
17#include "clang/AST/Type.h"
18#include "llvm/ADT/OwningPtr.h"
19
20using namespace clang;
21
22void
23Sema::ActOnParamDefaultArgument(DeclTy *param, SourceLocation EqualLoc,
24 ExprTy *defarg) {
25 ParmVarDecl *Param = (ParmVarDecl *)param;
26 llvm::OwningPtr<Expr> DefaultArg((Expr *)defarg);
27 QualType ParamType = Param->getType();
28
29 // Default arguments are only permitted in C++
30 if (!getLangOptions().CPlusPlus) {
31 Diag(EqualLoc, diag::err_param_default_argument,
32 DefaultArg->getSourceRange());
33 return;
34 }
35
36 // C++ [dcl.fct.default]p5
37 // A default argument expression is implicitly converted (clause
38 // 4) to the parameter type. The default argument expression has
39 // the same semantic constraints as the initializer expression in
40 // a declaration of a variable of the parameter type, using the
41 // copy-initialization semantics (8.5).
42 //
43 // FIXME: CheckSingleAssignmentConstraints has the wrong semantics
44 // for C++ (since we want copy-initialization, not copy-assignment),
45 // but we don't have the right semantics implemented yet. Because of
46 // this, our error message is also very poor.
47 QualType DefaultArgType = DefaultArg->getType();
48 Expr *DefaultArgPtr = DefaultArg.get();
49 AssignConvertType ConvTy = CheckSingleAssignmentConstraints(ParamType,
50 DefaultArgPtr);
51 if (DefaultArgPtr != DefaultArg.get()) {
52 DefaultArg.take();
53 DefaultArg.reset(DefaultArgPtr);
54 }
55 if (DiagnoseAssignmentResult(ConvTy, DefaultArg->getLocStart(),
56 ParamType, DefaultArgType, DefaultArg.get(),
57 "in default argument")) {
58 return;
59 }
60
61 // FIXME: C++ [dcl.fct.default]p3
62 // A default argument expression shall be specified only in the
63 // parameter-declaration-clause of a function declaration or in a
64 // template-parameter (14.1). It shall not be specified for a
65 // parameter pack. If it is specified in a
66 // parameter-declaration-clause, it shall not occur within a
67 // declarator or abstract-declarator of a parameter-declaration.
68
69 // Okay: add the default argument to the parameter
70 Param->setDefaultArg(DefaultArg.take());
71}
72
73// MergeCXXFunctionDecl - Merge two declarations of the same C++
74// function, once we already know that they have the same
75// type. Subroutine of MergeFunctionDecl.
76FunctionDecl *
77Sema::MergeCXXFunctionDecl(FunctionDecl *New, FunctionDecl *Old) {
78 // C++ [dcl.fct.default]p4:
79 //
80 // For non-template functions, default arguments can be added in
81 // later declarations of a function in the same
82 // scope. Declarations in different scopes have completely
83 // distinct sets of default arguments. That is, declarations in
84 // inner scopes do not acquire default arguments from
85 // declarations in outer scopes, and vice versa. In a given
86 // function declaration, all parameters subsequent to a
87 // parameter with a default argument shall have default
88 // arguments supplied in this or previous declarations. A
89 // default argument shall not be redefined by a later
90 // declaration (not even to the same value).
91 for (unsigned p = 0, NumParams = Old->getNumParams(); p < NumParams; ++p) {
92 ParmVarDecl *OldParam = Old->getParamDecl(p);
93 ParmVarDecl *NewParam = New->getParamDecl(p);
94
95 if(OldParam->getDefaultArg() && NewParam->getDefaultArg()) {
96 Diag(NewParam->getLocation(),
97 diag::err_param_default_argument_redefinition,
98 NewParam->getDefaultArg()->getSourceRange());
99 Diag(OldParam->getLocation(), diag::err_previous_definition);
100 } else if (OldParam->getDefaultArg()) {
101 // Merge the old default argument into the new parameter
102 NewParam->setDefaultArg(OldParam->getDefaultArg());
103 }
104 }
105
106 return New;
107}
108
109/// CheckCXXDefaultArguments - Verify that the default arguments for a
110/// function declaration are well-formed according to C++
111/// [dcl.fct.default].
112void Sema::CheckCXXDefaultArguments(FunctionDecl *FD) {
113 unsigned NumParams = FD->getNumParams();
114 unsigned p;
115
116 // Find first parameter with a default argument
117 for (p = 0; p < NumParams; ++p) {
118 ParmVarDecl *Param = FD->getParamDecl(p);
119 if (Param->getDefaultArg())
120 break;
121 }
122
123 // C++ [dcl.fct.default]p4:
124 // In a given function declaration, all parameters
125 // subsequent to a parameter with a default argument shall
126 // have default arguments supplied in this or previous
127 // declarations. A default argument shall not be redefined
128 // by a later declaration (not even to the same value).
129 unsigned LastMissingDefaultArg = 0;
130 for(; p < NumParams; ++p) {
131 ParmVarDecl *Param = FD->getParamDecl(p);
132 if (!Param->getDefaultArg()) {
133 if (Param->getIdentifier())
134 Diag(Param->getLocation(),
135 diag::err_param_default_argument_missing_name,
136 Param->getIdentifier()->getName());
137 else
138 Diag(Param->getLocation(),
139 diag::err_param_default_argument_missing);
140
141 LastMissingDefaultArg = p;
142 }
143 }
144
145 if (LastMissingDefaultArg > 0) {
146 // Some default arguments were missing. Clear out all of the
147 // default arguments up to (and including) the last missing
148 // default argument, so that we leave the function parameters
149 // in a semantically valid state.
150 for (p = 0; p <= LastMissingDefaultArg; ++p) {
151 ParmVarDecl *Param = FD->getParamDecl(p);
152 if (Param->getDefaultArg()) {
153 delete Param->getDefaultArg();
154 Param->setDefaultArg(0);
155 }
156 }
157 }
158}