@@ -328,17 +328,23 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
328
328
v.m_intent == LFortran::ASRUtils::intent_inout);
329
329
bool is_array = ASRUtils::is_array (v.m_type );
330
330
bool dummy = LFortran::ASRUtils::is_arg_dummy (v.m_intent );
331
- if (ASRUtils::is_pointer (v.m_type )) {
332
- ASR::ttype_t *t2 = ASR::down_cast<ASR::Pointer_t>(v.m_type )->m_type ;
331
+ ASR::ttype_t * v_m_type = v.m_type ;
332
+ if (ASR::is_a<ASR::Const_t>(*v_m_type)) {
333
+ if ( is_array ) {
334
+ v_m_type = ASR::down_cast<ASR::Const_t>(v_m_type)->m_type ;
335
+ }
336
+ }
337
+ if (ASRUtils::is_pointer (v_m_type)) {
338
+ ASR::ttype_t *t2 = ASR::down_cast<ASR::Pointer_t>(v_m_type)->m_type ;
333
339
if (ASRUtils::is_integer (*t2)) {
334
340
ASR::Integer_t *t = ASR::down_cast<ASR::Integer_t>(t2);
335
341
std::string type_name = " int" + std::to_string (t->m_kind * 8 ) + " _t" ;
336
- if ( !ASRUtils::is_array (v. m_type ) ) {
342
+ if ( !ASRUtils::is_array (v_m_type ) ) {
337
343
type_name.append (" *" );
338
344
}
339
345
if ( is_array ) {
340
346
bool is_fixed_size = true ;
341
- std::string dims = convert_dims_c (t->n_dims , t->m_dims , v. m_type , is_fixed_size, true );
347
+ std::string dims = convert_dims_c (t->n_dims , t->m_dims , v_m_type , is_fixed_size, true );
342
348
std::string encoded_type_name = " i" + std::to_string (t->m_kind * 8 );
343
349
generate_array_decl (sub, std::string (v.m_name ), type_name, dims,
344
350
encoded_type_name, t->m_dims , t->n_dims ,
@@ -348,14 +354,14 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
348
354
v.m_intent != ASRUtils::intent_out, is_fixed_size, true );
349
355
} else {
350
356
bool is_fixed_size = true ;
351
- std::string dims = convert_dims_c (t->n_dims , t->m_dims , v. m_type , is_fixed_size);
357
+ std::string dims = convert_dims_c (t->n_dims , t->m_dims , v_m_type , is_fixed_size);
352
358
sub = format_type_c (dims, type_name, v.m_name , use_ref, dummy);
353
359
}
354
360
} else if (ASR::is_a<ASR::Struct_t>(*t2)) {
355
361
ASR::Struct_t *t = ASR::down_cast<ASR::Struct_t>(t2);
356
362
std::string der_type_name = ASRUtils::symbol_name (t->m_derived_type );
357
363
bool is_fixed_size = true ;
358
- std::string dims = convert_dims_c (t->n_dims , t->m_dims , v. m_type , is_fixed_size);
364
+ std::string dims = convert_dims_c (t->n_dims , t->m_dims , v_m_type , is_fixed_size);
359
365
std::string ptr_char = " *" ;
360
366
if ( !use_ptr_for_derived_type ) {
361
367
ptr_char.clear ();
@@ -366,20 +372,20 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
366
372
sub = format_type_c (" " , " void**" , v.m_name , false , false );
367
373
} else {
368
374
diag.codegen_error_label (" Type number '"
369
- + std::to_string (v. m_type ->type )
375
+ + std::to_string (v_m_type ->type )
370
376
+ " ' not supported" , {v.base .base .loc }, " " );
371
377
throw Abort ();
372
378
}
373
379
} else {
374
380
std::string dims;
375
381
use_ref = use_ref && !is_array;
376
- if (ASRUtils::is_integer (*v. m_type )) {
382
+ if (ASRUtils::is_integer (*v_m_type )) {
377
383
headers.insert (" inttypes" );
378
- ASR::Integer_t *t = ASR::down_cast<ASR::Integer_t>(v. m_type );
384
+ ASR::Integer_t *t = ASR::down_cast<ASR::Integer_t>(v_m_type );
379
385
std::string type_name = " int" + std::to_string (t->m_kind * 8 ) + " _t" ;
380
386
if ( is_array ) {
381
387
bool is_fixed_size = true ;
382
- dims = convert_dims_c (t->n_dims , t->m_dims , v. m_type , is_fixed_size, true );
388
+ dims = convert_dims_c (t->n_dims , t->m_dims , v_m_type , is_fixed_size, true );
383
389
std::string encoded_type_name = " i" + std::to_string (t->m_kind * 8 );
384
390
bool is_struct_type_member = ASR::is_a<ASR::StructType_t>(
385
391
*ASR::down_cast<ASR::symbol_t >(v.m_parent_symtab ->asr_owner ));
@@ -392,16 +398,16 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
392
398
!is_struct_type_member, is_fixed_size);
393
399
} else {
394
400
bool is_fixed_size = true ;
395
- dims = convert_dims_c (t->n_dims , t->m_dims , v. m_type , is_fixed_size);
401
+ dims = convert_dims_c (t->n_dims , t->m_dims , v_m_type , is_fixed_size);
396
402
sub = format_type_c (dims, type_name, v.m_name , use_ref, dummy);
397
403
}
398
- } else if (ASRUtils::is_real (*v. m_type )) {
399
- ASR::Real_t *t = ASR::down_cast<ASR::Real_t>(v. m_type );
404
+ } else if (ASRUtils::is_real (*v_m_type )) {
405
+ ASR::Real_t *t = ASR::down_cast<ASR::Real_t>(v_m_type );
400
406
std::string type_name = " float" ;
401
407
if (t->m_kind == 8 ) type_name = " double" ;
402
408
if ( is_array ) {
403
409
bool is_fixed_size = true ;
404
- dims = convert_dims_c (t->n_dims , t->m_dims , v. m_type , is_fixed_size, true );
410
+ dims = convert_dims_c (t->n_dims , t->m_dims , v_m_type , is_fixed_size, true );
405
411
std::string encoded_type_name = " r" + std::to_string (t->m_kind * 8 );
406
412
bool is_struct_type_member = ASR::is_a<ASR::StructType_t>(
407
413
*ASR::down_cast<ASR::symbol_t >(v.m_parent_symtab ->asr_owner ));
@@ -414,17 +420,17 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
414
420
!is_struct_type_member, is_fixed_size);
415
421
} else {
416
422
bool is_fixed_size = true ;
417
- dims = convert_dims_c (t->n_dims , t->m_dims , v. m_type , is_fixed_size);
423
+ dims = convert_dims_c (t->n_dims , t->m_dims , v_m_type , is_fixed_size);
418
424
sub = format_type_c (dims, type_name, v.m_name , use_ref, dummy);
419
425
}
420
- } else if (ASRUtils::is_complex (*v. m_type )) {
426
+ } else if (ASRUtils::is_complex (*v_m_type )) {
421
427
headers.insert (" complex" );
422
- ASR::Complex_t *t = ASR::down_cast<ASR::Complex_t>(v. m_type );
428
+ ASR::Complex_t *t = ASR::down_cast<ASR::Complex_t>(v_m_type );
423
429
std::string type_name = " float complex" ;
424
430
if (t->m_kind == 8 ) type_name = " double complex" ;
425
431
if ( is_array ) {
426
432
bool is_fixed_size = true ;
427
- dims = convert_dims_c (t->n_dims , t->m_dims , v. m_type , is_fixed_size, true );
433
+ dims = convert_dims_c (t->n_dims , t->m_dims , v_m_type , is_fixed_size, true );
428
434
std::string encoded_type_name = " c" + std::to_string (t->m_kind * 8 );
429
435
generate_array_decl (sub, std::string (v.m_name ), type_name, dims,
430
436
encoded_type_name, t->m_dims , t->n_dims ,
@@ -434,18 +440,18 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
434
440
is_fixed_size);
435
441
} else {
436
442
bool is_fixed_size = true ;
437
- dims = convert_dims_c (t->n_dims , t->m_dims , v. m_type , is_fixed_size);
443
+ dims = convert_dims_c (t->n_dims , t->m_dims , v_m_type , is_fixed_size);
438
444
sub = format_type_c (dims, type_name, v.m_name , use_ref, dummy);
439
445
}
440
- } else if (ASRUtils::is_logical (*v. m_type )) {
441
- ASR::Logical_t *t = ASR::down_cast<ASR::Logical_t>(v. m_type );
446
+ } else if (ASRUtils::is_logical (*v_m_type )) {
447
+ ASR::Logical_t *t = ASR::down_cast<ASR::Logical_t>(v_m_type );
442
448
bool is_fixed_size = true ;
443
- dims = convert_dims_c (t->n_dims , t->m_dims , v. m_type , is_fixed_size);
449
+ dims = convert_dims_c (t->n_dims , t->m_dims , v_m_type , is_fixed_size);
444
450
sub = format_type_c (dims, " bool" , v.m_name , use_ref, dummy);
445
- } else if (ASRUtils::is_character (*v. m_type )) {
446
- ASR::Character_t *t = ASR::down_cast<ASR::Character_t>(v. m_type );
451
+ } else if (ASRUtils::is_character (*v_m_type )) {
452
+ ASR::Character_t *t = ASR::down_cast<ASR::Character_t>(v_m_type );
447
453
bool is_fixed_size = true ;
448
- std::string dims = convert_dims_c (t->n_dims , t->m_dims , v. m_type , is_fixed_size);
454
+ std::string dims = convert_dims_c (t->n_dims , t->m_dims , v_m_type , is_fixed_size);
449
455
sub = format_type_c (dims, " char *" , v.m_name , use_ref, dummy);
450
456
if ( v.m_intent == ASRUtils::intent_local &&
451
457
!(ASR::is_a<ASR::symbol_t >(*v.m_parent_symtab ->asr_owner ) &&
@@ -454,13 +460,13 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
454
460
sub += " = (char*) malloc(40 * sizeof(char))" ;
455
461
return sub;
456
462
}
457
- } else if (ASR::is_a<ASR::Struct_t>(*v. m_type )) {
463
+ } else if (ASR::is_a<ASR::Struct_t>(*v_m_type )) {
458
464
std::string indent (indentation_level*indentation_spaces, ' ' );
459
- ASR::Struct_t *t = ASR::down_cast<ASR::Struct_t>(v. m_type );
465
+ ASR::Struct_t *t = ASR::down_cast<ASR::Struct_t>(v_m_type );
460
466
std::string der_type_name = ASRUtils::symbol_name (t->m_derived_type );
461
467
if ( is_array ) {
462
468
bool is_fixed_size = true ;
463
- dims = convert_dims_c (t->n_dims , t->m_dims , v. m_type , is_fixed_size, true );
469
+ dims = convert_dims_c (t->n_dims , t->m_dims , v_m_type , is_fixed_size, true );
464
470
std::string encoded_type_name = " x" + der_type_name;
465
471
std::string type_name = std::string (" struct " ) + der_type_name;
466
472
generate_array_decl (sub, std::string (v.m_name ), type_name, dims,
@@ -471,7 +477,7 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
471
477
is_fixed_size);
472
478
} else if ( v.m_intent == ASRUtils::intent_local && pre_initialise_derived_type) {
473
479
bool is_fixed_size = true ;
474
- dims = convert_dims_c (t->n_dims , t->m_dims , v. m_type , is_fixed_size);
480
+ dims = convert_dims_c (t->n_dims , t->m_dims , v_m_type , is_fixed_size);
475
481
std::string value_var_name = v.m_parent_symtab ->get_unique_name (std::string (v.m_name ) + " _value" );
476
482
sub = format_type_c (dims, " struct " + der_type_name,
477
483
value_var_name, use_ref, dummy);
@@ -503,7 +509,7 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
503
509
return sub;
504
510
} else {
505
511
bool is_fixed_size = true ;
506
- dims = convert_dims_c (t->n_dims , t->m_dims , v. m_type , is_fixed_size);
512
+ dims = convert_dims_c (t->n_dims , t->m_dims , v_m_type , is_fixed_size);
507
513
if ( v.m_intent == ASRUtils::intent_in ||
508
514
v.m_intent == ASRUtils::intent_inout ) {
509
515
use_ref = false ;
@@ -516,13 +522,13 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
516
522
sub = format_type_c (dims, " struct " + der_type_name + ptr_char,
517
523
v.m_name , use_ref, dummy);
518
524
}
519
- } else if (ASR::is_a<ASR::Union_t>(*v. m_type )) {
525
+ } else if (ASR::is_a<ASR::Union_t>(*v_m_type )) {
520
526
std::string indent (indentation_level*indentation_spaces, ' ' );
521
- ASR::Union_t *t = ASR::down_cast<ASR::Union_t>(v. m_type );
527
+ ASR::Union_t *t = ASR::down_cast<ASR::Union_t>(v_m_type );
522
528
std::string der_type_name = ASRUtils::symbol_name (t->m_union_type );
523
529
if ( is_array ) {
524
530
bool is_fixed_size = true ;
525
- dims = convert_dims_c (t->n_dims , t->m_dims , v. m_type , is_fixed_size, true );
531
+ dims = convert_dims_c (t->n_dims , t->m_dims , v_m_type , is_fixed_size, true );
526
532
std::string encoded_type_name = " x" + der_type_name;
527
533
std::string type_name = std::string (" union " ) + der_type_name;
528
534
generate_array_decl (sub, std::string (v.m_name ), type_name, dims,
@@ -532,7 +538,7 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
532
538
v.m_intent != ASRUtils::intent_inout, is_fixed_size);
533
539
} else {
534
540
bool is_fixed_size = true ;
535
- dims = convert_dims_c (t->n_dims , t->m_dims , v. m_type , is_fixed_size);
541
+ dims = convert_dims_c (t->n_dims , t->m_dims , v_m_type , is_fixed_size);
536
542
if ( v.m_intent == ASRUtils::intent_in ||
537
543
v.m_intent == ASRUtils::intent_inout ) {
538
544
use_ref = false ;
@@ -541,38 +547,38 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
541
547
sub = format_type_c (dims, " union " + der_type_name,
542
548
v.m_name , use_ref, dummy);
543
549
}
544
- } else if (ASR::is_a<ASR::List_t>(*v. m_type )) {
545
- ASR::List_t* t = ASR::down_cast<ASR::List_t>(v. m_type );
550
+ } else if (ASR::is_a<ASR::List_t>(*v_m_type )) {
551
+ ASR::List_t* t = ASR::down_cast<ASR::List_t>(v_m_type );
546
552
std::string list_element_type = CUtils::get_c_type_from_ttype_t (t->m_type );
547
553
std::string list_type_c = list_api->get_list_type (t, list_element_type);
548
554
sub = format_type_c (" " , list_type_c, v.m_name ,
549
555
false , false );
550
- } else if (ASR::is_a<ASR::Tuple_t>(*v. m_type )) {
551
- ASR::Tuple_t* t = ASR::down_cast<ASR::Tuple_t>(v. m_type );
556
+ } else if (ASR::is_a<ASR::Tuple_t>(*v_m_type )) {
557
+ ASR::Tuple_t* t = ASR::down_cast<ASR::Tuple_t>(v_m_type );
552
558
std::string tuple_type_c = tuple_api->get_tuple_type (t);
553
559
sub = format_type_c (" " , tuple_type_c, v.m_name ,
554
560
false , false );
555
- } else if (ASR::is_a<ASR::CPtr_t>(*v. m_type )) {
561
+ } else if (ASR::is_a<ASR::CPtr_t>(*v_m_type )) {
556
562
sub = format_type_c (" " , " void*" , v.m_name , false , false );
557
- } else if (ASR::is_a<ASR::Const_t>(*v.m_type )) {
563
+ } else if (ASR::is_a<ASR::Enum_t>(*v_m_type)) {
564
+ ASR::Enum_t* enum_ = ASR::down_cast<ASR::Enum_t>(v_m_type);
565
+ ASR::EnumType_t* enum_type = ASR::down_cast<ASR::EnumType_t>(enum_->m_enum_type );
566
+ sub = format_type_c (" " , " enum " + std::string (enum_type->m_name ), v.m_name , false , false );
567
+ } else if (ASR::is_a<ASR::Const_t>(*v_m_type)) {
558
568
if ( v.m_intent == ASRUtils::intent_local ) {
559
569
LFORTRAN_ASSERT (v.m_symbolic_value );
560
570
visit_expr (*v.m_symbolic_value );
561
571
sub = " #define " + std::string (v.m_name ) + " " + src + " \n " ;
562
572
return sub;
563
573
} else {
564
574
std::string const_underlying_type = CUtils::get_c_type_from_ttype_t (
565
- ASR::down_cast<ASR::Const_t>(v. m_type )->m_type );
575
+ ASR::down_cast<ASR::Const_t>(v_m_type )->m_type );
566
576
sub = format_type_c (" " , " const " + const_underlying_type + " " ,
567
577
v.m_name , false , false );
568
578
}
569
- } else if (ASR::is_a<ASR::Enum_t>(*v.m_type )) {
570
- ASR::Enum_t* enum_ = ASR::down_cast<ASR::Enum_t>(v.m_type );
571
- ASR::EnumType_t* enum_type = ASR::down_cast<ASR::EnumType_t>(enum_->m_enum_type );
572
- sub = format_type_c (" " , " enum " + std::string (enum_type->m_name ), v.m_name , false , false );
573
579
} else {
574
580
diag.codegen_error_label (" Type number '"
575
- + std::to_string (v. m_type ->type )
581
+ + std::to_string (v_m_type ->type )
576
582
+ " ' not supported" , {v.base .base .loc }, " " );
577
583
throw Abort ();
578
584
}
0 commit comments