File: native_c_plus_plus.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 (677 lines) | stat: -rw-r--r-- 17,765 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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
-- 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_PLUS_PLUS

inherit NATIVE

creation make

feature {NATIVE_C_PLUS_PLUS_VISITOR}

   accept(visitor: NATIVE_C_PLUS_PLUS_VISITOR) is
      do
         visitor.visit_native_c_plus_plus(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
         standard_c_define_function(rf8, bcn, name)
         cpp.c_plus_plus_register(Current)
         rf8_memory := rf8
      end
   
   c_mapping_function(rf8: RUN_FEATURE_8; bcn, name: STRING) is
      do
         if ace.boost then
            c_mapping_external(rf8.base_feature,rf8.arg_count)
         else
            rf8.default_mapping_function
         end
      end

   c_define_procedure(rf7: RUN_FEATURE_7; bcn, name: STRING) is
      do
         standard_c_define_procedure(rf7, bcn, name)
         cpp.c_plus_plus_register(Current)
         rf7_memory := rf7
      end

   c_mapping_procedure(rf7: RUN_FEATURE_7; bcn, name: STRING) is
      do
         if ace.boost then
            c_mapping_external(rf7.base_feature,rf7.arg_count)
            cpp.put_string(fz_00)
         else
            rf7.default_mapping_procedure
         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 {C_PRETTY_PRINTER}

   c_plus_plus_definition is
      do
         if rf8_memory /= Void then
            c_plus_plus_function_definition(rf8_memory)
         else
            c_plus_plus_procedure_definition(rf7_memory)
         end
      end

feature {NONE}

   rf7_memory: RUN_FEATURE_7

   rf8_memory: RUN_FEATURE_8

   standard_c_define_function(rf8: RUN_FEATURE_8; bcn, name: STRING) is
      do
	 rf8.external_c_prototype_from(external_tag.start_position)
         if ace.no_check then
            body.clear
            body.extend('R')
            body.extend('=')
            wrapped_external_call(rf8.base_feature,rf8.arg_count)
            rf8.c_define_with_body(body)
         end
      end

   standard_c_define_procedure(rf7: RUN_FEATURE_7; bcn, name: STRING) is
      do
	 rf7.external_c_prototype_from(external_tag.start_position)
         if ace.no_check then
            body.clear
            wrapped_external_call(rf7.base_feature,rf7.arg_count)
            rf7.c_define_with_body(body)
         end
      end

   c_mapping_external(er: EXTERNAL_ROUTINE; arg_count: INTEGER) is
      local
         eruc, tcbd: BOOLEAN
      do
         eruc := use_current(er)
         if not eruc then
            tcbd := cpp.target_cannot_be_dropped
            if tcbd then
               cpp.put_character(',')
            end
         end
         cpp.put_string(er.external_name)
         cpp.put_character('(')
         if eruc then
            cpp.put_target_as_value
         end
         if arg_count > 0 then
            if eruc then
               cpp.put_character(',')
            end
            cpp.put_arguments
         end
         cpp.put_character(')')
         if not eruc and then tcbd then
            cpp.put_character(')')
         end
      end

   wrapped_external_call(er: EXTERNAL_ROUTINE; arg_count: INTEGER) is
      local
         i: INTEGER
      do
         body.append(er.external_name)
         body.extend('(')
         if use_current(er) then
            body.extend('C')
            if arg_count > 0 then
               body.extend(',')
            end
         end
         from
            i := 1
         until
            i > arg_count
         loop
            body.extend('a')
            i.append_in(body)
            i := i + 1
            if i <= arg_count then
               body.extend(',')
            end
         end
         body.append(fz_14)
      end

   c_plus_plus_function_definition(rf8: RUN_FEATURE_8) is
      require
         cpp.on_c
      local
         er: EXTERNAL_ROUTINE
	 args_count: INTEGER
      do
         er := rf8.base_feature
	 if not external_routine_memory.fast_has(er) then
	    external_routine_memory.add_last(er)
	    rf8.c_plus_plus_prototype(er)
	    body.clear
	    body.append(once "return ((")
	    rf8.result_type.c_type_for_external_in(body)
	    body.extend(')')
	    if rf8.arguments /= Void then
	       args_count := rf8.arguments.count
	    end
	    parse_external(args_count,external_tag.to_string,er)
	    body.append(once ");%N}%N")
	    cpp.put_string(body)
	 end
      end

   c_plus_plus_procedure_definition(rf7: RUN_FEATURE_7) is
      require
         cpp.on_c
      local
         er: EXTERNAL_ROUTINE
	 args_count: INTEGER
      do
         er := rf7.base_feature
	 if not external_routine_memory.fast_has(er) then
	    external_routine_memory.add_last(er)
	    rf7.c_plus_plus_prototype(er)
	    body.clear
	    if rf7.arguments /= Void then
	       args_count := rf7.arguments.count
	    end
	    parse_external(args_count,external_tag.to_string,er)
	    body.append(once ";%N}%N")
	    cpp.put_string(body)
	 end
      end

   parse_external(args_count: INTEGER; tag: STRING; er: EXTERNAL_ROUTINE) is
	 -- Lazy parsing (hope the tag is correct) of this syntax :
	 --
	 -- External -> "%"C++" [ "[" C++_feature "]" ]
	 --                     [ "(" {Type "," ...} ")" [ ":" Type ] ]
	 --                     [ "|" {include "," ...} ]
         --             "%""
	 -- C++_feature -> "static" C++_Class |
         --                "new" C++_Class |
	 --                "delete" C++_Class |
         --                "data_member" C++_Class |
	 --                C++_Class
	 -- include -> "%"" Manifest string "%"" |
	 --            "<" Manifest_string ">"
	 -- C++_Class -> Identifier include
      local
         i, state, args, parenthesis: INTEGER
         c: CHARACTER
      do
         from
	    check
	       tag.has_prefix(once "C++")
	    end
            i := 4
         until
            i > tag.count
         loop
            c := tag.item(i)
            inspect
               state
            when 0 then
	       -- Looks like : "C++"
               inspect
                  c
               when ' ', '%T', '%N' then
                  i := i + 1
               when '[' then
                  i := i + 1
                  state := 1
               else
                  i := error_at(i,tag,state)
               end
            when 1 then
	       -- Looks like : "C++ [" :
               if c = ' ' or else c = '%T' or else c = '%N' then
                  i := i + 1
               elseif i = tag.substring_index(once "static ",i) then
		  i := i + 7
		  i := parse_cpp_class(i,tag)
		  body.append(once "::")
		  body.append(er.c_plus_plus_name)
		  state := 2
               elseif i = tag.substring_index(once "new ",i) then
                  i := i + 4
		  body.append(once "new ")
		  i := parse_cpp_class(i,tag)
                  state := 3
               elseif i = tag.substring_index(once "delete ",i) then
                  i := i + 7
		  body.append(once "delete((")
		  i := parse_cpp_class(i,tag)
                  body.append(once "*)a1)")
		  if args /= 0 or else args_count /= 1 then
		     i := error_at(i,tag,state)
		  else
		     state := 4
		  end
               elseif i = tag.substring_index(once "data_member ",i) then
		  i := error_at(i,tag,state); -- really necessary ?
               else
		  args := args + 1
		  body.append(once "((")
		  i := parse_cpp_class(i,tag)
		  body.append(once "*)a1)->")
                  body.append(er.c_plus_plus_name)
                  state := 5
               end
            when 2 then
	       -- Looks like : "C++ [ static C++_Class " :
               inspect
                  c
               when ' ', '%T', '%N' then
                  i := i + 1
               when ']' then
                  i := i + 1
                  state := 8
	       else
		  i := error_at(i,tag,state)
	       end
            when 3 then
	       -- Looks like : "C++ [ new C++_Class" :
               inspect
                  c
               when ' ', '%T', '%N' then
                  i := i + 1
               when ']' then
                  i := i + 1
                  state := 6
	       else
		  i := error_at(i,tag,state)
	       end
            when 4 then
	       -- Looks like : "C++ [ delete C++_Class " :
               inspect
                  c
               when ' ', '%T', '%N' then
                  i := i + 1
               when ']' then
                  i := i + 1
                  state := 10
	       else
		  i := error_at(i,tag,state)
	       end
	    when 5 then
	       -- Looks like : "C++ [C++_Class" :
               inspect
                  c
               when ' ', '%T', '%N' then
                  i := i + 1
               when ']' then
                  i := i + 1
                  state := 7
	       else
		  i := error_at(i,tag,state)
	       end
            when 6 then
	       -- Looks like : "C++ [ new C++_CLASS ]" :
               inspect
                  c
               when ' ', '%T', '%N' then
                  i := i + 1
               when '(' then
		  i := parse_args(i,tag,args,args_count)
                  state := 13
	       else
		  i := error_at(i,tag,state)
	       end
            when 7 then
	       -- Looks like : "C++ [ C++_Class ] :
               inspect
                  c
               when ' ', '%T', '%N' then
                  i := i + 1
               when '(' then
		  i := parse_args(i,tag,args,args_count)
                  state := 11
	       else
		  i := error_at(i,tag,state)
	       end
            when 8 then
	       -- Looks like : "C++ [ static C++_Class ]" :
               inspect
                  c
               when ' ', '%T', '%N' then
                  i := i + 1
               when '(' then
		  i := parse_args(i,tag,args,args_count)
                  state := 9
	       else
		  i := error_at(i,tag,state)
	       end
            when 9 then
	       -- Looks like : "C++ [ static C++_Class ] ({type, ...})" :
	       i := i + 1
            when 10 then
	       -- Looks like : "C++ [ delete C++_Class ]" :
               inspect
                  c
               when ' ', '%T', '%N' then
                  i := i + 1
               when '(' then
		  parenthesis := parenthesis + 1
		  i := i + 1
               when ')' then
		  if parenthesis > 1 then
		     i := error_at(i,tag,state)
		  else
		     i := i + 1
		  end
	       else
		  i := error_at(i,tag,state)
	       end
            when 11 then
	       -- Looks like : "C++ * ( { type , ... } )" :
               inspect
                  c
               when ':' then
		  state := 12
               when '|' then
		  state := 13
	       else
	       end
	       i := i + 1
            when 12 then
	       -- Looks like : "C++ * ( { type , ... } ) : " :
               inspect
                  c
               when '|' then
		  state := 13
	       else
	       end
	       i := i + 1
            when 13 then
	       -- Looks like : "C++ * |" :
	       inspect
		  c
	       when ' ', '%T', '%N' then
		  i := i + 1
	       when '<', '%"' then
		  i := parse_include(i,tag)
	       when ',' then
		  i := i + 1
	       else
		  i := error_at(i,tag,state)
	       end
            end
         end
      end

   parse_args(s: INTEGER; tag: STRING; args, args_count: INTEGER): INTEGER is
      require
	 tag.item(s) = '('
      local
	 i, parenthesis, a, state: INTEGER
	 c: CHARACTER
      do
	 from
	    a := args
	    state := 40
	    body.extend('(')
	    i := s + 1
	 until
	    i > tag.count or else Result > i
	 loop
	    c := tag.item(i)
	    inspect
	       state
	    when 40 then
	       -- Before arg type :
               inspect
                  c
               when ' ', '%T', '%N' then
                  i := i + 1
	       when ')' then
	          Result := i + 1
	       else
		  body.extend('(')
		  state := 41
	       end
	    when 41 then
	       -- Inside some type :
               inspect
                  c
               when ',' then
		  i := i + 1
		  body.extend(')')
		  a := a + 1
		  body.extend('a')
		  a.append_in(body)
		  if a < args_count then
		     body.extend(',')
		  end
		  state := 40
               when '(' then
		  body.extend(c)
		  parenthesis := parenthesis + 1
		  i := i + 1
               when ')' then
		  if parenthesis = 0 then
		     body.extend(')')
		     a := a + 1
		     body.extend('a')
		     a.append_in(body)
		     Result := i + 1
		  else
		     body.extend(c)
		     parenthesis := parenthesis - 1
		     i := i + 1
		  end
	       else
		  body.extend(c)
		  i := i + 1
	       end
	    end
	 end
	 body.extend(')')
	 if Result = 0 or else a /= args_count then
	    Result := error_at(i,tag,state)
	 end
      end

   parse_include(s: INTEGER; tag: STRING): INTEGER is
      require
	 (once "%"<").has(tag.item(s))
      local
         include: STRING
	 i: INTEGER; c: CHARACTER
      do
	 from
	    buffer.clear
	    buffer.extend(tag.item(s))
	    i := s + 1
	 until
	    i > tag.count or else Result > i
	 loop
	    c := tag.item(i)
	    inspect
	       c
	    when '%"', '>' then
	       buffer.extend(c)
	       include := buffer.twin
	       if not include_memory.has(include) then
		  include_memory.add_last(include)
		  cpp.add_include(include)
	       end
	       Result := i + 1
	    else
	       buffer.extend(c)
	       i := i + 1
	    end
	 end
	 if Result = 0 then
	    Result := error_at(i,tag,20)
	 end
      end

   parse_cpp_class(s: INTEGER; tag: STRING): INTEGER is
      local
	 i, state: INTEGER
	 c: CHARACTER
      do
	 from
	    state := 30
	    i := s
	 until
	    i > tag.count or else state > 32
	 loop
	    c := tag.item(i)
	    inspect
	       state
	    when 30 then
	       -- Nothing :
	       inspect
		  c
	       when ' ', '%T', '%N' then
		  i := i + 1
	       when '<', '>', '%"', '(', ')' then
		  i := error_at(i,tag,state)
	       else
		  state := 31
	       end
	    when 31 then
	       -- Inside Identifier :
	       inspect
		  c
	       when ' ', '%T', '%N' then
		  state := 32
	       when '<', '%"' then
		  i := parse_include(i,tag)
		  state := 33
	       else
		  body.extend(c)
		  i := i + 1
	       end
	    when 32 then
	       -- After Identifier :
	       inspect
		  c
	       when ' ', '%T', '%N' then
		  i := i + 1
	       when '<', '%"' then
		  i := parse_include(i,tag)
		  state := 33
	       else
		  i := error_at(i,tag,state)
	       end
	    end
	 end
	 Result := i
      end

   error_at(error_index: INTEGER; tag: STRING; state: INTEGER): INTEGER is
      do
         if rf7_memory /= Void then
            error_handler.add_position(rf7_memory.base_feature.start_position)
         else
            error_handler.add_position(rf8_memory.base_feature.start_position)
         end
         error_handler.append("Bad external %"C++%" definition.%Nexternal %"")
         error_handler.append(tag)
         error_handler.append("%"%N_________")
         from
            Result := 1
         until
            Result > error_index
         loop
            error_handler.extend('_')
            Result := Result + 1
         end
         error_handler.extend('^')
         error_handler.append("%NSee SmartEiffel/tutorial/external/C++ directory %
		   %for more information.%N (Internal state = ")
         error_handler.append(state.to_string)
         error_handler.extend(')')
         error_handler.print_as_error
         Result := tag.count + 1
      end

   include_memory: FIXED_ARRAY[STRING] is
      once
         !!Result.with_capacity(4)
      end

   external_routine_memory: FIXED_ARRAY[EXTERNAL_ROUTINE] is
      once
         !!Result.with_capacity(4)
      end

end -- NATIVE_C_PLUS_PLUS