aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/ast
diff options
context:
space:
mode:
Diffstat (limited to 'gcc/rust/ast')
-rw-r--r--gcc/rust/ast/rust-ast-collector.cc46
-rw-r--r--gcc/rust/ast/rust-ast-visitor.cc10
-rw-r--r--gcc/rust/ast/rust-ast.cc25
-rw-r--r--gcc/rust/ast/rust-item.h30
4 files changed, 58 insertions, 53 deletions
diff --git a/gcc/rust/ast/rust-ast-collector.cc b/gcc/rust/ast/rust-ast-collector.cc
index 71cc098..9997523 100644
--- a/gcc/rust/ast/rust-ast-collector.cc
+++ b/gcc/rust/ast/rust-ast-collector.cc
@@ -172,9 +172,22 @@ TokenCollector::visit (Visitable &v)
void
TokenCollector::visit (FunctionParam &param)
{
- visit (param.get_pattern ());
- push (Rust::Token::make (COLON, UNDEF_LOCATION));
- visit (param.get_type ());
+ visit_items_as_lines (param.get_outer_attrs ());
+ if (!param.is_variadic ())
+ {
+ visit (param.get_pattern ());
+ push (Rust::Token::make (COLON, UNDEF_LOCATION));
+ visit (param.get_type ());
+ }
+ else
+ {
+ if (param.has_name ())
+ {
+ visit (param.get_pattern ());
+ push (Rust::Token::make (COLON, UNDEF_LOCATION));
+ }
+ push (Rust::Token::make (ELLIPSIS, UNDEF_LOCATION));
+ }
}
void
@@ -293,9 +306,23 @@ void
TokenCollector::visit (NamedFunctionParam &param)
{
auto name = param.get_name ();
- push (Rust::Token::make_identifier (param.get_locus (), std::move (name)));
- push (Rust::Token::make (COLON, UNDEF_LOCATION));
- visit (param.get_type ());
+ if (!param.is_variadic ())
+ {
+ push (
+ Rust::Token::make_identifier (param.get_locus (), std::move (name)));
+ push (Rust::Token::make (COLON, UNDEF_LOCATION));
+ visit (param.get_type ());
+ }
+ else
+ {
+ if (name != "")
+ {
+ push (Rust::Token::make_identifier (param.get_locus (),
+ std::move (name)));
+ push (Rust::Token::make (COLON, UNDEF_LOCATION));
+ }
+ push (Rust::Token::make (ELLIPSIS, UNDEF_LOCATION));
+ }
}
void
@@ -2209,13 +2236,6 @@ TokenCollector::visit (ExternalFunctionItem &function)
push (Rust::Token::make (LEFT_PAREN, UNDEF_LOCATION));
visit_items_joined_by_separator (function.get_function_params ());
- if (function.is_variadic ())
- {
- push (Rust::Token::make (COMMA, UNDEF_LOCATION));
- // TODO: Add variadic outer attributes?
- // TODO: Add variadic name once implemented.
- push (Rust::Token::make (ELLIPSIS, UNDEF_LOCATION));
- }
push (Rust::Token::make (RIGHT_PAREN, UNDEF_LOCATION));
if (function.has_return_type ())
diff --git a/gcc/rust/ast/rust-ast-visitor.cc b/gcc/rust/ast/rust-ast-visitor.cc
index 9fb1099..4b954fd 100644
--- a/gcc/rust/ast/rust-ast-visitor.cc
+++ b/gcc/rust/ast/rust-ast-visitor.cc
@@ -721,8 +721,11 @@ void
DefaultASTVisitor::visit (AST::FunctionParam &param)
{
visit_outer_attrs (param);
- visit (param.get_pattern ());
- visit (param.get_type ());
+ if (param.has_name ())
+ visit (param.get_pattern ());
+
+ if (!param.is_variadic ())
+ visit (param.get_type ());
}
void
@@ -1054,7 +1057,8 @@ void
DefaultASTVisitor::visit (AST::NamedFunctionParam &param)
{
visit_outer_attrs (param);
- visit (param.get_type ());
+ if (!param.is_variadic ())
+ visit (param.get_type ());
}
void
diff --git a/gcc/rust/ast/rust-ast.cc b/gcc/rust/ast/rust-ast.cc
index d9c493d..3e8d414 100644
--- a/gcc/rust/ast/rust-ast.cc
+++ b/gcc/rust/ast/rust-ast.cc
@@ -3036,7 +3036,7 @@ ExternalFunctionItem::as_string () const
// function params
str += "\n Function params: ";
- if (function_params.empty () && !has_variadics)
+ if (function_params.empty ())
{
str += "none";
}
@@ -3044,21 +3044,6 @@ ExternalFunctionItem::as_string () const
{
for (const auto &param : function_params)
str += "\n " + param.as_string ();
-
- if (has_variadics)
- {
- str += "\n variadic outer attrs: ";
- if (has_variadic_outer_attrs ())
- {
- for (const auto &attr : variadic_outer_attrs)
- str += "\n " + attr.as_string ();
- }
- else
- {
- str += "none";
- }
- str += "\n ... (variadic)";
- }
}
// add type on new line
@@ -3080,9 +3065,13 @@ NamedFunctionParam::as_string () const
{
std::string str = append_attributes (outer_attrs, OUTER);
- str += "\n" + name;
+ if (has_name ())
+ str += "\n" + name;
- str += "\n Type: " + param_type->as_string ();
+ if (is_variadic ())
+ str += "...";
+ else
+ str += "\n Type: " + param_type->as_string ();
return str;
}
diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h
index 7cdbc8b..70ef93d 100644
--- a/gcc/rust/ast/rust-item.h
+++ b/gcc/rust/ast/rust-item.h
@@ -618,6 +618,8 @@ public:
return param_name;
}
+ bool has_name () const { return param_name != nullptr; }
+
// TODO: is this better? Or is a "vis_block" better?
std::unique_ptr<Type> &get_type ()
{
@@ -4171,7 +4173,7 @@ class NamedFunctionParam
public:
/* Returns whether the named function parameter has a name (i.e. name is not
* '_'). */
- bool has_name () const { return name != "_"; }
+ bool has_name () const { return name != "_" && name != ""; }
bool has_outer_attrs () const { return !outer_attrs.empty (); }
@@ -4182,6 +4184,8 @@ public:
return param_type == nullptr && !variadic;
}
+ bool is_variadic () const { return variadic; }
+
std::string get_name () const { return name; }
location_t get_locus () { return locus; }
@@ -4294,8 +4298,6 @@ class ExternalFunctionItem : public ExternalItem
WhereClause where_clause;
std::vector<NamedFunctionParam> function_params;
- bool has_variadics;
- std::vector<Attribute> variadic_outer_attrs;
public:
// Returns whether item has generic parameters.
@@ -4314,12 +4316,10 @@ public:
bool has_visibility () const { return !visibility.is_error (); }
// Returns whether item has variadic parameters.
- bool is_variadic () const { return has_variadics; }
-
- // Returns whether item has outer attributes on its variadic parameters.
- bool has_variadic_outer_attrs () const
+ bool is_variadic () const
{
- return !variadic_outer_attrs.empty ();
+ return function_params.size () != 0
+ && function_params.back ().is_variadic ();
}
location_t get_locus () const { return locus; }
@@ -4331,17 +4331,14 @@ public:
Identifier item_name,
std::vector<std::unique_ptr<GenericParam>> generic_params,
std::unique_ptr<Type> return_type, WhereClause where_clause,
- std::vector<NamedFunctionParam> function_params, bool has_variadics,
- std::vector<Attribute> variadic_outer_attrs, Visibility vis,
+ std::vector<NamedFunctionParam> function_params, Visibility vis,
std::vector<Attribute> outer_attrs, location_t locus)
: ExternalItem (), outer_attrs (std::move (outer_attrs)),
visibility (std::move (vis)), item_name (std::move (item_name)),
locus (locus), generic_params (std::move (generic_params)),
return_type (std::move (return_type)),
where_clause (std::move (where_clause)),
- function_params (std::move (function_params)),
- has_variadics (has_variadics),
- variadic_outer_attrs (std::move (variadic_outer_attrs))
+ function_params (std::move (function_params))
{
// TODO: assert that if has variadic outer attrs, then has_variadics is
// true?
@@ -4351,10 +4348,7 @@ public:
ExternalFunctionItem (ExternalFunctionItem const &other)
: outer_attrs (other.outer_attrs), visibility (other.visibility),
item_name (other.item_name), locus (other.locus),
- where_clause (other.where_clause),
- function_params (other.function_params),
- has_variadics (other.has_variadics),
- variadic_outer_attrs (other.variadic_outer_attrs)
+ where_clause (other.where_clause), function_params (other.function_params)
{
node_id = other.node_id;
// guard to prevent null pointer dereference
@@ -4375,8 +4369,6 @@ public:
locus = other.locus;
where_clause = other.where_clause;
function_params = other.function_params;
- has_variadics = other.has_variadics;
- variadic_outer_attrs = other.variadic_outer_attrs;
node_id = other.node_id;
// guard to prevent null pointer dereference