forked from lcompilers/lpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_ast_to_asr.cpp
More file actions
2759 lines (2599 loc) · 125 KB
/
python_ast_to_asr.cpp
File metadata and controls
2759 lines (2599 loc) · 125 KB
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
#include <fstream>
#include <iostream>
#include <map>
#include <memory>
#include <string>
#include <cmath>
#include <vector>
#include <bitset>
#include <complex>
#include <sstream>
#include <iterator>
#include <libasr/asr.h>
#include <libasr/asr_utils.h>
#include <libasr/asr_verify.h>
#include <libasr/string_utils.h>
#include <libasr/utils.h>
#include <libasr/pass/global_stmts_program.h>
#include <lpython/python_ast.h>
#include <lpython/semantics/python_ast_to_asr.h>
#include <lpython/utils.h>
#include <lpython/semantics/semantic_exception.h>
#include <lpython/python_serialization.h>
#include <lpython/semantics/python_comptime_eval.h>
namespace LFortran::LPython {
LFortran::Result<LFortran::LPython::AST::ast_t*> parse_python_file(Allocator &al,
const std::string &runtime_library_dir,
const std::string &infile) {
std::string pycmd = "python " + runtime_library_dir + "/lpython_parser.py " + infile;
int err = std::system(pycmd.c_str());
if (err != 0) {
std::cerr << "The command '" << pycmd << "' failed." << std::endl;
return LFortran::Error();
}
std::string infile_ser = "ser.txt";
std::string input;
bool status = read_file(infile_ser, input);
if (!status) {
std::cerr << "The file '" << infile_ser << "' cannot be read." << std::endl;
return LFortran::Error();
}
LFortran::LPython::AST::ast_t* ast = LFortran::LPython::deserialize_ast(al, input);
return ast;
}
// Does a CPython style lookup for a module:
// * First the current directory (this is incorrect, we need to do it relative to the current file)
// * Then the LPython runtime directory
LFortran::Result<std::string> get_full_path(const std::string &filename,
const std::string &runtime_library_dir, bool <ypes, bool &numpy) {
ltypes = false;
numpy = false;
std::string input;
bool status = read_file(filename, input);
if (status) {
return filename;
} else {
std::string filename_intrinsic = runtime_library_dir + "/" + filename;
status = read_file(filename_intrinsic, input);
if (status) {
return filename_intrinsic;
} else {
// If this is `ltypes`, do a special lookup
if (filename == "ltypes.py") {
filename_intrinsic = runtime_library_dir + "/ltypes/" + filename;
status = read_file(filename_intrinsic, input);
if (status) {
ltypes = true;
return filename_intrinsic;
} else {
return LFortran::Error();
}
} else if (filename == "numpy.py") {
filename_intrinsic = runtime_library_dir + "/lpython_intrinsic_numpy.py";
status = read_file(filename_intrinsic, input);
if (status) {
numpy = true;
return filename_intrinsic;
} else {
return LFortran::Error();
}
} else {
return LFortran::Error();
}
}
}
}
ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,
const std::string &module_name,
const Location &loc, bool intrinsic,
const std::string &rl_path,
bool <ypes, bool &numpy,
const std::function<void (const std::string &, const Location &)> err) {
ltypes = false;
numpy = false;
LFORTRAN_ASSERT(symtab);
if (symtab->scope.find(module_name) != symtab->scope.end()) {
ASR::symbol_t *m = symtab->scope[module_name];
if (ASR::is_a<ASR::Module_t>(*m)) {
return ASR::down_cast<ASR::Module_t>(m);
} else {
err("The symbol '" + module_name + "' is not a module", loc);
}
}
LFORTRAN_ASSERT(symtab->parent == nullptr);
// Parse the module `module_name`.py to AST
std::string infile0 = module_name + ".py";
Result<std::string> rinfile = get_full_path(infile0, rl_path, ltypes,
numpy);
if (!rinfile.ok) {
err("Could not find the module '" + infile0 + "'", loc);
}
if (ltypes) return nullptr;
if (numpy) return nullptr;
std::string infile = rinfile.result;
Result<AST::ast_t*> r = parse_python_file(al, rl_path, infile);
if (!r.ok) {
err("The file '" + infile + "' failed to parse", loc);
}
LFortran::LPython::AST::ast_t* ast = r.result;
// Convert the module from AST to ASR
LFortran::LocationManager lm;
lm.in_filename = infile;
// TODO: diagnostic should be an argument to this function
diag::Diagnostics diagnostics;
Result<ASR::TranslationUnit_t*> r2 = python_ast_to_asr(al, *ast, diagnostics, false, false);
std::string input;
read_file(infile, input);
CompilerOptions compiler_options;
std::cerr << diagnostics.render(input, lm, compiler_options);
if (!r2.ok) {
LFORTRAN_ASSERT(diagnostics.has_error())
return nullptr; // Error
}
ASR::TranslationUnit_t* mod1 = r2.result;
// insert into `symtab`
ASR::Module_t *mod2 = ASRUtils::extract_module(*mod1);
mod2->m_name = s2c(al, module_name);
symtab->scope[module_name] = (ASR::symbol_t*)mod2;
mod2->m_symtab->parent = symtab;
mod2->m_intrinsic = intrinsic;
if (intrinsic) {
// TODO: I think we should just store intrinsic once, in the module
// itself
// Mark each function as intrinsic also
for (auto &item : mod2->m_symtab->scope) {
if (ASR::is_a<ASR::Subroutine_t>(*item.second)) {
ASR::Subroutine_t *s = ASR::down_cast<ASR::Subroutine_t>(item.second);
if (s->m_abi == ASR::abiType::Source) {
s->m_abi = ASR::abiType::Intrinsic;
}
if (s->n_body == 0) {
std::string name = s->m_name;
if (name == "ubound" || name == "lbound") {
s->m_deftype = ASR::deftypeType::Interface;
}
}
}
if (ASR::is_a<ASR::Function_t>(*item.second)) {
ASR::Function_t *s = ASR::down_cast<ASR::Function_t>(item.second);
if (s->m_abi == ASR::abiType::Source) {
s->m_abi = ASR::abiType::Intrinsic;
}
if (s->n_body == 0) {
std::string name = s->m_name;
if (name == "ubound" || name == "lbound") {
s->m_deftype = ASR::deftypeType::Interface;
}
}
}
}
}
// and return it
return mod2;
}
ASR::symbol_t* import_from_module(Allocator &al, ASR::Module_t *m, SymbolTable *current_scope,
std::string mname, std::string cur_sym_name, std::string new_sym_name,
const Location &loc) {
ASR::symbol_t *t = m->m_symtab->resolve_symbol(cur_sym_name);
if (!t) {
throw SemanticError("The symbol '" + cur_sym_name + "' not found in the module '" + mname + "'",
loc);
}
if (current_scope->scope.find(cur_sym_name) != current_scope->scope.end()) {
throw SemanticError(cur_sym_name + " already defined", loc);
}
if (ASR::is_a<ASR::Subroutine_t>(*t)) {
ASR::Subroutine_t *msub = ASR::down_cast<ASR::Subroutine_t>(t);
// `msub` is the Subroutine in a module. Now we construct
// an ExternalSymbol that points to
// `msub` via the `external` field.
Str name;
name.from_str(al, new_sym_name);
ASR::asr_t *sub = ASR::make_ExternalSymbol_t(
al, msub->base.base.loc,
/* a_symtab */ current_scope,
/* a_name */ name.c_str(al),
(ASR::symbol_t*)msub,
m->m_name, nullptr, 0, msub->m_name,
ASR::accessType::Public
);
return ASR::down_cast<ASR::symbol_t>(sub);
} else if (ASR::is_a<ASR::Function_t>(*t)) {
ASR::Function_t *mfn = ASR::down_cast<ASR::Function_t>(t);
// `mfn` is the Function in a module. Now we construct
// an ExternalSymbol that points to it.
Str name;
name.from_str(al, new_sym_name);
char *cname = name.c_str(al);
ASR::asr_t *fn = ASR::make_ExternalSymbol_t(
al, mfn->base.base.loc,
/* a_symtab */ current_scope,
/* a_name */ cname,
(ASR::symbol_t*)mfn,
m->m_name, nullptr, 0, mfn->m_name,
ASR::accessType::Public
);
return ASR::down_cast<ASR::symbol_t>(fn);
} else if (ASR::is_a<ASR::Variable_t>(*t)) {
ASR::Variable_t *mv = ASR::down_cast<ASR::Variable_t>(t);
// `mv` is the Variable in a module. Now we construct
// an ExternalSymbol that points to it.
Str name;
name.from_str(al, new_sym_name);
char *cname = name.c_str(al);
ASR::asr_t *v = ASR::make_ExternalSymbol_t(
al, mv->base.base.loc,
/* a_symtab */ current_scope,
/* a_name */ cname,
(ASR::symbol_t*)mv,
m->m_name, nullptr, 0, mv->m_name,
ASR::accessType::Public
);
return ASR::down_cast<ASR::symbol_t>(v);
} else if (ASR::is_a<ASR::GenericProcedure_t>(*t)) {
ASR::GenericProcedure_t *gt = ASR::down_cast<ASR::GenericProcedure_t>(t);
Str name;
name.from_str(al, new_sym_name);
char *cname = name.c_str(al);
ASR::asr_t *v = ASR::make_ExternalSymbol_t(
al, gt->base.base.loc,
/* a_symtab */ current_scope,
/* a_name */ cname,
(ASR::symbol_t*)gt,
m->m_name, nullptr, 0, gt->m_name,
ASR::accessType::Public
);
return ASR::down_cast<ASR::symbol_t>(v);
} else {
throw SemanticError("Only Subroutines, Functions and Variables are currently supported in 'import'",
loc);
}
LFORTRAN_ASSERT(false);
return nullptr;
}
template <class Derived>
class CommonVisitor : public AST::BaseVisitor<Derived> {
public:
diag::Diagnostics &diag;
ASR::asr_t *tmp;
Allocator &al;
SymbolTable *current_scope;
// The current_module contains the current module that is being visited;
// this is used to append to the module dependencies if needed
ASR::Module_t *current_module = nullptr;
Vec<char *> current_module_dependencies;
// True for the main module, false for every other one
// The main module is stored directly in TranslationUnit, other modules are Modules
bool main_module;
PythonIntrinsicProcedures intrinsic_procedures;
std::map<int, ASR::symbol_t*> &ast_overload;
CommonVisitor(Allocator &al, SymbolTable *symbol_table,
diag::Diagnostics &diagnostics, bool main_module,
std::map<int, ASR::symbol_t*> &ast_overload)
: diag{diagnostics}, al{al}, current_scope{symbol_table}, main_module{main_module},
ast_overload{ast_overload} {
current_module_dependencies.reserve(al, 4);
}
ASR::asr_t* resolve_variable(const Location &loc, const std::string &var_name) {
SymbolTable *scope = current_scope;
ASR::symbol_t *v = scope->resolve_symbol(var_name);
if (!v) {
diag.semantic_error_label("Variable '" + var_name
+ "' is not declared", {loc},
"'" + var_name + "' is undeclared");
throw SemanticAbort();
}
if( v->type == ASR::symbolType::Variable ) {
ASR::Variable_t* v_var = ASR::down_cast<ASR::Variable_t>(v);
if( v_var->m_type == nullptr &&
v_var->m_intent == ASR::intentType::AssociateBlock ) {
return (ASR::asr_t*)(v_var->m_symbolic_value);
}
}
return ASR::make_Var_t(al, loc, v);
}
ASR::symbol_t* resolve_intrinsic_function(const Location &loc, const std::string &remote_sym) {
LFORTRAN_ASSERT(intrinsic_procedures.is_intrinsic(remote_sym))
std::string module_name = intrinsic_procedures.get_module(remote_sym, loc);
SymbolTable *tu_symtab = ASRUtils::get_tu_symtab(current_scope);
std::string rl_path = get_runtime_library_dir();
bool ltypes, numpy;
ASR::Module_t *m = load_module(al, tu_symtab, module_name,
loc, true, rl_path,
ltypes, numpy,
[&](const std::string &msg, const Location &loc) { throw SemanticError(msg, loc); }
);
LFORTRAN_ASSERT(!ltypes)
LFORTRAN_ASSERT(!numpy)
ASR::symbol_t *t = m->m_symtab->resolve_symbol(remote_sym);
if (!t) {
throw SemanticError("The symbol '" + remote_sym
+ "' not found in the module '" + module_name + "'",
loc);
} else if (! (ASR::is_a<ASR::GenericProcedure_t>(*t)
|| ASR::is_a<ASR::Function_t>(*t)
|| ASR::is_a<ASR::Subroutine_t>(*t))) {
throw SemanticError("The symbol '" + remote_sym
+ "' found in the module '" + module_name + "', "
+ "but it is not a function, subroutine or a generic procedure.",
loc);
}
char *fn_name = ASRUtils::symbol_name(t);
ASR::asr_t *fn = ASR::make_ExternalSymbol_t(
al, t->base.loc,
/* a_symtab */ current_scope,
/* a_name */ fn_name,
t,
m->m_name, nullptr, 0, fn_name,
ASR::accessType::Private
);
std::string sym = fn_name;
current_scope->scope[sym] = ASR::down_cast<ASR::symbol_t>(fn);
ASR::symbol_t *v = ASR::down_cast<ASR::symbol_t>(fn);
// Now we need to add the module `m` with the intrinsic function
// into the current module dependencies
if (current_module) {
// We are in body visitor, the module is already constructed
// and available as current_module.
// Add the module `m` to current module dependencies
Vec<char*> vec;
vec.from_pointer_n_copy(al, current_module->m_dependencies,
current_module->n_dependencies);
if (!present(vec, m->m_name)) {
vec.push_back(al, m->m_name);
current_module->m_dependencies = vec.p;
current_module->n_dependencies = vec.size();
}
} else {
// We are in the symtab visitor or body visitor and we are
// constructing a module, so current_module is not available yet
// (the current_module_dependencies is not used in body visitor)
if (!present(current_module_dependencies, m->m_name)) {
current_module_dependencies.push_back(al, m->m_name);
}
}
return v;
}
// Function to create appropriate call based on symbol type. If it is external
// generic symbol then it changes the name accordingly.
ASR::asr_t* make_call_helper(Allocator &al, ASR::symbol_t* s, SymbolTable *current_scope,
Vec<ASR::call_arg_t> args, std::string call_name, const Location &loc) {
ASR::symbol_t *s_generic = nullptr, *stemp = s;
// handling ExternalSymbol
s = ASRUtils::symbol_get_past_external(s);
if (ASR::is_a<ASR::GenericProcedure_t>(*s)) {
s_generic = stemp;
ASR::GenericProcedure_t *p = ASR::down_cast<ASR::GenericProcedure_t>(s);
int idx = ASRUtils::select_generic_procedure(args, *p, loc,
[&](const std::string &msg, const Location &loc) { throw SemanticError(msg, loc); });
s = p->m_procs[idx];
std::string remote_sym = ASRUtils::symbol_name(s);
std::string local_sym = ASRUtils::symbol_name(s);
if (ASR::is_a<ASR::ExternalSymbol_t>(*stemp)) {
local_sym = std::string(p->m_name) + "@" + local_sym;
}
SymbolTable *symtab = current_scope;
while (symtab->parent != nullptr && symtab->scope.find(local_sym) == symtab->scope.end()) {
symtab = symtab->parent;
}
if (symtab->scope.find(local_sym) == symtab->scope.end()) {
LFORTRAN_ASSERT(ASR::is_a<ASR::ExternalSymbol_t>(*stemp));
std::string mod_name = ASR::down_cast<ASR::ExternalSymbol_t>(stemp)->m_module_name;
ASR::symbol_t *mt = symtab->scope[mod_name];
ASR::Module_t *m = ASR::down_cast<ASR::Module_t>(mt);
stemp = import_from_module(al, m, symtab, mod_name,
remote_sym, local_sym, loc);
LFORTRAN_ASSERT(ASR::is_a<ASR::ExternalSymbol_t>(*stemp));
symtab->scope[local_sym] = stemp;
s = ASRUtils::symbol_get_past_external(stemp);
} else {
stemp = symtab->scope[local_sym];
}
}
if (ASR::is_a<ASR::Function_t>(*s)) {
ASR::Function_t *func = ASR::down_cast<ASR::Function_t>(s);
ASR::ttype_t *a_type = ASRUtils::expr_type(func->m_return_var);
ASR::expr_t *value = nullptr;
if (ASRUtils::is_intrinsic_function2(func)) {
value = intrinsic_procedures.comptime_eval(call_name, al, loc, args);
}
return ASR::make_FunctionCall_t(al, loc, stemp,
s_generic, args.p, args.size(), a_type, value, nullptr);
} else if (ASR::is_a<ASR::Subroutine_t>(*s)) {
return ASR::make_SubroutineCall_t(al, loc, stemp,
s_generic, args.p, args.size(), nullptr);
} else {
throw SemanticError("Unsupported call type for " + call_name, loc);
}
}
// Convert Python AST type annotation to an ASR type
// Examples:
// i32, i64, f32, f64
// f64[256], i32[:]
ASR::ttype_t * ast_expr_to_asr_type(const Location &loc, const AST::expr_t &annotation) {
Vec<ASR::dimension_t> dims;
dims.reserve(al, 4);
std::string var_annotation;
if (AST::is_a<AST::Name_t>(annotation)) {
AST::Name_t *n = AST::down_cast<AST::Name_t>(&annotation);
var_annotation = n->m_id;
} else if (AST::is_a<AST::Subscript_t>(annotation)) {
AST::Subscript_t *s = AST::down_cast<AST::Subscript_t>(&annotation);
if (AST::is_a<AST::Name_t>(*s->m_value)) {
AST::Name_t *n = AST::down_cast<AST::Name_t>(s->m_value);
var_annotation = n->m_id;
} else {
throw SemanticError("Only Name in Subscript supported for now in annotation",
loc);
}
if (var_annotation == "tuple") {
Vec<ASR::ttype_t*> types;
types.reserve(al, 4);
if (AST::is_a<AST::Name_t>(*s->m_slice)) {
types.push_back(al, ast_expr_to_asr_type(loc, *s->m_slice));
} else if (AST::is_a<AST::Tuple_t>(*s->m_slice)) {
AST::Tuple_t *t = AST::down_cast<AST::Tuple_t>(s->m_slice);
for (size_t i=0; i<t->n_elts; i++) {
types.push_back(al, ast_expr_to_asr_type(loc, *t->m_elts[i]));
}
} else {
throw SemanticError("Only Name or Tuple in Subscript supported for now in `tuple` annotation",
loc);
}
ASR::ttype_t *type = ASRUtils::TYPE(ASR::make_Tuple_t(al, loc,
types.p, types.size()));
return type;
} else if (var_annotation == "set") {
if (AST::is_a<AST::Name_t>(*s->m_slice)) {
ASR::ttype_t *type = ast_expr_to_asr_type(loc, *s->m_slice);
return ASRUtils::TYPE(ASR::make_Set_t(al, loc, type));
} else {
throw SemanticError("Only Name in Subscript supported for now in `set`"
" annotation", loc);
}
} else if (var_annotation == "list") {
ASR::ttype_t *type = nullptr;
if (AST::is_a<AST::Name_t>(*s->m_slice) || AST::is_a<AST::Subscript_t>(*s->m_slice)) {
type = ast_expr_to_asr_type(loc, *s->m_slice);
return ASRUtils::TYPE(ASR::make_List_t(al, loc, type));
} else {
throw SemanticError("Only Name or Subscript inside Subscript supported for now in `list`"
" annotation", loc);
}
} else if (var_annotation == "dict") {
if (AST::is_a<AST::Tuple_t>(*s->m_slice)) {
AST::Tuple_t *t = AST::down_cast<AST::Tuple_t>(s->m_slice);
if (t->n_elts != 2) {
throw SemanticError("`dict` annotation must have 2 elements: types"
" of both keys and values", loc);
}
ASR::ttype_t *key_type = ast_expr_to_asr_type(loc, *t->m_elts[0]);
ASR::ttype_t *value_type = ast_expr_to_asr_type(loc, *t->m_elts[1]);
return ASRUtils::TYPE(ASR::make_Dict_t(al, loc, key_type, value_type));
} else {
throw SemanticError("`dict` annotation must have 2 elements: types of"
" both keys and values", loc);
}
} else {
ASR::dimension_t dim;
dim.loc = loc;
if (AST::is_a<AST::Slice_t>(*s->m_slice)) {
dim.m_start = nullptr;
dim.m_end = nullptr;
} else {
this->visit_expr(*s->m_slice);
ASR::expr_t *value = ASRUtils::EXPR(tmp);
if (ASR::is_a<ASR::ConstantInteger_t>(*value) || ASR::is_a<ASR::Var_t>(*value)) {
ASR::ttype_t *itype = ASRUtils::TYPE(ASR::make_Integer_t(al, loc,
4, nullptr, 0));
dim.m_start = ASR::down_cast<ASR::expr_t>(ASR::make_ConstantInteger_t(al, loc, 1, itype));
dim.m_end = value;
} else {
throw SemanticError("Only Integer, `:` or identifier in [] in Subscript supported for now in annotation",
loc);
}
}
dims.push_back(al, dim);
}
} else {
throw SemanticError("Only Name or Subscript supported for now in annotation of annotated assignment.",
loc);
}
ASR::ttype_t *type;
if (var_annotation == "i8") {
type = ASRUtils::TYPE(ASR::make_Integer_t(al, loc,
1, dims.p, dims.size()));
} else if (var_annotation == "i16") {
type = ASRUtils::TYPE(ASR::make_Integer_t(al, loc,
2, dims.p, dims.size()));
} else if (var_annotation == "i32") {
type = ASRUtils::TYPE(ASR::make_Integer_t(al, loc,
4, dims.p, dims.size()));
} else if (var_annotation == "i64") {
type = ASRUtils::TYPE(ASR::make_Integer_t(al, loc,
8, dims.p, dims.size()));
} else if (var_annotation == "f32") {
type = ASRUtils::TYPE(ASR::make_Real_t(al, loc,
4, dims.p, dims.size()));
} else if (var_annotation == "f64") {
type = ASRUtils::TYPE(ASR::make_Real_t(al, loc,
8, dims.p, dims.size()));
} else if (var_annotation == "c32") {
type = ASRUtils::TYPE(ASR::make_Complex_t(al, loc,
4, dims.p, dims.size()));
} else if (var_annotation == "c64") {
type = ASRUtils::TYPE(ASR::make_Complex_t(al, loc,
8, dims.p, dims.size()));
} else if (var_annotation == "str") {
type = ASRUtils::TYPE(ASR::make_Character_t(al, loc,
1, -2, nullptr, dims.p, dims.size()));
} else if (var_annotation == "bool") {
type = ASRUtils::TYPE(ASR::make_Logical_t(al, loc,
1, dims.p, dims.size()));
} else {
throw SemanticError("Unsupported type annotation: " + var_annotation, loc);
}
return type;
}
ASR::expr_t *index_add_one(const Location &loc, ASR::expr_t *idx) {
// Add 1 to the index `idx`, assumes `idx` is of type Integer 4
ASR::expr_t *overloaded = nullptr;
ASR::expr_t *comptime_value = nullptr;
ASR::ttype_t *a_type = ASRUtils::TYPE(ASR::make_Integer_t(al, loc,
4, nullptr, 0));
ASR::expr_t *constant_one = ASR::down_cast<ASR::expr_t>(ASR::make_ConstantInteger_t(
al, loc, 1, a_type));
return ASRUtils::EXPR(ASR::make_BinOp_t(al, loc, idx,
ASR::binopType::Add, constant_one, a_type,
comptime_value, overloaded));
}
// Casts `right` if needed to the type of `left`
// (to be used during assignment, BinOp, or compare)
ASR::expr_t* implicitcast_helper(ASR::ttype_t *left_type, ASR::expr_t *right,
bool is_assign=false) {
ASR::ttype_t *right_type = ASRUtils::expr_type(right);
if (ASRUtils::is_integer(*left_type) && ASRUtils::is_integer(*right_type)) {
int lkind = ASR::down_cast<ASR::Integer_t>(left_type)->m_kind;
int rkind = ASR::down_cast<ASR::Integer_t>(right_type)->m_kind;
if ((is_assign && (lkind != rkind)) || (lkind > rkind)) {
return ASR::down_cast<ASR::expr_t>(ASR::make_ImplicitCast_t(
al, right->base.loc, right, ASR::cast_kindType::IntegerToInteger,
left_type, nullptr));
}
} else if (ASRUtils::is_real(*left_type) && ASRUtils::is_real(*right_type)) {
bool is_l64 = ASR::down_cast<ASR::Real_t>(left_type)->m_kind == 8;
bool is_r64 = ASR::down_cast<ASR::Real_t>(right_type)->m_kind == 8;
if ((is_assign && (is_l64 != is_r64)) || (is_l64 && !is_r64)) {
return ASR::down_cast<ASR::expr_t>(ASR::make_ImplicitCast_t(
al, right->base.loc, right, ASR::cast_kindType::RealToReal,
left_type, nullptr));
}
} else if (ASRUtils::is_complex(*left_type) && ASRUtils::is_complex(*right_type)) {
bool is_l64 = ASR::down_cast<ASR::Complex_t>(left_type)->m_kind == 8;
bool is_r64 = ASR::down_cast<ASR::Complex_t>(right_type)->m_kind == 8;
if ((is_assign && (is_l64 != is_r64)) || (is_l64 && !is_r64)) {
return ASR::down_cast<ASR::expr_t>(ASR::make_ImplicitCast_t(
al, right->base.loc, right, ASR::cast_kindType::ComplexToComplex,
left_type, nullptr));
}
} else if (!is_assign && ASRUtils::is_real(*left_type) && ASRUtils::is_integer(*right_type)) {
return ASR::down_cast<ASR::expr_t>(ASR::make_ImplicitCast_t(
al, right->base.loc, right, ASR::cast_kindType::IntegerToReal,
left_type, nullptr));
} else if (is_assign && ASRUtils::is_real(*left_type) && ASRUtils::is_integer(*right_type)) {
throw SemanticError("Assigning integer to float is not supported",
right->base.loc);
} else if (is_assign && ASRUtils::is_complex(*left_type) && !ASRUtils::is_complex(*right_type)) {
throw SemanticError("Assigning non-complex to complex is not supported",
right->base.loc);
} else if (!is_assign && ASRUtils::is_complex(*left_type) && !ASRUtils::is_complex(*right_type)) {
if (ASRUtils::is_real(*right_type)) {
return ASR::down_cast<ASR::expr_t>(ASR::make_ImplicitCast_t(
al, right->base.loc, right, ASR::cast_kindType::RealToComplex,
left_type, nullptr));
} else if (ASRUtils::is_integer(*right_type)) {
return ASR::down_cast<ASR::expr_t>(ASR::make_ImplicitCast_t(
al, right->base.loc, right, ASR::cast_kindType::IntegerToComplex,
left_type, nullptr));
} else if (ASRUtils::is_logical(*right_type)) {
ASR::ttype_t* int_type = ASRUtils::TYPE(ASR::make_Integer_t(al,
right->base.loc, 4, nullptr, 0));
right = ASR::down_cast<ASR::expr_t>(ASR::make_ImplicitCast_t(
al, right->base.loc, right, ASR::cast_kindType::LogicalToInteger, int_type,
nullptr));
return ASR::down_cast<ASR::expr_t>(ASR::make_ImplicitCast_t(
al, right->base.loc, right, ASR::cast_kindType::IntegerToComplex, left_type,
nullptr));
} else {
std::string rtype = ASRUtils::type_to_str(right_type);
throw SemanticError("Casting " + rtype + " to complex is not Implemented",
right->base.loc);
}
}
if (!is_assign) { // This will only be used for BinOp
if (ASRUtils::is_logical(*left_type) && ASRUtils::is_logical(*right_type)) {
ASR::ttype_t* int_type = ASRUtils::TYPE(ASR::make_Integer_t(al,
right->base.loc, 4, nullptr, 0));
return ASR::down_cast<ASR::expr_t>(ASR::make_ImplicitCast_t(
al, right->base.loc, right, ASR::cast_kindType::LogicalToInteger, int_type,
nullptr));
} else if (ASRUtils::is_logical(*right_type)) {
ASR::ttype_t* int_type = ASRUtils::TYPE(ASR::make_Integer_t(al,
right->base.loc, 4, nullptr, 0));
if (ASRUtils::is_integer(*left_type)) {
return ASR::down_cast<ASR::expr_t>(ASR::make_ImplicitCast_t(
al, right->base.loc, right, ASR::cast_kindType::LogicalToInteger, int_type,
nullptr));
} else if (ASRUtils::is_real(*left_type)) {
right = ASR::down_cast<ASR::expr_t>(ASR::make_ImplicitCast_t(
al, right->base.loc, right, ASR::cast_kindType::LogicalToInteger, int_type,
nullptr));
return ASR::down_cast<ASR::expr_t>(ASR::make_ImplicitCast_t(
al, right->base.loc, right, ASR::cast_kindType::IntegerToReal, left_type,
nullptr));
} else if (ASRUtils::is_complex(*left_type)) {
right = ASR::down_cast<ASR::expr_t>(ASR::make_ImplicitCast_t(
al, right->base.loc, right, ASR::cast_kindType::LogicalToInteger, int_type,
nullptr));
return ASR::down_cast<ASR::expr_t>(ASR::make_ImplicitCast_t(
al, right->base.loc, right, ASR::cast_kindType::IntegerToComplex, left_type,
nullptr));
} else {
std::string ltype = ASRUtils::type_to_str(left_type);
throw SemanticError("Binary Operation not implemented for bool and " + ltype,
right->base.loc);
}
}
}
return right;
}
void make_BinOp_helper(ASR::expr_t *left, ASR::expr_t *right,
ASR::binopType op, const Location &loc, bool floordiv) {
ASR::ttype_t *left_type = ASRUtils::expr_type(left);
ASR::ttype_t *right_type = ASRUtils::expr_type(right);
ASR::ttype_t *dest_type = nullptr;
ASR::expr_t *value = nullptr;
bool right_is_int = ASRUtils::is_character(*left_type) && ASRUtils::is_integer(*right_type);
bool left_is_int = ASRUtils::is_integer(*left_type) && ASRUtils::is_character(*right_type);
// Handle normal division in python with reals
if (op == ASR::binopType::Div) {
if (ASRUtils::is_character(*left_type) || ASRUtils::is_character(*right_type)) {
diag.add(diag::Diagnostic(
"Division is not supported for string type",
diag::Level::Error, diag::Stage::Semantic, {
diag::Label("string not supported in division" ,
{left->base.loc, right->base.loc})
})
);
throw SemanticAbort();
}
// Floor div operation in python using (`//`)
if (floordiv) {
bool both_int = (ASRUtils::is_integer(*left_type) && ASRUtils::is_integer(*right_type));
if (both_int) {
left = implicitcast_helper(ASRUtils::expr_type(right), left);
right = implicitcast_helper(ASRUtils::expr_type(left), right);
dest_type = ASRUtils::expr_type(left);
} else {
dest_type = ASRUtils::TYPE(ASR::make_Real_t(al, loc,
8, nullptr, 0));
if (ASRUtils::is_integer(*left_type)) {
left = ASR::down_cast<ASR::expr_t>(ASR::make_ImplicitCast_t(
al, left->base.loc, left, ASR::cast_kindType::IntegerToReal, dest_type,
value));
}
if (ASRUtils::is_integer(*right_type)) {
right = ASR::down_cast<ASR::expr_t>(ASR::make_ImplicitCast_t(
al, right->base.loc, right, ASR::cast_kindType::IntegerToReal, dest_type,
value));
}
left = implicitcast_helper(ASRUtils::expr_type(right), left);
right = implicitcast_helper(ASRUtils::expr_type(left), right);
dest_type = ASRUtils::expr_type(left);
}
ASR::symbol_t *fn_div = resolve_intrinsic_function(loc, "_lpython_floordiv");
Vec<ASR::call_arg_t> args;
args.reserve(al, 1);
ASR::call_arg_t arg1, arg2;
arg1.loc = left->base.loc;
arg2.loc = right->base.loc;
arg1.m_value = left;
arg2.m_value = right;
args.push_back(al, arg1);
args.push_back(al, arg2);
tmp = make_call_helper(al, fn_div, current_scope, args, "_lpython_floordiv", loc);
return;
} else { // real divison in python using (`/`)
dest_type = ASRUtils::TYPE(ASR::make_Real_t(al, loc,
8, nullptr, 0));
if (ASRUtils::is_integer(*left_type)) {
left = ASR::down_cast<ASR::expr_t>(ASR::make_ImplicitCast_t(
al, left->base.loc, left, ASR::cast_kindType::IntegerToReal, dest_type,
value));
}
if (ASRUtils::is_integer(*right_type)) {
right = ASR::down_cast<ASR::expr_t>(ASR::make_ImplicitCast_t(
al, right->base.loc, right, ASR::cast_kindType::IntegerToReal, dest_type,
value));
}
}
} else if((ASRUtils::is_integer(*left_type) || ASRUtils::is_real(*left_type) ||
ASRUtils::is_complex(*left_type) || ASRUtils::is_logical(*left_type)) &&
(ASRUtils::is_integer(*right_type) || ASRUtils::is_real(*right_type) ||
ASRUtils::is_complex(*right_type) || ASRUtils::is_logical(*right_type))) {
left = implicitcast_helper(ASRUtils::expr_type(right), left);
right = implicitcast_helper(ASRUtils::expr_type(left), right);
dest_type = ASRUtils::expr_type(left);
} else if ((right_is_int || left_is_int) && op == ASR::binopType::Mul) {
// string repeat
ASR::stropType ops = ASR::stropType::Repeat;
int64_t left_int = 0, right_int = 0, dest_len = 0;
if (right_is_int) {
ASR::Character_t *left_type2 = ASR::down_cast<ASR::Character_t>(left_type);
LFORTRAN_ASSERT(left_type2->n_dims == 0);
right_int = ASR::down_cast<ASR::ConstantInteger_t>(
ASRUtils::expr_value(right))->m_n;
dest_len = left_type2->m_len * right_int;
if (dest_len < 0) dest_len = 0;
dest_type = ASR::down_cast<ASR::ttype_t>(
ASR::make_Character_t(al, loc, left_type2->m_kind,
dest_len, nullptr, nullptr, 0));
} else if (left_is_int) {
ASR::Character_t *right_type2 = ASR::down_cast<ASR::Character_t>(right_type);
LFORTRAN_ASSERT(right_type2->n_dims == 0);
left_int = ASR::down_cast<ASR::ConstantInteger_t>(
ASRUtils::expr_value(left))->m_n;
dest_len = right_type2->m_len * left_int;
if (dest_len < 0) dest_len = 0;
dest_type = ASR::down_cast<ASR::ttype_t>(
ASR::make_Character_t(al, loc, right_type2->m_kind,
dest_len, nullptr, nullptr, 0));
}
if (ASRUtils::expr_value(left) != nullptr && ASRUtils::expr_value(right) != nullptr) {
char* str = right_is_int ? ASR::down_cast<ASR::ConstantString_t>(
ASRUtils::expr_value(left))->m_s :
ASR::down_cast<ASR::ConstantString_t>(
ASRUtils::expr_value(right))->m_s;
int64_t repeat = right_is_int ? right_int : left_int;
char* result;
std::ostringstream os;
std::fill_n(std::ostream_iterator<std::string>(os), repeat, std::string(str));
result = s2c(al, os.str());
LFORTRAN_ASSERT((int64_t)strlen(result) == dest_len)
value = ASR::down_cast<ASR::expr_t>(ASR::make_ConstantString_t(
al, loc, result, dest_type));
}
tmp = ASR::make_StrOp_t(al, loc, left, ops, right, dest_type, value);
return;
} else if (ASRUtils::is_character(*left_type) && ASRUtils::is_character(*right_type)
&& op == ASR::binopType::Add) {
// string concat
ASR::stropType ops = ASR::stropType::Concat;
ASR::Character_t *left_type2 = ASR::down_cast<ASR::Character_t>(left_type);
ASR::Character_t *right_type2 = ASR::down_cast<ASR::Character_t>(right_type);
LFORTRAN_ASSERT(left_type2->n_dims == 0);
LFORTRAN_ASSERT(right_type2->n_dims == 0);
dest_type = ASR::down_cast<ASR::ttype_t>(
ASR::make_Character_t(al, loc, left_type2->m_kind,
left_type2->m_len + right_type2->m_len, nullptr, nullptr, 0));
if (ASRUtils::expr_value(left) != nullptr && ASRUtils::expr_value(right) != nullptr) {
char* left_value = ASR::down_cast<ASR::ConstantString_t>(
ASRUtils::expr_value(left))->m_s;
char* right_value = ASR::down_cast<ASR::ConstantString_t>(
ASRUtils::expr_value(right))->m_s;
char* result;
std::string result_s = std::string(left_value) + std::string(right_value);
result = s2c(al, result_s);
LFORTRAN_ASSERT((int64_t)strlen(result) == ASR::down_cast<ASR::Character_t>(dest_type)->m_len)
value = ASR::down_cast<ASR::expr_t>(ASR::make_ConstantString_t(
al, loc, result, dest_type));
}
tmp = ASR::make_StrOp_t(al, loc, left, ops, right, dest_type,
value);
return;
} else {
std::string ltype = ASRUtils::type_to_str(ASRUtils::expr_type(left));
std::string rtype = ASRUtils::type_to_str(ASRUtils::expr_type(right));
diag.add(diag::Diagnostic(
"Not Implemented: type mismatch in binary operator; only Integer, Real, Complex,"
" Logical combinations and string concatenation/repetition are implemented for now.",
diag::Level::Error, diag::Stage::Semantic, {
diag::Label("type mismatch (" + ltype + " and " + rtype + ")",
{left->base.loc, right->base.loc})
})
);
throw SemanticAbort();
}
// Check that the types are now the same
if (!ASRUtils::check_equal_type(ASRUtils::expr_type(left),
ASRUtils::expr_type(right))) {
std::string ltype = ASRUtils::type_to_str(ASRUtils::expr_type(left));
std::string rtype = ASRUtils::type_to_str(ASRUtils::expr_type(right));
diag.add(diag::Diagnostic(
"Type mismatch in binary operator, the types must be compatible",
diag::Level::Error, diag::Stage::Semantic, {
diag::Label("type mismatch (" + ltype + " and " + rtype + ")",
{left->base.loc, right->base.loc})
})
);
throw SemanticAbort();
}
ASR::ttype_t* int_type = ASRUtils::TYPE(ASR::make_Integer_t(al, loc, 4, nullptr, 0));
// Now, compute the result of the binary operations
if (ASRUtils::expr_value(left) != nullptr && ASRUtils::expr_value(right) != nullptr) {
if (ASRUtils::is_integer(*dest_type)) {
int64_t left_value = ASR::down_cast<ASR::ConstantInteger_t>(
ASRUtils::expr_value(left))->m_n;
int64_t right_value = ASR::down_cast<ASR::ConstantInteger_t>(
ASRUtils::expr_value(right))->m_n;
int64_t result;
switch (op) {
case (ASR::binopType::Add): { result = left_value + right_value; break; }
case (ASR::binopType::Sub): { result = left_value - right_value; break; }
case (ASR::binopType::Mul): { result = left_value * right_value; break; }
case (ASR::binopType::Div): { result = left_value / right_value; break; }
case (ASR::binopType::Pow): { result = std::pow(left_value, right_value); break; }
default: { LFORTRAN_ASSERT(false); } // should never happen
}
value = ASR::down_cast<ASR::expr_t>(ASR::make_ConstantInteger_t(
al, loc, result, dest_type));
}
else if (ASRUtils::is_real(*dest_type)) {
double left_value = ASR::down_cast<ASR::ConstantReal_t>(
ASRUtils::expr_value(left))->m_r;
double right_value = ASR::down_cast<ASR::ConstantReal_t>(
ASRUtils::expr_value(right))->m_r;
double result;
switch (op) {
case (ASR::binopType::Add): { result = left_value + right_value; break; }
case (ASR::binopType::Sub): { result = left_value - right_value; break; }
case (ASR::binopType::Mul): { result = left_value * right_value; break; }
case (ASR::binopType::Div): { result = left_value / right_value; break; }
case (ASR::binopType::Pow): { result = std::pow(left_value, right_value); break; }
default: { LFORTRAN_ASSERT(false); }
}
value = ASR::down_cast<ASR::expr_t>(ASR::make_ConstantReal_t(
al, loc, result, dest_type));
}
else if (ASRUtils::is_complex(*dest_type)) {
ASR::ConstantComplex_t *left0 = ASR::down_cast<ASR::ConstantComplex_t>(
ASRUtils::expr_value(left));
ASR::ConstantComplex_t *right0 = ASR::down_cast<ASR::ConstantComplex_t>(
ASRUtils::expr_value(right));
std::complex<double> left_value(left0->m_re, left0->m_im);
std::complex<double> right_value(right0->m_re, right0->m_im);
std::complex<double> result;
switch (op) {
case (ASR::binopType::Add): { result = left_value + right_value; break; }
case (ASR::binopType::Sub): { result = left_value - right_value; break; }
case (ASR::binopType::Mul): { result = left_value * right_value; break; }
case (ASR::binopType::Div): { result = left_value / right_value; break; }
case (ASR::binopType::Pow): { result = std::pow(left_value, right_value); break; }
default: { LFORTRAN_ASSERT(false); }
}
value = ASR::down_cast<ASR::expr_t>(ASR::make_ConstantComplex_t(al, loc,
std::real(result), std::imag(result), dest_type));
}
else if (ASRUtils::is_logical(*dest_type)) {
int8_t left_value = ASR::down_cast<ASR::ConstantLogical_t>(
ASRUtils::expr_value(left))->m_value;
int8_t right_value = ASR::down_cast<ASR::ConstantLogical_t>(
ASRUtils::expr_value(right))->m_value;
int8_t result;
switch (op) {
case (ASR::binopType::Add): { result = left_value + right_value; break; }
case (ASR::binopType::Sub): { result = left_value - right_value; break; }
case (ASR::binopType::Mul): { result = left_value * right_value; break; }
case (ASR::binopType::Pow): { result = std::pow(left_value, right_value); break; }
case (ASR::binopType::Div): { } // TODO: Handle division of logicals
default: { LFORTRAN_ASSERT(false); } // should never happen
}
value = ASR::down_cast<ASR::expr_t>(ASR::make_ConstantInteger_t(
al, loc, result, int_type));
dest_type = int_type;
}
}
ASR::expr_t *overloaded = nullptr;
tmp = ASR::make_BinOp_t(al, loc, left, op, right, dest_type,
value, overloaded);
}
void visit_Name(const AST::Name_t &x) {
std::string name = x.m_id;
ASR::symbol_t *s = current_scope->resolve_symbol(name);
if (s) {
tmp = ASR::make_Var_t(al, x.base.base.loc, s);
} else {
throw SemanticError("Variable '" + name + "' not declared",
x.base.base.loc);
}
}
void visit_NamedExpr(const AST::NamedExpr_t &x) {
this->visit_expr(*x.m_target);
ASR::expr_t *target = ASRUtils::EXPR(tmp);
ASR::ttype_t *target_type = ASRUtils::expr_type(target);
this->visit_expr(*x.m_value);
ASR::expr_t *value = ASRUtils::EXPR(tmp);
ASR::ttype_t *value_type = ASRUtils::expr_type(value);
LFORTRAN_ASSERT(ASRUtils::check_equal_type(target_type, value_type));
tmp = ASR::make_NamedExpr_t(al, x.base.base.loc, target, value, value_type);
}
void visit_ConstantInt(const AST::ConstantInt_t &x) {
int64_t i = x.m_value;
ASR::ttype_t *type = ASRUtils::TYPE(ASR::make_Integer_t(al, x.base.base.loc,
4, nullptr, 0));
tmp = ASR::make_ConstantInteger_t(al, x.base.base.loc, i, type);
}
void visit_ConstantFloat(const AST::ConstantFloat_t &x) {
double f = x.m_value;
ASR::ttype_t *type = ASRUtils::TYPE(ASR::make_Real_t(al, x.base.base.loc,
8, nullptr, 0));
tmp = ASR::make_ConstantReal_t(al, x.base.base.loc, f, type);
}
void visit_ConstantComplex(const AST::ConstantComplex_t &x) {
double re = x.m_re, im = x.m_im;
ASR::ttype_t *type = ASRUtils::TYPE(ASR::make_Complex_t(al, x.base.base.loc,
8, nullptr, 0));
tmp = ASR::make_ConstantComplex_t(al, x.base.base.loc, re, im, type);
}
void visit_ConstantStr(const AST::ConstantStr_t &x) {
char *s = x.m_value;
size_t s_size = std::string(s).size();
ASR::ttype_t *type = ASRUtils::TYPE(ASR::make_Character_t(al, x.base.base.loc,
1, s_size, nullptr, nullptr, 0));
tmp = ASR::make_ConstantString_t(al, x.base.base.loc, s, type);
}
void visit_ConstantBool(const AST::ConstantBool_t &x) {
bool b = x.m_value;
ASR::ttype_t *type = ASRUtils::TYPE(ASR::make_Logical_t(al, x.base.base.loc,
1, nullptr, 0));
tmp = ASR::make_ConstantLogical_t(al, x.base.base.loc, b, type);
}