cppInstanceIdentifier.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // Filename: cppInstanceIdentifier.cxx
  2. // Created by: drose (21Oct99)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) 2001 - 2004, 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://etc.cmu.edu/panda3d/docs/license/ .
  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::Modifier::named initializer_type constructor
  76. // Access: Public, Static
  77. // Description: This is used only for instance declarations that turn
  78. // out to be have a parameter list for an initializer.
  79. ////////////////////////////////////////////////////////////////////
  80. CPPInstanceIdentifier::Modifier CPPInstanceIdentifier::Modifier::
  81. initializer_type(CPPParameterList *params) {
  82. Modifier mod(IIT_initializer);
  83. mod._func_params = params;
  84. return mod;
  85. }
  86. ////////////////////////////////////////////////////////////////////
  87. // Function: CPPInstanceIdentifier::Constructor
  88. // Access: Public
  89. // Description:
  90. ////////////////////////////////////////////////////////////////////
  91. CPPInstanceIdentifier::
  92. CPPInstanceIdentifier(CPPIdentifier *ident) : _ident(ident) {
  93. }
  94. ////////////////////////////////////////////////////////////////////
  95. // Function: CPPInstanceIdentifier::unroll_type
  96. // Access: Public
  97. // Description: Unrolls the list of type punctuation on either side
  98. // of the identifier to determine the actual type
  99. // represented by the identifier, given the indicated
  100. // starting type (that is, the type name written to the
  101. // left of the identifier).
  102. ////////////////////////////////////////////////////////////////////
  103. CPPType *CPPInstanceIdentifier::
  104. unroll_type(CPPType *start_type) {
  105. CPPType *result = r_unroll_type(start_type, _modifiers.begin());
  106. return result;
  107. }
  108. ////////////////////////////////////////////////////////////////////
  109. // Function: CPPInstanceIdentifier::add_modifier
  110. // Access: Public
  111. // Description:
  112. ////////////////////////////////////////////////////////////////////
  113. void CPPInstanceIdentifier::
  114. add_modifier(CPPInstanceIdentifierType type) {
  115. _modifiers.push_back(Modifier(type));
  116. }
  117. ////////////////////////////////////////////////////////////////////
  118. // Function: CPPInstanceIdentifier::add_func_modifier
  119. // Access: Public
  120. // Description:
  121. ////////////////////////////////////////////////////////////////////
  122. void CPPInstanceIdentifier::
  123. add_func_modifier(CPPParameterList *params, int flags) {
  124. // As a special hack, if we added a parameter list to an operator
  125. // function, check if the parameter list is empty. If it is, this
  126. // is really a unary operator, so set the unary_op flag. Operators
  127. // () and [] are never considered unary operators.
  128. if (_ident != NULL &&
  129. _ident->get_simple_name().substr(0, 9) == "operator " &&
  130. _ident->get_simple_name() != string("operator ()") &&
  131. _ident->get_simple_name() != string("operator []")) {
  132. if (params->_parameters.empty()) {
  133. flags |= CPPFunctionType::F_unary_op;
  134. }
  135. }
  136. _modifiers.push_back(Modifier::func_type(params, flags));
  137. }
  138. ////////////////////////////////////////////////////////////////////
  139. // Function: CPPInstanceIdentifier::add_scoped_pointer_modifier
  140. // Access: Public
  141. // Description:
  142. ////////////////////////////////////////////////////////////////////
  143. void CPPInstanceIdentifier::
  144. add_scoped_pointer_modifier(CPPIdentifier *scoping) {
  145. _modifiers.push_back(Modifier::scoped_pointer_type(scoping));
  146. }
  147. ////////////////////////////////////////////////////////////////////
  148. // Function: CPPInstanceIdentifier::add_array_modifier
  149. // Access: Public
  150. // Description:
  151. ////////////////////////////////////////////////////////////////////
  152. void CPPInstanceIdentifier::
  153. add_array_modifier(CPPExpression *expr) {
  154. _modifiers.push_back(Modifier::array_type(expr));
  155. }
  156. ////////////////////////////////////////////////////////////////////
  157. // Function: CPPInstanceIdentifier::add_initializer_modifier
  158. // Access: Public
  159. // Description:
  160. ////////////////////////////////////////////////////////////////////
  161. void CPPInstanceIdentifier::
  162. add_initializer_modifier(CPPParameterList *params) {
  163. _modifiers.push_back(Modifier::initializer_type(params));
  164. }
  165. ////////////////////////////////////////////////////////////////////
  166. // Function: CPPInstanceIdentifier::get_initializer
  167. // Access: Public
  168. // Description: Returns the initializer parameter list that was set
  169. // for this particular instance, e.g. if the instance
  170. // were:
  171. //
  172. // int foo(0);
  173. //
  174. // this would return the parameter list (0). Returns
  175. // NULL if the instance did not use a parameter list
  176. // initializer.
  177. ////////////////////////////////////////////////////////////////////
  178. CPPParameterList *CPPInstanceIdentifier::
  179. get_initializer() const {
  180. Modifiers::const_iterator mi;
  181. for (mi = _modifiers.begin(); mi != _modifiers.end(); ++mi) {
  182. const Modifier &mod = (*mi);
  183. if (mod._type == IIT_initializer) {
  184. return mod._func_params;
  185. }
  186. }
  187. return NULL;
  188. }
  189. ////////////////////////////////////////////////////////////////////
  190. // Function: CPPInstanceIdentifier::get_scope
  191. // Access: Public
  192. // Description:
  193. ////////////////////////////////////////////////////////////////////
  194. CPPScope *CPPInstanceIdentifier::
  195. get_scope(CPPScope *current_scope, CPPScope *global_scope,
  196. CPPPreprocessor *error_sink) const {
  197. if (_ident == NULL) {
  198. return current_scope;
  199. } else {
  200. return _ident->get_scope(current_scope, global_scope, error_sink);
  201. }
  202. }
  203. ////////////////////////////////////////////////////////////////////
  204. // Function: CPPInstanceIdentifier::r_unroll_type
  205. // Access: Private
  206. // Description: The recursive implementation of unroll_type().
  207. ////////////////////////////////////////////////////////////////////
  208. CPPType *CPPInstanceIdentifier::
  209. r_unroll_type(CPPType *start_type,
  210. CPPInstanceIdentifier::Modifiers::const_iterator mi) {
  211. start_type = CPPType::new_type(start_type);
  212. if (mi == _modifiers.end()) {
  213. return start_type;
  214. }
  215. const Modifier &mod = (*mi);
  216. ++mi;
  217. CPPType *result = NULL;
  218. switch (mod._type) {
  219. case IIT_pointer:
  220. result = new CPPPointerType(r_unroll_type(start_type, mi));
  221. break;
  222. case IIT_reference:
  223. result = new CPPReferenceType(r_unroll_type(start_type, mi));
  224. break;
  225. case IIT_scoped_pointer:
  226. {
  227. CPPType *type = r_unroll_type(start_type, mi);
  228. CPPFunctionType *ftype = type->as_function_type();
  229. if (ftype != NULL) {
  230. ftype = new CPPFunctionType(*ftype);
  231. ftype->_class_owner = mod._scoping;
  232. ftype->_flags |= CPPFunctionType::F_method_pointer;
  233. type = ftype;
  234. }
  235. result = new CPPPointerType(type);
  236. }
  237. break;
  238. case IIT_array:
  239. result = new CPPArrayType(r_unroll_type(start_type, mi),
  240. mod._expr);
  241. break;
  242. case IIT_const:
  243. result = new CPPConstType(r_unroll_type(start_type, mi));
  244. break;
  245. case IIT_paren:
  246. result = r_unroll_type(start_type, mi);
  247. break;
  248. case IIT_func:
  249. {
  250. CPPType *return_type = r_unroll_type(start_type, mi);
  251. result = new CPPFunctionType(return_type, mod._func_params,
  252. mod._func_flags);
  253. }
  254. break;
  255. case IIT_initializer:
  256. // In this case, we have parsed an instance declaration with a set
  257. // of initializers as a parameter list. We lose the initializers
  258. // at this point, but the instance will put it back again.
  259. result = start_type;
  260. break;
  261. default:
  262. cerr << "Internal error--invalid CPPInstanceIdentifier\n";
  263. abort();
  264. }
  265. return CPPType::new_type(result);
  266. }