Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

dtool/src/cppparser/cppTemplateParameterList.cxx

Go to the documentation of this file.
00001 // Filename: cppTemplateParameterList.cxx
00002 // Created by:  drose (28Oct99)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 
00020 #include "cppTemplateParameterList.h"
00021 #include "cppClassTemplateParameter.h"
00022 #include "cppInstance.h"
00023 #include "cppExpression.h"
00024 
00025 ////////////////////////////////////////////////////////////////////
00026 //     Function: CPPTemplateParameterList::Constructor
00027 //       Access: Public
00028 //  Description:
00029 ////////////////////////////////////////////////////////////////////
00030 CPPTemplateParameterList::
00031 CPPTemplateParameterList() {
00032 }
00033 
00034 ////////////////////////////////////////////////////////////////////
00035 //     Function: CPPTemplateParameterList::Constructor
00036 //       Access: Public
00037 //  Description:
00038 ////////////////////////////////////////////////////////////////////
00039 string CPPTemplateParameterList::
00040 get_string() const {
00041   ostringstream strname;
00042   strname << "< " << *this << " >";
00043   return strname.str();
00044 }
00045 
00046 ////////////////////////////////////////////////////////////////////
00047 //     Function: CPPTemplateParameterList::build_subst_decl
00048 //       Access: Public
00049 //  Description: Matches up the actual parameters one-to-one with the
00050 //               formal parameters they are replacing, so the template
00051 //               may be instantiated by swapping out each occurrence
00052 //               of a template standin type with its appropriate
00053 //               replacement.
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   // Fill in the default template parameters.
00076   while (pfi != formal_params._parameters.end()) {
00077     CPPDeclaration *decl = (*pfi);
00078     if (decl->as_instance()) {
00079       // A value template parameter.  Its default is an expression.
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       // A class template parameter.
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 //     Function: CPPTemplateParameterList::is_fully_specified
00109 //       Access: Public
00110 //  Description: This function returns true if all the parameters in
00111 //               the list are real expressions or classes, and not
00112 //               types yet to-be-determined or template parameter
00113 //               types.  That is, this returns true for a normal
00114 //               template instantiation, and false for a template
00115 //               instantiation based on template parameters that have
00116 //               not yet been specified.
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 //     Function: CPPTemplateParameterList::is_tbd
00130 //       Access: Public
00131 //  Description: Returns true if any type within the parameter list is
00132 //               a CPPTBDType and thus isn't fully determined right
00133 //               now.
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 //     Function: CPPTemplateParameterList::Equivalence Operator
00153 //       Access: Public
00154 //  Description:
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 //     Function: CPPTemplateParameterList::Nonequivalence Operator
00171 //       Access: Public
00172 //  Description:
00173 ////////////////////////////////////////////////////////////////////
00174 bool CPPTemplateParameterList::
00175 operator != (const CPPTemplateParameterList &other) const {
00176   return !(*this == other);
00177 }
00178 
00179 ////////////////////////////////////////////////////////////////////
00180 //     Function: CPPTemplateParameterList::Ordering Operator
00181 //       Access: Public
00182 //  Description:
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 //     Function: CPPTemplateParameterList::substitute_decl
00199 //       Access: Public
00200 //  Description:
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 //     Function: CPPTemplateParameterList::output
00226 //       Access: Public
00227 //  Description:
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 //     Function: CPPTemplateParameterList::write_formal
00246 //       Access: Public
00247 //  Description: Writes the list as a set of formal parameters for a
00248 //               template scope.  Includes the keyword "template" and
00249 //               the angle brackets, as well as the trailing newline.
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 }

Generated on Thu May 1 22:12:55 2003 for DTool by doxygen1.3