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

dtool/src/cppparser/cppParameterList.cxx

Go to the documentation of this file.
00001 // Filename: cppParameterList.cxx
00002 // Created by:  drose (21Oct99)
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 "cppParameterList.h"
00021 #include "cppInstance.h"
00022 
00023 ////////////////////////////////////////////////////////////////////
00024 //     Function: CPPParameterList::Constructor
00025 //       Access: Public
00026 //  Description:
00027 ////////////////////////////////////////////////////////////////////
00028 CPPParameterList::
00029 CPPParameterList() {
00030   _includes_ellipsis = false;
00031 }
00032 
00033 ////////////////////////////////////////////////////////////////////
00034 //     Function: CPPParameterList::is_equivalent
00035 //       Access: Public
00036 //  Description: This is similar to operator == except it is more
00037 //               forgiving: it is true if only the length and order of
00038 //               types is the same, never minding the instance names
00039 //               or initial values.
00040 ////////////////////////////////////////////////////////////////////
00041 bool CPPParameterList::
00042 is_equivalent(const CPPParameterList &other) const {
00043   if (_includes_ellipsis != other._includes_ellipsis) {
00044     return false;
00045   }
00046   if (_parameters.size() != other._parameters.size()) {
00047     return false;
00048   }
00049   for (int i = 0; i < (int)_parameters.size(); i++) {
00050     if (!_parameters[i]->_type->is_equivalent(*other._parameters[i]->_type)) {
00051       return false;
00052     }
00053   }
00054   return true;
00055 }
00056 
00057 ////////////////////////////////////////////////////////////////////
00058 //     Function: CPPParameterList::Equality Operator
00059 //       Access: Public
00060 //  Description:
00061 ////////////////////////////////////////////////////////////////////
00062 bool CPPParameterList::
00063 operator == (const CPPParameterList &other) const {
00064   if (_includes_ellipsis != other._includes_ellipsis) {
00065     return false;
00066   }
00067   if (_parameters.size() != other._parameters.size()) {
00068     return false;
00069   }
00070   for (int i = 0; i < (int)_parameters.size(); i++) {
00071     if (*_parameters[i] != *other._parameters[i]) {
00072       return false;
00073     }
00074   }
00075   return true;
00076 }
00077 
00078 ////////////////////////////////////////////////////////////////////
00079 //     Function: CPPParameterList::Inequality Operator
00080 //       Access: Public
00081 //  Description:
00082 ////////////////////////////////////////////////////////////////////
00083 bool CPPParameterList::
00084 operator != (const CPPParameterList &other) const {
00085   return !(*this == other);
00086 }
00087 
00088 ////////////////////////////////////////////////////////////////////
00089 //     Function: CPPParameterList::Ordering Operator
00090 //       Access: Public
00091 //  Description:
00092 ////////////////////////////////////////////////////////////////////
00093 bool CPPParameterList::
00094 operator < (const CPPParameterList &other) const {
00095   if (_includes_ellipsis != other._includes_ellipsis) {
00096     return _includes_ellipsis < other._includes_ellipsis;
00097   }
00098   if (_parameters.size() != other._parameters.size()) {
00099     return _parameters.size() < other._parameters.size();
00100   }
00101   for (int i = 0; i < (int)_parameters.size(); i++) {
00102     if (*_parameters[i] != *other._parameters[i]) {
00103       return *_parameters[i] < *other._parameters[i];
00104     }
00105   }
00106   return false;
00107 }
00108 
00109 ////////////////////////////////////////////////////////////////////
00110 //     Function: CPPParameterList::is_tbd
00111 //       Access: Public
00112 //  Description: Returns true if any of the types in the parameter
00113 //               list are base on CPPTBDType.
00114 ////////////////////////////////////////////////////////////////////
00115 bool CPPParameterList::
00116 is_tbd() const {
00117   for (int i = 0; i < (int)_parameters.size(); i++) {
00118     if (_parameters[i]->_type->is_tbd()) {
00119       return true;
00120     }
00121   }
00122   return false;
00123 }
00124 
00125 ////////////////////////////////////////////////////////////////////
00126 //     Function: CPPParameterList::is_fully_specified
00127 //       Access: Public
00128 //  Description: Returns true if this declaration is an actual,
00129 //               factual declaration, or false if some part of the
00130 //               declaration depends on a template parameter which has
00131 //               not yet been instantiated.
00132 ////////////////////////////////////////////////////////////////////
00133 bool CPPParameterList::
00134 is_fully_specified() const {
00135   for (int i = 0; i < (int)_parameters.size(); i++) {
00136     if (!_parameters[i]->is_fully_specified()) {
00137       return false;
00138     }
00139   }
00140 
00141   return true;
00142 }
00143 
00144 ////////////////////////////////////////////////////////////////////
00145 //     Function: CPPParameterList::substitute_decl
00146 //       Access: Public
00147 //  Description:
00148 ////////////////////////////////////////////////////////////////////
00149 CPPParameterList *CPPParameterList::
00150 substitute_decl(CPPDeclaration::SubstDecl &subst,
00151                 CPPScope *current_scope, CPPScope *global_scope) {
00152   CPPParameterList *rep = new CPPParameterList;
00153   bool any_changed = false;
00154   for (int i = 0; i < (int)_parameters.size(); i++) {
00155     CPPInstance *inst =
00156       _parameters[i]->substitute_decl(subst, current_scope, global_scope)
00157       ->as_instance();
00158     if (inst != _parameters[i]) {
00159       any_changed = true;
00160     }
00161     rep->_parameters.push_back(inst);
00162   }
00163 
00164   if (!any_changed) {
00165     delete rep;
00166     rep = this;
00167   }
00168   return rep;
00169 }
00170 
00171 
00172 ////////////////////////////////////////////////////////////////////
00173 //     Function: CPPParameterList::resolve_type
00174 //       Access: Public
00175 //  Description: Returns an equivalent CPPParameterList, in which all
00176 //               of the individual types have been resolved.
00177 ////////////////////////////////////////////////////////////////////
00178 CPPParameterList *CPPParameterList::
00179 resolve_type(CPPScope *current_scope, CPPScope *global_scope) {
00180   CPPParameterList *rep = new CPPParameterList;
00181   bool any_changed = false;
00182   for (int i = 0; i < (int)_parameters.size(); i++) {
00183     CPPInstance *inst = _parameters[i];
00184     CPPType *new_type = inst->_type;
00185     if (new_type->is_tbd()) {
00186       new_type = new_type->resolve_type(current_scope, global_scope);
00187     }
00188 
00189     if (new_type != inst->_type) {
00190       any_changed = true;
00191       CPPInstance *new_inst = new CPPInstance(*inst);
00192       new_inst->_type = new_type;
00193       rep->_parameters.push_back(new_inst);
00194     } else {
00195       rep->_parameters.push_back(inst);
00196     }
00197   }
00198 
00199   if (!any_changed) {
00200     delete rep;
00201     rep = this;
00202   }
00203   return rep;
00204 }
00205 
00206 ////////////////////////////////////////////////////////////////////
00207 //     Function: CPPParameterList::output
00208 //       Access: Public
00209 //  Description: If num_default_parameters is >= 0, it indicates the
00210 //               number of default parameter values to show on output.
00211 //               Otherwise, all parameter values are shown.
00212 ////////////////////////////////////////////////////////////////////
00213 void CPPParameterList::
00214 output(ostream &out, CPPScope *scope, bool parameter_names,
00215        int num_default_parameters) const {
00216   if (!_parameters.empty()) {
00217     for (int i = 0; i < (int)_parameters.size(); i++) {
00218       if (i != 0) {
00219         out << ", ";
00220       }
00221 
00222       // Save the default value expression; we might be about to
00223       // temporarily clear it.
00224       CPPExpression *expr = _parameters[i]->_initializer;
00225 
00226       if (num_default_parameters >= 0 &&
00227           i < (int)_parameters.size() - num_default_parameters) {
00228         // Don't show the default value for this parameter.
00229         _parameters[i]->_initializer = (CPPExpression *)NULL;
00230       }
00231 
00232       if (parameter_names) {
00233         _parameters[i]->output(out, 0, scope, false);
00234       } else {
00235         _parameters[i]->_type->output(out, 0, scope, false);
00236       }
00237 
00238       // Restore the default value expression.
00239       _parameters[i]->_initializer = expr;
00240     }
00241     if (_includes_ellipsis) {
00242       out << ", ...";
00243     }
00244 
00245   } else if (_includes_ellipsis) {
00246     out << "...";
00247 
00248   } else {
00249     // No parameters.
00250     out << "void";
00251   }
00252 }

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