File: native_c.e

package info (click to toggle)
smarteiffel 1.1-11
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 12,288 kB
  • ctags: 40,785
  • sloc: ansic: 35,791; lisp: 4,036; sh: 1,783; java: 895; ruby: 613; python: 209; makefile: 115; csh: 78; cpp: 50
file content (433 lines) | stat: -rw-r--r-- 12,655 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
-- This file is part of SmartEiffel The GNU Eiffel Compiler Tools and Libraries
--
-- SmartEiffel is  free software;  you can redistribute it and/or  modify it
-- under  the terms of the  GNU General Public License, as published by  the
-- Free Software Foundation; either version 2, or (at your option) any later
-- version.
-- SmartEiffel is distributed in the hope that it will be useful but WITHOUT 
-- ANY WARRANTY;  without  even the implied warranty  of MERCHANTABILITY  or
-- FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
-- more details.  You should have received a copy of  the GNU General Public
-- License along with SmartEiffel;  see the file COPYING.  If not,  write to
-- the Free Software Foundation,  Inc., 59 Temple Place - Suite 330,  Boston, 
-- MA 02111-1307, USA.
--
-- Copyright(C) 1994-2002: INRIA - LORIA (INRIA Lorraine) - ESIAL U.H.P.
--			   - University of Nancy 1 - FRANCE
-- Copyright(C) 2003:      INRIA - LORIA (INRIA Lorraine) - I.U.T. Charlemagne
--			   - University of Nancy 2 - FRANCE
--
--		 Dominique COLNET, Suzanne COLLIN, Olivier ZENDRA,
--			   Philippe RIBET, Cyril ADRIAN
--
-- http://SmartEiffel.loria.fr - SmartEiffel@loria.fr
--
class NATIVE_C
   --
   -- For the external "C ..." definition.
   --

inherit NATIVE redefine make end

creation make

feature {NATIVE_C_VISITOR}

   accept(visitor: NATIVE_C_VISITOR) is
      do
         visitor.visit_native_c(Current)
      end

feature

   use_current(er: EXTERNAL_ROUTINE): BOOLEAN is do end

   stupid_switch_function(run_time_set: RUN_TIME_SET; name: STRING)
      : BOOLEAN is
      do
         Result := true
      end

   stupid_switch_procedure(run_time_set: RUN_TIME_SET; name: STRING)
      : BOOLEAN is
      do
         Result := true
      end

   notify_external_assignments(args: FORMAL_ARG_LIST; rt: E_TYPE) is
      do
	 assignment_handler.from_external(start_position, args, rt)
      end
   
   c_define_function(rf8: RUN_FEATURE_8; bcn, name: STRING) is
      do
	 parse_external_tag
	 if ace.no_check then
	    if need_prototype then 
	       rf8.external_c_prototype_from(start_position)
	    end
	    rf8.c_opening
	    c_mapping(rf8.base_feature,true,true,rf8.arg_count)
	    rf8.c_closing
	 end
      end

   c_mapping_function(rf8: RUN_FEATURE_8; bcn, name: STRING) is
      do
	 parse_external_tag
         if ace.no_check then
            rf8.default_mapping_function
         else
	    if need_prototype then 
	       rf8.external_c_prototype_from(start_position)
	    end
            c_mapping(rf8.base_feature,false,true,rf8.arg_count)
         end
      end

   c_define_procedure(rf7: RUN_FEATURE_7; bcn, name: STRING) is
      do
	 parse_external_tag
	 if ace.no_check then
	    if need_prototype then 
	       rf7.external_c_prototype_from(start_position)
	    end
	    rf7.c_opening
	    c_mapping(rf7.base_feature,true,false,rf7.arg_count)
	    rf7.c_closing
	 end
      end

   c_mapping_procedure(rf7: RUN_FEATURE_7; bcn, name: STRING) is
      do
	 parse_external_tag
         if ace.no_check then
            rf7.default_mapping_procedure
         else
	    if need_prototype then 
	       rf7.external_c_prototype_from(start_position)
	    end
            c_mapping(rf7.base_feature,false,false,rf7.arg_count)
         end
      end

   jvm_add_method_for_function(rf8: RUN_FEATURE_8; bcn, name: STRING) is
      do
      end

   jvm_define_function(rf8: RUN_FEATURE_8; bcn, name: STRING) is
      do
         fe_c2jvm(rf8)
      end

   jvm_mapping_function(rf8: RUN_FEATURE_8; bcn, name: STRING) is
      do
         fe_c2jvm(rf8)
      end

   jvm_add_method_for_procedure(rf7: RUN_FEATURE_7; bcn, name: STRING) is
      do
      end

   jvm_define_procedure(rf7: RUN_FEATURE_7; bcn, name: STRING) is
      do
         fe_c2jvm(rf7)
      end

   jvm_mapping_procedure(rf7: RUN_FEATURE_7; bcn, name: STRING) is
      do
         fe_c2jvm(rf7)
      end

feature {NONE}

   parsing_done, need_prototype: BOOLEAN

   macro: BOOLEAN
	 -- Is the "macro" keyword used?

   struct: STRING
	 -- Non Void when the "struct" keyword is used. The value is then the
	 -- appropriate C cast to access the corresponding C struct.

   access: STRING
	 -- Non Void if we have to access this `struct' name field.

   type: STRING
         -- Non Void if the type is precised.

   access_mode: INTEGER
         -- Either `set_access', `get_access' or `getset_access'

   set_access, get_access, getset_access: INTEGER is unique

   signature: FIXED_ARRAY[STRING]
	 -- Non Void when some external `signature' is used.

   c_inline: BOOLEAN
	 -- When the C inline is used.

   parse_external_tag is
      local
	 file_name: STRING
      do
	 if not parsing_done then
	    parsing_done := true
	    from
	       need_prototype := true
	       mini_buffer.start_with(external_tag)
	       mini_buffer.next
	       mini_buffer.skip_separators
	    until
	       mini_buffer.is_off
	    loop
	       if mini_buffer.a_keyword(fz_macro) then
		  macro := true
		  need_prototype := false
	       elseif mini_buffer.a_keyword(fz_struct) then
		  need_prototype := false
		  struct := mini_buffer.a_type_cast
		  if mini_buffer.a_keyword(fz_set) then
		     -- *** NO WARNING ... TO BE DISCUSS
                     -- error_handler.add_position(start_position)
                     -- error_handler.append(once "%"set%" seems to be deprecated and replaced by %"access%". We are waiting for ECMA though.")
                     -- error_handler.print_as_warning
		     access := mini_buffer.a_field_or_variable_name
		     access_mode := set_access
		  elseif mini_buffer.a_keyword(fz_get) then
		     -- *** NO WARNING ... TO BE DISCUSS
                     -- error_handler.add_position(start_position)
                     -- error_handler.append(once "%"get%" seems to be deprecated and replaced by %"access%". We are waiting for ECMA though.")
                     -- error_handler.print_as_warning
		     access := mini_buffer.a_field_or_variable_name
                     access_mode := get_access
                  elseif mini_buffer.a_keyword(fz_access) then
		     access := mini_buffer.a_field_or_variable_name
                     access_mode := getset_access
		  else
		     bad_external("%"set%", %"get%", or %"access%" keyword expected.")
		  end
               elseif mini_buffer.a_keyword(fz_type) then
                  type := mini_buffer.a_field_or_variable_name
               elseif mini_buffer.a_keyword(fz_use) then
	       elseif mini_buffer.a_keyword(fz_inline) then
		  c_inline := true
		  need_prototype := false
	       elseif mini_buffer.a_keyword(fz_signature) then
                  signature := mini_buffer.a_signature
	       elseif mini_buffer.item = '(' then
                  signature := mini_buffer.a_signature
	       else
		  inspect
		     mini_buffer.item
		  when ',', '|' then
		     mini_buffer.next
		     mini_buffer.skip_separators
		  else
		     file_name := mini_buffer.a_include
		     need_prototype := false
		     cpp.include_register(start_position,file_name)
		  end
	       end
	    end
	 end
      end

   bad_external(msg: STRING) is
      do
	 error_handler.add_position(start_position)
	 error_handler.append(msg)
	 error_handler.print_as_fatal_error
      end

   c_mapping(er: EXTERNAL_ROUTINE; wrapped, is_function: BOOLEAN
	     arg_count: INTEGER) is
	 -- Where `wrapped' means that the code is wrapped inside 
	 -- some function (-no_check mode).
      local
         tcbd, stop: BOOLEAN; p: POSITION; c_code, arg: STRING
	 cc: CHARACTER; i, arg_idx: INTEGER
      do
	 p := start_position
	 cpp.put_position_comment(p)
	 if signature /= Void and then signature.upper /= arg_count then
	    bad_external("Bad number of arguments of external signature.")
	 end
	 if wrapped then
	    if ace.no_check then
	       cpp.put_trace_or_sedb_instruction(p)
	    end
	    if is_function then cpp.put_string(fz_26); end
	 else
	    tcbd := cpp.target_cannot_be_dropped
	    if tcbd then cpp.put_character(',') end
	 end
         if struct /= Void then
	    if access_mode = set_access then
	       if is_function or else arg_count /= 2 then
		  error_handler.add_position(er.start_position)
		  bad_external("Bad prototype for C struture set external.")
	       end
	    elseif access_mode = get_access then
	       if (not is_function) or else arg_count /= 1 then
		  error_handler.add_position(er.start_position)
		  bad_external("Bad prototype for C struture get external.")
	       end
	    end
	    cpp.put_string(fz_79)
	    cpp.put_string(struct)
	    cpp.put_string(once "*)")
	    if wrapped then
	       cpp.put_string(as_a1)
	    else
	       cpp.put_ith_argument(1)
	    end
	    cpp.put_string(fz_72)
	    cpp.put_string(access)
	    cpp.put_character(')')
	    if not is_function then
	       cpp.put_string(fz_47)
               if type /= Void then
                  cpp.put_character('(')
                  cpp.put_string(type)
                  cpp.put_character(')')
               end
	       if wrapped then
		  cpp.put_string(once "a2")
	       else
		  cpp.put_ith_argument(2)
	       end
	       cpp.put_character(')')
	    end
	 elseif c_inline then
	    from
	       c_code := er.external_name
	       i := 1
	    until
	       i > c_code.count
	    loop
	       cc := c_code.item(i)
	       if cc /= '$' then
		  cpp.put_character(cc)
	       else
		  from
		     i := i + 1
		     arg := once "local buffer..."
		     arg.clear
		     stop := i > c_code.count
		  until
		     stop
		  loop
		     cc := c_code.item(i)
		     inspect
			cc
		     when 'a'..'z', 'A'..'Z', '0'..'9', '_' then
			arg.extend(cc)
			i := i + 1
		     else
			stop := true
			i := i - 1
		     end
		  end
		  arg := string_aliaser.item(arg)
		  arg_idx := er.argument_rank_of(arg)
		  if arg_idx > 0 then
		     if wrapped then
			cpp.put_character('a')
			cpp.put_integer(arg_idx)
		     else
			cpp.put_ith_argument(arg_idx)
		     end
		  else
		     error_handler.add_position(er.start_position)
		     error_handler.add_position(start_position)
		     error_handler.append("Unknown %"$")
		     error_handler.append(arg)
		     error_handler.append("%" argument in external C inline definition.")
		     error_handler.print_as_fatal_error
		  end
	       end
	       i := i + 1
	    end
	 else
	    cpp.put_string(er.external_name)
	    if arg_count > 0 then
	       cpp.put_character('(')
	       from
		  i := 1
	       until
		  i > arg_count
	       loop
		  if signature /= Void then
		     cpp.put_character('(')
		     cpp.put_character('(')
		     cpp.put_string(signature.item(i))
		     cpp.put_character(')')
		  end
		  if wrapped then
		     cpp.put_character('a')
		     cpp.put_integer(i)
		  else
		     cpp.put_ith_argument(i)
		  end
		  if signature /= Void then
		     cpp.put_character(')')
		  end
		  i := i + 1
		  if i <= arg_count then cpp.put_character(',') end
	       end
	       cpp.put_character(')')
	    elseif not macro then
	       cpp.put_string(fz_c_no_args_function)
	    end
	 end
	 if wrapped then
	    if is_function then
	       cpp.put_character(')')
	    end
	    cpp.put_character(';')
	 else
	    if tcbd then cpp.put_character(')') end
	    if not is_function then cpp.put_character(';') end
	 end
	 cpp.put_character('%N')
	 cpp.put_position_comment(p)
      end

   make(et: like external_tag) is
	 -- To handle old external obsolete notation.
      local
	 oldies, new_notation: STRING; obsolete_warning: BOOLEAN
      do
	 Precursor(et)
	 oldies := et.to_string
	 if (once "C_WithoutCurrent").is_equal(oldies) then
	    obsolete_warning := true
	    new_notation := "C"
	 elseif (once "C_WithCurrent").is_equal(oldies) then
	    obsolete_warning := true
	 elseif (once "C_InlineWithoutCurrent").is_equal(oldies) then
	    obsolete_warning := true
	    new_notation := "C macro"
	 elseif (once "C_InlineWithCurrent").is_equal(oldies) then
	    obsolete_warning := true
	 end
	 if obsolete_warning then
	    error_handler.add_position(start_position)
	    error_handler.append("Since release -0.74 this external C definition is %
		      %obsolete. You should update your code with the new %
		      %ETL external specification (see SmartEiffel/tutorial/%
		      %external/C directory for examples).")
	    if new_notation /= Void then
	       et.to_string.copy(new_notation)
	       error_handler.append("Obsolete notation replaced with %"")
	       error_handler.append(new_notation)
	       error_handler.append("%". Update your code before the next release.")
	       error_handler.print_as_warning
	    else
	       error_handler.print_as_error
	    end
	 end
      end

end -- NATIVE_C