-
Notifications
You must be signed in to change notification settings - Fork 171
/
Copy pathasr_utils.h
5458 lines (5056 loc) · 228 KB
/
asr_utils.h
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
#ifndef LFORTRAN_ASR_UTILS_H
#define LFORTRAN_ASR_UTILS_H
#include <functional>
#include <map>
#include <set>
#include <limits>
#include <libasr/assert.h>
#include <libasr/asr.h>
#include <libasr/string_utils.h>
#include <libasr/utils.h>
#include <libasr/casting_utils.h>
#include <complex>
#define ADD_ASR_DEPENDENCIES(current_scope, final_sym, current_function_dependencies) ASR::symbol_t* asr_owner_sym = nullptr; \
if(current_scope->asr_owner && ASR::is_a<ASR::symbol_t>(*current_scope->asr_owner) ) { \
asr_owner_sym = ASR::down_cast<ASR::symbol_t>(current_scope->asr_owner); \
} \
SymbolTable* temp_scope = current_scope; \
if (asr_owner_sym && temp_scope->get_counter() != ASRUtils::symbol_parent_symtab(final_sym)->get_counter() && \
!ASR::is_a<ASR::ExternalSymbol_t>(*final_sym) && !ASR::is_a<ASR::Variable_t>(*final_sym)) { \
if (ASR::is_a<ASR::AssociateBlock_t>(*asr_owner_sym) || ASR::is_a<ASR::Block_t>(*asr_owner_sym)) { \
temp_scope = temp_scope->parent; \
if (temp_scope->get_counter() != ASRUtils::symbol_parent_symtab(final_sym)->get_counter()) { \
current_function_dependencies.push_back(al, ASRUtils::symbol_name(final_sym)); \
} \
} else { \
current_function_dependencies.push_back(al, ASRUtils::symbol_name(final_sym)); \
} \
} \
#define ADD_ASR_DEPENDENCIES_WITH_NAME(current_scope, final_sym, current_function_dependencies, dep_name) ASR::symbol_t* asr_owner_sym = nullptr; \
if(current_scope->asr_owner && ASR::is_a<ASR::symbol_t>(*current_scope->asr_owner) ) { \
asr_owner_sym = ASR::down_cast<ASR::symbol_t>(current_scope->asr_owner); \
} \
SymbolTable* temp_scope = current_scope; \
if (asr_owner_sym && temp_scope->get_counter() != ASRUtils::symbol_parent_symtab(final_sym)->get_counter() && \
!ASR::is_a<ASR::ExternalSymbol_t>(*final_sym) && !ASR::is_a<ASR::Variable_t>(*final_sym)) { \
if (ASR::is_a<ASR::AssociateBlock_t>(*asr_owner_sym) || ASR::is_a<ASR::Block_t>(*asr_owner_sym)) { \
temp_scope = temp_scope->parent; \
if (temp_scope->get_counter() != ASRUtils::symbol_parent_symtab(final_sym)->get_counter()) { \
current_function_dependencies.push_back(al, dep_name); \
} \
} else { \
current_function_dependencies.push_back(al, dep_name); \
} \
} \
namespace LCompilers {
namespace ASRUtils {
ASR::symbol_t* import_class_procedure(Allocator &al, const Location& loc,
ASR::symbol_t* original_sym, SymbolTable *current_scope);
ASR::asr_t* make_Binop_util(Allocator &al, const Location& loc, ASR::binopType binop,
ASR::expr_t* lexpr, ASR::expr_t* rexpr, ASR::ttype_t* ttype);
ASR::asr_t* make_Cmpop_util(Allocator &al, const Location& loc, ASR::cmpopType cmpop,
ASR::expr_t* lexpr, ASR::expr_t* rexpr, ASR::ttype_t* ttype);
static inline double extract_real(const char *s) {
// TODO: this is inefficient. We should
// convert this in the tokenizer where we know most information
std::string x = s;
x = replace(x, "d", "e");
x = replace(x, "D", "E");
return std::atof(x.c_str());
}
static inline ASR::expr_t* EXPR(const ASR::asr_t *f)
{
return ASR::down_cast<ASR::expr_t>(f);
}
static inline ASR::stmt_t* STMT(const ASR::asr_t *f)
{
return ASR::down_cast<ASR::stmt_t>(f);
}
static inline ASR::case_stmt_t* CASE_STMT(const ASR::asr_t *f)
{
return ASR::down_cast<ASR::case_stmt_t>(f);
}
static inline ASR::ttype_t* TYPE(const ASR::asr_t *f)
{
return ASR::down_cast<ASR::ttype_t>(f);
}
static inline ASR::FunctionType_t* get_FunctionType(const ASR::Function_t* x) {
return ASR::down_cast<ASR::FunctionType_t>(x->m_function_signature);
}
static inline ASR::FunctionType_t* get_FunctionType(const ASR::Function_t& x) {
return ASR::down_cast<ASR::FunctionType_t>(x.m_function_signature);
}
static inline ASR::symbol_t *symbol_get_past_external(ASR::symbol_t *f)
{
if (f && f->type == ASR::symbolType::ExternalSymbol) {
ASR::ExternalSymbol_t *e = ASR::down_cast<ASR::ExternalSymbol_t>(f);
if( e->m_external == nullptr ) {
return nullptr;
}
LCOMPILERS_ASSERT(!ASR::is_a<ASR::ExternalSymbol_t>(*e->m_external));
return e->m_external;
} else {
return f;
}
}
template <typename T>
Location get_vec_loc(const Vec<T>& args) {
LCOMPILERS_ASSERT(args.size() > 0);
Location args_loc;
args_loc.first = args[0].loc.first;
args_loc.last = args[args.size() - 1].loc.last;
return args_loc;
}
static inline ASR::FunctionType_t* get_FunctionType(ASR::symbol_t* x) {
ASR::symbol_t* a_name_ = ASRUtils::symbol_get_past_external(x);
ASR::FunctionType_t* func_type = nullptr;
if( ASR::is_a<ASR::Function_t>(*a_name_) ) {
func_type = ASR::down_cast<ASR::FunctionType_t>(
ASR::down_cast<ASR::Function_t>(a_name_)->m_function_signature);
} else if( ASR::is_a<ASR::Variable_t>(*a_name_) ) {
func_type = ASR::down_cast<ASR::FunctionType_t>(
ASR::down_cast<ASR::Variable_t>(a_name_)->m_type);
} else if( ASR::is_a<ASR::ClassProcedure_t>(*a_name_) ) {
ASR::Function_t* func = ASR::down_cast<ASR::Function_t>(
ASRUtils::symbol_get_past_external(
ASR::down_cast<ASR::ClassProcedure_t>(a_name_)->m_proc));
func_type = ASR::down_cast<ASR::FunctionType_t>(func->m_function_signature);
} else {
LCOMPILERS_ASSERT(false);
}
return func_type;
}
static inline const ASR::symbol_t *symbol_get_past_external(const ASR::symbol_t *f)
{
if (f->type == ASR::symbolType::ExternalSymbol) {
ASR::ExternalSymbol_t *e = ASR::down_cast<ASR::ExternalSymbol_t>(f);
LCOMPILERS_ASSERT(!ASR::is_a<ASR::ExternalSymbol_t>(*e->m_external));
return e->m_external;
} else {
return f;
}
}
static inline ASR::ttype_t *type_get_past_pointer(ASR::ttype_t *f)
{
if (ASR::is_a<ASR::Pointer_t>(*f)) {
ASR::Pointer_t *e = ASR::down_cast<ASR::Pointer_t>(f);
LCOMPILERS_ASSERT(!ASR::is_a<ASR::Pointer_t>(*e->m_type));
return e->m_type;
} else {
return f;
}
}
static inline ASR::ttype_t *type_get_past_allocatable(ASR::ttype_t *f)
{
if (ASR::is_a<ASR::Allocatable_t>(*f)) {
ASR::Allocatable_t *e = ASR::down_cast<ASR::Allocatable_t>(f);
return type_get_past_allocatable(e->m_type);
} else {
return f;
}
}
static inline ASR::ttype_t *type_get_past_array(ASR::ttype_t *f)
{
if (ASR::is_a<ASR::Array_t>(*f)) {
ASR::Array_t *e = ASR::down_cast<ASR::Array_t>(f);
LCOMPILERS_ASSERT(!ASR::is_a<ASR::Array_t>(*e->m_type));
return e->m_type;
} else {
return f;
}
}
static inline int extract_kind_from_ttype_t(const ASR::ttype_t* type) {
if (type == nullptr) {
return -1;
}
switch (type->type) {
case ASR::ttypeType::Array: {
return extract_kind_from_ttype_t(ASR::down_cast<ASR::Array_t>(type)->m_type);
}
case ASR::ttypeType::Integer : {
return ASR::down_cast<ASR::Integer_t>(type)->m_kind;
}
case ASR::ttypeType::UnsignedInteger : {
return ASR::down_cast<ASR::UnsignedInteger_t>(type)->m_kind;
}
case ASR::ttypeType::Real : {
return ASR::down_cast<ASR::Real_t>(type)->m_kind;
}
case ASR::ttypeType::Complex: {
return ASR::down_cast<ASR::Complex_t>(type)->m_kind;
}
case ASR::ttypeType::Character: {
return ASR::down_cast<ASR::Character_t>(type)->m_kind;
}
case ASR::ttypeType::Logical: {
return ASR::down_cast<ASR::Logical_t>(type)->m_kind;
}
case ASR::ttypeType::Pointer: {
return extract_kind_from_ttype_t(ASR::down_cast<ASR::Pointer_t>(type)->m_type);
}
case ASR::ttypeType::Allocatable: {
return extract_kind_from_ttype_t(ASR::down_cast<ASR::Allocatable_t>(type)->m_type);
}
default : {
return -1;
}
}
}
static inline void set_kind_to_ttype_t(ASR::ttype_t* type, int kind) {
if (type == nullptr) {
return;
}
switch (type->type) {
case ASR::ttypeType::Array: {
set_kind_to_ttype_t(ASR::down_cast<ASR::Array_t>(type)->m_type, kind);
break;
}
case ASR::ttypeType::Integer : {
ASR::down_cast<ASR::Integer_t>(type)->m_kind = kind;
break;
}
case ASR::ttypeType::UnsignedInteger : {
ASR::down_cast<ASR::UnsignedInteger_t>(type)->m_kind = kind;
break;
}
case ASR::ttypeType::Real : {
ASR::down_cast<ASR::Real_t>(type)->m_kind = kind;
break;
}
case ASR::ttypeType::Complex: {
ASR::down_cast<ASR::Complex_t>(type)->m_kind = kind;
break;
}
case ASR::ttypeType::Character: {
ASR::down_cast<ASR::Character_t>(type)->m_kind = kind;
break;
}
case ASR::ttypeType::Logical: {
ASR::down_cast<ASR::Logical_t>(type)->m_kind = kind;
break;
}
case ASR::ttypeType::Pointer: {
set_kind_to_ttype_t(ASR::down_cast<ASR::Pointer_t>(type)->m_type, kind);
break;
}
case ASR::ttypeType::Allocatable: {
set_kind_to_ttype_t(ASR::down_cast<ASR::Allocatable_t>(type)->m_type, kind);
break;
}
default : {
return;
}
}
}
static inline ASR::Variable_t* EXPR2VAR(const ASR::expr_t *f)
{
return ASR::down_cast<ASR::Variable_t>(symbol_get_past_external(
ASR::down_cast<ASR::Var_t>(f)->m_v));
}
static inline ASR::Function_t* EXPR2FUN(const ASR::expr_t *f)
{
return ASR::down_cast<ASR::Function_t>(symbol_get_past_external(
ASR::down_cast<ASR::Var_t>(f)->m_v));
}
static inline ASR::ttype_t* expr_type(const ASR::expr_t *f)
{
return ASR::expr_type0(f);
}
static inline ASR::ttype_t* subs_expr_type(std::map<std::string, ASR::ttype_t*> subs,
const ASR::expr_t *expr) {
ASR::ttype_t *ttype = ASRUtils::expr_type(expr);
if (ASR::is_a<ASR::TypeParameter_t>(*ttype)) {
ASR::TypeParameter_t *tparam = ASR::down_cast<ASR::TypeParameter_t>(ttype);
ttype = subs[tparam->m_param];
}
return ttype;
}
static inline ASR::ttype_t* symbol_type(const ASR::symbol_t *f)
{
switch( f->type ) {
case ASR::symbolType::Variable: {
return ASR::down_cast<ASR::Variable_t>(f)->m_type;
}
case ASR::symbolType::EnumType: {
return ASR::down_cast<ASR::EnumType_t>(f)->m_type;
}
case ASR::symbolType::ExternalSymbol: {
return symbol_type(ASRUtils::symbol_get_past_external(f));
}
case ASR::symbolType::Function: {
return ASRUtils::expr_type(
ASR::down_cast<ASR::Function_t>(f)->m_return_var);
}
default: {
throw LCompilersException("Cannot return type of, " +
std::to_string(f->type) + " symbol.");
}
}
return nullptr;
}
static inline ASR::abiType symbol_abi(const ASR::symbol_t *f)
{
switch( f->type ) {
case ASR::symbolType::Variable: {
return ASR::down_cast<ASR::Variable_t>(f)->m_abi;
}
case ASR::symbolType::EnumType: {
return ASR::down_cast<ASR::EnumType_t>(f)->m_abi;
}
case ASR::symbolType::ExternalSymbol: {
return symbol_abi(ASR::down_cast<ASR::ExternalSymbol_t>(f)->m_external);
}
case ASR::symbolType::Function: {
return ASRUtils::get_FunctionType(*ASR::down_cast<ASR::Function_t>(f))->m_abi;
}
default: {
throw LCompilersException("Cannot return ABI of, " +
std::to_string(f->type) + " symbol.");
}
}
return ASR::abiType::Source;
}
static inline ASR::ttype_t* get_contained_type(ASR::ttype_t* asr_type, int overload=0) {
switch( asr_type->type ) {
case ASR::ttypeType::List: {
return ASR::down_cast<ASR::List_t>(asr_type)->m_type;
}
case ASR::ttypeType::Set: {
return ASR::down_cast<ASR::Set_t>(asr_type)->m_type;
}
case ASR::ttypeType::Dict: {
switch( overload ) {
case 0:
return ASR::down_cast<ASR::Dict_t>(asr_type)->m_key_type;
case 1:
return ASR::down_cast<ASR::Dict_t>(asr_type)->m_value_type;
default:
return asr_type;
}
}
case ASR::ttypeType::Enum: {
ASR::Enum_t* enum_asr = ASR::down_cast<ASR::Enum_t>(asr_type);
ASR::EnumType_t* enum_type = ASR::down_cast<ASR::EnumType_t>(enum_asr->m_enum_type);
return enum_type->m_type;
}
case ASR::ttypeType::Pointer: {
ASR::Pointer_t* pointer_asr = ASR::down_cast<ASR::Pointer_t>(asr_type);
return pointer_asr->m_type;
}
case ASR::ttypeType::Allocatable: {
ASR::Allocatable_t* pointer_asr = ASR::down_cast<ASR::Allocatable_t>(asr_type);
return pointer_asr->m_type;
}
default: {
return asr_type;
}
}
}
static inline ASR::array_physical_typeType extract_physical_type(ASR::ttype_t* e) {
switch( e->type ) {
case ASR::ttypeType::Array: {
return ASR::down_cast<ASR::Array_t>(e)->m_physical_type;
}
case ASR::ttypeType::Pointer: {
return extract_physical_type(ASRUtils::type_get_past_pointer(e));
}
case ASR::ttypeType::Allocatable: {
return extract_physical_type(ASRUtils::type_get_past_allocatable(e));
}
default:
throw LCompilersException("Cannot extract the physical type of " +
std::to_string(e->type) + " type.");
}
}
static inline ASR::abiType expr_abi(ASR::expr_t* e) {
switch( e->type ) {
case ASR::exprType::Var: {
return ASRUtils::symbol_abi(ASR::down_cast<ASR::Var_t>(e)->m_v);
}
case ASR::exprType::StructInstanceMember: {
return ASRUtils::symbol_abi(ASR::down_cast<ASR::StructInstanceMember_t>(e)->m_m);
}
case ASR::exprType::ArrayReshape: {
return ASRUtils::expr_abi(ASR::down_cast<ASR::ArrayReshape_t>(e)->m_array);
}
case ASR::exprType::GetPointer: {
return ASRUtils::expr_abi(ASR::down_cast<ASR::GetPointer_t>(e)->m_arg);
}
case ASR::exprType::ComplexIm: {
return ASRUtils::expr_abi(ASR::down_cast<ASR::ComplexIm_t>(e)->m_arg);
}
case ASR::exprType::ComplexRe: {
return ASRUtils::expr_abi(ASR::down_cast<ASR::ComplexRe_t>(e)->m_arg);
}
case ASR::exprType::ArrayPhysicalCast: {
return ASRUtils::expr_abi(ASR::down_cast<ASR::ArrayPhysicalCast_t>(e)->m_arg);
}
default:
throw LCompilersException("Cannot extract the ABI of " +
std::to_string(e->type) + " expression.");
}
}
static inline char *symbol_name(const ASR::symbol_t *f)
{
switch (f->type) {
case ASR::symbolType::Program: {
return ASR::down_cast<ASR::Program_t>(f)->m_name;
}
case ASR::symbolType::Module: {
return ASR::down_cast<ASR::Module_t>(f)->m_name;
}
case ASR::symbolType::Function: {
return ASR::down_cast<ASR::Function_t>(f)->m_name;
}
case ASR::symbolType::GenericProcedure: {
return ASR::down_cast<ASR::GenericProcedure_t>(f)->m_name;
}
case ASR::symbolType::Struct: {
return ASR::down_cast<ASR::Struct_t>(f)->m_name;
}
case ASR::symbolType::EnumType: {
return ASR::down_cast<ASR::EnumType_t>(f)->m_name;
}
case ASR::symbolType::UnionType: {
return ASR::down_cast<ASR::UnionType_t>(f)->m_name;
}
case ASR::symbolType::Variable: {
return ASR::down_cast<ASR::Variable_t>(f)->m_name;
}
case ASR::symbolType::ExternalSymbol: {
return ASR::down_cast<ASR::ExternalSymbol_t>(f)->m_name;
}
case ASR::symbolType::ClassProcedure: {
return ASR::down_cast<ASR::ClassProcedure_t>(f)->m_name;
}
case ASR::symbolType::CustomOperator: {
return ASR::down_cast<ASR::CustomOperator_t>(f)->m_name;
}
case ASR::symbolType::AssociateBlock: {
return ASR::down_cast<ASR::AssociateBlock_t>(f)->m_name;
}
case ASR::symbolType::Block: {
return ASR::down_cast<ASR::Block_t>(f)->m_name;
}
case ASR::symbolType::Requirement: {
return ASR::down_cast<ASR::Requirement_t>(f)->m_name;
}
case ASR::symbolType::Template: {
return ASR::down_cast<ASR::Template_t>(f)->m_name;
}
default : throw LCompilersException("Not implemented");
}
}
static inline bool get_class_proc_nopass_val(ASR::symbol_t* func_sym) {
func_sym = ASRUtils::symbol_get_past_external(func_sym);
bool nopass = false;
if (ASR::is_a<ASR::ClassProcedure_t>(*func_sym)) {
nopass = ASR::down_cast<ASR::ClassProcedure_t>(func_sym)->m_is_nopass;
}
return nopass;
}
static inline void encode_dimensions(size_t n_dims, std::string& res,
bool use_underscore_sep=false) {
if( n_dims == 0 ) {
return ;
}
if( use_underscore_sep ) {
res += "_";
} else {
res += "[";
}
for( size_t i = 0; i < n_dims; i++ ) {
if( use_underscore_sep ) {
res += "_";
} else {
res += ":";
}
if( i == n_dims - 1 ) {
if( use_underscore_sep ) {
res += "_";
} else {
res += "]";
}
} else {
if( use_underscore_sep ) {
res += "_";
} else {
res += ", ";
}
}
}
}
static inline std::string type_to_str(const ASR::ttype_t *t)
{
switch (t->type) {
case ASR::ttypeType::Integer: {
return "integer";
}
case ASR::ttypeType::UnsignedInteger: {
return "unsigned integer";
}
case ASR::ttypeType::Real: {
return "real";
}
case ASR::ttypeType::Complex: {
return "complex";
}
case ASR::ttypeType::Logical: {
return "logical";
}
case ASR::ttypeType::Character: {
return "character";
}
case ASR::ttypeType::Tuple: {
return "tuple";
}
case ASR::ttypeType::Set: {
return "set";
}
case ASR::ttypeType::Dict: {
return "dict";
}
case ASR::ttypeType::List: {
return "list";
}
case ASR::ttypeType::StructType: {
return ASRUtils::symbol_name(ASR::down_cast<ASR::StructType_t>(t)->m_derived_type);
}
case ASR::ttypeType::Class: {
return ASRUtils::symbol_name(ASR::down_cast<ASR::Class_t>(t)->m_class_type);
}
case ASR::ttypeType::Union: {
return "union";
}
case ASR::ttypeType::CPtr: {
return "type(c_ptr)";
}
case ASR::ttypeType::Pointer: {
return type_to_str(ASRUtils::type_get_past_pointer(
const_cast<ASR::ttype_t*>(t))) + " pointer";
}
case ASR::ttypeType::Allocatable: {
return type_to_str(ASRUtils::type_get_past_allocatable(
const_cast<ASR::ttype_t*>(t))) + " allocatable";
}
case ASR::ttypeType::Array: {
ASR::Array_t* array_t = ASR::down_cast<ASR::Array_t>(t);
std::string res = type_to_str(array_t->m_type);
encode_dimensions(array_t->n_dims, res, false);
return res;
}
case ASR::ttypeType::TypeParameter: {
ASR::TypeParameter_t* tp = ASR::down_cast<ASR::TypeParameter_t>(t);
return tp->m_param;
}
case ASR::ttypeType::SymbolicExpression: {
return "symbolic expression";
}
case ASR::ttypeType::FunctionType: {
ASR::FunctionType_t* ftp = ASR::down_cast<ASR::FunctionType_t>(t);
std::string result = "(";
for( size_t i = 0; i < ftp->n_arg_types; i++ ) {
result += type_to_str(ftp->m_arg_types[i]) + ", ";
}
result += "return_type: ";
if( ftp->m_return_var_type ) {
result += type_to_str(ftp->m_return_var_type);
} else {
result += "void";
}
result += ")";
return result;
}
default : throw LCompilersException("Not implemented " + std::to_string(t->type) + ".");
}
}
static inline std::string type_to_str_with_type(const ASR::ttype_t *t) {
std::string type = type_to_str(t);
std::string kind = std::to_string(extract_kind_from_ttype_t(t));
return type + "(" + kind + ")";
}
static inline std::string type_to_str_with_substitution(const ASR::ttype_t *t,
std::map<std::string, ASR::ttype_t*> subs)
{
if (ASR::is_a<ASR::TypeParameter_t>(*t)) {
ASR::TypeParameter_t* t_tp = ASR::down_cast<ASR::TypeParameter_t>(t);
t = subs[t_tp->m_param];
}
switch (t->type) {
case ASR::ttypeType::Pointer: {
return type_to_str_with_substitution(ASRUtils::type_get_past_pointer(
const_cast<ASR::ttype_t*>(t)), subs) + " pointer";
}
case ASR::ttypeType::Allocatable: {
return type_to_str_with_substitution(ASRUtils::type_get_past_allocatable(
const_cast<ASR::ttype_t*>(t)), subs) + " allocatable";
}
case ASR::ttypeType::Array: {
ASR::Array_t* array_t = ASR::down_cast<ASR::Array_t>(t);
std::string res = type_to_str_with_substitution(array_t->m_type, subs);
encode_dimensions(array_t->n_dims, res, false);
return res;
}
case ASR::ttypeType::FunctionType: {
ASR::FunctionType_t* ftp = ASR::down_cast<ASR::FunctionType_t>(t);
std::string result = "(";
for( size_t i = 0; i < ftp->n_arg_types; i++ ) {
result += type_to_str_with_substitution(ftp->m_arg_types[i], subs) + ", ";
}
result += "return_type: ";
if( ftp->m_return_var_type ) {
result += type_to_str_with_substitution(ftp->m_return_var_type, subs);
} else {
result += "void";
}
result += ")";
return result;
}
default : return type_to_str(t);
}
}
static inline std::string binop_to_str(const ASR::binopType t) {
switch (t) {
case (ASR::binopType::Add): { return " + "; }
case (ASR::binopType::Sub): { return " - "; }
case (ASR::binopType::Mul): { return "*"; }
case (ASR::binopType::Div): { return "/"; }
default : throw LCompilersException("Cannot represent the binary operator as a string");
}
}
static inline std::string cmpop_to_str(const ASR::cmpopType t) {
switch (t) {
case (ASR::cmpopType::Eq): { return " == "; }
case (ASR::cmpopType::NotEq): { return " != "; }
case (ASR::cmpopType::Lt): { return " < "; }
case (ASR::cmpopType::LtE): { return " <= "; }
case (ASR::cmpopType::Gt): { return " > "; }
case (ASR::cmpopType::GtE): { return " >= "; }
default : throw LCompilersException("Cannot represent the comparison as a string");
}
}
static inline std::string logicalbinop_to_str_python(const ASR::logicalbinopType t) {
switch (t) {
case (ASR::logicalbinopType::And): { return " && "; }
case (ASR::logicalbinopType::Or): { return " || "; }
case (ASR::logicalbinopType::Eqv): { return " == "; }
case (ASR::logicalbinopType::NEqv): { return " != "; }
default : throw LCompilersException("Cannot represent the boolean operator as a string");
}
}
static inline ASR::expr_t* expr_value(ASR::expr_t *f)
{
return ASR::expr_value0(f);
}
static inline std::pair<char**, size_t> symbol_dependencies(const ASR::symbol_t *f)
{
switch (f->type) {
case ASR::symbolType::Program: {
ASR::Program_t* sym = ASR::down_cast<ASR::Program_t>(f);
return std::make_pair(sym->m_dependencies, sym->n_dependencies);
}
case ASR::symbolType::Module: {
ASR::Module_t* sym = ASR::down_cast<ASR::Module_t>(f);
return std::make_pair(sym->m_dependencies, sym->n_dependencies);
}
case ASR::symbolType::Function: {
ASR::Function_t* sym = ASR::down_cast<ASR::Function_t>(f);
return std::make_pair(sym->m_dependencies, sym->n_dependencies);
}
case ASR::symbolType::Struct: {
ASR::Struct_t* sym = ASR::down_cast<ASR::Struct_t>(f);
return std::make_pair(sym->m_dependencies, sym->n_dependencies);
}
case ASR::symbolType::EnumType: {
ASR::EnumType_t* sym = ASR::down_cast<ASR::EnumType_t>(f);
return std::make_pair(sym->m_dependencies, sym->n_dependencies);
}
case ASR::symbolType::UnionType: {
ASR::UnionType_t* sym = ASR::down_cast<ASR::UnionType_t>(f);
return std::make_pair(sym->m_dependencies, sym->n_dependencies);
}
default : throw LCompilersException("Not implemented");
}
}
static inline bool is_present_in_current_scope(ASR::ExternalSymbol_t* external_symbol, SymbolTable* current_scope) {
SymbolTable* scope = external_symbol->m_parent_symtab;
while (scope != nullptr) {
if (scope->get_counter() == current_scope->get_counter()) {
return true;
}
scope = scope->parent;
}
return false;
}
static inline SymbolTable *symbol_parent_symtab(const ASR::symbol_t *f)
{
switch (f->type) {
case ASR::symbolType::Program: {
return ASR::down_cast<ASR::Program_t>(f)->m_symtab->parent;
}
case ASR::symbolType::Module: {
return ASR::down_cast<ASR::Module_t>(f)->m_symtab->parent;
}
case ASR::symbolType::Function: {
return ASR::down_cast<ASR::Function_t>(f)->m_symtab->parent;
}
case ASR::symbolType::GenericProcedure: {
return ASR::down_cast<ASR::GenericProcedure_t>(f)->m_parent_symtab;
}
case ASR::symbolType::Struct: {
return ASR::down_cast<ASR::Struct_t>(f)->m_symtab->parent;
}
case ASR::symbolType::EnumType: {
return ASR::down_cast<ASR::EnumType_t>(f)->m_symtab->parent;
}
case ASR::symbolType::UnionType: {
return ASR::down_cast<ASR::UnionType_t>(f)->m_symtab->parent;
}
case ASR::symbolType::Variable: {
return ASR::down_cast<ASR::Variable_t>(f)->m_parent_symtab;
}
case ASR::symbolType::ExternalSymbol: {
return ASR::down_cast<ASR::ExternalSymbol_t>(f)->m_parent_symtab;
}
case ASR::symbolType::ClassProcedure: {
return ASR::down_cast<ASR::ClassProcedure_t>(f)->m_parent_symtab;
}
case ASR::symbolType::CustomOperator: {
return ASR::down_cast<ASR::CustomOperator_t>(f)->m_parent_symtab;
}
case ASR::symbolType::AssociateBlock: {
return ASR::down_cast<ASR::AssociateBlock_t>(f)->m_symtab->parent;
}
case ASR::symbolType::Block: {
return ASR::down_cast<ASR::Block_t>(f)->m_symtab->parent;
}
case ASR::symbolType::Requirement: {
return ASR::down_cast<ASR::Requirement_t>(f)->m_symtab->parent;
}
case ASR::symbolType::Template: {
return ASR::down_cast<ASR::Template_t>(f)->m_symtab->parent;
}
default : throw LCompilersException("Not implemented");
}
}
// Returns the `symbol`'s symtab, or nullptr if the symbol has no symtab
static inline SymbolTable *symbol_symtab(const ASR::symbol_t *f)
{
switch (f->type) {
case ASR::symbolType::Program: {
return ASR::down_cast<ASR::Program_t>(f)->m_symtab;
}
case ASR::symbolType::Module: {
return ASR::down_cast<ASR::Module_t>(f)->m_symtab;
}
case ASR::symbolType::Function: {
return ASR::down_cast<ASR::Function_t>(f)->m_symtab;
}
case ASR::symbolType::GenericProcedure: {
return nullptr;
//throw LCompilersException("GenericProcedure does not have a symtab");
}
case ASR::symbolType::Struct: {
return ASR::down_cast<ASR::Struct_t>(f)->m_symtab;
}
case ASR::symbolType::EnumType: {
return ASR::down_cast<ASR::EnumType_t>(f)->m_symtab;
}
case ASR::symbolType::UnionType: {
return ASR::down_cast<ASR::UnionType_t>(f)->m_symtab;
}
case ASR::symbolType::Variable: {
return nullptr;
//throw LCompilersException("Variable does not have a symtab");
}
case ASR::symbolType::ExternalSymbol: {
return nullptr;
//throw LCompilersException("ExternalSymbol does not have a symtab");
}
case ASR::symbolType::ClassProcedure: {
return nullptr;
//throw LCompilersException("ClassProcedure does not have a symtab");
}
case ASR::symbolType::AssociateBlock: {
return ASR::down_cast<ASR::AssociateBlock_t>(f)->m_symtab;
}
case ASR::symbolType::Block: {
return ASR::down_cast<ASR::Block_t>(f)->m_symtab;
}
case ASR::symbolType::Requirement: {
return ASR::down_cast<ASR::Requirement_t>(f)->m_symtab;
}
case ASR::symbolType::Template: {
return ASR::down_cast<ASR::Template_t>(f)->m_symtab;
}
default : throw LCompilersException("Not implemented");
}
}
static inline ASR::symbol_t *get_asr_owner(const ASR::symbol_t *sym) {
const SymbolTable *s = symbol_parent_symtab(sym);
if( s->asr_owner == nullptr ||
!ASR::is_a<ASR::symbol_t>(*s->asr_owner) ) {
return nullptr;
}
return ASR::down_cast<ASR::symbol_t>(s->asr_owner);
}
// Returns the Module_t the symbol is in, or nullptr if not in a module
static inline ASR::Module_t *get_sym_module(const ASR::symbol_t *sym) {
const SymbolTable *s = symbol_parent_symtab(sym);
while (s->parent != nullptr) {
ASR::symbol_t *asr_owner = ASR::down_cast<ASR::symbol_t>(s->asr_owner);
if (ASR::is_a<ASR::Module_t>(*asr_owner)) {
return ASR::down_cast<ASR::Module_t>(asr_owner);
}
s = s->parent;
}
return nullptr;
}
static inline ASR::symbol_t *get_asr_owner(const ASR::expr_t *expr) {
switch( expr->type ) {
case ASR::exprType::Var: {
return ASRUtils::get_asr_owner(ASR::down_cast<ASR::Var_t>(expr)->m_v);
}
case ASR::exprType::StructInstanceMember: {
return ASRUtils::get_asr_owner(ASRUtils::symbol_get_past_external(
ASR::down_cast<ASR::StructInstanceMember_t>(expr)->m_m));
}
case ASR::exprType::GetPointer: {
return ASRUtils::get_asr_owner(ASR::down_cast<ASR::GetPointer_t>(expr)->m_arg);
}
case ASR::exprType::FunctionCall: {
return ASRUtils::get_asr_owner(ASR::down_cast<ASR::FunctionCall_t>(expr)->m_name);
}
default: {
return nullptr;
}
}
return nullptr;
}
// Returns the Module_t the symbol is in, or nullptr if not in a module
// or no asr_owner yet
static inline ASR::Module_t *get_sym_module0(const ASR::symbol_t *sym) {
const SymbolTable *s = symbol_parent_symtab(sym);
while (s->parent != nullptr) {
if (s->asr_owner != nullptr) {
ASR::symbol_t *asr_owner = ASR::down_cast<ASR::symbol_t>(s->asr_owner);
if (ASR::is_a<ASR::Module_t>(*asr_owner)) {
return ASR::down_cast<ASR::Module_t>(asr_owner);
}
}
s = s->parent;
}
return nullptr;
}
static inline bool is_c_ptr(ASR::symbol_t* v, std::string v_name="") {
if( v_name == "" ) {
v_name = ASRUtils::symbol_name(v);
}
ASR::symbol_t* v_orig = ASRUtils::symbol_get_past_external(v);
if( ASR::is_a<ASR::Struct_t>(*v_orig) ) {
ASR::Module_t* der_type_module = ASRUtils::get_sym_module0(v_orig);
return (der_type_module && std::string(der_type_module->m_name) ==
"lfortran_intrinsic_iso_c_binding" &&
der_type_module->m_intrinsic &&
v_name == "c_ptr");
}
return false;
}
// Returns true if the Function is intrinsic, otherwise false
template <typename T>
static inline bool is_intrinsic_procedure(const T *fn) {
ASR::symbol_t *sym = (ASR::symbol_t*)fn;
ASR::Module_t *m = get_sym_module0(sym);
if (m != nullptr) {
if (startswith(m->m_name, "lfortran_intrinsic")) return true;
}
return false;
}
static inline bool is_intrinsic_symbol(const ASR::symbol_t *fn) {
const ASR::symbol_t *sym = fn;
ASR::Module_t *m = get_sym_module0(sym);
if (m != nullptr) {
if (m->m_intrinsic) {
return true;
}
if (startswith(m->m_name, "lfortran_intrinsic")) return true;
}
return false;
}
// Returns true if the Function is intrinsic, otherwise false
// This version uses the `intrinsic` member of `Module`, so it
// should be used instead of is_intrinsic_procedure
static inline bool is_intrinsic_function2(const ASR::Function_t *fn) {
ASR::symbol_t *sym = (ASR::symbol_t*)fn;
ASR::Module_t *m = get_sym_module0(sym);
if (m != nullptr) {
if (m->m_intrinsic ||
ASRUtils::get_FunctionType(fn)->m_abi ==
ASR::abiType::Intrinsic) {
return true;
}
}
return false;
}
// Returns true if the Function is intrinsic, otherwise false
template <typename T>
static inline bool is_intrinsic_optimization(const T *routine) {
ASR::symbol_t *sym = (ASR::symbol_t*)routine;
if( ASR::is_a<ASR::ExternalSymbol_t>(*sym) ) {
ASR::ExternalSymbol_t* ext_sym = ASR::down_cast<ASR::ExternalSymbol_t>(sym);
return (std::string(ext_sym->m_module_name).find("lfortran_intrinsic_optimization") != std::string::npos);
}
ASR::Module_t *m = get_sym_module0(sym);
if (m != nullptr) {
return (std::string(m->m_name).find("lfortran_intrinsic_optimization") != std::string::npos);
}
return false;
}
// Returns true if all arguments have a `value`
static inline bool all_args_have_value(const Vec<ASR::expr_t*> &args) {
for (auto &a : args) {
ASR::expr_t *v = expr_value(a);
if (v == nullptr) return false;
}
return true;
}
static inline bool is_value_constant(ASR::expr_t *a_value) {
if( a_value == nullptr ) {
return false;
}
switch ( a_value->type ) {
case ASR::exprType::IntegerConstant:
case ASR::exprType::IntegerBOZ:
case ASR::exprType::UnsignedIntegerConstant:
case ASR::exprType::RealConstant:
case ASR::exprType::ComplexConstant:
case ASR::exprType::LogicalConstant:
case ASR::exprType::ImpliedDoLoop:
case ASR::exprType::PointerNullConstant:
case ASR::exprType::ArrayConstant:
case ASR::exprType::StringConstant: {
return true;
}
case ASR::exprType::RealBinOp:
case ASR::exprType::IntegerUnaryMinus:
case ASR::exprType::RealUnaryMinus:
case ASR::exprType::IntegerBinOp:
case ASR::exprType::StringLen: {