Skip to content

Commit 6c1c417

Browse files
authored
Merge pull request #24 from philberty/phil/compilation-simple
Phil/compilation simple
2 parents edf53ee + 3295942 commit 6c1c417

12 files changed

+920
-47
lines changed

gcc/rust/Make-lang.in

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ GRS_OBJS = \
7070
rust/rust-ast-full-test.o \
7171
rust/rust-session-manager.o \
7272
rust/rust-resolution.o \
73+
rust/rust-scan.o \
7374
rust/rust-compile.o \
7475
$(END)
7576
# removed object files from here

gcc/rust/analysis/rust-resolution.cc

+92-30
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
namespace Rust {
2020
namespace Analysis {
2121

22-
TypeResolution::TypeResolution (AST::Crate &crate) : crate (crate)
22+
TypeResolution::TypeResolution (AST::Crate &crate, TopLevelScan &toplevel)
23+
: crate (crate), toplevel (toplevel)
2324
{
2425
typeScope.Push ();
2526
scope.Push ();
@@ -50,9 +51,9 @@ TypeResolution::~TypeResolution ()
5051
}
5152

5253
bool
53-
TypeResolution::ResolveNamesAndTypes (AST::Crate &crate)
54+
TypeResolution::ResolveNamesAndTypes (AST::Crate &crate, TopLevelScan &toplevel)
5455
{
55-
TypeResolution resolver (crate);
56+
TypeResolution resolver (crate, toplevel);
5657
return resolver.go ();
5758
}
5859

@@ -65,6 +66,30 @@ TypeResolution::go ()
6566
return true;
6667
}
6768

69+
bool
70+
TypeResolution::typesAreCompatible (AST::Type *lhs, AST::Type *rhs,
71+
Location locus)
72+
{
73+
lhs->accept_vis (*this);
74+
rhs->accept_vis (*this);
75+
76+
auto rhsTypeStr = typeComparisonBuffer.back ();
77+
typeComparisonBuffer.pop_back ();
78+
auto lhsTypeStr = typeComparisonBuffer.back ();
79+
typeComparisonBuffer.pop_back ();
80+
81+
// FIXME this needs to handle the cases of an i8 going into an i32 which is
82+
// compatible
83+
if (lhsTypeStr.compare (rhsTypeStr))
84+
{
85+
rust_error_at (locus, "E0308: expected: %s, found %s",
86+
lhsTypeStr.c_str (), rhsTypeStr.c_str ());
87+
return false;
88+
}
89+
90+
return true;
91+
}
92+
6893
void
6994
TypeResolution::visit (AST::Token &tok)
7095
{}
@@ -124,18 +149,21 @@ TypeResolution::visit (AST::TypePathSegmentFunction &segment)
124149
void
125150
TypeResolution::visit (AST::TypePath &path)
126151
{
127-
printf ("TypePath: %s\n", path.as_string ().c_str ());
152+
// this may not be robust enough for type comparisons but lets try it for now
153+
typeComparisonBuffer.push_back (path.as_string ());
128154
}
129155

130156
void
131157
TypeResolution::visit (AST::QualifiedPathInExpression &path)
132158
{
133-
printf ("QualifiedPathInExpression: %s\n", path.as_string ().c_str ());
159+
typeComparisonBuffer.push_back (path.as_string ());
134160
}
135161

136162
void
137163
TypeResolution::visit (AST::QualifiedPathInType &path)
138-
{}
164+
{
165+
typeComparisonBuffer.push_back (path.as_string ());
166+
}
139167

140168
// rust-expr.h
141169
void
@@ -245,7 +273,7 @@ TypeResolution::visit (AST::ArithmeticOrLogicalExpr &expr)
245273
// scope will require knowledge of the type
246274

247275
// do the lhsType and the rhsType match
248-
// TODO
276+
typesAreCompatible (lhsType, rhsType, expr.right_expr->get_locus_slow ());
249277
}
250278

251279
void
@@ -254,9 +282,7 @@ TypeResolution::visit (AST::ComparisonExpr &expr)
254282

255283
void
256284
TypeResolution::visit (AST::LazyBooleanExpr &expr)
257-
{
258-
printf ("LazyBooleanExpr: %s\n", expr.as_string ().c_str ());
259-
}
285+
{}
260286

261287
void
262288
TypeResolution::visit (AST::TypeCastExpr &expr)
@@ -265,14 +291,41 @@ TypeResolution::visit (AST::TypeCastExpr &expr)
265291
void
266292
TypeResolution::visit (AST::AssignmentExpr &expr)
267293
{
268-
printf ("AssignmentExpr: %s\n", expr.as_string ().c_str ());
294+
size_t before;
295+
before = typeBuffer.size ();
296+
expr.visit_lhs (*this);
297+
if (typeBuffer.size () <= before)
298+
{
299+
rust_error_at (expr.locus, "unable to determine lhs type");
300+
return;
301+
}
302+
303+
auto lhsType = typeBuffer.back ();
304+
typeBuffer.pop_back ();
305+
306+
before = typeBuffer.size ();
307+
expr.visit_rhs (*this);
308+
if (typeBuffer.size () <= before)
309+
{
310+
rust_error_at (expr.locus, "unable to determine rhs type");
311+
return;
312+
}
313+
314+
auto rhsType = typeBuffer.back ();
315+
// not poping because we will be checking they match and the
316+
// scope will require knowledge of the type
317+
318+
// do the lhsType and the rhsType match
319+
if (!typesAreCompatible (lhsType, rhsType,
320+
expr.right_expr->get_locus_slow ()))
321+
return;
322+
323+
// is the lhs mutable?
269324
}
270325

271326
void
272327
TypeResolution::visit (AST::CompoundAssignmentExpr &expr)
273-
{
274-
printf ("CompoundAssignmentExpr: %s\n", expr.as_string ().c_str ());
275-
}
328+
{}
276329

277330
void
278331
TypeResolution::visit (AST::GroupedExpr &expr)
@@ -340,9 +393,13 @@ TypeResolution::visit (AST::EnumExprTuple &expr)
340393
void
341394
TypeResolution::visit (AST::EnumExprFieldless &expr)
342395
{}
396+
343397
void
344398
TypeResolution::visit (AST::CallExpr &expr)
345-
{}
399+
{
400+
printf ("CallExpr: %s\n", expr.as_string ().c_str ());
401+
}
402+
346403
void
347404
TypeResolution::visit (AST::MethodCallExpr &expr)
348405
{}
@@ -481,18 +538,25 @@ TypeResolution::visit (AST::UseDeclaration &use_decl)
481538
void
482539
TypeResolution::visit (AST::Function &function)
483540
{
541+
// always emit the function with return type in the event of nil return type
542+
// its a marker for a void function
484543
scope.Insert (function.function_name, function.return_type.get ());
485544

486545
scope.Push ();
487-
printf ("INSIDE FUNCTION: %s\n", function.function_name.c_str ());
488-
489546
for (auto &param : function.function_params)
490547
{
491-
printf ("FUNC PARAM: %s\n", param.as_string ().c_str ());
492-
}
548+
auto before = letPatternBuffer.size ();
549+
param.param_name->accept_vis (*this);
550+
if (letPatternBuffer.size () <= before)
551+
{
552+
rust_error_at (param.locus, "failed to analyse parameter name");
553+
return;
554+
}
493555

494-
// ensure return types
495-
// TODO
556+
auto paramName = letPatternBuffer.back ();
557+
letPatternBuffer.pop_back ();
558+
scope.Insert (paramName.variable_ident, param.type.get ());
559+
}
496560

497561
// walk the expression body
498562
for (auto &stmt : function.function_body->statements)
@@ -713,8 +777,11 @@ TypeResolution::visit (AST::LetStmt &stmt)
713777

714778
if (stmt.has_type () && stmt.has_init_expr ())
715779
{
716-
auto declaredTyped = stmt.type.get ();
717-
// TODO compare this type to the inferred type to ensure they match
780+
if (!typesAreCompatible (stmt.type.get (), inferedType,
781+
stmt.init_expr->get_locus_slow ()))
782+
{
783+
return;
784+
}
718785
}
719786
else if (stmt.has_type () && !stmt.has_init_expr ())
720787
{
@@ -727,8 +794,7 @@ TypeResolution::visit (AST::LetStmt &stmt)
727794
// ensure the decl has the type set for compilation later on
728795
if (!stmt.has_type ())
729796
{
730-
// FIXME
731-
// stmt.type = inferedType;
797+
stmt.inferedType = inferedType;
732798
}
733799

734800
// get all the names part of this declaration and add the types to the scope
@@ -743,16 +809,12 @@ TypeResolution::visit (AST::LetStmt &stmt)
743809
void
744810
TypeResolution::visit (AST::ExprStmtWithoutBlock &stmt)
745811
{
746-
printf ("ExprStmtWithoutBlock: %s\n", stmt.as_string ().c_str ());
747812
stmt.expr->accept_vis (*this);
748813
}
749814

750815
void
751816
TypeResolution::visit (AST::ExprStmtWithBlock &stmt)
752-
{
753-
printf ("ExprStmtWithBlock: %s\n", stmt.as_string ().c_str ());
754-
stmt.expr->accept_vis (*this);
755-
}
817+
{}
756818

757819
// rust-type.h
758820
void

gcc/rust/analysis/rust-resolution.h

+9-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "rust-system.h"
44
#include "rust-ast-full.h"
55
#include "rust-ast-visitor.h"
6+
#include "rust-scan.h"
67
#include "scope.h"
78

89
namespace Rust {
@@ -11,7 +12,7 @@ namespace Analysis {
1112
class TypeResolution : public AST::ASTVisitor
1213
{
1314
public:
14-
static bool ResolveNamesAndTypes (AST::Crate &crate);
15+
static bool ResolveNamesAndTypes (AST::Crate &crate, TopLevelScan &toplevel);
1516

1617
~TypeResolution ();
1718

@@ -221,16 +222,20 @@ class TypeResolution : public AST::ASTVisitor
221222
virtual void visit (AST::BareFunctionType &type);
222223

223224
private:
224-
TypeResolution (AST::Crate &crate);
225+
TypeResolution (AST::Crate &crate, TopLevelScan &toplevel);
225226

226227
bool go ();
227228

228-
Scope scope;
229-
Scope typeScope;
229+
bool typesAreCompatible (AST::Type *lhs, AST::Type *rhs, Location locus);
230+
231+
Scope<AST::Type *> scope;
232+
Scope<AST::Type *> typeScope;
230233
AST::Crate &crate;
234+
TopLevelScan &toplevel;
231235

232236
std::vector<AST::IdentifierPattern> letPatternBuffer;
233237
std::vector<AST::Type *> typeBuffer;
238+
std::vector<std::string> typeComparisonBuffer;
234239
};
235240

236241
} // namespace Analysis

0 commit comments

Comments
 (0)