forked from FreeFem/FreeFem-sources
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AFunction2.cpp
1138 lines (1014 loc) · 33.3 KB
/
AFunction2.cpp
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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// -*- Mode : c++ -*-
//
// SUMMARY :
// USAGE :
// ORG :
// AUTHOR : Frederic Hecht
// E-MAIL : [email protected]
//
/*
This file is part of Freefem++
Freefem++ is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
Freefem++ 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Freefem++; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
//#pragma dont_inline on
//#pragma inline_depth(1)
#include "config-wrapper.h"
#include <complex>
#include "AFunction.hpp"
#include <cstdarg>
#include <cstring>
#include "error.hpp"
#include "lex.hpp"
#include "RNM.hpp"
#include "Operator.hpp"
// for exec routine
#include "rgraph.hpp"
#include "InitFunct.hpp"
vector<pair<const E_Routine*,int> > *debugstack=0;
class vectorOfInst : public E_F0mps { public:
int n;
Expression * v;
vectorOfInst(int k): n(k),v(new Expression[k]) {ffassert(v);
for(int i=0;i<n;++i) v[i]=0; }
~vectorOfInst(){ delete [] v;}
bool empty() const {return n;}
AnyType operator()(Stack s) const {
for (int i=0;i<n;++i)
{
ffassert(v[i]);
(*(v[i]))(s);
}
return Nothing;
}
void eval(Stack s,int j=-1) const {
if(j>=0) j = n-j;
else j =0;
if(verbosity>999) cout << " eval vectorOfInst " << j << " " << n << endl;
for (int i=j;i<n;++i)
{
ffassert(v[i]);
(*(v[i]))(s);
}
}
private:
vectorOfInst(const vectorOfInst &);
void operator=(const vectorOfInst &);
};
double VersionNumber();
OneOperator::pair_find OneOperator::Find(const ArrayOfaType & at)const
{
const OneOperator *w=0,*oo;
int nn=0,p=-10000;
/* for (oo=this;oo;oo=oo->next)
if (oo->pref>=p && oo->WithOutCast(at))
{
if(p<oo->pref) {nn=0;p=oo->pref;}
nn++;
w=oo;}
if (nn) return make_pair(w,nn);*/
for (int ncast=0;ncast<=n;ncast++) // loop on the number of cast
{
p=-10000;
for (oo=this;oo;oo=oo->next)
if (oo->pref>=p && oo->WithCast(at,ncast))
{
if(p<oo->pref) {nn=0;p=oo->pref;}
nn++;
w=oo;}
if (nn) return make_pair(w,nn);
}
for (oo=this;oo;oo=oo->next)
if (oo->WithCast(at))
{nn++;
w=oo;}
return make_pair(w,nn);
}
OneOperator::pair_find OneOperator::FindWithOutCast(const ArrayOfaType & at)const
{
const OneOperator *w=0,*oo;
int n=0;
for (oo=this;oo;oo=oo->next)
if (oo->WithOutCast(at))
{n++;
w=oo;}
return make_pair(w,n);
}
// <<FindSameR>>
OneOperator* OneOperator::FindSameR(const ArrayOfaType & at)
{
if (this==tnull) return 0;
OneOperator *oo,*r;
int n=0;
for (oo=this;oo;oo=oo->next)
{
//if (oo->WithOutCast(at))
if (at==*oo) n++,r=oo;
else if (oo->WithOutCast(at)) n++,r=oo;
// if (n) cout << " \t " << oo << " " << *oo << " <-----> " << at << " n =" << n << endl;
}
// if (n>1) cout << "FindSameR " << n << endl;
// if (n) cout << *r << " <-----> " << at << " n =" << n << endl;
return n==1 ? r : 0;
}
void OneOperator::Show(ostream &f) const
{
const OneOperator *oo;
for (oo=this;oo;oo=oo->next)
f << "\t (" << *oo << ")\n";
}
void OneOperator::Show(const ArrayOfaType & at,ostream &f) const
{
const OneOperator *oo;
int n=0,np=0;
for (oo=this;oo;oo=oo->next)
if (oo->WithOutCast(at)) {n++;f << "\t (" << *oo << ")\n";}
if(n==0)
for (oo=this;oo;oo=oo->next)
if (oo->WithCast(at)) {
n++;
if (oo->pref) np++;
if (oo->pref)
f << " c(" << oo->pref << ") \t (" << *oo << ")\n" ;
else f << " \t c(" << *oo << ")\n";
}
if (n==0)
{
f << " List of choices "<< endl;
Show(f);
}
else if (np != 1)
f << " We have ambiguity " << n << endl;
}
const OneOperator * Polymorphic::Find(const char *op, const ArrayOfaType &at) const
{
const_iterator i=m.find(op);
if (i!=m.end())
{
OneOperator::pair_find r=i->second->Find(at);
if (r.second==1) return r.first;
}
return 0;
}
const OneOperator * Polymorphic::FindWithOutCast(const char *op, const ArrayOfaType &at) const
{
const_iterator i=m.find(op);
if (i!=m.end())
{
OneOperator::pair_find r=i->second->FindWithOutCast(at);
if (r.second==1) return r.first;
}
return 0;
}
void Polymorphic::Show(const char *op,const ArrayOfaType & at,ostream &f) const
{
const_iterator i=m.find(op);
if (i==m.end()) f << " unknow " << op << " operator " << endl;
else i->second->Show(at,f);
}
// <<C_F0_constructor_pop_char_basicAC_F0_impl>> cf [[file:AFunction.hpp::C_F0_constructor_pop_char_basicAC_F0_decl]]
C_F0::C_F0(const Polymorphic * poly,const char *op,const basicAC_F0 & p)
{
ArrayOfaType at(p);
if (poly) { // a Polymorphic => polymorphisme
const OneOperator * ff=poly->Find(op,at);
if (ff) {
if( verbosity>9999) {cout << endl;
poly->Show(op,at,cout);
cout << op << ": (in " << at << ") => " << " " << *ff<< "\n\n";}
// [[file:AFunction.hpp::OneOperator_code2]]
*this= ff->code2(p);
}
else
{ if(mpirank==0)
{
cerr << " error operator " << op << " " << at << endl;
poly->Show(op,at,cerr);
// const OneOperator * ff=
poly->Find(op,at);
}
CompileError();
}
}
else {
// no polymorphisme
if(mpirank==0){
cerr << " const Polymorphic * poly,const char *op,const basicAC_F0 & p) " << endl;
cerr << op << " " << at << endl;
}
CompileError();
}
}
// operator without parameter
C_F0::C_F0(const Polymorphic * pop,const char *op)
{
basicAC_F0 p;
p=0;
*this= C_F0(pop,op,p);
}
// operator unaire
C_F0::C_F0(const Polymorphic * pop,const char *op,const C_F0 & aa)
{
basicAC_F0 p;
C_F0 a(aa);
p=a;
*this= C_F0(pop,op,p);
}
// <<C_F0_constructor_binary_operator>> operator binaire
C_F0::C_F0(const Polymorphic * pop,const char *op,const C_F0 & a,const C_F0 & b)
{
C_F0 tab[2]={a,b};
basicAC_F0 p;
p=make_pair<int,C_F0*>(2,tab);
// [[file:AFunction.hpp::C_F0_constructor_pop_char_basicAC_F0_decl]]
*this=C_F0(pop,op,p);
}
// operator trinaire
C_F0::C_F0(const Polymorphic * pop,const char *op,const C_F0 & a,const C_F0 & b,const C_F0 & c)
{
C_F0 tab[3]={a,b,c};
basicAC_F0 p;
p=make_pair<int,C_F0*>(3,tab);
*this= C_F0(pop,op,p);
}
OneOperator::~OneOperator(){
OneOperator * d=next;
next=0;
if(! CodeAlloc::cleanning) // hash FH (pour les fuite de mémoire)
while(d)
{
OneOperator * dd=d->next;
d->next=0;
delete d;
d=dd;
}
}
OneOperator::OneOperator(aType rr)
: ArrayOfaType(),r(rr),next(0),pref(0) {throwassert(r);}
OneOperator::OneOperator(aType rr,aType a)
: ArrayOfaType(a,false),r(rr),next(0),pref(0) {throwassert(rr && a );}
OneOperator::OneOperator(aType rr,aType a,aType b)
: ArrayOfaType(a,b,false),r(rr),next(0),pref(0) {
throwassert(rr && a && b);}
OneOperator::OneOperator(aType rr,aType a,aType b,aType c)
: ArrayOfaType(a,b,c,false),r(rr),next(0),pref(0)
{throwassert(rr && a && b && c);}
OneOperator::OneOperator(aType rr,aType a,aType b,aType c,aType d)
: ArrayOfaType(a,b,c,d,false),r(rr),next(0),pref(0)
{throwassert(rr && a && b && c);}
OneOperator::OneOperator(aType rr,aType a,aType b,aType c,aType d,aType e)
: ArrayOfaType(a,b,c,d,e,false),r(rr),next(0),pref(0)
{throwassert(rr && a && b && c && d);} // Added by Fabian Dortu (5 parameters)
OneOperator::OneOperator(aType rr,aType a,aType b,aType c,aType d,aType e,aType f)
: ArrayOfaType(a,b,c,d,e,f,false),r(rr),next(0),pref(0)
{throwassert(rr && a && b && c && d && e && f);} // Added by Fabian Dortu (6 parameters)
OneOperator::OneOperator(aType rr,aType a,aType b,aType c,aType d,aType e,aType f, aType g)
: ArrayOfaType(a,b,c,d,e,f,g,false),r(rr),next(0),pref(0)
{throwassert(rr && a && b && c && d && e && f && g);} // Added by Fabian Dortu (7 parameters)
OneOperator::OneOperator(aType rr,aType a,aType b,aType c,aType d,aType e,aType f, aType g, aType h)
: ArrayOfaType(a,b,c,d,e,f,g,h,false),r(rr),next(0),pref(0)
{throwassert(rr && a && b && c && d && e && f && g && h);} // Added by Fabian Dortu (8 parameters)
OneOperator::OneOperator(aType rr,aType a,aType b,aType c,aType d,aType e,aType f, aType g, aType h, aType i)
: ArrayOfaType(a,b,c,d,e,f,g,h,i,false),r(rr),next(0),pref(0)
{throwassert(rr && a && b && c && d && e && f && g && h && i);} // Added by Fabian Dortu (9 parameters)
OneOperator::OneOperator(aType rr,aType a,aType b,aType c,aType d,aType e,aType f, aType g, aType h, aType i, aType j)
: ArrayOfaType(a,b,c,d,e,f,g,h,i,j,false),r(rr),next(0),pref(0)
{throwassert(rr && a && b && c && d && e && f && g && h && i && j);} // Added by Fabian Dortu (10 parameters)
OneOperator::OneOperator(aType rr,const ArrayOfaType &ta)
: ArrayOfaType(ta),r(rr),next(0),pref(0)
{throwassert(rr);}
OneOperator::OneOperator(aType rr,bool ellipse)
: ArrayOfaType(ellipse),r(rr),next(0),pref(0)
{throwassert(rr );}
OneOperator::OneOperator(aType rr,const ListOfId *l)
: ArrayOfaType(l),r(rr),next(0),pref(0)
{throwassert(rr );}
void Polymorphic::Addp(const char * op,Value pp,...) const
{
pair<iterator,bool> p=m.insert(pair<const Key,Value>(op,pp));
Value f= p.first->second;
if (!p.second) // not insert => old
*f += *pp;
va_list ap;
va_start(ap,pp);
for(pp=va_arg(ap,OneOperator * );pp;pp=va_arg(ap,OneOperator * ))
*f += *pp;
/* if ( ! strlen(op) )
{ // no polymorphisme
if(m.size() !=1 || !f->Simple()) {
cerr << " no polymorphisme and polymorphisme are mixed " << endl;
// for_each(m.begin,m.end(),ShowOn_cerr);
CompileError();
}
} */
}
void Polymorphic::Add(const char * op,Value *pp) const
{
if (*pp)
{
pair<iterator,bool> p=m.insert(pair<const Key,Value>(op,*pp));
Value f= p.first->second;
if (!p.second) // not insert => old
*f += **pp;
pp++;
for(;*pp;pp++)
*f += **pp;
/*if ( ! strlen(op) )
{ // no polymorphisme
if(m.size() !=1 || !f->Simple()) {
cerr << " no polymorphisme and polymorphisme are mixed " << endl;
// for_each(m.begin,m.end(),ShowOn_cerr);
CompileError();
} } */ }
}
// <<FindType>>
int FindType(const char * name)
{
C_F0 r;
ListOfTOfId::const_iterator i=tables_of_identifier.begin();
for(;i!=tables_of_identifier.end();++i)
{
TableOfIdentifier * ti=*i;
r = ti->Find(name);
if (r.NotNull()) return r.TYPEOFID();
}
return 0;
}
/// <<Find>> uses [[file:global.cpp::tables_of_identifier]]
C_F0 Find(const char * name)
{
C_F0 r;
ListOfTOfId::const_iterator i=tables_of_identifier.begin();
for(;i!=tables_of_identifier.end();++i)
{
TableOfIdentifier * ti=*i;
r = ti->Find(name);
if (r.NotNull()) return r;
}
if(mpirank==0)
cerr << " The Identifier " << name << " does not exist " << endl;
CompileError();
return r;
}
vectorOfInst* TableOfIdentifier::newdestroy()
{
int k=0;
// cout << "\n\t List of destroy variables " << m.size() << " : " ;
for (pKV * i=listofvar;i;i=i->second.next)
{
if (i->second.del && i->second.first->ExistDestroy() )
// cout << i->first << ", " ;
assert(i->second.first);
if (i->second.del && i->second.first->ExistDestroy() ) k++;
}
ffassert(nIdWithDelete==k);
// cout << endl;
/* old code
ListOfInst *l=new ListOfInst(k);
for (pKV * i=listofvar;i;i=i->second.next)
if (i->second.del && i->second.first->ExistDestroy())
l->Add(i->second.first->Destroy(i->second) );
*/
// new code
vectorOfInst * l= new vectorOfInst(k);
int j=0;
for (pKV * i=listofvar;i;i=i->second.next)
if (i->second.del && i->second.first->ExistDestroy())
l->v[j++]=i->second.first->Destroy(i->second) ;
ffassert(j==k);
return l;
}
C_F0 TableOfIdentifier::destroy() {return C_F0(newdestroy());}
void TableOfIdentifier::clear()
{
for (iterator i=m.begin();i!=m.end();++i)
{
// delete i->first;
}
m.clear();
}
Expression basicForEachType::Destroy(const C_F0 & e) const
{
return destroy ? NewExpression(destroy,e) : (Expression) e;
}
basicForEachType::~basicForEachType()
{
if(casting) delete casting;
ti.clear();
}
basicForEachType::basicForEachType(const type_info & k,
const size_t s,
const E_F1_funcT_Type * p,
basicForEachType *rr,
Function1 iv,Function1 id, Function1 dreturn)
: ktype(&k),//ktypefunc(0),
size(s),
un_ptr_type(rr?rr:this),
casting(0), // no casting to
un_ptr(p),
InitExp(iv),
DoOnReturn(dreturn),
//funct_type(0),
destroy(id) {}
void basicForEachType::SetArgs(const ListOfId *lid) const
{ SHOWVERB(cout << "SetArgs::\n ") ;ffassert(lid==0 || lid->size()==0);}
TableOfIdentifier::TableOfIdentifier() : listofvar(0),nIdWithDelete(0) {}
TableOfIdentifier:: ~TableOfIdentifier() {}
Block::Block(Block * f):fatherblock(f),top(f?f->top:BeginOffset*sizeof(void*)),topmax(top)
{
itabl=tables_of_identifier.insert(tables_of_identifier.begin(),&table);
}
Block::~Block(){}
/*
vectorOfInst * Block::newclose(Block *& c) {
tables_of_identifier.erase(itabl);
c=fatherblock;
if (fatherblock) {fatherblock->topmax=topmax;
fatherblock->top=top;}
vectorOfInst * r;
r = table.newdestroy();
delete this;
return r;}
*/
vectorOfInst * Block::snewclose(Block *& c) {
Block * a=c;
tables_of_identifier.erase(a->itabl);
c=a->fatherblock;
if (a->fatherblock) {a->fatherblock->topmax=a->topmax;
a->fatherblock->top=a->top;}
vectorOfInst * r;
r = a->table.newdestroy();
delete a;
return r;}
CC_F0 Block::close(Block *& c,C_F0 ins)
{
CListOfInst inst;
CC_F0 cins; cins=ins;
inst = cins;
inst.setclose(Block::snewclose(c));
CC_F0 rr;
rr=inst;
return rr;
}
CC_F0 Block::close(Block *& c,CListOfInst inst) {
inst.setclose(Block::snewclose(c));
CC_F0 rr;
rr=inst;
return rr;
}
Block * Block::open(Block *& cb)
{
return cb = new Block(cb);
}
const Type_Expr & TableOfIdentifier::New(Key k,const Type_Expr & v,bool del)
{
if( this != &Global) {
if ( Global.m.find(k) != Global.m.end() )
{
if(mpirank==0 && (verbosity>0))
cerr << "\n *** Warning The identifier " << k << " hide a Global identifier \n";
}
}
pair<iterator,bool> p=m.insert(pKV(k,Value(v,listofvar,del)));
listofvar = &*m.find(k);
if (!p.second)
{
if(mpirank==0) {
cerr << " The identifier " << k << " exists \n";
cerr << " \t the existing type is " << *p.first->second.first << endl;
cerr << " \t the new type is " << *v.first << endl;
}
CompileError();
}
if(del && v.first->ExistDestroy() )
{
nIdWithDelete++;
if(verbosity>9999) cout << "\n \t add ExistDestroy" << endl;
}
return v;
}
void TableOfIdentifier::Add(Key k,Key op,OneOperator *p0,OneOperator *p1,
OneOperator *p2,OneOperator *p3,OneOperator *p4,OneOperator *p5,OneOperator *p6)
{
iterator i= m.find(k);
if (i==m.end()) // new
{
Value poly0=Value(atype<Polymorphic*>(),new Polymorphic(),listofvar);
i=m.insert(pair<const Key,Value>(k,poly0)).first;
listofvar= &*i;
}
const Polymorphic * p= dynamic_cast<const Polymorphic *>(i->second.second);
if ( !p) {
if(mpirank==0)
cerr << k << " is not a Polymorphic id " << endl;
CompileError();
}
p->Add(op,p0,p1,p2,p3,p4,p5,p6);
}
ArrayOfaType::ArrayOfaType(const ListOfId * l)
: n(l->size()),t(new aType[n]),ellipse(false)
{
for (int i=0;i<n;i++)
{
t[i]=(*l)[i].r;
if ( ! t[i])
{
if(mpirank==0)
cerr << " Argument " << i << " '"<< (*l)[i].id << "' without type\n";
CompileError("DCL routine: Argument without type ");
}
}
}
bool ArrayOfaType::WithOutCast( const ArrayOfaType & a) const
{
if ( ( !ellipse && (a.n != n)) || (ellipse && n > a.n) ) return false;
for (int i=0;i<n;i++)
if (! a.t[i]->SametypeRight(t[i]))
return false;
// cerr << " TRUE " << endl;
return true;
}
bool ArrayOfaType::WithCast( const ArrayOfaType & a,int nbcast) const
{
if ( ( !ellipse && (a.n != n)) || (ellipse && n > a.n) ) return false;
for (int i=0;i<n;i++)
if ( a.t[i]->SametypeRight(t[i])) ;
else if (! t[i]->CastingFrom(a.t[i])) return false;
else if ( --nbcast <0) return false;
return true;
}
void basicForEachType::AddCast(CastFunc f1,CastFunc f2,CastFunc f3,CastFunc f4,
CastFunc f5,CastFunc f6,CastFunc f7,CastFunc f8)
{
CastFunc ff[]={f1,f2,f3,f4,f5,f6,f7,f8,0};
for (int i=0;ff[i];i++)
{
ffassert(this == *ff[i] );
if (casting->FindSameR(*ff[i]))
{
if(mpirank==0)
{
cerr << " The casting to " << *ff[i] << " exists " << endl;
cerr << " List of cast " << endl;
casting->Show(cerr);
}
CompileError();
}
if (casting) *casting += *ff[i];
else casting = ff[i];
/*
if( ! mapofcast.insert(make_pair<const aType,CastFunc>(ff[i]->a,ff[i])).second)
{
cerr << " The casting to "<< *this << " from " << ff[i]->a << " exists " << endl;
cerr << " List of cast " << endl;
for_each(mapofcast.begin(),mapofcast.end(),CerrCast);
CompileError();
} */
}
}
ostream & operator<<(ostream & f,const OneOperator & a)
{
// for(const OneOperator * tt=&a;tt;tt=tt->next)
f << "\t " << * (a.r) << " : " <<(const ArrayOfaType &) a;
return f;
}
ostream & operator<<(ostream & f,const Polymorphic & a)
{
Polymorphic::const_iterator i;
if(&a==E_F0::tnull) return f << "Null " << endl;
for (i=a.m.begin();i!=a.m.end();i++)
{
f << " operator" << i->first << " : " << endl;
i->second->Show(f);
}
return f;
}
ostream & operator<<(ostream & f,const ArrayOfaType & a)
{
for (int i=0;i<a.n;i++)
f << (i ? ", " : " ") << *a.t[i];
if (a.ellipse ) f << "... ";
else f << " ";
return f;}
ostream & operator<<(ostream & f,const TableOfIdentifier & t )
{
TableOfIdentifier::const_iterator i;
for(i=t.m.begin();i!=t.m.end();i++)
{
TableOfIdentifier::Value v=i->second;
f << i->first << ": " << *v.first << " <- " ;
const Polymorphic * p=dynamic_cast<const Polymorphic *>(v.second);
if(p) f << "Polymorphic " << *p << endl;
else f << " Simple @" << v.second << endl;
}
return f;
}
Expression NewExpression(Function1 f,Expression a)
{
ffassert(f);
return new E_F0_Func1(f,a);
}
Expression NewExpression(Function2 f,Expression a,Expression b)
{
ffassert(f);
return new E_F0_Func2(f,a,b);
}
// <<ShowType>>
void ShowType(ostream & f)
{
map<const string,basicForEachType *>::const_iterator i;
for(i=map_type.begin();i!=map_type.end();i++)
{
f << " --"<< i->first <<" = " ;
i->second->Show(f) ;
f << endl;
}
}
void basicForEachType::Show(ostream & f) const {
f << " " <<* this << endl;
if (casting) casting->Show(f) ;
if (ti.m.size())
{
TableOfIdentifier::const_iterator mc=ti.m.begin();
TableOfIdentifier::const_iterator end=ti.m.end();
for (;mc != end;mc++)
{
f << " " << mc->first << ", type :" << *mc->second.first << endl;
const Polymorphic * op =dynamic_cast<const Polymorphic *>(mc->second.second) ;
if ( op ) f << *op << endl;
}
}
}
E_Routine::E_Routine(const Routine * routine,const basicAC_F0 & args)
: code(routine->ins),
//clean(routine->clean),
rt(routine->tret),
nbparam(args.size()),
param(new Expression[nbparam]),
name(routine->name)
{
assert(routine->ins);
for (int i=0;i<args.size();i++) // bug pb copie des string dec 2007 FH ???????????????
{
if(verbosity>10000) cout << "E_Routine " << *routine->param[i].r << " <- " << *args[i].left() << endl;
param[i]=routine->param[i].r->CastTo(args[i]);
}
};
E_Routine::~E_Routine() {
if(verbosity>10000) cout << "~E_Routine()"<< endl;
delete [] param;}
/*
struct CleanE_Routine {
const E_Routine * er;
Stack s;
AnyType * l;
CleanE_Routine(const E_Routine * r,Stack ss,AnyType *ll): er(r),s(ss),l(ll) {}
~CleanE_Routine() {
// cout << " Clean E_routine " << er <<endl;
// (*er->clean)(s);
delete [] l;
}
};
*/
AnyType E_Routine::operator()(Stack s) const {
//cout << " E_Routine:: push " <<debugstack <<" " << TheCurrentLine << " " <<debugstack->size() << endl;
debugstack->push_back(pair<const E_Routine*,int>(this,TheCurrentLine));
const int lgsave=BeginOffset*sizeof(void*);
char save[lgsave];
AnyType ret=Nothing;
memcpy(save,s,lgsave); // save
AnyType *listparam;
// Add2StackOfPtr2Free(s,new CleanE_Routine(this,s,listparam=new AnyType[nbparam]));
Add2StackOfPtr2FreeA(s,listparam=new AnyType[nbparam]);
// AnyType *listparam =Add2StackOfPtr2FreeA(s,new AnyType[nbparam]);
//
// WhereStackOfPtr2Free(s)->Add2StackOfPtr2Free(s,listparam);
// to day the memory gestion of the local variable are static,
for (int i=0;i<nbparam;i++)
listparam[i]= (*param[i])(s); // set of the parameter
Stack_Ptr<AnyType>(s,ParamPtrOffset) = listparam;
WhereStackOfPtr2Free(s)=new StackOfPtr2Free(s);// FH mars 2006
try {
ret=(*code)(s);
}
catch( E_exception & e) {
// cout << " catch " << e.what() << " clean & throw " << endl;
if (e.type() == E_exception::e_return)
ret = e.r;
else
ErrorExec("E_exception: break or contine not in loop ",1);
}
catch(...) { // clean and rethrow the exception
//::delete [] listparam;
// (*clean)(s);
WhereStackOfPtr2Free(s)->clean(); // FH mars 2005
memcpy(s,save,lgsave); // restore
TheCurrentLine=debugstack->back().second;
debugstack->pop_back();
// cout << " E_Routine:: ... pop " <<debugstack <<" " << TheCurrentLine << " " <<debugstack->size() << endl;
throw ;
}
// (*clean)(s); // the clean is done in CleanE_Routine delete .
// delete [] listparam; after return
memcpy(s,save,lgsave); // restore
TheCurrentLine=debugstack->back().second;
debugstack->pop_back();
// cout << " E_Routine:: pop " <<debugstack <<" " << TheCurrentLine << " " <<debugstack->size() << endl;
// il faudrait que les variable locale soit detruire apres le return
// cf routine clean, pour le cas ou l'on retourne un tableau local.
// plus safe ????? FH. (fait 2008)
// mais pb si a = f()+g() OK les pointeurs des instruction sont detruit
// en fin d'instruction programme de l'appelant FH 2007
// ... ou alors changer le return ???? qui doit copie le resultat.. (voir)
return ret;
}
extern Block *currentblock;// def in lg.ypp
void ListOfInst::Add(const C_F0 & ins) {
if( (!ins.Empty()) ) {
if( verbosity > 9999 )
cout << " Add " << n << " " << TheCurrentLine << endl;
if (n%nx==0){
Expression * l = new Expression [n+nx];
int * ln = new int [n+nx];
int * lsd = new int [n+nx];
for (int i=0;i<n;i++) {
l[i]=list[i];
ln[i]=linenumber[i];
lsd[i]=lsldel[i];
}
delete [] list;
delete [] linenumber;
delete [] lsldel;
list =l;
linenumber=ln;
lsldel=lsd;
}
throwassert(list);
linenumber[n]= TheCurrentLine;
lsldel[n]=currentblock->nIdWithDelete();
// NbNewVarWithDel=0;
list[n++] = ins;
}
}
/// <<ListOfInst::operator()>> Iteratively calls each item in the local array #list of type #Expression
AnyType ListOfInst::operator()(Stack s) const {
AnyType r;
int i;
double s0=CPUtime(),s1=s0,ss0=s0;
StackOfPtr2Free * sptr = WhereStackOfPtr2Free(s);
try { // modif FH oct 2006
for (i=0;i<n;i++)
{
TheCurrentLine=linenumber[i] ;
r=(*list[i])(s);
sptr->clean(); // modif FH mars 2006 clean Ptr
s1=CPUtime();
if (showCPU)
cout << " CPU: "<< i <<" " << linenumber[i] << ": " << s1-s0 << "s" << " " << s1-ss0 << "s" << " / " << " " <<lsldel[i] <<endl;
s0=CPUtime();
}
if(atclose) { if(verbosity>99999) cout << " ListOfInst::operator() " << n << " " << atclose->n << " // " << lsldel[n-1] << endl;
atclose->eval(s,atclose->n);}// Add for sep 2016 FH
}
catch( E_exception & e)
{
if(verbosity>999) cout << " catch E_exception " << i << " " << lsldel[i] << endl;
if(atclose) {atclose->eval(s,lsldel[i]);}// Add sep 2016 FH for clean init varaible
if (e.type() != E_exception::e_return)
sptr->clean(); // pour ne pas detruire la valeur retourne ... FH jan 2007
throw; // rethow
}
catch(...)
{
if(verbosity>999) cout << " catch .... " << i << " " << lsldel[i] << endl;
if(atclose) {atclose->eval(s,lsldel[i]);}
sptr->clean();
throw;
}
return r;}
/*
AnyType E_block::operator()(Stack s) const {
StackOfPtr2Free * sptr = WhereStackOfPtr2Free(s);
if (clean)
{
try {
for (int i=0;i<n;i++) {
TheCurrentLine=linenumber[i];
(*code[i])(s);
sptr->clean();
}}
catch( E_exception & e) {
(*clean)(s);
if (e.type() != E_exception::e_return)
sptr->clean();
throw; // rethow
}
catch(...) { // catch all for cleanning
(*clean)(s);
sptr->clean();
// if(verbosity>50)
// cout << " catch " << e.what() << " clean & throw " << endl;
// throw(e);
throw; // rethow
}
(*clean)(s);
sptr->clean();
}
else // not catch exception if no clean (optimization}
for (int i=0;i<n;i++)
{
(*code[i])(s);
sptr->clean(); // mars 2006 FH clean Ptr
}
return Nothing;
}
*/
void ShowDebugStack()
{
if (mpisize)
cerr << " current line = " << TheCurrentLine
<< " mpirank " << mpirank << " / " << mpisize <<endl;
else
cerr << " current line = " << TheCurrentLine << endl;
if(debugstack)
for (int i=0; i<debugstack->size(); ++i)
{
cerr << " call " << debugstack->at(i).first->name<< " at line "
<<debugstack->at(i).second << endl;
}
}
int E_F0::Optimize(deque<pair<Expression,int> > &l,MapOfE_F0 & m, size_t & n)
{
int rr = find(m);
if (rr) return rr;
if( (verbosity / 10)% 10 == 1)
cout << "\n new expression : " << n << " mi=" << MeshIndependent()<< " " << typeid(*this).name()
<< " :" << *this << endl;
return insert(this,l,m,n);
}
class E_F0para :public E_F0 { public:
const int i;
AnyType operator()(Stack s) const {
// return (* Stack_Ptr<Expression>(s,ParamPtrOffset)[i])(s);
return Stack_Ptr<AnyType>(s,ParamPtrOffset)[i];
}
E_F0para(int ii) : i(ii){}
};
Routine::Routine(aType tf,aType tr,const char * iden, ListOfId *l,Block * & cb)
: OneOperator(tr,l),offset(cb->OffSet(sizeof(void*))),
tfunc(tf),tret(tr),name(iden),param(*l),
currentblock(new Block(cb)),ins(0)//,clean(0)
{
delete l; // add FH 24032005 (trap )
cb = currentblock;
// cout <<"Routine: tf = " << *tf << " " << *tr << endl;
for (size_t i=0;i<param.size();i++)
{
// cout << "Routine " << i << " ref= " << param[i].ref << " " << *param[i].r << " " << *param[i].r->right() << endl;
currentblock->NewID(param[i].r,param[i].id,C_F0(new E_F0para(i),// modif FH 2007
param[i].r),
// (param[i].ref ? param[i].r : param[i].r->right() ),
!param[i].ref);
}
}
Block * Routine::Set(CListOfInst instrs)
{
instrs.setclose(Block::snewclose(currentblock));
ins=instrs;
// clean = (C_F0) currentblock->close(currentblock);
return currentblock;}
E_F0 * Routine::code(const basicAC_F0 & args) const
{
return new E_Routine(this,args);
}