|
1 | 1 | #include "rust-resolution.h"
|
| 2 | +#include "rust-diagnostics.h" |
2 | 3 |
|
3 | 4 | namespace Rust {
|
4 | 5 | namespace Analysis {
|
5 | 6 |
|
6 |
| -TypeResolution::TypeResolution (AST::Crate &crate) : scope (), crate (crate) {} |
| 7 | +TypeResolution::TypeResolution (AST::Crate &crate) : scope (), crate (crate) |
| 8 | +{ |
| 9 | + // push all builtin types |
| 10 | + // base is parse_path_ident_segment based up on segments |
| 11 | + /* scope.Insert ("u8", |
| 12 | + new AST::MaybeNamedParam (Identifier ("u8"), |
| 13 | + AST::MaybeNamedParam::IDENTIFIER, |
| 14 | + NULL, Location ()));*/ |
| 15 | +} |
7 | 16 |
|
8 | 17 | TypeResolution::~TypeResolution () {}
|
9 | 18 |
|
@@ -40,7 +49,9 @@ TypeResolution::visit (AST::AttrInputMetaItemContainer &input)
|
40 | 49 |
|
41 | 50 | void
|
42 | 51 | TypeResolution::visit (AST::IdentifierExpr &ident_expr)
|
43 |
| -{} |
| 52 | +{ |
| 53 | + printf ("IdentifierExpr %s\n", ident_expr.as_string ().c_str ()); |
| 54 | +} |
44 | 55 |
|
45 | 56 | void
|
46 | 57 | TypeResolution::visit (AST::Lifetime &lifetime)
|
@@ -80,7 +91,11 @@ TypeResolution::visit (AST::QualifiedPathInType &path)
|
80 | 91 | // rust-expr.h
|
81 | 92 | void
|
82 | 93 | TypeResolution::visit (AST::LiteralExpr &expr)
|
83 |
| -{} |
| 94 | +{ |
| 95 | + printf ("LiteralExpr: %s\n", expr.as_string ().c_str ()); |
| 96 | + // figure out what this type is and push it onto the |
| 97 | +} |
| 98 | + |
84 | 99 | void
|
85 | 100 | TypeResolution::visit (AST::AttrInputLiteral &attr_input)
|
86 | 101 | {}
|
@@ -526,44 +541,70 @@ TypeResolution::visit (AST::SlicePattern &pattern)
|
526 | 541 | void
|
527 | 542 | TypeResolution::visit (AST::EmptyStmt &stmt)
|
528 | 543 | {}
|
529 |
| -void |
530 | 544 |
|
| 545 | +void |
531 | 546 | TypeResolution::visit (AST::LetStmt &stmt)
|
532 | 547 | {
|
533 |
| - printf ("Within LetStmt: %s\n", stmt.as_string ().c_str ()); |
534 |
| - |
535 |
| - if (!stmt.has_type ()) |
| 548 | + AST::Type *inferedType = NULL; |
| 549 | + if (stmt.has_type ()) |
| 550 | + { |
| 551 | + inferedType = stmt.type.get (); |
| 552 | + } |
| 553 | + else if (stmt.has_init_expr ()) |
536 | 554 | {
|
537 |
| - // infer the type |
538 |
| - printf ("XXX UNKNOWN TYPE PLEASE INFER ME\n"); |
| 555 | + stmt.init_expr->accept_vis (*this); |
| 556 | + |
| 557 | + if (typeBuffer.empty ()) |
| 558 | + { |
| 559 | + rust_error_at ( |
| 560 | + stmt.init_expr->get_locus_slow (), |
| 561 | + "unable to determine type for declaration from init expr"); |
| 562 | + return; |
| 563 | + } |
| 564 | + |
| 565 | + inferedType = typeBuffer.back (); |
| 566 | + typeBuffer.pop_back (); |
| 567 | + } |
| 568 | + else |
| 569 | + { |
| 570 | + rust_error_at (stmt.locus, "unable to determine type for declaration"); |
| 571 | + return; |
539 | 572 | }
|
540 | 573 |
|
541 | 574 | // TODO check we know what the type is
|
542 | 575 |
|
| 576 | + // get all the names part of this declaration and add the types to the scope |
543 | 577 | stmt.variables_pattern->accept_vis (*this);
|
544 |
| - |
545 | 578 | for (auto it = letPatternBuffer.begin (); it != letPatternBuffer.end (); it++)
|
546 | 579 | {
|
547 |
| - scope.Insert (it->variable_ident, stmt.type.get ()); |
| 580 | + scope.Insert (it->variable_ident, inferedType); |
548 | 581 | }
|
549 |
| - |
550 | 582 | letPatternBuffer.clear ();
|
551 | 583 | }
|
552 | 584 |
|
553 | 585 | void
|
554 | 586 | TypeResolution::visit (AST::ExprStmtWithoutBlock &stmt)
|
555 |
| -{} |
| 587 | +{ |
| 588 | + printf ("ExprStmtWithoutBlock: %s\n", stmt.as_string ().c_str ()); |
| 589 | + stmt.expr->accept_vis (*this); |
| 590 | +} |
| 591 | + |
556 | 592 | void
|
557 | 593 | TypeResolution::visit (AST::ExprStmtWithBlock &stmt)
|
558 |
| -{} |
| 594 | +{ |
| 595 | + printf ("ExprStmtWithBlock: %s\n", stmt.as_string ().c_str ()); |
| 596 | + stmt.expr->accept_vis (*this); |
| 597 | +} |
559 | 598 |
|
560 | 599 | // rust-type.h
|
561 | 600 | void
|
562 | 601 | TypeResolution::visit (AST::TraitBound &bound)
|
563 | 602 | {}
|
| 603 | + |
564 | 604 | void
|
565 | 605 | TypeResolution::visit (AST::ImplTraitType &type)
|
566 | 606 | {}
|
| 607 | + |
567 | 608 | void
|
568 | 609 | TypeResolution::visit (AST::TraitObjectType &type)
|
569 | 610 | {}
|
|
0 commit comments