@@ -104,7 +104,13 @@ TypeResolution::typesAreCompatible (AST::Type *lhs, AST::Type *rhs,
104
104
}
105
105
106
106
AST::Type *val = NULL ;
107
- return scope.LookupType (lhsTypeStr, &val);
107
+ if (!scope.LookupType (lhsTypeStr, &val))
108
+ {
109
+ rust_error_at (locus, " Unknown type: %s" , lhsTypeStr.c_str ());
110
+ return false ;
111
+ }
112
+
113
+ return true ;
108
114
}
109
115
110
116
bool
@@ -395,19 +401,112 @@ TypeResolution::visit (AST::CompoundAssignmentExpr &expr)
395
401
void
396
402
TypeResolution::visit (AST::GroupedExpr &expr)
397
403
{}
398
- // void TypeResolution::visit(ArrayElems& elems) {}
404
+
399
405
void
400
406
TypeResolution::visit (AST::ArrayElemsValues &elems)
401
- {}
407
+ {
408
+ // we need to generate the AST::ArrayType for this array init_expression
409
+ // we can get the size via get_num_values() but we need to ensure each element
410
+ // are type compatible
411
+
412
+ bool failed = false ;
413
+ AST::Type *last_inferred_type = nullptr ;
414
+ elems.iterate ([&] (AST::Expr *expr) mutable -> bool {
415
+ size_t before;
416
+ before = typeBuffer.size ();
417
+ expr->accept_vis (*this );
418
+ if (typeBuffer.size () <= before)
419
+ {
420
+ rust_error_at (expr->get_locus_slow (),
421
+ " unable to determine element type" );
422
+ return false ;
423
+ }
424
+
425
+ AST::Type *inferedType = typeBuffer.back ();
426
+ typeBuffer.pop_back ();
427
+
428
+ if (last_inferred_type == nullptr )
429
+ last_inferred_type = inferedType;
430
+ else
431
+ {
432
+ if (!typesAreCompatible (last_inferred_type, inferedType,
433
+ expr->get_locus_slow ()))
434
+ {
435
+ failed = true ;
436
+ return false ;
437
+ }
438
+ }
439
+
440
+ return true ;
441
+ });
442
+
443
+ // nothing to do when its failed
444
+ if (failed)
445
+ return ;
446
+
447
+ auto capacity
448
+ = new AST::LiteralExpr (std::to_string (elems.get_num_values ()),
449
+ AST::Literal::INT,
450
+ Linemap::predeclared_location ());
451
+ auto arrayType = new AST::ArrayType (last_inferred_type->clone_type (),
452
+ std::unique_ptr<AST::Expr> (capacity),
453
+ Linemap::predeclared_location ());
454
+ typeBuffer.push_back (arrayType);
455
+ }
456
+
402
457
void
403
458
TypeResolution::visit (AST::ArrayElemsCopied &elems)
404
- {}
459
+ {
460
+ printf (" ArrayElemsCopied: %s\n " , elems.as_string ().c_str ());
461
+ }
462
+
405
463
void
406
464
TypeResolution::visit (AST::ArrayExpr &expr)
407
- {}
465
+ {
466
+ auto elements = expr.get_internal_elements ();
467
+ elements->accept_vis (*this );
468
+ }
469
+
408
470
void
409
471
TypeResolution::visit (AST::ArrayIndexExpr &expr)
410
- {}
472
+ {
473
+ printf (" ArrayIndexExpr: %s\n " , expr.as_string ().c_str ());
474
+
475
+ auto before = typeBuffer.size ();
476
+ expr.get_array_expr ()->accept_vis (*this );
477
+ if (typeBuffer.size () <= before)
478
+ {
479
+ rust_error_at (expr.get_locus_slow (),
480
+ " unable to determine type for array index expression" );
481
+ return ;
482
+ }
483
+ AST::Type *array_expr_type = typeBuffer.back ();
484
+ typeBuffer.pop_back ();
485
+
486
+ before = typeBuffer.size ();
487
+ expr.get_index_expr ()->accept_vis (*this );
488
+ if (typeBuffer.size () <= before)
489
+ {
490
+ rust_error_at (expr.get_index_expr ()->get_locus_slow (),
491
+ " unable to determine type for index expression" );
492
+ return ;
493
+ }
494
+
495
+ AST::Type *array_index_type = typeBuffer.back ();
496
+ typeBuffer.pop_back ();
497
+
498
+ printf (" Array expr type %s array index expr type: [%s]\n " ,
499
+ array_expr_type->as_string ().c_str (),
500
+ array_index_type->as_string ().c_str ());
501
+
502
+ // the the element type from the array_expr_type and it _must_ be an array
503
+ // TODO
504
+
505
+ // check the index_type should be an i32 which should really be
506
+ // more permissive
507
+ // TODO
508
+ }
509
+
411
510
void
412
511
TypeResolution::visit (AST::TupleExpr &expr)
413
512
{}
@@ -1015,7 +1114,7 @@ TypeResolution::visit (AST::LetStmt &stmt)
1015
1114
return ;
1016
1115
}
1017
1116
1018
- AST::Type *inferedType = NULL ;
1117
+ AST::Type *inferedType = nullptr ;
1019
1118
if (stmt.has_init_expr ())
1020
1119
{
1021
1120
auto before = typeBuffer.size ();
@@ -1048,6 +1147,51 @@ TypeResolution::visit (AST::LetStmt &stmt)
1048
1147
return ;
1049
1148
}
1050
1149
}
1150
+ else if (stmt.has_type ())
1151
+ {
1152
+ auto before = typeComparisonBuffer.size ();
1153
+ stmt.type ->accept_vis (*this );
1154
+ if (typeComparisonBuffer.size () <= before)
1155
+ {
1156
+ rust_error_at (stmt.locus , " failed to understand type for lhs" );
1157
+ return ;
1158
+ }
1159
+ auto typeString = typeComparisonBuffer.back ();
1160
+ typeComparisonBuffer.pop_back ();
1161
+
1162
+ AST::Type *val = NULL ;
1163
+ if (!scope.LookupType (typeString, &val))
1164
+ {
1165
+ rust_error_at (stmt.locus , " LetStmt has unknown type: %s" ,
1166
+ stmt.type ->as_string ().c_str ());
1167
+ return ;
1168
+ }
1169
+ }
1170
+ else if (inferedType != nullptr )
1171
+ {
1172
+ auto before = typeComparisonBuffer.size ();
1173
+ inferedType->accept_vis (*this );
1174
+ if (typeComparisonBuffer.size () <= before)
1175
+ {
1176
+ rust_error_at (stmt.locus , " failed to understand type for lhs" );
1177
+ return ;
1178
+ }
1179
+ auto typeString = typeComparisonBuffer.back ();
1180
+ typeComparisonBuffer.pop_back ();
1181
+
1182
+ AST::Type *val = NULL ;
1183
+ if (!scope.LookupType (typeString, &val))
1184
+ {
1185
+ rust_error_at (stmt.locus , " Inferred unknown type: %s" ,
1186
+ inferedType->as_string ().c_str ());
1187
+ return ;
1188
+ }
1189
+ }
1190
+ else
1191
+ {
1192
+ rust_fatal_error (stmt.locus , " Failed to determine any type for LetStmt" );
1193
+ return ;
1194
+ }
1051
1195
1052
1196
// ensure the decl has the type set for compilation later on
1053
1197
if (!stmt.has_type ())
@@ -1115,9 +1259,13 @@ TypeResolution::visit (AST::RawPointerType &type)
1115
1259
void
1116
1260
TypeResolution::visit (AST::ReferenceType &type)
1117
1261
{}
1262
+
1118
1263
void
1119
1264
TypeResolution::visit (AST::ArrayType &type)
1120
- {}
1265
+ {
1266
+ typeComparisonBuffer.push_back (type.get_element_type ()->as_string ());
1267
+ }
1268
+
1121
1269
void
1122
1270
TypeResolution::visit (AST::SliceType &type)
1123
1271
{}
0 commit comments