cppInstanceIdentifier.cxx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // Filename: cppInstanceIdentifier.cxx
  2. // Created by: drose (21Oct99)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved
  8. //
  9. // All use of this software is subject to the terms of the Panda 3d
  10. // Software license. You should have received a copy of this license
  11. // along with this source code; you will also find a current copy of
  12. // the license at http://www.panda3d.org/license.txt .
  13. //
  14. // To contact the maintainers of this program write to
  15. // [email protected] .
  16. //
  17. ////////////////////////////////////////////////////////////////////
  18. #include "cppInstanceIdentifier.h"
  19. #include "cppPointerType.h"
  20. #include "cppReferenceType.h"
  21. #include "cppArrayType.h"
  22. #include "cppConstType.h"
  23. #include "cppFunctionType.h"
  24. #include "cppParameterList.h"
  25. #include "cppIdentifier.h"
  26. ////////////////////////////////////////////////////////////////////
  27. // Function: CPPInstanceIdentifier::Modifier::Constructor
  28. // Access: Public
  29. // Description:
  30. ////////////////////////////////////////////////////////////////////
  31. CPPInstanceIdentifier::Modifier::
  32. Modifier(CPPInstanceIdentifierType type) :
  33. _type(type)
  34. {
  35. _func_params = NULL;
  36. _func_flags = 0;
  37. _scoping = NULL;
  38. _expr = NULL;
  39. }
  40. ////////////////////////////////////////////////////////////////////
  41. // Function: CPPInstanceIdentifier::Modifier::named func_type constructor
  42. // Access: Public, Static
  43. // Description:
  44. ////////////////////////////////////////////////////////////////////
  45. CPPInstanceIdentifier::Modifier CPPInstanceIdentifier::Modifier::
  46. func_type(CPPParameterList *params, int flags) {
  47. Modifier mod(IIT_func);
  48. mod._func_params = params;
  49. mod._func_flags = flags;
  50. return mod;
  51. }
  52. ////////////////////////////////////////////////////////////////////
  53. // Function: CPPInstanceIdentifier::Modifier::named array_type constructor
  54. // Access: Public, Static
  55. // Description:
  56. ////////////////////////////////////////////////////////////////////
  57. CPPInstanceIdentifier::Modifier CPPInstanceIdentifier::Modifier::
  58. array_type(CPPExpression *expr) {
  59. Modifier mod(IIT_array);
  60. mod._expr = expr;
  61. return mod;
  62. }
  63. ////////////////////////////////////////////////////////////////////
  64. // Function: CPPInstanceIdentifier::Modifier::named scoped_pointer_type
  65. // Access: Public, Static
  66. // Description:
  67. ////////////////////////////////////////////////////////////////////
  68. CPPInstanceIdentifier::Modifier CPPInstanceIdentifier::Modifier::
  69. scoped_pointer_type(CPPIdentifier *scoping) {
  70. Modifier mod(IIT_scoped_pointer);
  71. mod._scoping = scoping;
  72. return mod;
  73. }
  74. ////////////////////////////////////////////////////////////////////
  75. // Function: CPPInstanceIdentifier::Constructor
  76. // Access: Public
  77. // Description:
  78. ////////////////////////////////////////////////////////////////////
  79. CPPInstanceIdentifier::
  80. CPPInstanceIdentifier(CPPIdentifier *ident) : _ident(ident) {
  81. }
  82. ////////////////////////////////////////////////////////////////////
  83. // Function: CPPInstanceIdentifier::unroll_type
  84. // Access: Public
  85. // Description: Unrolls the list of type punctuation on either side
  86. // of the identifier to determine the actual type
  87. // represented by the identifier, given the indicated
  88. // starting type (that is, the type name written to the
  89. // left of the identifier).
  90. ////////////////////////////////////////////////////////////////////
  91. CPPType *CPPInstanceIdentifier::
  92. unroll_type(CPPType *start_type) {
  93. CPPType *result = r_unroll_type(start_type, _modifiers.begin());
  94. return result;
  95. }
  96. ////////////////////////////////////////////////////////////////////
  97. // Function: CPPInstanceIdentifier::add_modifier
  98. // Access: Public
  99. // Description:
  100. ////////////////////////////////////////////////////////////////////
  101. void CPPInstanceIdentifier::
  102. add_modifier(CPPInstanceIdentifierType type) {
  103. _modifiers.push_back(Modifier(type));
  104. }
  105. ////////////////////////////////////////////////////////////////////
  106. // Function: CPPInstanceIdentifier::add_modifier
  107. // Access: Public
  108. // Description:
  109. ////////////////////////////////////////////////////////////////////
  110. void CPPInstanceIdentifier::
  111. add_func_modifier(CPPParameterList *params, int flags) {
  112. _modifiers.push_back(Modifier::func_type(params, flags));
  113. }
  114. void CPPInstanceIdentifier::
  115. add_scoped_pointer_modifier(CPPIdentifier *scoping) {
  116. _modifiers.push_back(Modifier::scoped_pointer_type(scoping));
  117. }
  118. ////////////////////////////////////////////////////////////////////
  119. // Function: CPPInstanceIdentifier::add_modifier
  120. // Access: Public
  121. // Description:
  122. ////////////////////////////////////////////////////////////////////
  123. void CPPInstanceIdentifier::
  124. add_array_modifier(CPPExpression *expr) {
  125. _modifiers.push_back(Modifier::array_type(expr));
  126. }
  127. ////////////////////////////////////////////////////////////////////
  128. // Function: CPPInstanceIdentifier::get_scope
  129. // Access: Public
  130. // Description:
  131. ////////////////////////////////////////////////////////////////////
  132. CPPScope *CPPInstanceIdentifier::
  133. get_scope(CPPScope *current_scope, CPPScope *global_scope,
  134. CPPPreprocessor *error_sink) const {
  135. if (_ident == NULL) {
  136. return current_scope;
  137. } else {
  138. return _ident->get_scope(current_scope, global_scope, error_sink);
  139. }
  140. }
  141. ////////////////////////////////////////////////////////////////////
  142. // Function: CPPInstanceIdentifier::r_unroll_type
  143. // Access: Private
  144. // Description: The recursive implementation of unroll_type().
  145. ////////////////////////////////////////////////////////////////////
  146. CPPType *CPPInstanceIdentifier::
  147. r_unroll_type(CPPType *start_type,
  148. CPPInstanceIdentifier::Modifiers::const_iterator mi) {
  149. start_type = CPPType::new_type(start_type);
  150. if (mi == _modifiers.end()) {
  151. return start_type;
  152. }
  153. const Modifier &mod = (*mi);
  154. ++mi;
  155. CPPType *result = NULL;
  156. switch (mod._type) {
  157. case IIT_pointer:
  158. result = new CPPPointerType(r_unroll_type(start_type, mi));
  159. break;
  160. case IIT_reference:
  161. result = new CPPReferenceType(r_unroll_type(start_type, mi));
  162. break;
  163. case IIT_scoped_pointer:
  164. {
  165. CPPType *type = r_unroll_type(start_type, mi);
  166. CPPFunctionType *ftype = type->as_function_type();
  167. if (ftype != NULL) {
  168. ftype = new CPPFunctionType(*ftype);
  169. ftype->_class_owner = mod._scoping;
  170. ftype->_flags |= CPPFunctionType::F_method_pointer;
  171. type = ftype;
  172. }
  173. result = new CPPPointerType(type);
  174. }
  175. break;
  176. case IIT_array:
  177. result = new CPPArrayType(r_unroll_type(start_type, mi),
  178. mod._expr);
  179. break;
  180. case IIT_const:
  181. result = new CPPConstType(r_unroll_type(start_type, mi));
  182. break;
  183. case IIT_paren:
  184. result = r_unroll_type(start_type, mi);
  185. break;
  186. case IIT_func:
  187. {
  188. CPPType *return_type = r_unroll_type(start_type, mi);
  189. result = new CPPFunctionType(return_type, mod._func_params,
  190. mod._func_flags);
  191. }
  192. break;
  193. default:
  194. cerr << "Internal error--invalid CPPInstanceIdentifier\n";
  195. abort();
  196. }
  197. return CPPType::new_type(result);
  198. }