00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "cppTemplateParameterList.h"
00021 #include "cppClassTemplateParameter.h"
00022 #include "cppInstance.h"
00023 #include "cppExpression.h"
00024
00025
00026
00027
00028
00029
00030 CPPTemplateParameterList::
00031 CPPTemplateParameterList() {
00032 }
00033
00034
00035
00036
00037
00038
00039 string CPPTemplateParameterList::
00040 get_string() const {
00041 ostringstream strname;
00042 strname << "< " << *this << " >";
00043 return strname.str();
00044 }
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 void CPPTemplateParameterList::
00056 build_subst_decl(const CPPTemplateParameterList &formal_params,
00057 CPPDeclaration::SubstDecl &subst,
00058 CPPScope *current_scope, CPPScope *global_scope) const {
00059 Parameters::const_iterator pfi, pai;
00060 for (pfi = formal_params._parameters.begin(), pai = _parameters.begin();
00061 pfi != formal_params._parameters.end() && pai != _parameters.end();
00062 ++pfi, ++pai) {
00063 CPPDeclaration *formal = *pfi;
00064 CPPDeclaration *actual = *pai;
00065
00066 if (actual->as_type()) {
00067 actual = actual->as_type()->resolve_type(current_scope, global_scope);
00068 }
00069
00070 if (!(formal == actual)) {
00071 subst.insert(CPPDeclaration::SubstDecl::value_type(formal, actual));
00072 }
00073 }
00074
00075
00076 while (pfi != formal_params._parameters.end()) {
00077 CPPDeclaration *decl = (*pfi);
00078 if (decl->as_instance()) {
00079
00080 CPPInstance *inst = decl->as_instance();
00081 if (inst->_initializer != NULL) {
00082 CPPDeclaration *decl =
00083 inst->_initializer->substitute_decl(subst, current_scope,
00084 global_scope);
00085 if (!(*decl == *inst)) {
00086 subst.insert(CPPDeclaration::SubstDecl::value_type
00087 (inst, decl));
00088 }
00089 }
00090 } else if (decl->as_class_template_parameter()) {
00091
00092 CPPClassTemplateParameter *cparam = decl->as_class_template_parameter();
00093 if (cparam->_default_type != NULL) {
00094 CPPDeclaration *decl =
00095 cparam->_default_type->substitute_decl(subst, current_scope,
00096 global_scope);
00097 if (!(*cparam == *decl)) {
00098 subst.insert(CPPDeclaration::SubstDecl::value_type
00099 (cparam, decl));
00100 }
00101 }
00102 }
00103 ++pfi;
00104 }
00105 }
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118 bool CPPTemplateParameterList::
00119 is_fully_specified() const {
00120 for (int i = 0; i < (int)_parameters.size(); i++) {
00121 if (!_parameters[i]->is_fully_specified()) {
00122 return false;
00123 }
00124 }
00125 return true;
00126 }
00127
00128
00129
00130
00131
00132
00133
00134
00135 bool CPPTemplateParameterList::
00136 is_tbd() const {
00137 for (int i = 0; i < (int)_parameters.size(); i++) {
00138 CPPType *type = _parameters[i]->as_type();
00139 if (type != (CPPType *)NULL &&
00140 (type->is_tbd() || type->as_class_template_parameter() != NULL)) {
00141 return true;
00142 }
00143 CPPExpression *expr = _parameters[i]->as_expression();
00144 if (expr != NULL && expr->is_tbd()) {
00145 return true;
00146 }
00147 }
00148 return false;
00149 }
00150
00151
00152
00153
00154
00155
00156 bool CPPTemplateParameterList::
00157 operator == (const CPPTemplateParameterList &other) const {
00158 if (_parameters.size() != other._parameters.size()) {
00159 return false;
00160 }
00161 for (int i = 0; i < (int)_parameters.size(); i++) {
00162 if (*_parameters[i] != *other._parameters[i]) {
00163 return false;
00164 }
00165 }
00166 return true;
00167 }
00168
00169
00170
00171
00172
00173
00174 bool CPPTemplateParameterList::
00175 operator != (const CPPTemplateParameterList &other) const {
00176 return !(*this == other);
00177 }
00178
00179
00180
00181
00182
00183
00184 bool CPPTemplateParameterList::
00185 operator < (const CPPTemplateParameterList &other) const {
00186 if (_parameters.size() != other._parameters.size()) {
00187 return _parameters.size() < other._parameters.size();
00188 }
00189 for (int i = 0; i < (int)_parameters.size(); i++) {
00190 if (*_parameters[i] != *other._parameters[i]) {
00191 return *_parameters[i] < *other._parameters[i];
00192 }
00193 }
00194 return false;
00195 }
00196
00197
00198
00199
00200
00201
00202 CPPTemplateParameterList *CPPTemplateParameterList::
00203 substitute_decl(CPPDeclaration::SubstDecl &subst,
00204 CPPScope *current_scope, CPPScope *global_scope) {
00205 CPPTemplateParameterList *rep = new CPPTemplateParameterList(*this);
00206
00207 bool anything_changed = false;
00208 for (int i = 0; i < (int)rep->_parameters.size(); i++) {
00209 rep->_parameters[i] =
00210 _parameters[i]->substitute_decl(subst, current_scope, global_scope);
00211 if (rep->_parameters[i] != _parameters[i]) {
00212 anything_changed = true;
00213 }
00214 }
00215
00216 if (!anything_changed) {
00217 delete rep;
00218 rep = this;
00219 }
00220
00221 return rep;
00222 }
00223
00224
00225
00226
00227
00228
00229 void CPPTemplateParameterList::
00230 output(ostream &out, CPPScope *scope) const {
00231 if (!_parameters.empty()) {
00232 Parameters::const_iterator pi = _parameters.begin();
00233 (*pi)->output(out, 0, scope, false);
00234
00235 ++pi;
00236 while (pi != _parameters.end()) {
00237 out << ", ";
00238 (*pi)->output(out, 0, scope, false);
00239 ++pi;
00240 }
00241 }
00242 }
00243
00244
00245
00246
00247
00248
00249
00250
00251 void CPPTemplateParameterList::
00252 write_formal(ostream &out, CPPScope *scope) const {
00253 out << "template<";
00254 if (!_parameters.empty()) {
00255 Parameters::const_iterator pi = _parameters.begin();
00256 (*pi)->output(out, 0, scope, true);
00257
00258 ++pi;
00259 while (pi != _parameters.end()) {
00260 out << ", ";
00261 (*pi)->output(out, 0, scope, true);
00262 ++pi;
00263 }
00264 }
00265 out << ">\n";
00266 }