// this assumes all fields are in order from type resolution and if a
// base struct was specified those fields are filed via accessors
std::vector<tree> arguments;
- for (size_t i = 0; i < expr.get_arguments ().size (); i++)
+ for (size_t i = 0; i < expr.num_params (); i++)
{
auto &argument = expr.get_arguments ().at (i);
auto rvalue = CompileExpr::Compile (*argument, ctx);
void
PrivacyReporter::visit (HIR::LetStmt &stmt)
{
- check_type_privacy (stmt.get_type ());
+ if (stmt.has_type ())
+ check_type_privacy (stmt.get_type ());
- stmt.get_init_expr ().accept_vis (*this);
+ if (stmt.has_init_expr ())
+ stmt.get_init_expr ().accept_vis (*this);
}
void
// along with GCC; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
+#include "optional.h"
#include "rust-ast-lower-item.h"
#include "rust-ast-lower-stmt.h"
#include "rust-ast-lower-type.h"
{
HIR::Pattern *variables
= ASTLoweringPattern::translate (stmt.get_pattern (), true);
- HIR::Type *type = stmt.has_type ()
- ? ASTLoweringType::translate (stmt.get_type ())
- : nullptr;
- HIR::Expr *init_expression
- = stmt.has_init_expr () ? ASTLoweringExpr::translate (stmt.get_init_expr ())
- : nullptr;
+
+ auto type
+ = stmt.has_type () ? tl::optional<std::unique_ptr<Type>> (
+ std::unique_ptr<Type> (ASTLoweringType::translate (stmt.get_type ())))
+ : tl::nullopt;
+ auto init_expression
+ = stmt.has_init_expr ()
+ ? tl::optional<std::unique_ptr<Expr>> (std::unique_ptr<HIR::Expr> (
+ ASTLoweringExpr::translate (stmt.get_init_expr ())))
+ : tl::nullopt;
auto crate_num = mappings.get_current_crate ();
Analysis::NodeMapping mapping (crate_num, stmt.get_node_id (),
UNKNOWN_LOCAL_DEFID);
translated
= new HIR::LetStmt (mapping, std::unique_ptr<HIR::Pattern> (variables),
- std::unique_ptr<HIR::Expr> (init_expression),
- std::unique_ptr<HIR::Type> (type),
+ std::move (init_expression), std::move (type),
stmt.get_outer_attrs (), stmt.get_locus ());
}
bool has_default_expression () { return default_expression != nullptr; }
std::string get_name () { return name; }
- Type &get_type () { return *type; }
+ Type &get_type ()
+ {
+ rust_assert (type);
+ return *type;
+ }
Expr &get_default_expression () { return *default_expression; }
protected:
#include "rust-common.h"
#include "rust-hir-visibility.h"
#include "rust-hir-generic-param.h"
+#include "rust-system.h"
namespace Rust {
namespace HIR {
Identifier get_type_representation () const { return type_representation; }
- Type &get_type () { return *type; }
+ Type &get_type ()
+ {
+ rust_assert (type);
+ return *type;
+ }
Analysis::NodeMapping get_type_mappings () const;
ImplicitSelfKind get_self_kind () const { return self_kind; }
- Type &get_type () { return *type; }
+ Type &get_type ()
+ {
+ rust_assert (type);
+ return *type;
+ }
Analysis::NodeMapping get_mappings () { return mappings; }
Pattern &get_param_name () { return *param_name; }
- Type &get_type () { return *type; }
+ Type &get_type ()
+ {
+ rust_assert (type);
+ return *type;
+ }
const Analysis::NodeMapping &get_mappings () const { return mappings; }
};
WhereClause &get_where_clause () { return where_clause; }
- Type &get_type_aliased () { return *existing_type; }
+ Type &get_type_aliased ()
+ {
+ rust_assert (existing_type);
+ return *existing_type;
+ }
Identifier get_new_type_name () const { return new_type_name; }
void accept_vis (HIRImplVisitor &vis) override;
void accept_vis (HIRVisItemVisitor &vis) override;
- Type &get_type () { return *type; }
+ Type &get_type ()
+ {
+ rust_assert (type);
+ return *type;
+ }
Expr &get_expr () { return *const_expr; }
bool is_mut () const { return mut == Mutability::Mut; }
- Expr &get_expr () { return *expr; }
+ Expr &get_expr ()
+ {
+ rust_assert (expr);
+ return *expr;
+ }
- Type &get_type () { return *type; }
+ Type &get_type ()
+ {
+ rust_assert (type);
+ return *type;
+ }
ItemKind get_item_kind () const override { return ItemKind::Static; }
bool has_expr () const { return expr != nullptr; }
- Type &get_type () { return *type; }
+ Type &get_type ()
+ {
+ rust_assert (type);
+ return *type;
+ }
- Expr &get_expr () { return *expr; }
+ Expr &get_expr ()
+ {
+ rust_assert (expr);
+ return *expr;
+ }
const std::string trait_identifier () const override final
{
location_t get_locus () const override final { return locus; }
- Type &get_type () { return *impl_type; };
+ Type &get_type ()
+ {
+ rust_assert (impl_type);
+ return *impl_type;
+ };
- bool has_type () { return impl_type == nullptr; }
+ bool has_type () { return impl_type != nullptr; }
std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
{
Identifier get_param_name () const { return name; }
- Type &get_type () { return *param_type; }
+ Type &get_type ()
+ {
+ rust_assert (param_type);
+ return *param_type;
+ }
Analysis::NodeMapping get_mappings () const { return mappings; }
};
Identifier &get_identifier () { return identifier; }
const Identifier &get_identifier () const { return identifier; }
- Type &get_type () { return *type; }
- const Type &get_type () const { return *type; }
+ Type &get_type ()
+ {
+ rust_assert (type);
+ return *type;
+ }
+ const Type &get_type () const
+ {
+ rust_assert (type);
+ return *type;
+ }
location_t get_locus () const { return locus; }
};
Analysis::NodeMapping get_mappings () const { return mappings; }
- Type &get_type () { return *type; }
+ Type &get_type ()
+ {
+ rust_assert (type);
+ return *type;
+ }
TypePath &get_trait () { return *trait; }
// <http://www.gnu.org/licenses/>.
#include "rust-hir-stmt.h"
+#include "optional.h"
+#include "rust-system.h"
namespace Rust {
namespace HIR {
LetStmt::LetStmt (Analysis::NodeMapping mappings,
std::unique_ptr<Pattern> variables_pattern,
- std::unique_ptr<Expr> init_expr, std::unique_ptr<Type> type,
+ tl::optional<std::unique_ptr<Expr>> init_expr,
+ tl::optional<std::unique_ptr<Type>> type,
AST::AttrVec outer_attrs, location_t locus)
: Stmt (std::move (mappings)), outer_attrs (std::move (outer_attrs)),
variables_pattern (std::move (variables_pattern)), type (std::move (type)),
variables_pattern = other.variables_pattern->clone_pattern ();
// guard to prevent null dereference (always required)
- if (other.init_expr != nullptr)
- init_expr = other.init_expr->clone_expr ();
- if (other.type != nullptr)
- type = other.type->clone_type ();
+ if (other.has_init_expr ())
+ init_expr = other.get_init_expr ().clone_expr ();
+
+ if (other.has_type ())
+ type = other.get_type ().clone_type ();
+ else
+ type = tl::nullopt;
}
LetStmt &
variables_pattern = nullptr;
// guard to prevent null dereference (always required)
- if (other.init_expr != nullptr)
- init_expr = other.init_expr->clone_expr ();
+ if (other.has_init_expr ())
+ init_expr = other.get_init_expr ().clone_expr ();
else
init_expr = nullptr;
- if (other.type != nullptr)
- type = other.type->clone_type ();
+ if (other.has_type ())
+ type = other.get_type ().clone_type ();
else
- type = nullptr;
+ type = tl::nullopt;
return *this;
}
#include "rust-hir.h"
#include "rust-hir-path.h"
#include "rust-hir-expr.h"
+#include "rust-system.h"
namespace Rust {
namespace HIR {
std::unique_ptr<Pattern> variables_pattern;
- // bool has_type;
- std::unique_ptr<Type> type;
+ tl::optional<std::unique_ptr<Type>> type;
- // bool has_init_expr;
- std::unique_ptr<Expr> init_expr;
+ tl::optional<std::unique_ptr<Expr>> init_expr;
location_t locus;
bool has_outer_attrs () const { return !outer_attrs.empty (); }
// Returns whether let statement has a given return type.
- bool has_type () const { return type != nullptr; }
+ bool has_type () const { return type.has_value (); }
// Returns whether let statement has an initialisation expression.
- bool has_init_expr () const { return init_expr != nullptr; }
+ bool has_init_expr () const { return init_expr.has_value (); }
std::string as_string () const override;
LetStmt (Analysis::NodeMapping mappings,
std::unique_ptr<Pattern> variables_pattern,
- std::unique_ptr<Expr> init_expr, std::unique_ptr<Type> type,
- AST::AttrVec outer_attrs, location_t locus);
+ tl::optional<std::unique_ptr<Expr>> init_expr,
+ tl::optional<std::unique_ptr<Type>> type, AST::AttrVec outer_attrs,
+ location_t locus);
// Copy constructor with clone
LetStmt (LetStmt const &other);
}
std::vector<AST::Attribute> &get_outer_attrs () { return outer_attrs; }
- HIR::Type &get_type () { return *type; }
+ HIR::Type &get_type ()
+ {
+ rust_assert (*type);
+ return *type.value ();
+ }
+
+ const HIR::Type &get_type () const
+ {
+ rust_assert (*type);
+ return *type.value ();
+ }
- HIR::Expr &get_init_expr () { return *init_expr; }
+ HIR::Expr &get_init_expr ()
+ {
+ rust_assert (*init_expr);
+ return *init_expr.value ();
+ }
+
+ const HIR::Expr &get_init_expr () const
+ {
+ rust_assert (*init_expr);
+ return *init_expr.value ();
+ }
HIR::Pattern &get_pattern () { return *variables_pattern; }
if (has_type ())
{
- str += " : " + type->as_string ();
+ str += " : " + get_type ().as_string ();
}
if (has_init_expr ())
{
- str += " = " + init_expr->as_string ();
+ str += " = " + get_init_expr ().as_string ();
}
return str;
ok = adt->lookup_variant_by_id (variant_id, &lookup_variant);
rust_assert (ok);
- variant = std::move (*lookup_variant);
+ variant = std::move (*lookup_variant->clone ());
}
else
{
rust_assert (adt->number_of_variants () == 1);
- variant = std::move (*adt->get_variants ().at (0));
+ variant = std::move (*adt->get_variants ().at (0)->clone ());
}
-
infered
= TyTy::TypeCheckCallExpr::go (function_tyty, expr, variant, context);
return;
// <http://www.gnu.org/licenses/>.
#include "rust-hir-type-check-item.h"
+#include "optional.h"
#include "rust-canonical-path.h"
#include "rust-diagnostics.h"
#include "rust-hir-type-check-enumitem.h"
new TyTy::VariantDef (struct_decl.get_mappings ().get_hirid (),
struct_decl.get_mappings ().get_defid (),
struct_decl.get_identifier ().as_string (), ident,
- TyTy::VariantDef::VariantType::TUPLE, nullptr,
+ TyTy::VariantDef::VariantType::TUPLE, tl::nullopt,
std::move (fields)));
// Process #[repr(X)] attribute, if any
new TyTy::VariantDef (struct_decl.get_mappings ().get_hirid (),
struct_decl.get_mappings ().get_defid (),
struct_decl.get_identifier ().as_string (), ident,
- TyTy::VariantDef::VariantType::STRUCT, nullptr,
+ TyTy::VariantDef::VariantType::STRUCT, tl::nullopt,
std::move (fields)));
// Process #[repr(X)] attribute, if any
new TyTy::VariantDef (union_decl.get_mappings ().get_hirid (),
union_decl.get_mappings ().get_defid (),
union_decl.get_identifier ().as_string (), ident,
- TyTy::VariantDef::VariantType::STRUCT, nullptr,
+ TyTy::VariantDef::VariantType::STRUCT, tl::nullopt,
std::move (fields)));
auto *type
#include "rust-tyty.h"
+#include "optional.h"
#include "rust-tyty-visitor.h"
#include "rust-hir-map.h"
#include "rust-location.h"
VariantDef::VariantDef (HirId id, DefId defid, std::string identifier,
RustIdent ident,
- std::unique_ptr<HIR::Expr> &&discriminant)
+ tl::optional<std::unique_ptr<HIR::Expr>> &&discriminant)
: id (id), defid (defid), identifier (identifier), ident (ident),
discriminant (std::move (discriminant))
VariantDef::VariantDef (HirId id, DefId defid, std::string identifier,
RustIdent ident, VariantType type,
- std::unique_ptr<HIR::Expr> &&discriminant,
+ tl::optional<std::unique_ptr<HIR::Expr>> &&discriminant,
std::vector<StructFieldType *> fields)
: id (id), defid (defid), identifier (identifier), ident (ident), type (type),
discriminant (std::move (discriminant)), fields (fields)
static VariantDef node
= VariantDef (UNKNOWN_HIRID, UNKNOWN_DEFID, "",
{Resolver::CanonicalPath::create_empty (), UNKNOWN_LOCATION},
- nullptr);
+ tl::nullopt);
return node;
}
HIR::Expr &
VariantDef::get_discriminant ()
{
- rust_assert (discriminant != nullptr);
- return *discriminant;
+ return *discriminant.value ();
+}
+
+const HIR::Expr &
+VariantDef::get_discriminant () const
+{
+ return *discriminant.value ();
+}
+
+bool
+VariantDef::has_discriminant () const
+{
+ return discriminant.has_value ();
}
std::string
VariantDef::as_string () const
{
if (type == VariantType::NUM)
- return identifier + " = " + discriminant->as_string ();
+ return identifier
+ + (has_discriminant () ? " = " + get_discriminant ().as_string ()
+ : "");
std::string buffer;
for (size_t i = 0; i < fields.size (); ++i)
for (auto &f : fields)
cloned_fields.push_back ((StructFieldType *) f->clone ());
+ auto &&discriminant_opt = has_discriminant ()
+ ? tl::optional<std::unique_ptr<HIR::Expr>> (
+ get_discriminant ().clone_expr ())
+ : tl::nullopt;
+
return new VariantDef (id, defid, identifier, ident, type,
- discriminant->clone_expr (), cloned_fields);
+ std::move (discriminant_opt), cloned_fields);
}
VariantDef *
for (auto &f : fields)
cloned_fields.push_back ((StructFieldType *) f->monomorphized_clone ());
+ auto discriminant_opt = has_discriminant ()
+ ? tl::optional<std::unique_ptr<HIR::Expr>> (
+ get_discriminant ().clone_expr ())
+ : tl::nullopt;
+
return new VariantDef (id, defid, identifier, ident, type,
- discriminant->clone_expr (), cloned_fields);
+ std::move (discriminant_opt), cloned_fields);
}
const RustIdent &
static std::string variant_type_string (VariantType type);
VariantDef (HirId id, DefId defid, std::string identifier, RustIdent ident,
- std::unique_ptr<HIR::Expr> &&discriminant);
+ tl::optional<std::unique_ptr<HIR::Expr>> &&discriminant);
VariantDef (HirId id, DefId defid, std::string identifier, RustIdent ident,
- VariantType type, std::unique_ptr<HIR::Expr> &&discriminant,
+ VariantType type,
+ tl::optional<std::unique_ptr<HIR::Expr>> &&discriminant,
std::vector<StructFieldType *> fields);
static VariantDef &get_error_node ();
bool lookup_field (const std::string &lookup, StructFieldType **field_lookup,
size_t *index) const;
+ bool has_discriminant () const;
+
HIR::Expr &get_discriminant ();
+ const HIR::Expr &get_discriminant () const;
std::string as_string () const;
std::string identifier;
RustIdent ident;
VariantType type;
+
// can either be a structure or a discriminant value
- std::unique_ptr<HIR::Expr> discriminant;
+ tl::optional<std::unique_ptr<HIR::Expr>> discriminant;
+
std::vector<StructFieldType *> fields;
};