FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
lpython/src/lpython/semantics/python_ast_to_asr.cpp at main · salvo-polizzi/lpython · GitHub
lpython/src/lpython/semantics/python_ast_to_asr.cpp at main · salvo-polizzi/lpython · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
salvo-polizzi
/
lpython
Public
forked from
lcompilers/lpython
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
lpython
/
src
/
lpython
/
semantics
/
python_ast_to_asr.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
8076 lines (7663 loc) · 402 KB
Breadcrumbs
lpython
/
src
/
lpython
/
semantics
/
python_ast_to_asr.cpp
Copy path
File metadata and controls
8076 lines (7663 loc) · 402 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
<
set
>
#
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/config.h
>
#
include
<
libasr/string_utils.h
>
#
include
<
libasr/utils.h
>
#
include
<
libasr/pass/instantiate_template.h
>
#
include
<
libasr/pass/wrap_global_stmts.h
>
#
include
<
libasr/pass/intrinsic_function_registry.h
>
#
include
<
libasr/pass/intrinsic_array_function_registry.h
>
#
include
<
libasr/modfile.h
>
#
include
<
libasr/casting_utils.h
>
#
include
<
libasr/codegen/asr_to_fortran.h
>
#
include
<
libasr/pickle.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
>
#
include
<
lpython/semantics/python_attribute_eval.h
>
#
include
<
lpython/semantics/python_intrinsic_eval.h
>
#
include
<
lpython/parser/parser.h
>
#
include
<
libasr/serialization.h
>
namespace
LCompilers
::LPython {
int
save_pyc_files
(
const
ASR
::TranslationUnit_t &u,
std::string infile) {
diag::Diagnostics diagnostics;
LCOMPILERS_ASSERT
(
asr_verify
(u,
true
, diagnostics));
std::string modfile_binary =
save_pycfile
(u);
while
( infile.
back
() !=
'
.
'
) {
infile.
pop_back
();
}
std::string modfile = infile +
"
pyc
"
;
{
std::ofstream out;
out.
open
(modfile, std::ofstream::out | std::ofstream::binary);
out << modfile_binary;
}
return
0
;
}
//
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
Result<std::string>
get_full_path
(
const
std::string &filename,
const
std::string &runtime_library_dir, std::string& input,
bool
&lpython,
bool
& enum_py) {
lpython =
false
;
enum_py =
false
;
std::string file_path;
if
( *(runtime_library_dir.
rbegin
()) ==
'
/
'
) {
file_path = runtime_library_dir + filename;
}
else
{
file_path = runtime_library_dir +
"
/
"
+ filename;
}
bool
status =
read_file
(file_path, input);
if
(status) {
return
file_path;
}
else
{
//
If this is `lpython`, do a special lookup
if
(filename ==
"
lpython.py
"
) {
file_path = runtime_library_dir +
"
/lpython/
"
+ filename;
status =
read_file
(file_path, input);
if
(status) {
lpython =
true
;
return
file_path;
}
else
{
return
Error
();
}
}
else
if
(
startswith
(filename,
"
numpy.py
"
)) {
file_path = runtime_library_dir +
"
/lpython_intrinsic_
"
+ filename;
status =
read_file
(file_path, input);
if
(status) {
return
file_path;
}
else
{
return
Error
();
}
}
else
if
(
startswith
(filename,
"
enum.py
"
)) {
enum_py =
true
;
return
Error
();
}
else
{
return
Error
();
}
}
}
bool
set_module_path
(std::string infile0, std::vector<std::string> &rl_path,
std::string& infile, std::string& path_used, std::string& input,
bool
& lpython,
bool
& enum_py) {
for
(
auto
path: rl_path) {
Result<std::string> rinfile =
get_full_path
(infile0, path, input, lpython, enum_py);
if
(rinfile.
ok
) {
infile = rinfile.
result
;
path_used = path;
return
true
;
}
}
return
false
;
}
ASR
::TranslationUnit_t*
compile_module_till_asr
(
Allocator& al, SymbolTable* symtab, std::string module_name,
std::vector<std::string> &rl_path, std::string infile,
const
Location &loc, diag::Diagnostics &diagnostics, LocationManager &lm,
const
std::function<
void
(
const
std::string &,
const
Location &)> err,
bool allow_implicit_casting) {
{
LocationManager::FileLocations fl;
fl.
in_filename
= infile;
lm.
files
.
push_back
(fl);
std::string input =
read_file
(infile);
lm.
file_ends
.
push_back
(lm.
file_ends
.
back
() + input.
size
());
lm.
init_simple
(input);
}
Result<
AST
::
ast_t
*> r =
parse_python_file
(al, rl_path[
0
], infile,
diagnostics, lm.
file_ends
.
end
()[-
2
],
false
);
if
(!r.
ok
) {
err
(
"
The file '
"
+ infile +
"
' failed to parse
"
, loc);
}
LCompilers::LPython::
AST
::
ast_t
* ast = r.
result
;
//
Convert the module from AST to ASR
CompilerOptions compiler_options;
compiler_options.
po
.
disable_main
=
true
;
compiler_options.
symtab_only
=
false
;
Result<
ASR
::TranslationUnit_t*> r2 =
python_ast_to_asr
(al, lm, symtab, *ast,
diagnostics, compiler_options,
false
, module_name, infile, allow_implicit_casting);
//
TODO: Uncomment once a check is added for ensuring
//
that module.py file hasn't changed between
//
builds.
//
save_pyc_files(*r2.result, infile + "c");
if
(!r2.
ok
) {
LCOMPILERS_ASSERT
(diagnostics.
has_error
())
return
nullptr
;
//
Error
}
return
r2.
result
;
}
void
fill_module_dependencies
(SymbolTable* symtab, SetChar& mod_deps,
Allocator& al) {
if
( symtab ==
nullptr
) {
return
;
}
for
(
auto
& itr: symtab->
get_scope
() ) {
if
(
ASR
::is_a<
ASR
::ExternalSymbol_t>(*itr.
second
) ) {
ASR
::ExternalSymbol_t* ext_sym =
ASR
::down_cast<
ASR
::ExternalSymbol_t>(itr.
second
);
mod_deps.
push_back
(al, ext_sym->
m_module_name
);
}
else
{
SymbolTable* sym_symtab =
ASRUtils::symbol_symtab
(itr.
second
);
fill_module_dependencies
(sym_symtab, mod_deps, al);
}
}
}
ASR
::Module_t*
load_module
(Allocator &al, SymbolTable *symtab,
const
std::string &module_name,
const
Location &loc, diag::Diagnostics &diagnostics,
LocationManager &lm,
bool
intrinsic,
std::vector<std::string> &rl_path,
bool
&lpython,
bool
& enum_py,
bool
& copy,
bool
& sympy,
const
std::function<
void
(
const
std::string &,
const
Location &)> err,
bool allow_implicit_casting) {
lpython =
false
;
enum_py =
false
;
copy =
false
;
sympy =
false
;
if
( module_name ==
"
copy
"
) {
copy =
true
;
return
nullptr
;
}
if
(module_name ==
"
sympy
"
) {
sympy =
true
;
return
nullptr
;
}
LCOMPILERS_ASSERT
(symtab);
if
(symtab->
get_scope
().
find
(module_name) != symtab->
get_scope
().
end
()) {
ASR
::
symbol_t
*m = symtab->
get_symbol
(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);
}
}
LCOMPILERS_ASSERT
(symtab->
parent
==
nullptr
);
//
Parse the module `module_name`.py to AST
std::string infile0 = module_name +
"
.py
"
;
std::string infile0c = infile0 +
"
c
"
;
std::string path_used =
"
"
, infile;
bool
compile_module =
true
;
ASR
::TranslationUnit_t* mod1 =
nullptr
;
std::string input;
std::string mod1_name =
"
"
;
bool
found =
set_module_path
(infile0c, rl_path, infile,
path_used, input, lpython, enum_py);
if
( !found ) {
input.
clear
();
found =
set_module_path
(infile0, rl_path, infile,
path_used, input, lpython, enum_py);
}
else
{
mod1 =
load_pycfile
(al, input,
false
);
fix_external_symbols
(*mod1, *
ASRUtils::get_tu_symtab
(symtab));
diag::Diagnostics diagnostics;
LCOMPILERS_ASSERT
(
asr_verify
(*mod1,
true
, diagnostics));
compile_module =
false
;
}
if
( enum_py ) {
return
nullptr
;
}
if
(!found) {
err
(
"
Could not find the module '
"
+ module_name +
"
'. If an import path
"
"
is available, please use the `-I` option to specify it
"
, loc);
}
if
(lpython)
return
nullptr
;
if
( compile_module ) {
diagnostics.
add
(
diag::Diagnostic
(
"
The module '
"
+ module_name +
"
' located in
"
+ infile +
"
cannot be loaded
"
,
diag::Level::Warning, diag::Stage::Semantic, {
diag::Label
(
"
imported here
"
, {loc})
})
);
if
(module_name ==
"
__init__
"
) {
std::string module_dir_name = infile.
substr
(
0
, infile.
find_last_of
(
'
/
'
));
//
assign module directory name
mod1_name = module_dir_name.
substr
(module_dir_name.
find_last_of
(
'
/
'
) +
1
);
}
else
{
mod1_name = module_name;
}
mod1 =
compile_module_till_asr
(al, symtab, mod1_name, rl_path, infile, loc, diagnostics,
lm, err, allow_implicit_casting);
if
(mod1 ==
nullptr
) {
throw
SemanticAbort
();
}
else
{
diagnostics.
diagnostics
.
pop_back
();
}
}
ASR
::
symbol_t
* mod1_sym = symtab->
resolve_symbol
(mod1_name);
if
(!mod1_sym) {
throw
SemanticError
(
"
Module `
"
+ module_name +
"
` not found
"
, loc);
}
ASR
::Module_t* mod1_mod =
ASR
::down_cast<
ASR
::Module_t>(mod1_sym);
if
(intrinsic) {
mod1_mod->
m_intrinsic
=
true
;
//
TODO: I think we should just store intrinsic once, in the module
//
itself
//
Mark each function as intrinsic also
for
(
auto
&item : mod1_mod->
m_symtab
->
get_scope
()) {
if
(
ASR
::is_a<
ASR
::Function_t>(*item.
second
)) {
ASR
::Function_t *s =
ASR
::down_cast<
ASR
::Function_t>(item.
second
);
if
(
ASRUtils::get_FunctionType
(s)->
m_abi
==
ASR
::abiType::Source) {
ASRUtils::get_FunctionType
(s)->
m_abi
=
ASR
::abiType::Intrinsic;
}
if
(s->
n_body
==
0
) {
std::string name = s->
m_name
;
if
(name ==
"
ubound
"
|| name ==
"
lbound
"
) {
ASRUtils::get_FunctionType
(s)->
m_deftype
=
ASR
::deftypeType::Interface;
}
}
}
}
}
//
and return it
return
mod1_mod;
}
//
Here, we call the global_initializer & global_statements to
//
initialize and execute the global symbols
void
get_calls_to_global_init_and_stmts
(Allocator &al,
const
Location &loc, SymbolTable* scope,
ASR
::Module_t* mod, std::vector<
ASR
::
asr_t
*> &tmp_vec) {
std::string mod_name = mod->
m_name
;
std::string g_func_name = mod_name +
"
global_init
"
;
ASR
::
symbol_t
*g_func = mod->
m_symtab
->
get_symbol
(g_func_name);
if
(g_func && !scope->
get_symbol
(g_func_name)) {
ASR
::
symbol_t
*es =
ASR
::down_cast<
ASR
::
symbol_t
>(
ASR::make_ExternalSymbol_t
(al, mod->
base
.
base
.
loc
,
scope,
s2c
(al, g_func_name), g_func,
s2c
(al, mod_name),
nullptr
,
0
,
s2c
(al, g_func_name),
ASR
::accessType::Public));
scope->
add_symbol
(g_func_name, es);
tmp_vec.
push_back
(
ASRUtils::make_SubroutineCall_t_util
(al, loc,
es, g_func,
nullptr
,
0
,
nullptr
,
nullptr
,
false
));
}
g_func_name = mod_name +
"
global_stmts
"
;
g_func = mod->
m_symtab
->
get_symbol
(g_func_name);
if
(g_func && !scope->
get_symbol
(g_func_name)) {
ASR
::
symbol_t
*es =
ASR
::down_cast<
ASR
::
symbol_t
>(
ASR::make_ExternalSymbol_t
(al, mod->
base
.
base
.
loc
,
scope,
s2c
(al, g_func_name), g_func,
s2c
(al, mod_name),
nullptr
,
0
,
s2c
(al, g_func_name),
ASR
::accessType::Public));
scope->
add_symbol
(g_func_name, es);
tmp_vec.
push_back
(
ASRUtils::make_SubroutineCall_t_util
(al, loc,
es, g_func,
nullptr
,
0
,
nullptr
,
nullptr
,
false
));
}
}
template
<
class
Struct
>
class
CommonVisitor
:
public
AST
::BaseVisitor<Struct> {
public:
diag::Diagnostics &diag;
ASR
::
asr_t
*tmp;
/*
If `tmp` is not null, then `tmp_vec` is ignored and `tmp` is used as the only result (statement or
expression). If `tmp` is null, then `tmp_vec` is used to return any number of statements:
0 (no statement returned), 1 (redundant, one should use `tmp` for that), 2, 3, ... etc.
*/
std::vector<
ASR
::
asr_t
*> tmp_vec;
//
Used to store the initializer for the global variables like list, ...
Vec<
ASR
::
asr_t
*> global_init;
Allocator &al;
LocationManager &lm;
SymbolTable *current_scope;
SetChar 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;
//
This is the current module name that we are compiling from AST to ASR
//
It is an empty string for main_module
std::string module_name;
PythonIntrinsicProcedures intrinsic_procedures;
ProceduresDatabase procedures_db;
AttributeHandler attr_handler;
IntrinsicNodeHandler intrinsic_node_handler;
std::map<
int
,
ASR
::
symbol_t
*> &ast_overload;
std::string parent_dir;
std::vector<std::string> import_paths;
/*
current_body exists only for Functions, For, If (& its Else part), While.
current_body does not exist for Modules, ClassDef/Structs.
*/
Vec<
ASR
::
stmt_t
*> *current_body;
ASR
::
expr_t
* assign_asr_target;
std::map<std::string,
int
> generic_func_nums;
std::map<std::string, std::map<std::string,
ASR
::
ttype_t
*>> generic_func_subs;
std::vector<
ASR
::
symbol_t
*> rt_vec;
std::map<std::string, std::string> context_map;
SetChar dependencies;
bool
allow_implicit_casting;
//
Stores the name of imported functions and the modules they are imported from
std::map<std::string, std::string> imported_functions;
bool
using_args_attr =
false
;
std::map<std::string, std::string> numpy2lpythontypes = {
{
"
bool
"
,
"
bool
"
},
{
"
bool_
"
,
"
bool
"
},
{
"
int8
"
,
"
i8
"
},
{
"
int16
"
,
"
i16
"
},
{
"
int32
"
,
"
i32
"
},
{
"
int64
"
,
"
i64
"
},
{
"
uint8
"
,
"
u8
"
},
{
"
uint16
"
,
"
u16
"
},
{
"
uint32
"
,
"
u32
"
},
{
"
uint64
"
,
"
u64
"
},
{
"
float32
"
,
"
f32
"
},
{
"
float64
"
,
"
f64
"
},
{
"
float_
"
,
"
f64
"
},
{
"
complex64
"
,
"
c32
"
},
{
"
complex128
"
,
"
c64
"
},
{
"
complex_
"
,
"
c64
"
},
{
"
object
"
,
"
T
"
}
};
CommonVisitor
(Allocator &al, LocationManager &lm, SymbolTable *symbol_table,
diag::Diagnostics &diagnostics,
bool
main_module, std::string module_name,
std::map<
int
,
ASR
::
symbol_t
*> &ast_overload, std::string parent_dir,
std::vector<std::string> import_paths,
bool
allow_implicit_casting_)
: diag{diagnostics}, al{al}, lm{lm}, current_scope{symbol_table}, main_module{main_module}, module_name{module_name},
ast_overload{ast_overload}, parent_dir{parent_dir}, import_paths{import_paths},
current_body{
nullptr
}, assign_asr_target{
nullptr
}, allow_implicit_casting{allow_implicit_casting_} {
current_module_dependencies.
reserve
(al,
4
);
global_init.
reserve
(al,
1
);
}
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
();
}
return
ASR::make_Var_t
(al, loc, v);
}
ASR
::
symbol_t
*
resolve_intrinsic_function
(
const
Location &loc,
const
std::string &remote_sym) {
LCOMPILERS_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
();
std::vector<std::string> paths = {rl_path, parent_dir};
bool
lpython, enum_py, copy, sympy;
ASR
::Module_t *m =
load_module
(al, tu_symtab, module_name,
loc, diag, lm,
true
, paths,
lpython, enum_py, copy, sympy,
[&](
const
std::string &msg,
const
Location &loc) {
throw
SemanticError
(msg, loc); },
allow_implicit_casting);
LCOMPILERS_ASSERT
(!lpython && !enum_py)
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)
)) {
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);
std::string sym = fn_name;
ASR
::
symbol_t
*v =
nullptr
;
if
( current_scope->
get_symbol
(sym) ==
nullptr
) {
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
);
current_scope->
add_symbol
(sym,
ASR
::down_cast<
ASR
::
symbol_t
>(fn));
v =
ASR
::down_cast<
ASR
::
symbol_t
>(fn);
}
else
{
v = current_scope->
get_symbol
(sym);
}
current_module_dependencies.
push_back
(al, m->
m_name
);
return
v;
}
void
handle_builtin_attribute
(
ASR
::
expr_t
*s, std::string attr_name,
const
Location &loc, Vec<
ASR
::
expr_t
*> &args) {
tmp = attr_handler.
get_attribute
(s, attr_name, al, loc, args, diag);
return
;
}
void
handle_symbolic_attribute
(
ASR
::
expr_t
*s, std::string attr_name,
const
Location &loc, Vec<
ASR
::
expr_t
*> &args) {
tmp = attr_handler.
get_symbolic_attribute
(s, attr_name, al, loc, args, diag);
return
;
}
void
fill_expr_in_ttype_t
(std::vector<
ASR
::
expr_t
*>& exprs,
ASR
::
dimension_t
* dims,
size_t
n_dims) {
for
(
size_t
i =
0
; i < n_dims; i++ ) {
exprs.
push_back
(dims[i].
m_start
);
exprs.
push_back
(dims[i].
m_length
);
}
}
void
fix_exprs_ttype_t
(std::vector<
ASR
::
expr_t
*>& exprs,
Vec<
ASR
::
call_arg_t
>& orig_args,
ASR
::Function_t* orig_func=
nullptr
) {
ASRUtils::ExprStmtDuplicator
expr_duplicator
(al);
expr_duplicator.
allow_procedure_calls
=
true
;
ASRUtils::ReplaceArgVisitor
arg_replacer
(al, current_scope, orig_func,
orig_args, dependencies,
current_module_dependencies);
for
(
size_t
i =
0
; i < exprs.
size
(); i++ ) {
ASR
::
expr_t
* expri = exprs[i];
if
(expri) {
expr_duplicator.
success
=
true
;
ASR
::
expr_t
* expri_copy = expr_duplicator.
duplicate_expr
(expri);
LCOMPILERS_ASSERT
(expr_duplicator.
success
);
arg_replacer.
current_expr
= &expri_copy;
arg_replacer.
replace_expr
(expri_copy);
exprs[i] = expri_copy;
}
}
}
void
fill_new_dims
(
ASR
::Array_t* t,
const
std::vector<
ASR
::
expr_t
*>& func_calls,
Vec<
ASR
::
dimension_t
>& new_dims) {
new_dims.
reserve
(al, t->
n_dims
);
for
(
size_t
i =
0
, j =
0
; i < func_calls.
size
(); i +=
2
, j++ ) {
ASR
::
dimension_t
new_dim;
if
(func_calls[i] !=
nullptr
) {
new_dim.
loc
= func_calls[i]->
base
.
loc
;
new_dim.
m_start
= func_calls[i];
new_dim.
m_length
= func_calls[i +
1
];
new_dims.
push_back
(al, new_dim);
}
else
{
new_dims.
push_back
(al, t->
m_dims
[j]);
}
}
}
ASR
::
ttype_t
*
handle_return_type
(
ASR
::
ttype_t
*return_type,
const
Location &loc,
Vec<
ASR
::
call_arg_t
>& args,
ASR
::Function_t* f=
nullptr
) {
//
Rebuild the return type if needed and make FunctionCalls use ExternalSymbol
std::vector<
ASR
::
expr_t
*> func_calls;
switch
( return_type->
type
) {
case
ASR
::ttypeType::Allocatable: {
ASR
::Allocatable_t*
allocatable_t
=
ASR
::down_cast<
ASR
::Allocatable_t>(return_type);
return
ASRUtils::TYPE
(
ASR::make_Allocatable_t
(al, loc,
ASRUtils::type_get_past_allocatable
(
ASRUtils::type_get_past_pointer
(
handle_return_type
(
allocatable_t
->
m_type
, loc, args, f)))));
}
case
ASR
::ttypeType::Pointer: {
ASR
::Pointer_t*
pointer_t
=
ASR
::down_cast<
ASR
::Pointer_t>(return_type);
return
ASRUtils::TYPE
(
ASR::make_Pointer_t
(al, loc,
ASRUtils::type_get_past_allocatable
(
ASRUtils::type_get_past_pointer
(
handle_return_type
(
pointer_t
->
m_type
, loc, args, f)))));
}
case
ASR
::ttypeType::Array: {
ASR
::Array_t* t =
ASR
::down_cast<
ASR
::Array_t>(return_type);
ASR
::
ttype_t
* t_m_type =
handle_return_type
(t->
m_type
, loc, args, f);
fill_expr_in_ttype_t
(func_calls, t->
m_dims
, t->
n_dims
);
fix_exprs_ttype_t
(func_calls, args, f);
Vec<
ASR
::
dimension_t
> new_dims;
fill_new_dims
(t, func_calls, new_dims);
return
ASRUtils::make_Array_t_util
(al, loc, t_m_type, new_dims.
p
, new_dims.
size
());
}
case
ASR
::ttypeType::Character: {
ASR
::Character_t *t =
ASR
::down_cast<
ASR
::Character_t>(return_type);
func_calls.
push_back
(t->
m_len_expr
);
fix_exprs_ttype_t
(func_calls, args, f);
int64_t
a_len = t->
m_len
;
if
( func_calls[
0
] ) {
a_len = ASRUtils::extract_len<SemanticError>(func_calls[
0
], loc);
}
return
ASRUtils::TYPE
(
ASR::make_Character_t
(al, loc, t->
m_kind
, a_len, func_calls[
0
]));
}
case
ASR
::ttypeType::Struct: {
ASR
::Struct_t* struct_t_type =
ASR
::down_cast<
ASR
::Struct_t>(return_type);
ASR
::
symbol_t
*sym = struct_t_type->
m_derived_type
;
ASR
::
symbol_t
*es_s = current_scope->
resolve_symbol
(
ASRUtils::symbol_name
(sym));
if
(es_s ==
nullptr
) {
ASR
::StructType_t *st =
ASR
::down_cast<
ASR
::StructType_t>(sym);
ASR
::Module_t* sym_module =
ASRUtils::get_sym_module
(sym);
LCOMPILERS_ASSERT
(sym_module !=
nullptr
);
std::string st_name =
"
1_
"
+
std::string
(st->
m_name
);
if
(current_scope->
get_symbol
(st_name)) {
sym = current_scope->
get_symbol
(st_name);
}
else
{
sym =
ASR
::down_cast<
ASR
::
symbol_t
>(
ASR::make_ExternalSymbol_t
(
al, st->
base
.
base
.
loc
, current_scope,
s2c
(al, st_name),
sym, sym_module->
m_name
,
nullptr
,
0
, st->
m_name
,
ASR
::accessType::Public));
current_scope->
add_symbol
(st_name, sym);
}
}
else
{
sym = es_s;
}
return
ASRUtils::TYPE
(
ASR::make_Struct_t
(al, loc, sym));
}
default
: {
return
return_type;
}
}
return
nullptr
;
}
void
visit_AsyncFunctionDef
(
const
AST
::AsyncFunctionDef_t &x){
throw
SemanticError
(
"
The `async` keyword is currently not supported
"
, x.
base
.
base
.
loc
);
}
void
visit_expr_list
(
AST
::
expr_t
** exprs,
size_t
n,
Vec<
ASR
::
expr_t
*>& exprs_vec) {
LCOMPILERS_ASSERT
(exprs_vec.
reserve_called
);
for
(
size_t
i =
0
; i < n; i++ ) {
this
->
visit_expr
(*exprs[i]);
exprs_vec.
push_back
(al,
ASRUtils::EXPR
(tmp));
}
}
void
visit_expr_list
(
AST
::
expr_t
** exprs,
size_t
n,
Vec<
ASR
::
call_arg_t
>& call_args_vec) {
LCOMPILERS_ASSERT
(call_args_vec.
reserve_called
);
for
(
size_t
i =
0
; i < n; i++ ) {
this
->
visit_expr
(*exprs[i]);
ASR
::
expr_t
* expr =
nullptr
;
ASR
::
call_arg_t
arg;
arg.
loc
.
first
= -
1
;
arg.
loc
.
last
= -
1
;
if
(tmp) {
expr =
ASRUtils::EXPR
(tmp);
arg.
loc
= expr->
base
.
loc
;
}
arg.
m_value
= expr;
call_args_vec.
push_back
(al, arg);
}
}
int64_t
find_argument_position_from_name
(
ASR
::Function_t* orig_func, std::string arg_name,
const
Location& call_loc,
bool
raise_error) {
int64_t
arg_position = -
1
;
for
(
size_t
i =
0
; i < orig_func->
n_args
; i++ ) {
ASR
::Var_t* arg_Var =
ASR
::down_cast<
ASR
::Var_t>(orig_func->
m_args
[i]);
std::string original_arg_name =
ASRUtils::symbol_name
(arg_Var->
m_v
);
if
( original_arg_name == arg_name ) {
return
i;
}
}
for
(
size_t
i =
0
; i <
ASRUtils::get_FunctionType
(orig_func)->
n_restrictions
; i++ ) {
ASR
::
symbol_t
* rt =
ASRUtils::get_FunctionType
(orig_func)->
m_restrictions
[i];
std::string rt_name =
ASRUtils::symbol_name
(rt);
if
( rt_name == arg_name ) {
return
-
2
;
}
}
if
( raise_error && arg_position == -
1
) {
throw
SemanticError
(
"
Function
"
+
std::string
(orig_func->
m_name
) +
"
doesn't have an argument named '
"
+ arg_name +
"
'
"
,
call_loc);
}
return
arg_position;
}
bool
visit_expr_list
(
AST
::
expr_t
** pos_args,
size_t
n_pos_args,
AST
::
keyword_t
* kwargs,
size_t
n_kwargs,
Vec<
ASR
::
call_arg_t
>& call_args_vec,
std::map<std::string,
ASR
::
symbol_t
*>& rt_subs,
ASR
::Function_t* orig_func,
const
Location& call_loc,
bool
raise_error=
true
) {
LCOMPILERS_ASSERT
(call_args_vec.
reserve_called
);
//
Fill the whole call_args_vec with nullptr
//
This is for error handling later on.
for
(
size_t
i =
0
; i < n_pos_args + n_kwargs; i++ ) {
ASR
::
call_arg_t
call_arg;
Location loc;
loc.
first
= loc.
last
=
1
;
call_arg.
m_value
=
nullptr
;
call_arg.
loc
= loc;
call_args_vec.
push_back
(al, call_arg);
}
//
Now handle positional arguments in the following loop
for
(
size_t
i =
0
; i < n_pos_args; i++ ) {
this
->
visit_expr
(*pos_args[i]);
ASR
::
expr_t
* expr =
ASRUtils::EXPR
(tmp);
call_args_vec.
p
[i].
loc
= expr->
base
.
loc
;
call_args_vec.
p
[i].
m_value
= expr;
}
//
Now handle keyword arguments in the following loop
for
(
size_t
i =
0
; i < n_kwargs; i++ ) {
this
->
visit_expr
(*kwargs[i].
m_value
);
ASR
::
expr_t
* expr =
ASRUtils::EXPR
(tmp);
std::string arg_name =
std::string
(kwargs[i].
m_arg
);
int64_t
arg_pos =
find_argument_position_from_name
(orig_func, arg_name, call_loc, raise_error);
if
( arg_pos == -
1
) {
return
false
;
}
//
Special treatment for argument to generic function's restriction
if
(arg_pos == -
2
) {
if
(
ASR
::is_a<
ASR
::Var_t>(*expr)) {
ASR
::Var_t* var =
ASR
::down_cast<
ASR
::Var_t>(expr);
rt_subs[arg_name] = var->
m_v
;
}
continue
;
}
if
( call_args_vec[arg_pos].
m_value
!=
nullptr
) {
if
( !raise_error ) {
return
false
;
}
throw
SemanticError
(
std::string
(orig_func->
m_name
) +
"
() got multiple values for argument '
"
+ arg_name +
"
'
"
,
call_loc);
}
call_args_vec.
p
[arg_pos].
loc
= expr->
base
.
loc
;
call_args_vec.
p
[arg_pos].
m_value
= expr;
}
return
true
;
}
int64_t
find_argument_position_from_name
(
ASR
::StructType_t* orig_struct, std::string arg_name) {
for
(
size_t
i =
0
; i < orig_struct->
n_members
; i++ ) {
std::string original_arg_name =
std::string
(orig_struct->
m_members
[i]);
if
( original_arg_name == arg_name ) {
return
i;
}
}
return
-
1
;
}
void
visit_expr_list
(
AST
::
expr_t
** pos_args,
size_t
n_pos_args,
AST
::
keyword_t
* kwargs,
size_t
n_kwargs,
Vec<
ASR
::
call_arg_t
>& call_args_vec,
ASR
::StructType_t* orig_struct,
const
Location &loc) {
LCOMPILERS_ASSERT
(call_args_vec.
reserve_called
);
//
Fill the whole call_args_vec with nullptr
//
This is for error handling later on.
for
(
size_t
i =
0
; i < n_pos_args + n_kwargs; i++ ) {
ASR
::
call_arg_t
call_arg;
Location loc;
loc.
first
= loc.
last
=
1
;
call_arg.
m_value
=
nullptr
;
call_arg.
loc
= loc;
call_args_vec.
push_back
(al, call_arg);
}
//
Now handle positional arguments in the following loop
for
(
size_t
i =
0
; i < n_pos_args; i++ ) {
this
->
visit_expr
(*pos_args[i]);
ASR
::
expr_t
* expr =
ASRUtils::EXPR
(tmp);
call_args_vec.
p
[i].
loc
= expr->
base
.
loc
;
call_args_vec.
p
[i].
m_value
= expr;
}
//
Now handle keyword arguments in the following loop
for
(
size_t
i =
0
; i < n_kwargs; i++ ) {
this
->
visit_expr
(*kwargs[i].
m_value
);
ASR
::
expr_t
* expr =
ASRUtils::EXPR
(tmp);
std::string arg_name =
std::string
(kwargs[i].
m_arg
);
int64_t
arg_pos =
find_argument_position_from_name
(orig_struct, arg_name);
if
( arg_pos == -
1
) {
throw
SemanticError
(
"
Member '
"
+ arg_name +
"
' not found in struct
"
, kwargs[i].
loc
);
}
else
if
(arg_pos >= (
int64_t
)call_args_vec.
size
()) {
throw
SemanticError
(
"
Not enough arguments to
"
+
std::string
(orig_struct->
m_name
)
+
"
(), expected
"
+
std::to_string
(orig_struct->
n_members
), loc);
}
if
( call_args_vec[arg_pos].
m_value
!=
nullptr
) {
throw
SemanticError
(
std::string
(orig_struct->
m_name
) +
"
() got multiple values for argument '
"
+ arg_name +
"
'
"
, kwargs[i].
loc
);
}
call_args_vec.
p
[arg_pos].
loc
= expr->
base
.
loc
;
call_args_vec.
p
[arg_pos].
m_value
= expr;
}
}
void
visit_expr_list_with_cast
(
ASR
::
expr_t
** m_args,
size_t
n_args,
Vec<
ASR
::
call_arg_t
>& call_args_vec,
Vec<
ASR
::
call_arg_t
>& args,
bool
check_type_equality=
true
) {
LCOMPILERS_ASSERT
(call_args_vec.
reserve_called
);
for
(
size_t
i =
0
; i < n_args; i++) {
ASR
::
call_arg_t
c_arg;
c_arg.
loc
= args[i].
loc
;
c_arg.
m_value
= args[i].
m_value
;
cast_helper
(m_args[i], c_arg.
m_value
,
true
);
ASR
::
ttype_t
* left_type =
ASRUtils::expr_type
(m_args[i]);
ASR
::
ttype_t
* right_type =
ASRUtils::expr_type
(c_arg.
m_value
);
if
( check_type_equality && !
ASRUtils::check_equal_type
(left_type, right_type) ) {
std::string ltype =
ASRUtils::type_to_str_python
(left_type);
std::string rtype =
ASRUtils::type_to_str_python
(right_type);
diag.
add
(
diag::Diagnostic
(
"
Type mismatch in procedure call; the types must be compatible
"
,
diag::Level::Error, diag::Stage::Semantic, {
diag::Label
(
"
type mismatch (passed argument type is
"
+ rtype +
"
but required type is
"
+ ltype +
"
)
"
,
{ c_arg.
loc
, left_type->
base
.
loc
})
})
);
throw
SemanticAbort
();
}
call_args_vec.
push_back
(al, c_arg);
}
}
ASR
::
ttype_t
*
get_type_from_var_annotation
(std::string var_annotation,
const
Location& loc, Vec<
ASR
::
dimension_t
>& dims,
AST
::
expr_t
** m_args=
nullptr
,
[[maybe_unused]]
size_t
n_args=
0
,
bool
raise_error=
true
,
ASR
::abiType abi=
ASR
::abiType::Source,
bool
is_argument=
false
) {
ASR
::
ttype_t
* type =
nullptr
;
ASR
::
symbol_t
*s = current_scope->
resolve_symbol
(var_annotation);
if
(s) {
if
(
ASR
::is_a<
ASR
::Variable_t>(*s)) {
ASR
::Variable_t *var_sym =
ASR
::down_cast<
ASR
::Variable_t>(s);
if
(var_sym->
m_type
->
type
==
ASR
::ttypeType::TypeParameter) {
ASR
::TypeParameter_t *type_param =
ASR
::down_cast<
ASR
::TypeParameter_t>(var_sym->
m_type
);
type =
ASRUtils::TYPE
(
ASR::make_TypeParameter_t
(al, loc, type_param->
m_param
));
type =
ASRUtils::make_Array_t_util
(al, loc, type, dims.
p
, dims.
size
(), abi, is_argument);
}
}
else
{
ASR
::
symbol_t
*der_sym =
ASRUtils::symbol_get_past_external
(s);
if
( der_sym ) {
if
(
ASR
::is_a<
ASR
::StructType_t>(*der_sym) ) {
type =
ASRUtils::TYPE
(
ASR::make_Struct_t
(al, loc, s));
type =
ASRUtils::make_Array_t_util
(al, loc, type, dims.
p
, dims.
size
(), abi, is_argument);
}
else
if
(
ASR
::is_a<
ASR
::EnumType_t>(*der_sym) ) {
type =
ASRUtils::TYPE
(
ASR::make_Enum_t
(al, loc, s));
type =
ASRUtils::make_Array_t_util
(al, loc, type, dims.
p
, dims.
size
(), abi, is_argument);
}
else
if
(
ASR
::is_a<
ASR
::UnionType_t>(*der_sym) ) {
type =
ASRUtils::TYPE
(
ASR::make_Union_t
(al, loc, s));
type =
ASRUtils::make_Array_t_util
(al, loc, type, dims.
p
, dims.
size
(), abi, is_argument);
}
}
}
}
else
if
(var_annotation ==
"
i8
"
) {
type =
ASRUtils::TYPE
(
ASR::make_Integer_t
(al, loc,
1
));
type =
ASRUtils::make_Array_t_util
(al, loc, type, dims.
p
, dims.
size
(), abi, is_argument);
}
else
if
(var_annotation ==
"
i16
"
) {
type =
ASRUtils::TYPE
(
ASR::make_Integer_t
(al, loc,
2
));
type =
ASRUtils::make_Array_t_util
(al, loc, type, dims.
p
, dims.
size
(), abi, is_argument);
}
else
if
(var_annotation ==
"
i32
"
) {
type =
ASRUtils::TYPE
(
ASR::make_Integer_t
(al, loc,
4
));
type =
ASRUtils::make_Array_t_util
(al, loc, type, dims.
p
, dims.
size
(), abi, is_argument);
}
else
if
(var_annotation ==
"
i64
"
) {
type =
ASRUtils::TYPE
(
ASR::make_Integer_t
(al, loc,
8
));
type =
ASRUtils::make_Array_t_util
(al, loc, type, dims.
p
, dims.
size
(), abi, is_argument);
}
else
if
(var_annotation ==
"
u8
"
) {
type =
ASRUtils::TYPE
(
ASR::make_UnsignedInteger_t
(al, loc,
1
));
type =
ASRUtils::make_Array_t_util
(al, loc, type, dims.
p
, dims.
size
(), abi, is_argument);
}
else
if
(var_annotation ==
"
u16
"
) {
type =
ASRUtils::TYPE
(
ASR::make_UnsignedInteger_t
(al, loc,
2
));
type =
ASRUtils::make_Array_t_util
(al, loc, type, dims.
p
, dims.
size
(), abi, is_argument);
}
else
if
(var_annotation ==
"
u32
"
) {
type =
ASRUtils::TYPE
(
ASR::make_UnsignedInteger_t
(al, loc,
4
));
type =
ASRUtils::make_Array_t_util
(al, loc, type, dims.
p
, dims.
size
(), abi, is_argument);
}
else
if
(var_annotation ==
"
u64
"
) {
type =
ASRUtils::TYPE
(
ASR::make_UnsignedInteger_t
(al, loc,
8
));
type =
ASRUtils::make_Array_t_util
(al, loc, type, dims.
p
, dims.
size
(), abi, is_argument);
}
else
if
(var_annotation ==
"
f32
"
) {
type =
ASRUtils::TYPE
(
ASR::make_Real_t
(al, loc,
4
));
type =
ASRUtils::make_Array_t_util
(al, loc, type, dims.
p
, dims.
size
(), abi, is_argument);
}
else
if
(var_annotation ==
"
f64
"
) {
type =
ASRUtils::TYPE
(
ASR::make_Real_t
(al, loc,
8
));
type =
ASRUtils::make_Array_t_util
(al, loc, type, dims.
p
, dims.
size
(), abi, is_argument);
}
else
if
(var_annotation ==
"
c32
"
) {
type =
ASRUtils::TYPE
(
ASR::make_Complex_t
(al, loc,
4
));
type =
ASRUtils::make_Array_t_util
(al, loc, type, dims.
p
, dims.
size
(), abi, is_argument);
}
else
if
(var_annotation ==
"
c64
"
) {
type =
ASRUtils::TYPE
(
ASR::make_Complex_t
(al, loc,
8
));
type =
ASRUtils::make_Array_t_util
(al, loc, type, dims.
p
, dims.
size
(), abi, is_argument);
}
else
if
(var_annotation ==
"
str
"
) {
type =
ASRUtils::TYPE
(
ASR::make_Character_t
(al, loc,
1
, -
2
,
nullptr
));
type =
ASRUtils::make_Array_t_util
(al, loc, type, dims.
p
, dims.
size
(), abi, is_argument);
}
else
if
(var_annotation ==
"
bool
"
|| var_annotation ==
"
i1
"
) {
type =
ASRUtils::TYPE
(
ASR::make_Logical_t
(al, loc,
4
));
type =
ASRUtils::make_Array_t_util
(al, loc, type, dims.
p
, dims.
size
(), abi, is_argument);
}
else
if
(var_annotation ==
"
CPtr
"
) {
type =
ASRUtils::TYPE
(
ASR::make_CPtr_t
(al, loc));
}
else
if
(var_annotation ==
"
pointer
"
) {
LCOMPILERS_ASSERT
(n_args ==
1
);
AST
::
expr_t
* underlying_type = m_args[
0
];
bool
is_allocatable =
false
;
type =
ast_expr_to_asr_type
(underlying_type->
base
.
loc
, *underlying_type, is_allocatable);
type =
ASRUtils::TYPE
(
ASR::make_Pointer_t
(al, loc, type));
}
else
if
(var_annotation ==
"
S
"
) {
type =
ASRUtils::TYPE
(
ASR::make_SymbolicExpression_t
(al, loc));
}
if
( !type && raise_error ) {
throw
SemanticError
(
"
Unsupported type annotation:
"
+ var_annotation, loc);
}
return
type;
}
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,
bool
skip_current_scope_check=
false
) {
new_sym_name =
ASRUtils::get_mangled_name
(m, new_sym_name);
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 '
"
+
std::string
(m->
m_name
) +
"
'
"
,
loc);
}
if
(!skip_current_scope_check &&
current_scope->
get_scope
().
find
(new_sym_name) != current_scope->
get_scope
().
end
()) {
throw
SemanticError
(new_sym_name +
"
already defined
"
, loc);
}
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, loc,
/*
a_symtab
*/
current_scope,
/*
a_name
*/
cname,
(
ASR
::
symbol_t
*)mfn,
m->
m_name
,
nullptr
,
0
, mfn->
m_name
,
ASR
::accessType::Public
);
current_module_dependencies.
push_back
(al, m->
m_name
);
return
ASR
::down_cast<
ASR
::
symbol_t
>(fn);
}
else
if
(
ASR
::is_a<
ASR
::StructType_t>(*t)) {
ASR
::StructType_t *st =
ASR
::down_cast<
ASR
::StructType_t>(t);
//
`st` is the StructType 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
*est =
ASR::make_ExternalSymbol_t
(
al, loc,
/*
a_symtab
*/
current_scope,
/*
a_name
*/
cname,
(
ASR
::
symbol_t
*)st,
m->
m_name
,
nullptr
,
0
, st->
m_name
,
ASR
::accessType::Public
);
current_module_dependencies.
push_back
(al, m->
m_name
);
return
ASR
::down_cast<
ASR
::
symbol_t
>(est);
}
else
if
(
ASR
::is_a<
ASR
::EnumType_t>(*t)) {
ASR
::EnumType_t *et =
ASR
::down_cast<
ASR
::EnumType_t>(t);
Str name;
name.
from_str
(al, new_sym_name);
char
*cname = name.
c_str
(al);
ASR
::
asr_t
*est =
ASR::make_ExternalSymbol_t
(
al, loc,
/*
a_symtab
*/
current_scope,
/*
a_name
*/
cname,
(
ASR
::
symbol_t
*)et,
m->
m_name
,
nullptr
,
0
, et->
m_name
,
ASR
::accessType::Public
);
current_module_dependencies.
push_back
(al, m->
m_name
);
return
ASR
::down_cast<
ASR
::
symbol_t
>(est);
}
else
if
(
ASR
::is_a<
ASR
::UnionType_t>(*t)) {
ASR
::UnionType_t *ut =
ASR
::down_cast<
ASR
::UnionType_t>(t);
Str name;
name.
from_str
(al, new_sym_name);
char
*cname = name.
c_str
(al);
ASR
::
asr_t
*est =
ASR::make_ExternalSymbol_t
(
al, loc,
/*
a_symtab
*/
current_scope,
/*
a_name
*/
cname,
(
ASR
::
symbol_t
*)ut,
m->
m_name
,
nullptr
,
0
, ut->
m_name
,
ASR
::accessType::Public
);
current_module_dependencies.
push_back
(al, m->
m_name
);
return
ASR
::down_cast<
ASR
::
symbol_t
>(est);
}
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, loc,
/*
a_symtab
*/
current_scope,
/*
a_name
*/
cname,
(
ASR
::
symbol_t
*)mv,
m->
m_name
,
nullptr
,
0
, mv->
m_name
,
ASR
::accessType::Public
);
current_module_dependencies.
push_back
(al, m->
m_name
);
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, loc,
/*
a_symtab
*/
current_scope,
/*
a_name
*/
cname,
(
ASR
::
symbol_t
*)gt,
m->
m_name
,
nullptr
,
0
, gt->
m_name
,
ASR
::accessType::Public
);
current_module_dependencies.
push_back
(al, m->
m_name
);
return
ASR
::down_cast<
ASR
::
symbol_t
>(v);
}
else
if
(
ASR
::is_a<
ASR
::ExternalSymbol_t>(*t)) {
ASR
::ExternalSymbol_t *es =
ASR
::down_cast<
ASR
::ExternalSymbol_t>(t);
SymbolTable *symtab = current_scope;
//
while (symtab->parent != nullptr) symtab = symtab->parent;
ASR
::
symbol_t
*sym = symtab->
resolve_symbol
(es->
m_module_name
);
ASR
::Module_t *m =
ASR
::down_cast<
ASR
::Module_t>(sym);
return
import_from_module
(al, m, symtab, es->
m_module_name
,
View remainder of file in raw view
Footer
© 2026 GitHub, Inc.
Footer navigation
Terms
Privacy
Security
Status
Community
Docs
Contact
You can’t perform that action at this time.
Back
|
FazBrowse Home
|
New Git URL