Skip to content

Commit c986b38

Browse files
committed
ASR: Support param intent in, out, inout
1 parent 8d679fa commit c986b38

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,6 +1581,29 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
15811581
}
15821582
}
15831583

1584+
AST::expr_t* get_var_intent_and_annotation(AST::expr_t *annotation, ASR::intentType &intent) {
1585+
if (AST::is_a<AST::Subscript_t>(*annotation)) {
1586+
AST::Subscript_t *s = AST::down_cast<AST::Subscript_t>(annotation);
1587+
if (AST::is_a<AST::Name_t>(*s->m_value)) {
1588+
std::string ann_name = AST::down_cast<AST::Name_t>(s->m_value)->m_id;
1589+
if (ann_name == "In") {
1590+
intent = ASRUtils::intent_in;
1591+
return s->m_slice;
1592+
} else if (ann_name == "InOut") {
1593+
intent = ASRUtils::intent_inout;
1594+
return s->m_slice;
1595+
} else if (ann_name == "Out") {
1596+
intent = ASRUtils::intent_out;
1597+
return s->m_slice;
1598+
}
1599+
return annotation;
1600+
} else {
1601+
throw SemanticError("Only Name in Subscript supported for now in annotation", annotation->base.loc);
1602+
}
1603+
}
1604+
return annotation;
1605+
}
1606+
15841607
// Convert Python AST type annotation to an ASR type
15851608
// Examples:
15861609
// i32, i64, f32, f64
@@ -3741,7 +3764,9 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
37413764
if (x.m_args.m_args[i].m_annotation == nullptr) {
37423765
throw SemanticError("Argument does not have a type", loc);
37433766
}
3744-
ASR::ttype_t *arg_type = ast_expr_to_asr_type(x.base.base.loc, *x.m_args.m_args[i].m_annotation);
3767+
ASR::intentType s_intent = ASRUtils::intent_unspecified;
3768+
AST::expr_t* arg_annotation_type = get_var_intent_and_annotation(x.m_args.m_args[i].m_annotation, s_intent);
3769+
ASR::ttype_t *arg_type = ast_expr_to_asr_type(x.base.base.loc, *arg_annotation_type);
37453770
// Set the function as generic if an argument is typed with a type parameter
37463771
if (ASRUtils::is_generic(*arg_type)) {
37473772
ASR::ttype_t* arg_type_type = ASRUtils::get_type_parameter(arg_type);
@@ -3766,12 +3791,13 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
37663791
}
37673792

37683793
std::string arg_s = arg;
3769-
37703794
ASR::expr_t *value = nullptr;
37713795
ASR::expr_t *init_expr = nullptr;
3772-
ASR::intentType s_intent = ASRUtils::intent_in;
3773-
if (ASRUtils::is_array(arg_type)) {
3774-
s_intent = ASRUtils::intent_inout;
3796+
if (s_intent == ASRUtils::intent_unspecified) {
3797+
s_intent = ASRUtils::intent_in;
3798+
if (ASRUtils::is_array(arg_type)) {
3799+
s_intent = ASRUtils::intent_inout;
3800+
}
37753801
}
37763802
ASR::storage_typeType storage_type =
37773803
ASR::storage_typeType::Default;

0 commit comments

Comments
 (0)