gccrs: Change FunctionParam to represent variadic params
Variadic were represented at the function level while retaining most
informations of a given parameter.
gcc/rust/ChangeLog:
* ast/rust-item.h (class FunctionParam): Add some informations to
function parameters in order to be able to store variadic argument as
a function parameter.
gccrs: Make use of the Contextual visitor in validation
Use the new contextual ast visitor to reduce the amount of code in the
ast validation visitor.
gcc/rust/ChangeLog:
* checks/errors/rust-ast-validation.cc (ASTValidation::visit): Adapt
the call to the new visit functions.
(ASTValidation::check): Launch the parent class visitor root function.
* checks/errors/rust-ast-validation.h (class ASTValidation): Inherit
from the contextual visitor.
gccrs: Add a new visitor that gathers context information
This visitor is intended to be used by other visitors that require
context at some point for a given item.
gcc/rust/ChangeLog:
* ast/rust-ast-visitor.cc (ContextualASTVisitor::visit): Add multiple
context saving calls.
* ast/rust-ast-visitor.h (class DefaultASTVisitor): Make visit
functions virtual.
(class ContextualASTVisitor): Add a stack like container for the
current context chain.
This will allow us to derive other visitors from it and overload only a
few selected visit methods.
gcc/rust/ChangeLog:
* Make-lang.in: Add the new visitor object file.
* ast/rust-ast-visitor.h (class DefaultASTVisitor): Create the default
visitor class.
* ast/rust-ast.h: Add a new reference getter for visitor pattern.
* ast/rust-ast-visitor.cc: New file.
gccrs: Emit an error on associated const without values
Associated const with no value that are not in trait impl are prohibited.
gcc/rust/ChangeLog:
* checks/errors/rust-ast-validation.cc (ASTValidation::check): Launch
check over the whole given crate.
(ASTValidation::visit): Implement visitor for some members of the ast.
* checks/errors/rust-ast-validation.h: Update some prototype according
to implemented visitor functions. Also add a context tracker.
Add the ast validation and feature gating steps to the compile pipeline.
gcc/rust/ChangeLog:
* lang.opt: Add the new compile options and update the enum values.
* rust-session-manager.h (struct CompileOptions): Add the new steps to
the enumeration.
gccrs: Add more checks for expr value in early visitors
Early passes visitors may encounter constant item without a value, this
is expected as the pass rejecting a constant without an expression is
done later during the ast validation.
The lexer cannot distinguish the difference between a float literal and a
tuple index in some cases. This means we should fix this while parsing
depending on the context.
gcc/rust/ChangeLog:
* expand/rust-macro-invoc-lexer.cc (MacroInvocLexer::split_current_token):
Add implementation for multiple token split.
* expand/rust-macro-invoc-lexer.h: Add function prototype.
* expand/rust-proc-macro-invoc-lexer.cc (ProcMacroInvocLexer::split_current_token):
Add implementation for 2+ token split for procedural macros.
* expand/rust-proc-macro-invoc-lexer.h: Add function prototype.
* lex/rust-lex.cc (Lexer::split_current_token): Add function to split a
token in multiple other tokens.
* lex/rust-lex.h: Add function prototype for split_current_token.
* parse/rust-parse-impl.h (Parser::left_denotation): Handle float tuple
index identified as a float literal.
Multiple references to procedural macro token trees were left as magic
number in the code. This commit introduces some constexpr for those along
some explanation for the selected value.
gcc/rust/ChangeLog:
* backend/rust-compile-base.cc (get_attributes): Add documentation for
indices 3 and 4.
(get_trait_name): Add documentation for index 1.
The compiler cannot infer the array length from the type, we should
therefore hand it the information. The proc macro buffer missed that
information.
gcc/rust/ChangeLog:
* backend/rust-compile.cc (proc_macro_buffer): Update type builder with
array length information.
(proc_macro_array): Update type initializer with array length
information.
gccrs: Put common functions in their own namespace
Half of the functions introduced recently had a similar goal while the
other half had a similar goal too. Introducing some namespace to separate
those will keep the code cleaner and avoid confusion.
* backend/rust-compile-base.h: Make static function address_expression
public.
* backend/rust-compile.cc (CompileCrate::add_proc_macro_symbols): Add
new global variable in export function.
(build_bang_proc_macro): Add a function to build the bang proc macro
structure type.
(build_proc_macro): Add a function to build the proc macro structure
type.
(build_proc_macro_payload): Add a function to build the proc macro
union used in proc macro structures.
(init_derive_proc_macro): Add a function to initialize custom derive
proc macros.
(init_attribute_proc_macro): Add a function to initialize attribute
proc macros.
(init_bang_proc_macro): Add a function to initialize bang proc macros.
(init_proc_macro): Add a function to initialize proc macro structures.
(initialize_proc_macro_array): Add a function to initialize the proc
macro buffer array.
(CompileCrate::add_proc_macro_symbols): Add call to the new functions
to correctly initialize proc macros as well as their entrypoint.
Change proc macro entrypoint from a fixed constant declaration to a
proper generation from the stable crate id. Although the stable crate id
is not in use yet, the mechanism to handle it is.
gcc/rust/ChangeLog:
* expand/rust-proc-macro.cc (CustomDeriveProcMacro::CustomDeriveProcMacro):
Remove constant string declaration.
(load_macros_array): Add call to the new generation function.
(generate_proc_macro_decls_symbol): Add a new function to generate the
entrypoint symbol name from the stable crate id.
(PROC_MACRO_DECLS_FMT_ARGS):
New macro to keep formats arguments in sync between each call.
* expand/rust-proc-macro.h (generate_proc_macro_decls_symbol): Add
function prototype.
* rust-system.h: Include <iomanip>.
gccrs: Add utility function to build proc macro types
Add some utility function to build proc macro entrypoint related types.
Those functions will help generate all required metadata in order for
proc macros to be expanded properly.
gcc/rust/ChangeLog:
* backend/rust-compile.cc (build_attribute_array): Add a function to
build the attribute array type.
(build_derive_proc_macro): Add a function to build the derive proc
macro type.
(build_bang_proc_macro): Add a function to build the bang proc macro
type.
(build_attribute_proc_macro): Add a function to build the attribute
proc macro type.
(build_proc_macro): Add a function to build the proc macro tagged union
type.
(build_proc_macro_buffer): Add a function to build the proc macro
buffer type.
(build_entrypoint): Add a function to build the proc macro entrypoint
type.
* backend/rust-compile.h: Add function prototype.
Collect informations on procedural macros in the compiled crate. For
attribute and bang procedural macros we only require the final address
as well as the name of the function. Derive procedural macros are a bit
different, we collect the fonction's address through it's fndecl tree as
well as the trait's name and the multiple attributes.
gcc/rust/ChangeLog:
* backend/rust-compile-base.cc (HIRCompileBase::setup_fndecl):
Make the function non static in order to be able to access the
compile context. Also add the whole proc macro infomrmation
collection.
(get_attributes): Add a function to retrieve the different
attributes from a derive procedural macro definition attribute.
(get_trait_name): Add a function to retrieve the trait name from
a derive procedural macro definition attribute.
* backend/rust-compile-base.h: Add function prototypes.
gccrs: Add containers for proc macro collection mappings
Add one container for each kind of procedural macro mapping. Also add a
new structure to gather informations required for derive procedural
macros. Add different functions to fill those new containers.
gcc/rust/ChangeLog:
* backend/rust-compile-context.h (struct CustomDeriveInfo): Add
new derive procedural macro metadata information holder for
mappings.
gccrs: Change ABI setup and add gccrs_proc_macro attr
Change the way the ABI is setup on a function to avoid duplicates. ABI
is setup by the setup function only now. Add a new attribute to the
function "gccrs_proc_macro" in order to differentiate it from another
type of function.
gcc/rust/ChangeLog:
* backend/rust-compile-base.cc (handle_proc_macro_common): Add
new attribute "gccrs_proc_macro" to all procedural macro
functions.
(get_abi): Add a function to retrieve the correct ABI depending
on wether the function is a proc macro or not.
We need to make sure proc macros have the C abi in order to be called by
the compiler with dlopen.
gcc/rust/ChangeLog:
* backend/rust-compile-base.cc (HIRCompileBase::setup_fndecl):
Add proc macro handlers dispatch.
(handle_proc_macro_common): Add a function for common behavior
between all kinds of proc macros.
* backend/rust-compile-base.h: Add function prototypes.
Jakub Dupak [Wed, 18 Oct 2023 20:36:38 +0000 (22:36 +0200)]
gccrs: borrowck: BIR dump
gcc/rust/ChangeLog:
* Make-lang.in: Build BIR dump.
* checks/errors/borrowck/rust-borrow-checker.cc (mkdir_wrapped): Cross-platform mkdir.
(dump_function_bir): Save dump to file.
(BorrowChecker::go): Run dump during borrowck.
* checks/errors/borrowck/rust-bir-dump.cc: New file.
* checks/errors/borrowck/rust-bir-dump.h: New file.
Jakub Dupak [Wed, 18 Oct 2023 18:43:17 +0000 (20:43 +0200)]
gccrs: borrowck: Create BIR builders (visitors)
gcc/rust/ChangeLog:
* Make-lang.in: Compile BIR expr visitor.
* checks/errors/borrowck/rust-borrow-checker.cc (BorrowChecker::go): Use BIR builder.
* rust-session-manager.cc (Session::compile_crate): Run borrow checker.
* checks/errors/borrowck/rust-bir-builder-expr-stmt.cc: New file.
* checks/errors/borrowck/rust-bir-builder-expr-stmt.h: New file.
* checks/errors/borrowck/rust-bir-builder-internal.h: New file.
* checks/errors/borrowck/rust-bir-builder-lazyboolexpr.h: New file.
* checks/errors/borrowck/rust-bir-builder-pattern.h: New file.
* checks/errors/borrowck/rust-bir-builder-struct.h: New file.
* checks/errors/borrowck/rust-bir-builder.h: New file.
Jakub Dupak [Wed, 18 Oct 2023 17:49:59 +0000 (19:49 +0200)]
gccrs: borrowck: Create Borrow-checker IR (BIR)
gcc/rust/ChangeLog:
* checks/errors/borrowck/rust-borrow-checker.cc: Include to compile new code.
* checks/errors/borrowck/rust-bir-place.h: New file.
* checks/errors/borrowck/rust-bir-visitor.h: New file.
* checks/errors/borrowck/rust-bir.h: New file.
Const with no value expression may exist either in trait or in disabled
blocks. This means we should be able to parse those correctly.
gcc/rust/ChangeLog:
* ast/rust-item.h: Add a new constructor for const with no value
expression.
* parse/rust-parse-impl.h (Parser::parse_const_item): Allow const with
no expression value.
Jakub Dupak [Mon, 16 Oct 2023 13:17:33 +0000 (15:17 +0200)]
gccrs: hir: Lower labelled block
gcc/rust/ChangeLog:
* hir/rust-ast-lower.cc (ASTLoweringBlock::visit): Call loop lowering and add it to constr.
* hir/tree/rust-hir-expr.h (class LoopLabel): Move before BlockExpr and add to BlockExpr.
Macro rules named macro_rules may cause some problems if not handled
correctly. This new test ensure we always compile those macros named
macro_rules correctly as well as other macro definitions.
Change the constraints around macro rules declaration in order to allow
macro_rules named macro as well as tighter constraint around macro rules
definitions.
gcc/rust/ChangeLog:
* parse/rust-parse-impl.h (Parser::is_macro_rules_def): Add a function
that checks tokens given by the lexer represents an accurate macro
definition. This will reduce code duplication.
(Parser::parse_item): Replace condition with call to new checking
function.
(Parser::parse_stmt): Likewise.
* parse/rust-parse.h: Add function prototype for is_macro_rules_def.
Jakub Dupak [Tue, 3 Oct 2023 14:14:39 +0000 (16:14 +0200)]
gccrs: TyTy: TyTy improved subclass casting and checking
Associate each subclass with its kind and create cast/match+cast
methods.
gcc/rust/ChangeLog:
* typecheck/rust-tyty.cc (InferType::InferType): Use static constant for kind information.
(ErrorType::ErrorType): Use static constant for kind information.
(TupleType::TupleType): Use static constant for kind information.
(BoolType::BoolType): Use static constant for kind information.
(IntType::IntType): Use static constant for kind information.
(UintType::UintType): Use static constant for kind information.
(FloatType::FloatType): Use static constant for kind information.
(USizeType::USizeType): Use static constant for kind information.
(ISizeType::ISizeType): Use static constant for kind information.
(CharType::CharType): Use static constant for kind information.
(ReferenceType::ReferenceType): Use static constant for kind information.
(PointerType::PointerType): Use static constant for kind information.
(ParamType::ParamType): Use static constant for kind information.
(StrType::StrType): Use static constant for kind information.
(NeverType::NeverType): Use static constant for kind information.
(PlaceholderType::PlaceholderType): Use static constant for kind information.
* typecheck/rust-tyty.h: Add static kind information to all TyTy classes.
Create safe cast and check methods.
Raiki Tamura [Tue, 8 Aug 2023 08:13:29 +0000 (17:13 +0900)]
gccrs: Initial implementation of v0 mangling
gcc/rust/ChangeLog:
* backend/rust-compile-context.h: Modify declaration.
* backend/rust-mangle.cc (struct V0Path): New struct.
(v0_path): New function.
(legacy_mangle_name): Take Context as argument.
(v0_numeric_prefix): Fix type strings.
(v0_complex_type_prefix): New function.
(v0_add_integer_62): Deleted
(v0_integer_62): New function.
(v0_add_opt_integer_62): Deleted.
(v0_opt_integer_62): New function.
(v0_add_disambiguator): Deleted.
(v0_disambiguator): New function.
(v0_type_prefix): Support more types.
(v0_generic_args): New function.
(v0_add_identifier): Deleted.
(v0_identifier): New function.
(v0_type_path): New function.
(v0_function_path): New function.
(v0_scope_path): New function.
(v0_crate_path): New function.
(v0_inherent_or_trait_impl_path): New function.
(v0_mangle_item): Use v0_path.
(Mangler::mangle_item): Take Context as argument.
* backend/rust-mangle.h (class Context): Add forward declaration.
* hir/tree/rust-hir-item.h: Fix include.