Rust interface literal internals were taking a type and storing that
type. This lead to multiple problems such as various conversion from
string to int/float/other type as well as dead end on undetermined types
(type checker runs at a later stage).
libgrust/ChangeLog:
* libproc_macro/rust/bridge.rs: Add ffistring module.
* libproc_macro/rust/bridge/literal.rs: Rework type
internals.
* libproc_macro/rust/bridge/ffistring.rs: New file.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
`{ ... }.f;` is parsed as a single statement in rust. This means that we can't
determine whether an expression statement will need a semicolon terminator
until we finish parsing it. To handle this we change expression parsing to
check for this case by inspecting the expression returned from null_denotation
and looking ahead for a `.` or `?` token.
gcc/rust/ChangeLog:
* ast/rust-ast.h (Expr::as_expr_without_block): Remove.
(Expr::set_outer_attrs): Make public in base class.
* expand/rust-macro-expand.cc:
Add fixme comment for pre-existing bug.
* hir/tree/rust-hir.h: Remove Expr::as_expr_without_block.
* parse/rust-parse-impl.h (Parser::parse_lifetime): Use lifetime_from_token.
(Parser::lifetime_from_token): New method.
(Parser::null_denotation): Handle labelled loop expressions and for loop expressions.
(Parser::parse_loop_label): Make initial token a parameter.
(Parser::parse_labelled_loop_expr): Likewise.
(Parser::parse_for_loop_expr): Allow FOR token to already be skipped.
(Parser::parse_expr): Handle expr_can_be_stmt.
(Parser::parse_expr_with_block): Remove.
(Parser::parse_expr_stmt_with_block): Remove.
(Parser::parse_expr_stmt_without_block): Remove.
(Parser::parse_expr_without_block): Remove.
(Parser::parse_stmt_or_expr_with_block): Remove.
(Parser::parse_expr_stmt): Use parse_expr directly.
(Parser::parse_match_expr): Likewise.
(Parser::parse_stmt): Use parse_expr_stmt in more cases.
(Parser::parse_stmt_or_expr):
Rename from parse_stmt_or_expr_without_block, use parse_expr directly.
(Parser::parse_block_expr): Update error message.
* parse/rust-parse.h: Update declarations.
gcc/testsuite/ChangeLog:
* rust/compile/for_expr.rs: New test.
* rust/compile/issue-407-2.rs: Update compiler output.
* rust/compile/issue-407.rs: Update compiler output.
* rust/compile/issue-867.rs: Update compiler output.
* rust/compile/issue-2189.rs: New test.
* rust/compile/macro_call_statement.rs: New test.
* rust/compile/stmt_with_block_dot.rs: New test.
* rust/compile/torture/loop8.rs: New test.
Signed-off-by: Matthew Jasper <mjjasper1@gmail.com>
The suffix condition was wrong, the code was assigning a suffix to
unsuffixed literals. The literals length equals the end of digit
parsing position means there is no suffix.
gcc/rust/ChangeLog:
* util/rust-token-converter.cc (dispatch_float_literals): Invert
suffix condition.
(dispatch_integer_literals): Likewise.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Fix multiple issues regarding style around loops and conditions with one
single element.
gcc/rust/ChangeLog:
* util/rust-token-converter.cc (convert): Fix style.
(from_ident): Likewise.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Inputs of the converter are never changed and may thus be qualified as
const.
gcc/rust/ChangeLog:
* util/rust-token-converter.cc (dispatch_float_literals): Make
input immutable.
(dispatch_integer_literals): Likewise.
(convert): Likewise.
* util/rust-token-converter.h (convert): Likewise.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Add the implementation to convert an Ident structure back to a token.
gcc/rust/ChangeLog:
* util/rust-token-converter.cc (from_tokenstream): Add
conversion of Ident structures.
(from_ident): Likewise.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Add the implementation of the function to convert Literal back to
tokens. Also change the function signature to accept const.
gcc/rust/ChangeLog:
* util/rust-token-converter.cc (from_literal): Add function
implementation.
(string_literal): String literal specific handler.
(byte_string_literal): Byte string specific handler.
(unsigned_literal): Unsigned literal specific handler.
(signed_literal): Signed literal specific handler.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Add the implementation of the Punct conversion function to tokens.
gcc/rust/ChangeLog:
* util/rust-token-converter.cc (from_punct): Add conversion
implementation.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Make the conversion to tokenstream accept a const pointer vector instead
of a simple pointer vector because no changes are made to the original
tokens.
gcc/rust/ChangeLog:
* util/rust-token-converter.cc (dispatch_float_literals): Accept
const_TokenPtr instead of TokenPtr.
(dispatch_integer_literals): Likewise.
(convert): Accept vector of const_TokenPtr instead of a vector
of TokenPtr.
* util/rust-token-converter.h (convert): Likewise.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
We do not need mutability on the output vector. Also add an accumulator
for punct tokens.
gcc/rust/ChangeLog:
* util/rust-token-converter.cc (from_tokenstream): Add vector
for joined punct accumulation.
(from_ident): Accept const pointer vector.
(from_literal): Likewise.
(from_punct): Likewise.
(from_group): Likewise.
(from_tokentree): Likewise.
(convert): Likewise.
* util/rust-token-converter.h (convert): Likewise.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Add the from_tokentree function which converts a tokentree to it's
token representation. This function was previously inlined in the
from_tokenstream function but I wanted to keep things clear and
coherent.
gcc/rust/ChangeLog:
* util/rust-token-converter.cc (from_tokenstream): Add call to
from_tokentree.
(from_tokentree): Add implementation, from the from_tokenstream
function.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Add conversion of a given Group reference.
gcc/rust/ChangeLog:
* util/rust-token-converter.cc (from_punct): Add group
conversion.
(from_group): Likewise.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Add a tokenstream conversion function dispatching to inner elements
gcc/rust/ChangeLog:
* util/rust-token-converter.cc (to_tokenstream): Change function
name from to_tokenstream to convert.
(convert): Likewise.
(from_tokenstream): Add tokenstream handler.
(from_ident): Add empty function.
(from_literal): Likewise.
(from_punct): Likewise.
(from_group): Likewise.
* util/rust-token-converter.h (to_tokenstream): Change function
name from to_tokenstream to convert.
(convert): Likewise.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Since the introduction of this variable the code has changed and no
value order preservation is required anymore, the comparison can be done
inline.
gcc/rust/ChangeLog:
* util/rust-token-converter.cc (dispatch_float_literals): Remove
suffixed temporary variable.
(dispatch_integer_literals): Likewise.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Move the token vector to tokenstream converter out of the TokenCollector
class. This code is not exactly related to the token collection and the
code would be clearer with this rather lengthy conversion split on it's
own.
gcc/rust/ChangeLog:
* Make-lang.in: Add new rust-token-converter file.
* ast/rust-ast-collector.cc (pop_group): Function moved from
TokenCollector.
(dispatch_float_literals): Likewise.
(dispatch_integer_literals): Likewise.
(TokenCollector::collect): Removed function.
* ast/rust-ast-collector.h: Removed function prototype.
* util/rust-token-converter.cc: New file.
* util/rust-token-converter.h: New file.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
The header guard did not match the file name anymore.
gcc/rust/ChangeLog:
* ast/rust-ast-collector.h (RUST_AST_TOKENSTREAM_H): Rename
headerguard from RUST_AST_TOKENSTREAM_H ...
(RUST_AST_COLLECTOR_H): ... to RUST_AST_COLLECTOR_H.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
The file does not contain any TokenStream declaration anymore and thus
should be named more appropriately.
gcc/rust/ChangeLog:
* Make-lang.in: Change file name.
* ast/rust-ast-tokenstream.cc: Moved to...
* ast/rust-ast-collector.cc: ...here.
* ast/rust-ast-tokenstream.h: Moved to...
* ast/rust-ast-collector.h: ...here.
* rust-session-manager.cc: Change header name.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Rename the TokenStream class to TokenCollector as it may induce errors
in the future as it is not a tokenstream as defined by the rust
interface. Furthermore this change match more closely the actual behavior
of the class.
gcc/rust/ChangeLog:
* ast/rust-ast-tokenstream.cc (TokenStream::TokenStream): Rename
TokenStream to TokenCollector.
(TokenCollector::TokenCollector): Likewise.
(TokenStream::collect_tokens): Likewise.
(TokenCollector::collect_tokens): Likewise.
(TokenStream::collect): Likewise.
(TokenCollector::collect): Likewise.
(TokenStream::visit): Likewise.
(TokenCollector::visit): Likewise.
(TokenStream::visit_items_joined_by_separator): Likewise.
(TokenCollector::visit_items_joined_by_separator): Likewise.
(TokenStream::visit_as_line): Likewise.
(TokenCollector::visit_as_line): Likewise.
(TokenStream::visit_items_as_lines): Likewise.
(TokenCollector::visit_items_as_lines): Likewise.
(TokenStream::visit_items_as_block): Likewise.
(TokenCollector::visit_items_as_block): Likewise.
(TokenStream::trailing_comma): Likewise.
(TokenCollector::trailing_comma): Likewise.
(TokenStream::newline): Likewise.
(TokenCollector::newline): Likewise.
(TokenStream::indentation): Likewise.
(TokenCollector::indentation): Likewise.
(TokenStream::increment_indentation): Likewise.
(TokenCollector::increment_indentation): Likewise.
(TokenStream::decrement_indentation): Likewise.
(TokenCollector::decrement_indentation): Likewise.
(TokenStream::visit_closure_common): Likewise.
(TokenCollector::visit_closure_common): Likewise.
(TokenStream::visit_loop_common): Likewise.
(TokenCollector::visit_loop_common): Likewise.
(TokenStream::visit_function_common): Likewise.
(TokenCollector::visit_function_common): Likewise.
* ast/rust-ast-tokenstream.h (class TokenStream): Likewise.
(class TokenCollector): Likewise.
* rust-session-manager.cc (Session::dump_tokenstream): Rename
token vector for clearer intent.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Add the conversion handler for float/double literal tokens to
tokenstream Literals.
gcc/rust/ChangeLog:
* ast/rust-ast-tokenstream.cc (dispatch_float_literals): Add
dispatch function for floating point literals.
(TokenStream::collect): Add call to dispatcher.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Add a first draft for the literal integer conversion to tokenstream
Literal types.
gcc/rust/ChangeLog:
* ast/rust-ast-tokenstream.cc (dispatch_integer_literals):
Add a static function to dispatch depending on the core type.
(TokenStream::collect): Add call to dispatch function.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Add conversion to tokenstream Literals for string and byte strings.
gcc/rust/ChangeLog:
* ast/rust-ast-tokenstream.cc (TokenStream::collect): Add
conversion for byte string and string.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Add the conversion from tokens to punct structures in tokenstream
conversion function.
gcc/rust/ChangeLog:
* ast/rust-ast-tokenstream.cc (TokenStream::collect): Add joint
punct token conversion.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Add the code to convert single punctuation tokens to a tokenstream
element.
gcc/rust/ChangeLog:
* ast/rust-ast-tokenstream.cc (TokenStream::collect): Add
conversion for single punctuation tokens.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Add conversion for group delimiters.
gcc/rust/ChangeLog:
* ast/rust-ast-tokenstream.cc (pop_group): Add a function to
easily collect a group from a given stack at the end of it.
(TokenStream::collect): Collect tokens as a rust compatible
Tokenstream type.
* ast/rust-ast-tokenstream.h (RUST_AST_TOKENSTREAM_H): Move
includes to stay constrained by guards.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Fixes Issue #1568
Added HIR node HIR::InlineAsm similar to the one found in rustc. In this
I also changed the defination of the AST::InlineAsm node, so that we can
reuse many of it's data members in the HIR::InlineAsm node.
gcc/rust/ChangeLog:
* ast/rust-ast-full-decls.h (enum class): Added declaration.
(struct AnonConst): Added declaration.
(struct InlineAsmRegOrRegClass): Added declaration.
(struct InlineAsmOperand): Added declaration.
(struct InlineAsmPlaceHolder): Added declaration.
(struct InlineAsmTemplatePiece): Added declaration.
(struct TupleClobber): Added declaration.
(struct TupleTemplateStr): Added declaration.
* ast/rust-expr.h (class InlineAsm): Defined all it's data members outside.
(enum class InlineAsmOptions): Converted this to a enum class so we could use it in the HIR.
(struct AnonConst): Defined it independent of the AST::InlineAsm node.
(struct InlineAsmRegOrRegClass): Defined it independent of the AST::InlineAsm node.
(struct InlineAsmOperand): Defined it independent of the AST::InlineAsm node.
(struct InlineAsmPlaceHolder): Defined it independent of the AST::InlineAsm node.
(struct InlineAsmTemplatePiece): Defined it independent of the AST::InlineAsm node.
(struct TupleClobber): Defined it independent of the AST::InlineAsm node.
(struct TupleTemplateStr): Defined it independent of the AST::InlineAsm node.
* hir/tree/rust-hir-expr.h (class InlineAsmReg): Added defination.
(class InlineAsmRegClass): Added defination.
(struct InlineAsmRegOrRegClass): Added defination.
(class InlineAsm): Added defination.
* hir/tree/rust-hir-full-decls.h (class InlineAsmReg): Added declaration.
(class InlineAsmRegClass): Added declaration.
(struct InlineAsmRegOrRegClass): Added declaration.
(class InlineAsm): Added declaration.
Signed-off-by: M V V S Manoj Kumar <mvvsmanojkumar@gmail.com>
DST's are not truely reference types they are "unsized types" so
the exact size of them is not known at compile time. We actually
achieve this by pretending they are a reference but really its
struct we pass around.
Fixes#2180
gcc/rust/ChangeLog:
* backend/rust-compile-expr.cc (CompileExpr::visit): refactr
(CompileExpr::get_fn_addr_from_dyn): likewise
(CompileExpr::get_receiver_from_dyn): likewise
(CompileExpr::type_cast_expression): likewise
* backend/rust-compile-type.cc (TyTyResolveCompile::visit): likewise
(TyTyResolveCompile::create_dyn_obj_record): likewise
(TyTyResolveCompile::create_slice_type_record): likewise
(TyTyResolveCompile::create_str_type_record): likewise
* backend/rust-compile-type.h: likewise
* backend/rust-compile.cc (HIRCompileBase::coercion_site1): likewise
(HIRCompileBase::coerce_to_dyn_object): refactor
* backend/rust-tree.h (SLICE_FLAG): removed
(SLICE_TYPE_P): removed
(RS_DST_FLAG): new flag
(RS_DST_FLAG_P): new predicate
* typecheck/rust-tyty.cc (ReferenceType::is_dyn_object): new helper
(ReferenceType::is_dyn_obj_type): likewise
(PointerType::is_dyn_object): likewise
(PointerType::is_dyn_obj_type): likewise
* typecheck/rust-tyty.h (class DynamicObjectType): moved up
gcc/testsuite/ChangeLog:
* rust/execute/torture/issue-2180.rs: New test.
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
We had a very inconsistant way for dealing with unit-types in gccrs we
tried to optimize the case for a function returning unit type to be clever
and not emit any return value for unit types. Then for other cases we would
use an empty constructor for an empty tuple and in others use a zero
percsion integer. This was all just confusing and made the IR less
conformant to Rust. In this patch I change all of this to use an empty
tuple type for all cases so we pass around {} which maps over to Rust and
gets optimized away in the middle end anyway.
In the patch we also remove old gccgo code which optimizes away zero
size types to void_type_node which is why my original attempt at doing this
two years ago failed.
Fixes#2188
gcc/rust/ChangeLog:
* backend/rust-compile-base.cc (HIRCompileBase::compile_function_body): use unit_expression
(HIRCompileBase::unit_expression): new helper
* backend/rust-compile-base.h: update prototype
* backend/rust-compile-block.cc (CompileBlock::visit): use unit_expression
* backend/rust-compile-expr.cc (CompileExpr::visit): likewise
(CompileExpr::generate_closure_function): likewise
* backend/rust-compile-implitem.cc (CompileTraitItem::visit): cleanup
* backend/rust-compile-item.cc (CompileItem::visit): likewise
* backend/rust-compile-pattern.cc (CompilePatternLet::visit): likewise
* backend/rust-compile-resolve-path.cc (ResolvePathRef::resolve): likewise
* backend/rust-compile-type.cc (TyTyResolveCompile::get_unit_type): new helper
(TyTyResolveCompile::visit): use new unit_type helper
* backend/rust-compile-type.h: likewise
* rust-backend.h: simplify the return_expression
* rust-gcc.cc (Gcc_backend::function_type): likewise
(Gcc_backend::return_statement): likewise
* backend/rust-constexpr.cc (eval_constant_expression): remove bad assertion
gcc/testsuite/ChangeLog:
* rust/compile/issue-2188.rs: New test.
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Calling get variable expression will return an expression but we are
checking for unused decls so lets actually pass the decl.
Addresses #2188
gcc/rust/ChangeLog:
* checks/lints/rust-lint-unused-var.cc (UnusedVariables::Lint): use the decl not the expr
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
When we have a block expression for cosntants or statics we need to ensure
we compile the locals for the implicit function we generate in GIMPLE
before feeding it directly into the constant folder to evaluate the data.
Fixes#2178
gcc/rust/ChangeLog:
* backend/rust-compile-base.cc: add missing compile_locals call
gcc/testsuite/ChangeLog:
* rust/compile/issue-2178.rs: New test.
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
gcc/rust/ChangeLog:
* resolve/rust-early-name-resolver.cc (is_macro_use_module): New function.
(EarlyNameResolver::accumulate_escaped_macros): New function.
(EarlyNameResolver::go): Use `accumulate_escaped_macros`.
(EarlyNameResolver::visit): Likewise.
* resolve/rust-early-name-resolver.h: Declare `accumulate_escaped_macros`.
gcc/testsuite/ChangeLog:
* rust/compile/macro_use1.rs: New test.
* rust/execute/torture/macro_use1.rs: New test.
* rust/compile/nested_macro_use1.rs: New test.
* rust/compile/nested_macro_use2.rs: New test.
Both the AST::Crate and AST::Module class are std::unique_ptr<AST::Item>
containers, and may require spurious insertions in these containers,
for example when expanding a procedural macro, or in our case, escaping
macros through the #[macro_use] attribute. These functions allow you
to replace *all* of the items contained in such a container.
gcc/rust/ChangeLog:
* ast/rust-ast.h: Add take_items() and set_items() method to Crate.
* ast/rust-item.h: Add take_items() and set_items() method to Module.
This commit regroups information related to builtin macros in one place instead
of spreading it over multiple files. It also adds a simple bi-directional
hashmap in order to perform lookups from a key as well as a value.
gcc/rust/ChangeLog:
* ast/rust-macro.cc (builtin_macro_from_string): Move function.
* ast/rust-macro.h (enum class): Move enum.
(builtin_macro_from_string): Move function.
* expand/rust-macro-builtins.cc (builtin_macro_from_string): New function.
(make_macro_path_str): Use new bi-map.
(parse_single_string_literal): Use new `BuiltinMacro` enum.
(MacroBuiltin::include_bytes_handler): Likewise.
(MacroBuiltin::include_str_handler): Likewise.
(MacroBuiltin::compile_error_handler): Likewise.
(MacroBuiltin::concat_handler): Likewise.
(MacroBuiltin::env_handler): Likewise.
(MacroBuiltin::include_handler): Likewise.
(MacroBuiltin::sorry): New function.
* expand/rust-macro-builtins.h (enum class): Move enum here.
(builtin_macro_from_string): New function declaration.
* resolve/rust-early-name-resolver.cc (EarlyNameResolver::visit): Use
new function.
* util/rust-hir-map.cc (Mappings::insert_macro_def): Remove old
builtin macro map.