@@ -2669,9 +2669,9 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
2669
2669
2670
2670
void create_add_variable_to_scope(std::string& var_name,
2671
2671
ASR::ttype_t* type, const Location& loc, ASR::abiType abi,
2672
- ASR::storage_typeType storage_type=ASR::storage_typeType::Default) {
2672
+ ASR::storage_typeType storage_type=ASR::storage_typeType::Default,
2673
+ ASR::intentType s_intent = ASRUtils::intent_local) {
2673
2674
2674
- ASR::intentType s_intent = ASRUtils::intent_local;
2675
2675
ASR::abiType current_procedure_abi_type = abi;
2676
2676
ASR::accessType s_access = ASR::accessType::Public;
2677
2677
ASR::presenceType s_presence = ASR::presenceType::Required;
@@ -2891,10 +2891,10 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
2891
2891
ASR::expr_t* &init_expr,
2892
2892
bool wrap_derived_type_in_pointer=false,
2893
2893
ASR::abiType abi=ASR::abiType::Source,
2894
- bool inside_struct =false) {
2894
+ bool inside_dataclass=false, bool inside_class =false) {
2895
2895
bool is_allocatable = false, is_const = false;
2896
2896
ASR::ttype_t *type = nullptr;
2897
- if( inside_struct ) {
2897
+ if( inside_dataclass || inside_class ) {
2898
2898
type = ast_expr_to_asr_type(x.m_annotation->base.loc, *x.m_annotation, is_allocatable, is_const, true);
2899
2899
} else {
2900
2900
type = ast_expr_to_asr_type(x.m_annotation->base.loc, *x.m_annotation, is_allocatable, is_const, true, abi);
@@ -2917,8 +2917,13 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
2917
2917
storage_type = ASR::storage_typeType::Parameter;
2918
2918
}
2919
2919
2920
- create_add_variable_to_scope(var_name, type,
2921
- x.base.base.loc, abi, storage_type);
2920
+ if ( inside_class ) {
2921
+ create_add_variable_to_scope(var_name, type,
2922
+ x.base.base.loc, abi, storage_type, ASRUtils::intent_classmember);
2923
+ } else {
2924
+ create_add_variable_to_scope(var_name, type,
2925
+ x.base.base.loc, abi, storage_type);
2926
+ }
2922
2927
2923
2928
ASR::expr_t* assign_asr_target_copy = assign_asr_target;
2924
2929
this->visit_expr(*x.m_target);
@@ -2957,7 +2962,7 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
2957
2962
cast_helper(type, init_expr, init_expr->base.loc);
2958
2963
}
2959
2964
2960
- if (!inside_struct || is_const) {
2965
+ if (!inside_dataclass || is_const) {
2961
2966
process_variable_init_val(current_scope->get_symbol(var_name), x.base.base.loc, init_expr);
2962
2967
}
2963
2968
@@ -3034,7 +3039,7 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
3034
3039
Vec<char*>& member_names, Vec<char*>& member_fn_names,
3035
3040
SetChar& struct_dependencies, Vec<ASR::call_arg_t> &member_init,
3036
3041
bool is_enum_scope=false, ASR::abiType abi=ASR::abiType::Source,
3037
- bool is_class_scope = false) {
3042
+ bool is_class_scope = false, bool is_dataclass_scope = false ) {
3038
3043
int64_t prev_value = 1;
3039
3044
for( size_t i = 0; i < x.n_body; i++ ) {
3040
3045
if (AST::is_a<AST::Expr_t>(*x.m_body[i])) {
@@ -3082,7 +3087,7 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
3082
3087
ASR::ttype_t* i64_type = ASRUtils::TYPE(ASR::make_Integer_t(al, x.base.base.loc, 8));
3083
3088
init_expr = ASRUtils::EXPR(ASR::make_IntegerConstant_t(al, x.base.base.loc, -1, i64_type));
3084
3089
}
3085
- visit_AnnAssignUtil(*ann_assign, var_name, init_expr, false, abi, true );
3090
+ visit_AnnAssignUtil(*ann_assign, var_name, init_expr, false, abi, is_dataclass_scope, is_class_scope );
3086
3091
ASR::symbol_t* var_sym = current_scope->resolve_symbol(var_name);
3087
3092
ASR::call_arg_t c_arg;
3088
3093
c_arg.loc = var_sym->base.loc;
@@ -5425,20 +5430,22 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
5425
5430
throw SemanticError("Variable: '" + std::string(n->m_id) + "' is not declared",
5426
5431
x->base.loc);
5427
5432
}
5428
- ASR::Variable_t* v = ASR::down_cast<ASR::Variable_t>(s);
5429
- if (v->m_intent == ASR::intentType::In) {
5430
- std::string msg = "Hint: create a new local variable with a different name";
5431
- if (ASRUtils::is_aggregate_type(v->m_type)) {
5432
- msg = "Use InOut[" + ASRUtils::type_to_str_python(v->m_type) + "] to allow assignment";
5433
+ if (ASR::is_a<ASR::Variable_t>(*s)) {
5434
+ ASR::Variable_t* v = ASR::down_cast<ASR::Variable_t>(s);
5435
+ if (v->m_intent == ASR::intentType::In) {
5436
+ std::string msg = "Hint: create a new local variable with a different name";
5437
+ if (ASRUtils::is_aggregate_type(v->m_type)) {
5438
+ msg = "Use InOut[" + ASRUtils::type_to_str_python(v->m_type) + "] to allow assignment";
5439
+ }
5440
+ diag.add(diag::Diagnostic(
5441
+ "Assignment to an input function parameter `"
5442
+ + std::string(v->m_name) + "` is not allowed",
5443
+ diag::Level::Error, diag::Stage::Semantic, {
5444
+ diag::Label(msg, {x->base.loc})
5445
+ })
5446
+ );
5447
+ throw SemanticAbort();
5433
5448
}
5434
- diag.add(diag::Diagnostic(
5435
- "Assignment to an input function parameter `"
5436
- + std::string(v->m_name) + "` is not allowed",
5437
- diag::Level::Error, diag::Stage::Semantic, {
5438
- diag::Label(msg, {x->base.loc})
5439
- })
5440
- );
5441
- throw SemanticAbort();
5442
5449
}
5443
5450
return true;
5444
5451
} else if (AST::is_a<AST::Subscript_t>(*x)) {
@@ -5583,15 +5590,17 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
5583
5590
AST::Attribute_t *attr = AST::down_cast<AST::Attribute_t>(x.m_targets[i]);
5584
5591
if (AST::is_a<AST::Name_t>(*attr->m_value)) {
5585
5592
std::string name = AST::down_cast<AST::Name_t>(attr->m_value)->m_id;
5586
- ASR::symbol_t *s = current_scope->get_symbol (name);
5593
+ ASR::symbol_t *s = current_scope->resolve_symbol (name);
5587
5594
if (!s) {
5588
5595
throw SemanticError("Variable: '" + name + "' is not declared",
5589
5596
x.base.base.loc);
5590
5597
}
5591
- ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(s);
5592
- ASR::ttype_t *type = v->m_type;
5593
- if (ASRUtils::is_immutable(type)) {
5594
- throw SemanticError("readonly attribute", x.base.base.loc);
5598
+ if (ASR::is_a<ASR::Variable_t>(*s)) {
5599
+ ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(s);
5600
+ ASR::ttype_t *type = v->m_type;
5601
+ if (ASRUtils::is_immutable(type)) {
5602
+ throw SemanticError("readonly attribute", x.base.base.loc);
5603
+ }
5595
5604
}
5596
5605
}
5597
5606
}
@@ -6522,14 +6531,16 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
6522
6531
}
6523
6532
if( ASR::is_a<ASR::Variable_t>(*struct_member) ) {
6524
6533
ASR::Variable_t* struct_member_variable = ASR::down_cast<ASR::Variable_t>(struct_member);
6525
- ASR::expr_t* struct_type_var = ASRUtils::EXPR(ASR::make_Var_t(al, x.base.base.loc, org_sym));
6526
- tmp = ASR::make_StructStaticMember_t(al, x.base.base.loc,
6527
- struct_type_var, struct_member, struct_member_variable->m_type,
6528
- nullptr);
6534
+ if (struct_member_variable->m_intent == ASRUtils::intent_classmember) {
6535
+ tmp = ASR::make_StructStaticMember_t(al, x.base.base.loc,
6536
+ s2c(al, struct_type->m_name), struct_member, struct_member_variable->m_type,
6537
+ nullptr);
6538
+ } else {
6539
+ throw SemanticError(std::string(x.m_attr) + " not a class member in " + std::string(struct_type->m_name), x.base.base.loc);
6540
+ }
6529
6541
} else if( ASR::is_a<ASR::UnionType_t>(*struct_member) ) {
6530
- ASR::expr_t* struct_type_var = ASRUtils::EXPR(ASR::make_Var_t(al, x.base.base.loc, org_sym));
6531
6542
ASR::ttype_t* union_type = ASRUtils::TYPE(ASR::make_Union_t(al, x.base.base.loc, struct_member));
6532
- tmp = ASR::make_StructStaticMember_t(al, x.base.base.loc, struct_type_var , struct_member, union_type, nullptr);
6543
+ tmp = ASR::make_StructStaticMember_t(al, x.base.base.loc, s2c(al, struct_type->m_name) , struct_member, union_type, nullptr);
6533
6544
}
6534
6545
} else if (ASR::is_a<ASR::Module_t>(*t)) {
6535
6546
ASR::Module_t *m = ASR::down_cast<ASR::Module_t>(t);
0 commit comments