/src/tools/semantics/types/utils/liberty_type_descriptor.e

http://github.com/tybor/Liberty · Specman e · 200 lines · 161 code · 24 blank · 15 comment · 4 complexity · 3d1edef073c6a20cb9f8f96106527e05 MD5 · raw file

  1. -- This file is part of Liberty Eiffel.
  2. --
  3. -- Liberty Eiffel is free software: you can redistribute it and/or modify
  4. -- it under the terms of the GNU General Public License as published by
  5. -- the Free Software Foundation, version 3 of the License.
  6. --
  7. -- Liberty Eiffel is distributed in the hope that it will be useful,
  8. -- but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. -- GNU General Public License for more details.
  11. --
  12. -- You should have received a copy of the GNU General Public License
  13. -- along with Liberty Eiffel. If not, see <http://www.gnu.org/licenses/>.
  14. --
  15. class LIBERTY_TYPE_DESCRIPTOR
  16. inherit
  17. LIBERTY_TYPE_LISTENER
  18. redefine
  19. copy, is_equal
  20. end
  21. insert
  22. HASHABLE
  23. redefine
  24. copy
  25. end
  26. create {ANY}
  27. make
  28. feature {ANY}
  29. file: FIXED_STRING
  30. parameters: TRAVERSABLE[LIBERTY_TYPE]
  31. cluster: LIBERTY_CLUSTER is
  32. do
  33. Result := class_descriptor.cluster
  34. end
  35. name: FIXED_STRING is
  36. do
  37. Result := class_descriptor.name
  38. end
  39. position: LIBERTY_POSITION is
  40. do
  41. Result := class_descriptor.position
  42. end
  43. feature {ANY}
  44. hash_code: INTEGER
  45. is_equal (other: like Current): BOOLEAN is
  46. do
  47. if hash_code = other.hash_code then
  48. Result := same_as(other)
  49. end
  50. end
  51. same_as (other: like Current): BOOLEAN is
  52. local
  53. i, o: INTEGER
  54. do
  55. Result := class_descriptor.is_equal(other.class_descriptor) and then other.parameters.count = parameters.count
  56. from
  57. i := parameters.lower
  58. until
  59. not Result or else i > parameters.upper
  60. loop
  61. o := i - parameters.lower + other.parameters.lower
  62. Result := parameters.item(i) = other.parameters.item(o)
  63. i := i + 1
  64. end
  65. end
  66. copy (other: like Current) is
  67. do
  68. class_descriptor := other.class_descriptor.twin
  69. parameters := other.parameters
  70. end
  71. feature {LIBERTY_TYPE_DESCRIPTOR_CHANGE_LISTENER}
  72. add_change_listener (a_listener: LIBERTY_TYPE_DESCRIPTOR_CHANGE_LISTENER) is
  73. require
  74. a_listener /= Void
  75. not has_change_listener(a_listener)
  76. do
  77. change_listeners.add_last(a_listener)
  78. ensure
  79. has_change_listener(a_listener)
  80. end
  81. remove_change_listener (a_listener: LIBERTY_TYPE_DESCRIPTOR_CHANGE_LISTENER) is
  82. require
  83. a_listener /= Void
  84. has_change_listener(a_listener)
  85. do
  86. change_listeners.remove(change_listeners.fast_first_index_of(a_listener))
  87. ensure
  88. not has_change_listener(a_listener)
  89. end
  90. has_change_listener (a_listener: LIBERTY_TYPE_DESCRIPTOR_CHANGE_LISTENER): BOOLEAN is
  91. require
  92. a_listener /= Void
  93. do
  94. Result := change_listeners.fast_has(a_listener)
  95. end
  96. feature {}
  97. make (a_class_descriptor: like class_descriptor; a_parameters: like parameters) is
  98. require
  99. a_class_descriptor /= Void
  100. a_parameters /= Void
  101. do
  102. class_descriptor := a_class_descriptor
  103. parameters := a_parameters
  104. file := a_class_descriptor.file
  105. create {FAST_ARRAY[LIBERTY_TYPE_DESCRIPTOR_CHANGE_LISTENER]} change_listeners.with_capacity(2)
  106. compute_hash_code
  107. listen_to_parameters
  108. ensure
  109. class_descriptor = a_class_descriptor
  110. parameters = a_parameters
  111. end
  112. change_listeners: COLLECTION[LIBERTY_TYPE_DESCRIPTOR_CHANGE_LISTENER]
  113. feature {}
  114. compute_hash_code is
  115. local
  116. i, h: INTEGER
  117. do
  118. h := class_descriptor.hash_code
  119. from
  120. i := parameters.lower
  121. until
  122. i > parameters.upper
  123. loop
  124. h := h #*31 #+ parameters.item(i).hash_code
  125. i := i + 1
  126. end
  127. if h > 0 then
  128. hash_code := h
  129. else
  130. hash_code := ~h
  131. end
  132. end
  133. listen_to_parameters is
  134. local
  135. i: INTEGER
  136. do
  137. from
  138. i := parameters.lower
  139. until
  140. i > parameters.upper
  141. loop
  142. if not parameters.item(i).is_known then
  143. parameters.item(i).add_listener(Current)
  144. end
  145. i := i + 1
  146. end
  147. end
  148. fire_type_descriptor_changed is
  149. local
  150. i: INTEGER
  151. do
  152. from
  153. i := change_listeners.lower
  154. until
  155. i > change_listeners.upper
  156. loop
  157. change_listeners.item(i).on_type_descriptor_changed(Current)
  158. i := i + 1
  159. end
  160. end
  161. feature {LIBERTY_TYPE}
  162. on_type_known (t: LIBERTY_TYPE) is
  163. do
  164. fire_type_descriptor_changed
  165. end
  166. on_type_built (t: LIBERTY_ACTUAL_TYPE) is
  167. do
  168. -- ignored
  169. end
  170. feature {LIBERTY_TYPE_DESCRIPTOR}
  171. class_descriptor: LIBERTY_CLASS_DESCRIPTOR
  172. invariant
  173. file /= Void
  174. parameters /= Void
  175. change_listeners /= Void
  176. end