PageRenderTime 25ms CodeModel.GetById 14ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/src/tools/wrappers-generator/c_field.e

http://github.com/tybor/Liberty
Specman e | 177 lines | 112 code | 18 blank | 47 comment | 5 complexity | 58b32f93112aa588cdf98b8fdf80c1bd MD5 | raw file
  1class C_FIELD
  2
  3inherit
  4   IDENTIFIED_NODE
  5   CONTEXTED_NODE
  6   FILED_NODE
  7   TYPED_NODE
  8   STORABLE_NODE
  9   WRAPPER_FEATURE
 10
 11create {GCCXML_TREE}
 12   make
 13
 14feature {ANY}
 15   store
 16      local
 17         container_type: COMPOSED_NODE
 18      do
 19         container_type := composed_types.reference_at(context)
 20         if container_type /= Void then
 21            -- Add Current to its container
 22            container_type.fields.add_last(Current)
 23         else
 24            -- Fields may be part of a composed type that will be not be wrapped, for example PThread structures when wrapping GObject introspection facilities. Therefore its container will not be present in the composed types!
 25            log("Ignoring field #(1) of #(2) in %"#(3)%"%N" # c_string_name # context.to_utf8)
 26         end
 27      end
 28
 29   is_fundamental: BOOLEAN
 30      do
 31         Result := types.at(dequalify(type)).is_fundamental
 32      end
 33
 34   is_void: BOOLEAN False
 35
 36   has_wrapper: BOOLEAN
 37      local field_type: C_TYPE
 38      do
 39          -- You may want to write "Result := types.at(dequalify(type)).has_wrapper" but that's simplicistic
 40          field_type := types.reference_at(dequalify(type))
 41          if field_type/=Void then
 42              Result:=field_type.has_wrapper
 43          end
 44      rescue
 45          log("C_FIELD.has_wrapper failed%N")
 46          print_run_time_stack
 47          die_with_code (exit_failure_code)
 48          -- if type/=Void then
 49          --     field_type := types.at(dequalify(type))
 50          --     if field_type /= Void then
 51          --         Result := field_type.has_wrapper
 52          --     else
 53          --         log(once "Warning: C field #(1) in line  #(2) is of an unknown type!%N"
 54          --         # c_string_name # &line )
 55          --         Result := False
 56          --     end
 57          -- else
 58          --     log(once "Warning: C field #(1) in line #(2) is typeless!%N"
 59          --     # c_string_name # &line )
 60          --     Result := False
 61          -- end
 62      end
 63
 64   wrapper_type: STRING
 65      do
 66         Result := types.at(dequalify(type)).wrapper_type
 67      end
 68
 69   container: COMPOSED_NODE
 70         -- The node representing the C entity that contains Current field
 71      do
 72         if stored_parent = Void then
 73            stored_parent := composed_types.at(context)
 74         end
 75         Result := stored_parent
 76      ensure
 77         Result /= Void
 78      end
 79
 80   is_to_be_emitted: BOOLEAN True
 81         -- all fields of a composed node will be emitted (if possible)
 82
 83   wrap_on (a_stream: OUTPUT_STREAM)
 84      do
 85         not_yet_implemented
 86      end
 87
 88   append_getter_and_setter (a_structure_name: STRING)
 89      require
 90         a_structure_name /= Void
 91      local
 92         setter, getter, getter_description, setter_description: STRING; eiffel_field: ABSTRACT_STRING
 93      do
 94		  if is_anonymous then
 95			  log(once "Anonymous field in #(1) at line #(2): not wrappable." # a_structure_name # &line_row)
 96			  queries.append(once "%T-- Anonymous field at line #(1).%N" # &line_row)
 97		  elseif not is_public then
 98			  log(once "Private field #(1) in #(2) not wrapped." # c_string_name # a_structure_name)
 99			  queries.append(once "%T-- Unwrapped private field #(1).%N" # c_string_name)
100		  elseif not has_wrapper then
101			  log(once "Field #(1) in #(2) doesn't have a wrapper.%N" # c_string_name # a_structure_name)
102			  queries.append(once "%T-- Unwrappable field #(1).%N"#c_string_name)
103			  -- doesn't have a valid wrapper
104		  else -- we can actually wrap it
105            eiffel_field := adapt(c_string_name, once "_field")
106            setter := a_structure_name + once "_set_" + eiffel_field
107            getter := a_structure_name + once "_get_" + eiffel_field
108            setter.to_lower
109            getter.to_lower
110            -- TODO: descriptions := feature_descriptions.reference_at(a_structure_name)
111            getter_description := "-- TODO: getter description%N"
112            setter_description := "-- TODO: setter description%N"
113            -- if descriptions /= Void then
114            --    setter_description:=formatted_description(descriptions.reference_at(eiffel_field))
115            --    getter_description:=formatted_description(descriptions.reference_at(eiffel_field))
116            -- else setter_description:=once ""; getter_description:=once ""
117            -- end
118            -- log(once "Field #(1).#(2) (%"#(3)%", %"#(4)%" " #
119            -- a_structure_name # c_field # setter_description # getter_description)
120
121            queries.append(once "   #(1) (a_structure: POINTER): #(2) %N%
122            %         -- Query for #(3) field of #(4) structure.%N%
123            %         #(5)%N%
124            %      external %"plug_in%"%N%
125            %      alias %"{%N%
126            %         location: %".%"%N%
127            %         module_name: %"plugin%"%N%
128            %         feature_name: %"#(1)%"%N%
129            %      }%"%N%
130            %      end%N%N" # getter # wrapper_type #  c_string_name # a_structure_name
131			# getter_description)
132
133            setters.append(once "   #(1) (a_structure: POINTER; a_value: #(2)) %N%
134            %         -- Setter for #(3) field of #(4) structure.%N%
135            %         #(5)%N%
136            %      external %"plug_in%"%N%
137            %      alias %"{%N%
138            %         location: %".%"%N%
139            %         module_name: %"plugin%"%N%
140            %         feature_name: %"#(1)%"%N%
141            %      }%"%N%
142            %      end%N%N" # setter # wrapper_type # c_string_name # a_structure_name # setter_description)
143            -- log(once "command, ")
144            -- -- Note: Type safety is assured by Eiffel and GCC-XML so we can
145            -- -- be less type-strict-paranoid here and use some type-casts.
146            -- TODO: the next print_on commands cause compiler crash if
147            -- they operate on a ROPE, made using the "|" operator instead
148            -- of +.
149
150            ("#define #(1)(a_structure) (((#(2) #(3)*) (a_structure))->#(4))%N%N"
151			# getter # container.c_type # container.c_string_name 
152			# c_string_name).print_on(include)
153            -- the above line previously was written, rather unreadably like this ("#define " + getter + "(a_structure) (((" + container.c_type + " " + container.c_string_name + "*) (a_structure))->" + c_string_name + ")%N%N").print_on(include)
154            ("#define #(1)(a_structure,a_value) do {(((#(2) #(3)*)(a_structure)))->#(4) = (a_value);}while(0)%N%N"
155			# setter # container.c_type # container.c_string_name
156			# c_string_name).print_on(include)
157            -- the above line previously was written, rather unreadably like this  ("#define " + setter + "(a_structure,a_value) do {(((" + container.c_type + " " + container.c_string_name + "*)(a_structure)))->" + c_string_name + " = (a_value);}while(0)%N%N").print_on(include)
158		end
159      end
160
161feature {} -- Implementation
162   stored_parent: COMPOSED_NODE
163
164end -- class C_FIELD
165
166-- Copyright (C) 2008-2017: Paolo Redaelli
167
168-- wrappers-generator  is free software: you can redistribute it and/or modify it
169-- under the terms of the GNU General Public License as publhed by the Free
170-- Software Foundation, either version 2 of the License, or (at your option)
171-- any later version.
172-- wrappers-generator is distributed in the hope that it will be useful, but
173-- WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
174-- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
175-- more details.
176-- You should have received a copy of the GNU General Public License along with
177-- th program.  If not, see <http://www.gnu.org/licenses/>.