LCOV - code coverage report
Current view: top level - gcc/rust - rust-backend.h Coverage Total Hit
Test: gcc.info Lines: 100.0 % 2 2
Test Date: 2026-02-28 14:20:25 Functions: - 0 0
Legend: Lines:     hit not hit

            Line data    Source code
       1              : // Copyright (C) 2020-2026 Free Software Foundation, Inc.
       2              : 
       3              : // This file is part of GCC.
       4              : 
       5              : // GCC is free software; you can redistribute it and/or modify it under
       6              : // the terms of the GNU General Public License as published by the Free
       7              : // Software Foundation; either version 3, or (at your option) any later
       8              : // version.
       9              : 
      10              : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      11              : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
      12              : // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      13              : // for more details.
      14              : 
      15              : // You should have received a copy of the GNU General Public License
      16              : // along with GCC; see the file COPYING3.  If not see
      17              : // <http://www.gnu.org/licenses/>.
      18              : 
      19              : #ifndef RUST_BACKEND_H
      20              : #define RUST_BACKEND_H
      21              : 
      22              : #include <gmp.h>
      23              : #include <mpfr.h>
      24              : #include <mpc.h>
      25              : 
      26              : #include "rust-location.h"
      27              : #include "rust-linemap.h"
      28              : #include "rust-diagnostics.h"
      29              : #include "util/rust-operators.h"
      30              : #include "util/rust-ggc.h"
      31              : #include "util/optional.h"
      32              : #include "tree.h"
      33              : #include "rust-gcc.h"
      34              : 
      35              : // Pointers to these types are created by the backend, passed to the
      36              : // frontend, and passed back to the backend.  The types must be
      37              : // defined by the backend using these names.
      38              : 
      39              : // The backend representation of a variable.
      40              : class Bvariable;
      41              : 
      42              : // The backend interface.  This is a pure abstract class that a
      43              : // specific backend will implement.
      44              : 
      45              : namespace Backend {
      46              : 
      47              : namespace GGC {
      48              : 
      49              : using Rust::GGC::Ident;
      50              : 
      51              : } // namespace GGC
      52              : 
      53              : void init ();
      54              : 
      55              : // Name/type/location.  Used for function parameters, struct fields,
      56              : // interface methods.
      57              : struct typed_identifier
      58              : {
      59              :   GGC::Ident name;
      60              :   tree type;
      61              :   location_t location;
      62              : 
      63       141276 :   typed_identifier (GGC::Ident a_name, tree a_type, location_t a_location)
      64       133165 :     : name (a_name), type (a_type), location (a_location)
      65              :   {}
      66              : };
      67              : 
      68              : // debug
      69              : void debug (tree);
      70              : void debug (Bvariable *);
      71              : 
      72              : tree get_identifier_node (const std::string &str);
      73              : 
      74              : // Types.
      75              : 
      76              : // Get the wchar type
      77              : tree wchar_type ();
      78              : 
      79              : // Get the Host pointer size in bits
      80              : int get_pointer_size ();
      81              : 
      82              : // Get the raw str type const char*
      83              : tree raw_str_type ();
      84              : 
      85              : // Get an unnamed integer type with the given signedness and number
      86              : // of bits.
      87              : tree integer_type (bool is_unsigned, int bits);
      88              : 
      89              : // Get an unnamed floating point type with the given number of bits
      90              : // (32 or 64).
      91              : tree float_type (int bits);
      92              : 
      93              : // Get a pointer type.
      94              : tree pointer_type (tree to_type);
      95              : 
      96              : // Get a reference type.
      97              : tree reference_type (tree to_type);
      98              : 
      99              : // make type immutable
     100              : tree immutable_type (tree base);
     101              : 
     102              : // Get a function type.  The receiver, parameter, and results are
     103              : // generated from the types in the Function_type.  The Function_type
     104              : // is provided so that the names are available.  This should return
     105              : // not the type of a Go function (which is a pointer to a struct)
     106              : // but the type of a C function pointer (which will be used as the
     107              : // type of the first field of the struct).  If there is more than
     108              : // one result, RESULT_STRUCT is a struct type to hold the results,
     109              : // and RESULTS may be ignored; if there are zero or one results,
     110              : // RESULT_STRUCT is NULL.
     111              : tree function_type (const typed_identifier &receiver,
     112              :                     const std::vector<typed_identifier> &parameters,
     113              :                     const std::vector<typed_identifier> &results,
     114              :                     tree result_struct, location_t location);
     115              : 
     116              : tree function_type_variadic (const typed_identifier &receiver,
     117              :                              const std::vector<typed_identifier> &parameters,
     118              :                              const std::vector<typed_identifier> &results,
     119              :                              tree result_struct, location_t location);
     120              : 
     121              : tree function_ptr_type (tree result, const std::vector<tree> &praameters,
     122              :                         location_t location);
     123              : 
     124              : // Get a struct type.
     125              : tree struct_type (const std::vector<typed_identifier> &fields,
     126              :                   bool layout = true);
     127              : 
     128              : // Get a union type.
     129              : tree union_type (const std::vector<typed_identifier> &fields,
     130              :                  bool layout = true);
     131              : 
     132              : // Get an array type.
     133              : tree array_type (tree element_type, tree length);
     134              : 
     135              : // Return a named version of a type.  The location is the location
     136              : // of the type definition.  This will not be called for a type
     137              : // created via placeholder_pointer_type, placeholder_struct_type, or
     138              : // placeholder_array_type..  (It may be called for a pointer,
     139              : // struct, or array type in a case like "type P *byte; type Q P".)
     140              : tree named_type (GGC::Ident name, tree, location_t);
     141              : 
     142              : // Return the size of a type.
     143              : int64_t type_size (tree);
     144              : 
     145              : // Return the alignment of a type.
     146              : int64_t type_alignment (tree);
     147              : 
     148              : // Return the alignment of a struct field of this type.  This is
     149              : // normally the same as type_alignment, but not always.
     150              : int64_t type_field_alignment (tree);
     151              : 
     152              : // Return the offset of field INDEX in a struct type.  INDEX is the
     153              : // entry in the FIELDS std::vector parameter of struct_type or
     154              : // set_placeholder_struct_type.
     155              : int64_t type_field_offset (tree, size_t index);
     156              : 
     157              : // Expressions.
     158              : 
     159              : // Return an expression for a zero value of the given type.  This is
     160              : // used for cases such as local variable initialization and
     161              : // converting nil to other types.
     162              : tree zero_expression (tree);
     163              : 
     164              : // Create a reference to a variable.
     165              : tree var_expression (Bvariable *var, location_t);
     166              : 
     167              : // Return an expression for the floating point value VAL in BTYPE.
     168              : tree float_constant_expression (tree btype, mpfr_t val);
     169              : 
     170              : // Return an expression for the string value VAL.
     171              : tree string_constant_expression (const std::string &val);
     172              : 
     173              : // Get a char literal
     174              : tree char_constant_expression (char c);
     175              : 
     176              : // Get a char literal
     177              : tree wchar_constant_expression (wchar_t c);
     178              : 
     179              : // Get a size literal
     180              : tree size_constant_expression (size_t val);
     181              : 
     182              : // Return an expression for the boolean value VAL.
     183              : tree boolean_constant_expression (bool val);
     184              : 
     185              : // Return an expression that converts EXPR to TYPE.
     186              : tree convert_expression (tree type, tree expr, location_t);
     187              : 
     188              : // Return an expression for the field at INDEX in BSTRUCT.
     189              : tree struct_field_expression (tree bstruct, size_t index, location_t);
     190              : 
     191              : // Create an expression that executes BSTAT before BEXPR.
     192              : tree compound_expression (tree bstat, tree bexpr, location_t);
     193              : 
     194              : // Return an expression that executes THEN_EXPR if CONDITION is true, or
     195              : // ELSE_EXPR otherwise and returns the result as type BTYPE, within the
     196              : // specified function FUNCTION.  ELSE_EXPR may be NULL.  BTYPE may be NULL.
     197              : tree conditional_expression (tree function, tree btype, tree condition,
     198              :                              tree then_expr, tree else_expr, location_t);
     199              : 
     200              : // Return an expression for the negation operation OP EXPR.
     201              : // Supported values of OP are enumerated in NegationOperator.
     202              : tree negation_expression (NegationOperator op, tree expr, location_t);
     203              : 
     204              : // Return an expression for the operation LEFT OP RIGHT.
     205              : // Supported values of OP are enumerated in ArithmeticOrLogicalOperator.
     206              : tree arithmetic_or_logical_expression (ArithmeticOrLogicalOperator op,
     207              :                                        tree left, tree right, location_t loc);
     208              : 
     209              : // Return an expression for the operation LEFT OP RIGHT.
     210              : // Supported values of OP are enumerated in ArithmeticOrLogicalOperator.
     211              : // This function adds overflow checking and returns a list of statements to
     212              : // add to the current function context. The `receiver` variable refers to the
     213              : // variable which will contain the result of that operation.
     214              : tree arithmetic_or_logical_expression_checked (ArithmeticOrLogicalOperator op,
     215              :                                                tree left, tree right,
     216              :                                                location_t loc,
     217              :                                                Bvariable *receiver);
     218              : 
     219              : // Return an expression for the operation LEFT OP RIGHT.
     220              : // Supported values of OP are enumerated in ComparisonOperator.
     221              : tree comparison_expression (ComparisonOperator op, tree left, tree right,
     222              :                             location_t loc);
     223              : 
     224              : // Return an expression for the operation LEFT OP RIGHT.
     225              : // Supported values of OP are enumerated in LazyBooleanOperator.
     226              : tree lazy_boolean_expression (LazyBooleanOperator op, tree left, tree right,
     227              :                               location_t);
     228              : 
     229              : // Return an expression that constructs BTYPE with VALS.  BTYPE must be the
     230              : // backend representation a of struct.  VALS must be in the same order as the
     231              : // corresponding fields in BTYPE.
     232              : tree constructor_expression (tree btype, bool is_variant,
     233              :                              const std::vector<tree> &vals, int, location_t);
     234              : 
     235              : // Return an expression that constructs an array of BTYPE with INDEXES and
     236              : // VALS.  INDEXES and VALS must have the same amount of elements. Each index
     237              : // in INDEXES must be in the same order as the corresponding value in VALS.
     238              : tree array_constructor_expression (tree btype,
     239              :                                    const std::vector<unsigned long> &indexes,
     240              :                                    const std::vector<tree> &vals, location_t);
     241              : 
     242              : tree array_initializer (tree, tree, tree, tree, tree, tree *, location_t);
     243              : 
     244              : // Return an expression for ARRAY[INDEX] as an l-value.  ARRAY is a valid
     245              : // fixed-length array, not a slice.
     246              : tree array_index_expression (tree array, tree index, location_t);
     247              : 
     248              : // Return an expresison for SLICE[INDEX] as an l-value.  SLICE is represented
     249              : // with a DST.
     250              : tree slice_index_expression (tree slice, tree index, location_t);
     251              : 
     252              : // Create an expression for a call to FN with ARGS, taking place within
     253              : // caller CALLER.
     254              : tree call_expression (tree fn, const std::vector<tree> &args, tree static_chain,
     255              :                       location_t);
     256              : 
     257              : // Statements.
     258              : 
     259              : // Create a variable initialization statement in the specified
     260              : // function.  This initializes a local variable at the point in the
     261              : // program flow where it is declared.
     262              : tree init_statement (tree, Bvariable *var, tree init);
     263              : 
     264              : // Create an assignment statement within the specified function.
     265              : tree assignment_statement (tree lhs, tree rhs, location_t);
     266              : 
     267              : // Create return statement for an decl for a value (can be NULL_TREE) at a
     268              : // location
     269              : tree return_statement (tree fndecl, tree val, location_t);
     270              : 
     271              : // Create an if statement within a function.  ELSE_BLOCK may be NULL.
     272              : tree if_statement (tree, tree condition, tree then_block, tree else_block,
     273              :                    location_t);
     274              : 
     275              : // infinite loop expressions
     276              : tree loop_expression (tree body, location_t);
     277              : 
     278              : // exit expressions
     279              : tree exit_expression (tree condition, location_t);
     280              : 
     281              : // Create a single statement from two statements.
     282              : tree compound_statement (tree, tree);
     283              : 
     284              : // Create a single statement from a list of statements.
     285              : tree statement_list (const std::vector<tree> &);
     286              : 
     287              : // Create a statement that attempts to execute BSTAT and calls EXCEPT_STMT if
     288              : // an exception occurs. EXCEPT_STMT may be NULL.  FINALLY_STMT may be NULL and
     289              : // if not NULL, it will always be executed.  This is used for handling defers
     290              : // in Go functions.  In C++, the resulting code is of this form:
     291              : //   try { BSTAT; } catch { EXCEPT_STMT; } finally { FINALLY_STMT; }
     292              : tree exception_handler_statement (tree bstat, tree except_stmt,
     293              :                                   tree finally_stmt, location_t);
     294              : 
     295              : // Blocks.
     296              : 
     297              : // Create a block.  The frontend will call this function when it
     298              : // starts converting a block within a function.  FUNCTION is the
     299              : // current function.  ENCLOSING is the enclosing block; it will be
     300              : // NULL for the top-level block in a function.  VARS is the list of
     301              : // local variables defined within this block; each entry will be
     302              : // created by the local_variable function.  START_LOCATION is the
     303              : // location of the start of the block, more or less the location of
     304              : // the initial curly brace.  END_LOCATION is the location of the end
     305              : // of the block, more or less the location of the final curly brace.
     306              : // The statements will be added after the block is created.
     307              : tree block (tree function, tree enclosing, const std::vector<Bvariable *> &vars,
     308              :             location_t start_location, location_t end_location);
     309              : 
     310              : // Add the statements to a block.  The block is created first.  Then
     311              : // the statements are created.  Then the statements are added to the
     312              : // block.  This will called exactly once per block.  The vector may
     313              : // be empty if there are no statements.
     314              : void block_add_statements (tree, const std::vector<tree> &);
     315              : 
     316              : // Variables.
     317              : 
     318              : // Create a global variable. NAME is the package-qualified name of
     319              : // the variable.  ASM_NAME is the encoded identifier for the
     320              : // variable, incorporating the package, and made safe for the
     321              : // assembler.  BTYPE is the type of the variable.  IS_EXTERNAL is
     322              : // true if the variable is defined in some other package.  IS_HIDDEN
     323              : // is true if the variable is not exported (name begins with a lower
     324              : // case letter).  IN_UNIQUE_SECTION is true if the variable should
     325              : // be put into a unique section if possible; this is intended to
     326              : // permit the linker to garbage collect the variable if it is not
     327              : // referenced.  LOCATION is where the variable was defined.
     328              : Bvariable *global_variable (GGC::Ident name, GGC::Ident asm_name, tree btype,
     329              :                             bool is_external, bool is_hidden,
     330              :                             bool in_unique_section, location_t location);
     331              : 
     332              : // A global variable will 1) be initialized to zero, or 2) be
     333              : // initialized to a constant value, or 3) be initialized in the init
     334              : // function.  In case 2, the frontend will call
     335              : // global_variable_set_init to set the initial value.  If this is
     336              : // not called, the backend should initialize a global variable to 0.
     337              : // The init function may then assign a value to it.
     338              : void global_variable_set_init (Bvariable *, tree);
     339              : 
     340              : // Create a local variable.  The frontend will create the local
     341              : // variables first, and then create the block which contains them.
     342              : // FUNCTION is the function in which the variable is defined.  NAME
     343              : // is the name of the variable.  TYPE is the type.  DECL_VAR, if not
     344              : // null, gives the location at which the value of this variable may
     345              : // be found, typically used to create an inner-scope reference to an
     346              : // outer-scope variable, to extend the lifetime of the variable beyond
     347              : // the inner scope.  IS_ADDRESS_TAKEN is true if the address of this
     348              : // variable is taken (this implies that the address does not escape
     349              : // the function, as otherwise the variable would be on the heap).
     350              : // LOCATION is where the variable is defined.  For each local variable
     351              : // the frontend will call init_statement to set the initial value.
     352              : LocalVariable local_variable (tree function, GGC::Ident name, tree type,
     353              :                               Bvariable *decl_var, location_t location);
     354              : 
     355              : // Create a function parameter.  This is an incoming parameter, not
     356              : // a result parameter (result parameters are treated as local
     357              : // variables).  The arguments are as for local_variable.
     358              : LocalVariable parameter_variable (tree function, GGC::Ident name, tree type,
     359              :                                   location_t location);
     360              : 
     361              : // Create a static chain parameter.  This is the closure parameter.
     362              : LocalVariable static_chain_variable (tree function, GGC::Ident name, tree type,
     363              :                                      location_t location);
     364              : 
     365              : // Create a temporary variable.  A temporary variable has no name,
     366              : // just a type.  We pass in FUNCTION and BLOCK in case they are
     367              : // needed.  If INIT is not NULL, the variable should be initialized
     368              : // to that value.  Otherwise the initial value is irrelevant--the
     369              : // backend does not have to explicitly initialize it to zero.
     370              : // ADDRESS_IS_TAKEN is true if the programs needs to take the
     371              : // address of this temporary variable.  LOCATION is the location of
     372              : // the statement or expression which requires creating the temporary
     373              : // variable, and may not be very useful.  This function should
     374              : // return a variable which can be referenced later and should set
     375              : // *PSTATEMENT to a statement which initializes the variable.
     376              : LocalVariable temporary_variable (tree fndecl, tree bind_tree, tree type,
     377              :                                   tree init, bool address_is_taken,
     378              :                                   location_t location, tree *pstatement);
     379              : 
     380              : // Labels.
     381              : 
     382              : // Create a new label.  NAME will be tl::nullopt if this is a label
     383              : // created by the frontend for a loop construct.  The location is
     384              : // where the label is defined.
     385              : tree label (tree, tl::optional<GGC::Ident> name, location_t);
     386              : 
     387              : // Create a statement which defines a label.  This statement will be
     388              : // put into the codestream at the point where the label should be
     389              : // defined.
     390              : tree label_definition_statement (tree);
     391              : 
     392              : // Create a goto statement to a label.
     393              : tree goto_statement (tree, location_t);
     394              : 
     395              : // Create an expression for the address of a label.  This is used to
     396              : // get the return address of a deferred function which may call
     397              : // recover.
     398              : tree label_address (tree, location_t);
     399              : 
     400              : // Lookup a field from a type given its name.
     401              : // Build the `component` tree with `Backend::get_identifier_node`.
     402              : //
     403              : // Forked from the C frontend.
     404              : tree lookup_field (const_tree, tree);
     405              : 
     406              : // Functions.
     407              : 
     408              : // Bit flags to pass to the function method.
     409              : 
     410              : // Set if this is a function declaration rather than a definition;
     411              : // the definition will be in another compilation unit.
     412              : static const unsigned int function_is_declaration = 1 << 0;
     413              : 
     414              : // Set if the function should never be inlined because they call
     415              : // recover and must be visible for correct panic recovery.
     416              : static const unsigned int function_is_uninlinable = 1 << 1;
     417              : 
     418              : // Set if the function does not return.  This is set for the
     419              : // implementation of panic.
     420              : static const unsigned int function_does_not_return = 1 << 2;
     421              : 
     422              : // Set if the function should be put in a unique section if
     423              : // possible.  This is used for field tracking.
     424              : static const unsigned int function_in_unique_section = 1 << 3;
     425              : 
     426              : // Declare or define a function of FNTYPE.
     427              : // NAME is the Go name of the function.  ASM_NAME, if not tl::nullopt,
     428              : // is the name that should be used in the symbol table; this
     429              : // will be non-empty if a magic extern comment is used.  FLAGS is
     430              : // bit flags described above.
     431              : tree function (tree fntype, GGC::Ident name, tl::optional<GGC::Ident> asm_name,
     432              :                unsigned int flags, location_t);
     433              : 
     434              : // Create a statement that runs all deferred calls for FUNCTION.  This should
     435              : // be a statement that looks like this in C++:
     436              : //   finish:
     437              : //     try { DEFER_RETURN; } catch { CHECK_DEFER; goto finish; }
     438              : tree function_defer_statement (tree function, tree undefer, tree check_defer,
     439              :                                location_t);
     440              : 
     441              : // Record PARAM_VARS as the variables to use for the parameters of FUNCTION.
     442              : // This will only be called for a function definition.  Returns true on
     443              : // success, false on failure.
     444              : bool function_set_parameters (tree function,
     445              :                               const std::vector<Bvariable *> &param_vars);
     446              : 
     447              : // Utility.
     448              : 
     449              : // Write the definitions for all TYPE_DECLS, CONSTANT_DECLS,
     450              : // FUNCTION_DECLS, and VARIABLE_DECLS declared globally.
     451              : void write_global_definitions (const std::vector<tree> &type_decls,
     452              :                                const std::vector<tree> &constant_decls,
     453              :                                const std::vector<tree> &function_decls,
     454              :                                const std::vector<Bvariable *> &variable_decls);
     455              : 
     456              : // TODO: make static
     457              : 
     458              : tree fill_in_fields (tree, const std::vector<typed_identifier> &, bool);
     459              : 
     460              : tree fill_in_array (tree, tree, tree);
     461              : 
     462              : tree non_zero_size_type (tree);
     463              : 
     464              : tree convert_tree (tree, tree, location_t);
     465              : 
     466              : } // namespace Backend
     467              : 
     468              : #endif // RUST_BACKEND_H
        

Generated by: LCOV version 2.4-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.