Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Examine the Suffix hint on integers to apply apropriate TyTy type. #122

Merged
merged 1 commit into from
Jan 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions gcc/rust/ast/rust-ast-full-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4940,28 +4940,30 @@ MacroParser::parse_literal ()
{
case CHAR_LITERAL:
skip_token ();
return Literal (tok->as_string (), Literal::CHAR);
return Literal (tok->as_string (), Literal::CHAR, tok->get_type_hint ());
case STRING_LITERAL:
skip_token ();
return Literal (tok->as_string (), Literal::STRING);
return Literal (tok->as_string (), Literal::STRING,
tok->get_type_hint ());
case BYTE_CHAR_LITERAL:
skip_token ();
return Literal (tok->as_string (), Literal::BYTE);
return Literal (tok->as_string (), Literal::BYTE, tok->get_type_hint ());
case BYTE_STRING_LITERAL:
skip_token ();
return Literal (tok->as_string (), Literal::BYTE_STRING);
return Literal (tok->as_string (), Literal::BYTE_STRING,
tok->get_type_hint ());
case INT_LITERAL:
skip_token ();
return Literal (tok->as_string (), Literal::INT);
return Literal (tok->as_string (), Literal::INT, tok->get_type_hint ());
case FLOAT_LITERAL:
skip_token ();
return Literal (tok->as_string (), Literal::FLOAT);
return Literal (tok->as_string (), Literal::FLOAT, tok->get_type_hint ());
case TRUE_LITERAL:
skip_token ();
return Literal ("true", Literal::BOOL);
return Literal ("true", Literal::BOOL, tok->get_type_hint ());
case FALSE_LITERAL:
skip_token ();
return Literal ("false", Literal::BOOL);
return Literal ("false", Literal::BOOL, tok->get_type_hint ());
default:
rust_error_at (tok->get_locus (), "expected literal - found '%s'",
get_token_description (tok->get_id ()));
Expand Down Expand Up @@ -5284,7 +5286,8 @@ Token::to_token_stream () const
Attribute
MetaNameValueStr::to_attribute () const
{
LiteralExpr lit_expr (str, Literal::LitType::STRING, Location ());
LiteralExpr lit_expr (str, Literal::LitType::STRING,
PrimitiveCoreType::CORETYPE_UNKNOWN, Location ());
return Attribute (SimplePath::from_str (ident),
std::unique_ptr<AttrInputLiteral> (
new AttrInputLiteral (std::move (lit_expr))));
Expand Down
16 changes: 13 additions & 3 deletions gcc/rust/ast/rust-ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ class Token : public TokenTree, public MacroMatch

Location get_locus () const { return locus; }

PrimitiveCoreType get_type_hint () const { return type_hint; }

protected:
// No virtual for now as not polymorphic but can be in future
/*virtual*/ Token *clone_token_impl () const { return new Token (*this); }
Expand Down Expand Up @@ -250,17 +252,25 @@ struct Literal
* (or generics) */
std::string value_as_string;
LitType type;
PrimitiveCoreType type_hint;

public:
std::string as_string () const { return value_as_string; }

LitType get_lit_type () const { return type; }

Literal (std::string value_as_string, LitType type)
: value_as_string (std::move (value_as_string)), type (type)
PrimitiveCoreType get_type_hint () const { return type_hint; }

Literal (std::string value_as_string, LitType type,
PrimitiveCoreType type_hint)
: value_as_string (std::move (value_as_string)), type (type),
type_hint (type_hint)
{}

static Literal create_error () { return Literal ("", CHAR); }
static Literal create_error ()
{
return Literal ("", CHAR, PrimitiveCoreType::CORETYPE_UNKNOWN);
}

// Returns whether literal is in an invalid state.
bool is_error () const { return value_as_string == ""; }
Expand Down
4 changes: 2 additions & 2 deletions gcc/rust/ast/rust-expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ class LiteralExpr : public ExprWithoutBlock
Literal::LitType get_lit_type () const { return literal.get_lit_type (); }

LiteralExpr (std::string value_as_string, Literal::LitType type,
Location locus,
PrimitiveCoreType type_hint, Location locus,
std::vector<Attribute> outer_attrs = std::vector<Attribute> ())
: ExprWithoutBlock (std::move (outer_attrs)),
literal (std::move (value_as_string), type), locus (locus)
literal (std::move (value_as_string), type, type_hint), locus (locus)
{}

LiteralExpr (Literal literal, Location locus,
Expand Down
4 changes: 2 additions & 2 deletions gcc/rust/ast/rust-pattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class LiteralPattern : public Pattern

LiteralPattern (std::string val, Literal::LitType type, Location locus,
bool has_minus = false)
: lit (Literal (std::move (val), type)), has_minus (has_minus),
locus (locus)
: lit (Literal (std::move (val), type, PrimitiveCoreType::CORETYPE_STR)),
has_minus (has_minus), locus (locus)
{}

Location get_locus () const { return locus; }
Expand Down
24 changes: 24 additions & 0 deletions gcc/rust/backend/rust-compile-tyty.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,18 @@ class TyTyCompile : public TyTy::TyVisitor
= backend->named_type ("i32", backend->integer_type (false, 32),
Linemap::predeclared_location ());
return;

case TyTy::IntType::I64:
translated
= backend->named_type ("i64", backend->integer_type (false, 64),
Linemap::predeclared_location ());
return;

case TyTy::IntType::I128:
translated
= backend->named_type ("i128", backend->integer_type (false, 128),
Linemap::predeclared_location ());
return;
}
gcc_unreachable ();
}
Expand All @@ -135,6 +147,18 @@ class TyTyCompile : public TyTy::TyVisitor
= backend->named_type ("i32", backend->integer_type (true, 32),
Linemap::predeclared_location ());
return;

case TyTy::UintType::U64:
translated
= backend->named_type ("u64", backend->integer_type (true, 64),
Linemap::predeclared_location ());
return;

case TyTy::UintType::U128:
translated
= backend->named_type ("u128", backend->integer_type (true, 128),
Linemap::predeclared_location ());
return;
}
gcc_unreachable ();
}
Expand Down
1 change: 1 addition & 0 deletions gcc/rust/hir/rust-ast-lower-expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ class ASTLoweringExpr : public ASTLoweringBase
UNKNOWN_LOCAL_DEFID);

translated = new HIR::LiteralExpr (mapping, expr.as_string (), type,
expr.get_literal ().get_type_hint (),
expr.get_locus ());
}

Expand Down
5 changes: 3 additions & 2 deletions gcc/rust/hir/tree/rust-hir-expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@ class LiteralExpr : public ExprWithoutBlock
Literal::LitType get_lit_type () const { return literal.get_lit_type (); }

LiteralExpr (Analysis::NodeMapping mappings, std::string value_as_string,
Literal::LitType type, Location locus,
Literal::LitType type, PrimitiveCoreType type_hint,
Location locus,
std::vector<Attribute> outer_attrs = std::vector<Attribute> ())
: ExprWithoutBlock (std::move (mappings), std::move (outer_attrs)),
literal (std::move (value_as_string), type), locus (locus)
literal (std::move (value_as_string), type, type_hint), locus (locus)
{}

LiteralExpr (Analysis::NodeMapping mappings, Literal literal, Location locus,
Expand Down
37 changes: 4 additions & 33 deletions gcc/rust/hir/tree/rust-hir-full-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4904,38 +4904,8 @@ DelimTokenTree::to_token_stream () const
Literal
MacroParser::parse_literal ()
{
const std::unique_ptr<Token> &tok = peek_token ();
switch (tok->get_id ())
{
case CHAR_LITERAL:
skip_token ();
return Literal (tok->as_string (), Literal::CHAR);
case STRING_LITERAL:
skip_token ();
return Literal (tok->as_string (), Literal::STRING);
case BYTE_CHAR_LITERAL:
skip_token ();
return Literal (tok->as_string (), Literal::BYTE);
case BYTE_STRING_LITERAL:
skip_token ();
return Literal (tok->as_string (), Literal::BYTE_STRING);
case INT_LITERAL:
skip_token ();
return Literal (tok->as_string (), Literal::INT);
case FLOAT_LITERAL:
skip_token ();
return Literal (tok->as_string (), Literal::FLOAT);
case TRUE_LITERAL:
skip_token ();
return Literal ("true", Literal::BOOL);
case FALSE_LITERAL:
skip_token ();
return Literal ("false", Literal::BOOL);
default:
rust_error_at (tok->get_locus (), "expected literal - found '%s'",
get_token_description (tok->get_id ()));
return Literal::create_error ();
}
// marcos need to be removed from HIR
gcc_unreachable ();
}

SimplePath
Expand Down Expand Up @@ -5037,7 +5007,8 @@ Attribute
MetaNameValueStr::to_attribute () const
{
LiteralExpr lit_expr (Analysis::NodeMapping::get_error (), str,
Literal::LitType::STRING, Location ());
Literal::LitType::STRING,
PrimitiveCoreType::CORETYPE_STR, Location ());
return Attribute (SimplePath::from_str (ident),
std::unique_ptr<AttrInputLiteral> (
new AttrInputLiteral (std::move (lit_expr))));
Expand Down
4 changes: 2 additions & 2 deletions gcc/rust/hir/tree/rust-hir-pattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class LiteralPattern : public Pattern

LiteralPattern (std::string val, Literal::LitType type, Location locus,
bool has_minus = false)
: lit (Literal (std::move (val), type)), has_minus (has_minus),
locus (locus)
: lit (Literal (std::move (val), type, PrimitiveCoreType::CORETYPE_STR)),
has_minus (has_minus), locus (locus)
{}

Location get_locus () const { return locus; }
Expand Down
16 changes: 11 additions & 5 deletions gcc/rust/hir/tree/rust-hir.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,21 +235,27 @@ struct Literal
};

private:
/* TODO: maybe make subclasses of each type of literal with their typed values
* (or generics) */
std::string value_as_string;
LitType type;
PrimitiveCoreType type_hint;

public:
std::string as_string () const { return value_as_string; }

LitType get_lit_type () const { return type; }

Literal (std::string value_as_string, LitType type)
: value_as_string (std::move (value_as_string)), type (type)
PrimitiveCoreType get_type_hint () const { return type_hint; }

Literal (std::string value_as_string, LitType type,
PrimitiveCoreType type_hint)
: value_as_string (std::move (value_as_string)), type (type),
type_hint (type_hint)
{}

static Literal create_error () { return Literal ("", CHAR); }
static Literal create_error ()
{
return Literal ("", CHAR, PrimitiveCoreType::CORETYPE_UNKNOWN);
}

// Returns whether literal is in an invalid state.
bool is_error () const { return value_as_string == ""; }
Expand Down
39 changes: 25 additions & 14 deletions gcc/rust/parse/rust-parse-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,8 @@ Parser<ManagedTokenSource>::parse_attr_input ()
}

// create actual LiteralExpr
AST::LiteralExpr lit_expr (t->get_str (), lit_type, t->get_locus ());
AST::LiteralExpr lit_expr (t->get_str (), lit_type, t->get_type_hint (),
t->get_locus ());

std::unique_ptr<AST::AttrInputLiteral> attr_input_lit (
new AST::AttrInputLiteral (std::move (lit_expr)));
Expand Down Expand Up @@ -7374,7 +7375,8 @@ Parser<ManagedTokenSource>::parse_literal_expr (
// create literal based on stuff in switch
return std::unique_ptr<AST::LiteralExpr> (
new AST::LiteralExpr (std::move (literal_value), std::move (type),
t->get_locus (), std::move (outer_attrs)));
t->get_type_hint (), t->get_locus (),
std::move (outer_attrs)));
}

// Parses a return expression (including any expression to return).
Expand Down Expand Up @@ -9954,7 +9956,8 @@ Parser<ManagedTokenSource>::parse_literal_or_range_pattern ()
lexer.skip_token ();
std::unique_ptr<AST::RangePatternBound> lower (
new AST::RangePatternBoundLiteral (
AST::Literal (range_lower->get_str (), type),
AST::Literal (range_lower->get_str (), type,
PrimitiveCoreType::CORETYPE_UNKNOWN),
range_lower->get_locus (), has_minus));

std::unique_ptr<AST::RangePatternBound> upper
Expand Down Expand Up @@ -9995,26 +9998,30 @@ Parser<ManagedTokenSource>::parse_range_pattern_bound ()
lexer.skip_token ();
return std::unique_ptr<AST::RangePatternBoundLiteral> (
new AST::RangePatternBoundLiteral (
AST::Literal (range_lower->get_str (), AST::Literal::CHAR),
AST::Literal (range_lower->get_str (), AST::Literal::CHAR,
range_lower->get_type_hint ()),
range_lower_locus));
case BYTE_CHAR_LITERAL:
lexer.skip_token ();
return std::unique_ptr<AST::RangePatternBoundLiteral> (
new AST::RangePatternBoundLiteral (
AST::Literal (range_lower->get_str (), AST::Literal::BYTE),
AST::Literal (range_lower->get_str (), AST::Literal::BYTE,
range_lower->get_type_hint ()),
range_lower_locus));
case INT_LITERAL:
lexer.skip_token ();
return std::unique_ptr<AST::RangePatternBoundLiteral> (
new AST::RangePatternBoundLiteral (
AST::Literal (range_lower->get_str (), AST::Literal::INT),
AST::Literal (range_lower->get_str (), AST::Literal::INT,
range_lower->get_type_hint ()),
range_lower_locus));
case FLOAT_LITERAL:
lexer.skip_token ();
fprintf (stderr, "warning: used deprecated float range pattern bound");
return std::unique_ptr<AST::RangePatternBoundLiteral> (
new AST::RangePatternBoundLiteral (
AST::Literal (range_lower->get_str (), AST::Literal::FLOAT),
AST::Literal (range_lower->get_str (), AST::Literal::FLOAT,
range_lower->get_type_hint ()),
range_lower_locus));
case MINUS:
// branch on next token
Expand All @@ -10025,15 +10032,17 @@ Parser<ManagedTokenSource>::parse_range_pattern_bound ()
lexer.skip_token (1);
return std::unique_ptr<AST::RangePatternBoundLiteral> (
new AST::RangePatternBoundLiteral (
AST::Literal (range_lower->get_str (), AST::Literal::INT),
AST::Literal (range_lower->get_str (), AST::Literal::INT,
range_lower->get_type_hint ()),
range_lower_locus, true));
case FLOAT_LITERAL:
lexer.skip_token (1);
fprintf (stderr,
"warning: used deprecated float range pattern bound");
return std::unique_ptr<AST::RangePatternBoundLiteral> (
new AST::RangePatternBoundLiteral (
AST::Literal (range_lower->get_str (), AST::Literal::FLOAT),
AST::Literal (range_lower->get_str (), AST::Literal::FLOAT,
range_lower->get_type_hint ()),
range_lower_locus, true));
default:
rust_error_at (range_lower->get_locus (),
Expand Down Expand Up @@ -12110,22 +12119,24 @@ Parser<ManagedTokenSource>::null_denotation (
// encode as int?
return std::unique_ptr<AST::LiteralExpr> (
new AST::LiteralExpr (tok->get_str (), AST::Literal::INT,
tok->get_locus ()));
tok->get_type_hint (), tok->get_locus ()));
case FLOAT_LITERAL:
// encode as float?
return std::unique_ptr<AST::LiteralExpr> (
new AST::LiteralExpr (tok->get_str (), AST::Literal::FLOAT,
tok->get_locus ()));
tok->get_type_hint (), tok->get_locus ()));
case STRING_LITERAL:
return std::unique_ptr<AST::LiteralExpr> (
new AST::LiteralExpr (tok->get_str (), AST::Literal::STRING,
tok->get_locus ()));
tok->get_type_hint (), tok->get_locus ()));
case TRUE_LITERAL:
return std::unique_ptr<AST::LiteralExpr> (
new AST::LiteralExpr ("true", AST::Literal::BOOL, tok->get_locus ()));
new AST::LiteralExpr ("true", AST::Literal::BOOL, tok->get_type_hint (),
tok->get_locus ()));
case FALSE_LITERAL:
return std::unique_ptr<AST::LiteralExpr> (
new AST::LiteralExpr ("false", AST::Literal::BOOL, tok->get_locus ()));
new AST::LiteralExpr ("false", AST::Literal::BOOL,
tok->get_type_hint (), tok->get_locus ()));
case LEFT_PAREN: { // have to parse whole expression if inside brackets
/* recursively invoke parse_expression with lowest priority possible as
* it it were a top-level expression. */
Expand Down
Loading