diff options
Diffstat (limited to 'gcc/rust/ast/rust-ast-full-test.cc')
-rw-r--r-- | gcc/rust/ast/rust-ast-full-test.cc | 504 |
1 files changed, 285 insertions, 219 deletions
diff --git a/gcc/rust/ast/rust-ast-full-test.cc b/gcc/rust/ast/rust-ast-full-test.cc index 0cdac5f..030b0b3 100644 --- a/gcc/rust/ast/rust-ast-full-test.cc +++ b/gcc/rust/ast/rust-ast-full-test.cc @@ -171,13 +171,33 @@ Attribute::as_string () const { std::string path_str = path.as_string (); if (attr_input == nullptr) - { - return path_str; - } + return path_str; else - { - return path_str + attr_input->as_string (); - } + return path_str + attr_input->as_string (); +} + +// Copy constructor must deep copy attr_input as unique pointer +Attribute::Attribute (Attribute const &other) + : path (other.path), locus (other.locus) +{ + // guard to protect from null pointer dereference + if (other.attr_input != nullptr) + attr_input = other.attr_input->clone_attr_input (); +} + +// overload assignment operator to use custom clone method +Attribute & +Attribute::operator= (Attribute const &other) +{ + path = other.path; + locus = other.locus; + // guard to protect from null pointer dereference + if (other.attr_input != nullptr) + attr_input = other.attr_input->clone_attr_input (); + else + attr_input = nullptr; + + return *this; } std::string @@ -307,7 +327,13 @@ std::string VisItem::as_string () const { // FIXME: can't do formatting on string to make identation occur. - std::string str = Item::as_string (); + std::string str; + + if (!outer_attrs.empty ()) + { + for (const auto &attr : outer_attrs) + str += attr.as_string () + "\n"; + } if (has_visibility ()) { @@ -318,7 +344,7 @@ VisItem::as_string () const } // Creates a string that reflects the outer attributes stored. -std::string +/*std::string Item::as_string () const { std::string str; @@ -332,7 +358,7 @@ Item::as_string () const } return str; -} +}*/ std::string Module::as_string () const @@ -654,33 +680,23 @@ Method::as_string () const else { for (const auto ¶m : function_params) - { - str += "\n " + param.as_string (); - } + str += "\n " + param.as_string (); } str += "\n Return type: "; if (has_return_type ()) - { - str += return_type->as_string (); - } + str += return_type->as_string (); else - { - str += "none (void)"; - } + str += "none (void)"; str += "\n Where clause: "; if (has_where_clause ()) - { - str += where_clause.as_string (); - } + str += where_clause.as_string (); else - { - str += "none"; - } + str += "none"; str += "\n Block expr (body): \n "; - str += expr->as_string (); + str += function_body->as_string (); return str; } @@ -1089,7 +1105,7 @@ Function::as_string () const if ("" != qstr) str += qstr + " "; - if (has_function_return_type ()) + if (has_return_type ()) { // DEBUG: null pointer check if (return_type == nullptr) @@ -1154,9 +1170,7 @@ Function::as_string () const } if (has_where_clause ()) - { - str += " where " + where_clause.as_string (); - } + str += " where " + where_clause.as_string (); str += "\n"; @@ -1187,9 +1201,7 @@ WhereClause::as_string () const else { for (const auto &item : where_clause_items) - { - str += "\n " + item->as_string (); - } + str += "\n " + item->as_string (); } return str; @@ -1214,9 +1226,7 @@ BlockExpr::as_string () const /* note that this does not print them with "inner attribute" syntax - * just the body */ for (const auto &attr : inner_attrs) - { - str += "\n" + indent_spaces (stay) + attr.as_string (); - } + str += "\n" + indent_spaces (stay) + attr.as_string (); } // statements @@ -1246,13 +1256,9 @@ BlockExpr::as_string () const // final expression str += "\n" + indent_spaces (stay) + "final expression: "; if (expr == nullptr) - { - str += "none"; - } + str += "none"; else - { - str += "\n" + expr->as_string (); - } + str += "\n" + expr->as_string (); str += "\n" + indent_spaces (out) + "}"; return str; @@ -1264,9 +1270,7 @@ TraitImpl::as_string () const std::string str = VisItem::as_string (); if (has_unsafe) - { - str += "unsafe "; - } + str += "unsafe "; str += "impl "; @@ -1279,20 +1283,14 @@ TraitImpl::as_string () const else { for (const auto ¶m : generic_params) - { - str += "\n " + param->as_string (); - } + str += "\n " + param->as_string (); } str += "\n Has exclam: "; if (has_exclam) - { - str += "true"; - } + str += "true"; else - { - str += "false"; - } + str += "false"; str += "\n TypePath (to trait): " + trait_path.as_string (); @@ -1300,13 +1298,9 @@ TraitImpl::as_string () const str += "\n Where clause: "; if (!has_where_clause ()) - { - str += "none"; - } + str += "none"; else - { - str += where_clause.as_string (); - } + str += where_clause.as_string (); // inner attributes str += "\n inner attributes: "; @@ -1319,9 +1313,7 @@ TraitImpl::as_string () const /* note that this does not print them with "inner attribute" syntax - * just the body */ for (const auto &attr : inner_attrs) - { - str += "\n " + attr.as_string (); - } + str += "\n " + attr.as_string (); } str += "\n trait impl items: "; @@ -1332,9 +1324,7 @@ TraitImpl::as_string () const else { for (const auto &item : impl_items) - { - str += "\n " + item->as_string (); - } + str += "\n " + item->as_string (); } return str; @@ -1384,8 +1374,14 @@ TypeAlias::as_string () const std::string MacroInvocationSemi::as_string () const { + std::string str; + // get outer attrs - std::string str = MacroItem::as_string (); + if (!outer_attrs.empty ()) + { + for (const auto &attr : outer_attrs) + str += attr.as_string () + "\n"; + } str += "\n" + path.as_string () + "!"; @@ -1444,9 +1440,7 @@ ExternBlock::as_string () const /* note that this does not print them with "inner attribute" syntax - * just the body */ for (const auto &attr : inner_attrs) - { - str += "\n " + attr.as_string (); - } + str += "\n " + attr.as_string (); } str += "\n external items: "; @@ -1457,9 +1451,7 @@ ExternBlock::as_string () const else { for (const auto &item : extern_items) - { - str += "\n " + item->as_string (); - } + str += "\n " + item->as_string (); } return str; @@ -1482,7 +1474,16 @@ MacroRule::as_string () const std::string MacroRulesDefinition::as_string () const { - std::string str ("macro_rules!"); + std::string str; + + // get outer attrs + if (!outer_attrs.empty ()) + { + for (const auto &attr : outer_attrs) + str += attr.as_string () + "\n"; + } + + str += "macro_rules!"; str += rule_name; @@ -1494,9 +1495,7 @@ MacroRulesDefinition::as_string () const else { for (const auto &rule : rules) - { - str += "\n " + rule.as_string (); - } + str += "\n " + rule.as_string (); } str += "\n Delim type: "; @@ -1531,9 +1530,7 @@ PathInExpression::as_string () const std::string str; if (has_opening_scope_resolution) - { - str = "::"; - } + str = "::"; return str + PathPattern::as_string (); } @@ -1564,9 +1561,7 @@ ClosureParam::as_string () const std::string str (pattern->as_string ()); if (has_type_given ()) - { - str += " : " + type->as_string (); - } + str += " : " + type->as_string (); return str; } @@ -1576,13 +1571,9 @@ ClosureExpr::as_string () const { std::string str ("ClosureExpr:\n Has move: "); if (has_move) - { - str += "true"; - } + str += "true"; else - { - str += "false"; - } + str += "false"; str += "\n Params: "; if (params.empty ()) @@ -1592,9 +1583,7 @@ ClosureExpr::as_string () const else { for (const auto ¶m : params) - { - str += "\n " + param.as_string (); - } + str += "\n " + param.as_string (); } return str; @@ -1618,9 +1607,7 @@ PathPattern::as_string () const std::string str; for (const auto &segment : segments) - { - str += segment.as_string () + "::"; - } + str += segment.as_string () + "::"; // basically a hack - remove last two characters of string (remove final ::) str.erase (str.length () - 2); @@ -1635,9 +1622,7 @@ QualifiedPathType::as_string () const str += type_to_invoke_on->as_string (); if (has_as_clause ()) - { - str += " as " + trait_path.as_string (); - } + str += " as " + trait_path.as_string (); return str + ">"; } @@ -1654,14 +1639,10 @@ BorrowExpr::as_string () const std::string str ("&"); if (double_borrow) - { - str += "&"; - } + str += "&"; if (is_mut) - { - str += "mut "; - } + str += "mut "; str += main_or_left_expr->as_string (); @@ -1673,10 +1654,8 @@ ReturnExpr::as_string () const { std::string str ("return "); - if (has_return_expr ()) - { - str += return_expr->as_string (); - } + if (has_returned_expr ()) + str += return_expr->as_string (); return str; } @@ -1697,9 +1676,7 @@ GroupedExpr::as_string () const /* note that this does not print them with "inner attribute" syntax - * just the body */ for (const auto &attr : inner_attrs) - { - str += "\n " + attr.as_string (); - } + str += "\n " + attr.as_string (); } str += "\n Expr in parens: " + expr_in_parens->as_string (); @@ -1719,9 +1696,7 @@ ContinueExpr::as_string () const std::string str ("continue "); if (has_label ()) - { - str += label.as_string (); - } + str += label.as_string (); return str; } @@ -1856,9 +1831,7 @@ MethodCallExpr::as_string () const for (const auto ¶m : params) { if (param == nullptr) - { - return "ERROR_MARK_STRING - method call expr param is null"; - } + return "ERROR_MARK_STRING - method call expr param is null"; str += "\n " + param->as_string (); } @@ -1997,9 +1970,7 @@ IfLetExpr::as_string () const else { for (const auto &pattern : match_arm_patterns) - { - str += "\n " + pattern->as_string (); - } + str += "\n " + pattern->as_string (); } str += "\n Scrutinee expr: " + value->as_string (); @@ -2188,9 +2159,7 @@ CallExpr::as_string () const for (const auto ¶m : params) { if (param == nullptr) - { - return "ERROR_MARK_STRING - call expr param is null"; - } + return "ERROR_MARK_STRING - call expr param is null"; str += "\n " + param->as_string (); } @@ -2206,13 +2175,9 @@ WhileLoopExpr::as_string () const str += "\n Label: "; if (!has_loop_label ()) - { - str += "none"; - } + str += "none"; else - { - str += loop_label.as_string (); - } + str += loop_label.as_string (); str += "\n Conditional expr: " + condition->as_string (); @@ -2228,13 +2193,9 @@ WhileLetLoopExpr::as_string () const str += "\n Label: "; if (!has_loop_label ()) - { - str += "none"; - } + str += "none"; else - { - str += loop_label.as_string (); - } + str += loop_label.as_string (); str += "\n Match arm patterns: "; if (match_arm_patterns.empty ()) @@ -2244,12 +2205,10 @@ WhileLetLoopExpr::as_string () const else { for (const auto &pattern : match_arm_patterns) - { - str += "\n " + pattern->as_string (); - } + str += "\n " + pattern->as_string (); } - str += "\n Scrutinee expr: " + condition->as_string (); + str += "\n Scrutinee expr: " + scrutinee->as_string (); str += "\n Loop block: " + loop_block->as_string (); @@ -2263,13 +2222,9 @@ LoopExpr::as_string () const str += "\n Label: "; if (!has_loop_label ()) - { - str += "none"; - } + str += "none"; else - { - str += loop_label.as_string (); - } + str += loop_label.as_string (); str += "\n Loop block: " + loop_block->as_string (); @@ -2292,20 +2247,14 @@ ArrayExpr::as_string () const /* note that this does not print them with "inner attribute" syntax - * just the body */ for (const auto &attr : inner_attrs) - { - str += "\n " + attr.as_string (); - } + str += "\n " + attr.as_string (); } str += "\n Array elems: "; if (!has_array_elems ()) - { - str += "none"; - } + str += "none"; else - { - str += internal_elements->as_string (); - } + str += internal_elements->as_string (); return str; } @@ -2322,14 +2271,10 @@ BreakExpr::as_string () const std::string str ("break "); if (has_label ()) - { - str += label.as_string () + " "; - } + str += label.as_string () + " "; if (has_break_expr ()) - { - str += break_expr->as_string (); - } + str += break_expr->as_string (); return str; } @@ -2354,9 +2299,7 @@ MatchArm::as_string () const /* note that this does not print them with "outer attribute" syntax - * just the body */ for (const auto &attr : outer_attrs) - { - str += "\n " + attr.as_string (); - } + str += "\n " + attr.as_string (); } str += "\nPatterns: "; @@ -2367,20 +2310,14 @@ MatchArm::as_string () const else { for (const auto &pattern : match_arm_patterns) - { - str += "\n " + pattern->as_string (); - } + str += "\n " + pattern->as_string (); } str += "\nGuard expr: "; if (!has_match_arm_guard ()) - { - str += "none"; - } + str += "none"; else - { - str += guard_expr->as_string (); - } + str += guard_expr->as_string (); return str; } @@ -3694,9 +3631,7 @@ StructExprTuple::as_string () const /* note that this does not print them with "inner attribute" syntax - * just the body */ for (const auto &attr : inner_attrs) - { - str += "\n" + indent_spaces (stay) + attr.as_string (); - } + str += "\n" + indent_spaces (stay) + attr.as_string (); } indent_spaces (out); indent_spaces (out); @@ -3722,9 +3657,7 @@ StructExprStruct::as_string () const /* note that this does not print them with "inner attribute" syntax - * just the body */ for (const auto &attr : inner_attrs) - { - str += "\n " + attr.as_string (); - } + str += "\n " + attr.as_string (); } return str; @@ -3734,13 +3667,9 @@ std::string StructBase::as_string () const { if (base_struct != nullptr) - { - return base_struct->as_string (); - } + return base_struct->as_string (); else - { - return "ERROR_MARK_STRING - invalid struct base had as string applied"; - } + return "ERROR_MARK_STRING - invalid struct base had as string applied"; } std::string @@ -3775,25 +3704,59 @@ StructExprStructFields::as_string () const else { for (const auto &field : fields) - { - str += "\n " + field->as_string (); - } + str += "\n " + field->as_string (); } str += "\n Struct base: "; if (!has_struct_base ()) + str += "none"; + else + str += struct_base.as_string (); + + return str; +} + +std::string +EnumExprStruct::as_string () const +{ + std::string str ("StructExprStruct (or subclass): "); + + str += "\n Path: " + get_enum_variant_path ().as_string (); + + str += "\n Fields: "; + if (fields.empty ()) { str += "none"; } else { - str += struct_base.as_string (); + for (const auto &field : fields) + str += "\n " + field->as_string (); } return str; } std::string +EnumExprFieldWithVal::as_string () const +{ + // used to get value string + return value->as_string (); +} + +std::string +EnumExprFieldIdentifierValue::as_string () const +{ + return field_name + " : " + EnumExprFieldWithVal::as_string (); +} + +std::string +EnumExprFieldIndexValue::as_string () const +{ + return std::to_string (index) + " : " + EnumExprFieldWithVal::as_string (); +} + +std::string EnumItem::as_string () const { // outer attributes @@ -3807,9 +3770,7 @@ EnumItem::as_string () const /* note that this does not print them with "outer attribute" syntax - * just the body */ for (const auto &attr : outer_attrs) - { - str += "\n " + attr.as_string (); - } + str += "\n " + attr.as_string (); } str += "\n" + variant_name; @@ -3942,6 +3903,7 @@ EnumItemDiscriminant::as_string () const return str; } +#if 0 std::string ExternalItem::as_string () const { @@ -3956,9 +3918,7 @@ ExternalItem::as_string () const /* note that this does not print them with "outer attribute" syntax - * just the body */ for (const auto &attr : outer_attrs) - { str += "\n " + attr.as_string (); - } } // start visibility on new line and with a space @@ -3966,21 +3926,35 @@ ExternalItem::as_string () const return str; } +#endif std::string ExternalStaticItem::as_string () const { - std::string str = ExternalItem::as_string (); + // outer attributes + std::string str = "outer attributes: "; + if (outer_attrs.empty ()) + { + str += "none"; + } + else + { + /* note that this does not print them with "outer attribute" syntax - + * just the body */ + for (const auto &attr : outer_attrs) + str += "\n " + attr.as_string (); + } + + // start visibility on new line and with a space + str += "\n" + visibility.as_string () + " "; str += "static "; if (has_mut) - { - str += "mut "; - } + str += "mut "; // add name - str += get_item_name (); + str += item_name; // add type on new line str += "\n Type: " + item_type->as_string (); @@ -3991,12 +3965,27 @@ ExternalStaticItem::as_string () const std::string ExternalFunctionItem::as_string () const { - std::string str = ExternalItem::as_string (); + // outer attributes + std::string str = "outer attributes: "; + if (outer_attrs.empty ()) + { + str += "none"; + } + else + { + /* note that this does not print them with "outer attribute" syntax - + * just the body */ + for (const auto &attr : outer_attrs) + str += "\n " + attr.as_string (); + } + + // start visibility on new line and with a space + str += "\n" + visibility.as_string () + " "; str += "fn "; // add name - str += get_item_name (); + str += item_name; // generic params str += "\n Generic params: "; @@ -4024,19 +4013,28 @@ ExternalFunctionItem::as_string () const // function params str += "\n Function params: "; - if (function_params.empty ()) + if (function_params.empty () && !has_variadics) { str += "none"; } else { for (const auto ¶m : function_params) - { - str += "\n " + param.as_string (); - } + str += "\n " + param.as_string (); + if (has_variadics) { - str += "\n .. (variadic)"; + 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)"; } } @@ -4046,13 +4044,9 @@ ExternalFunctionItem::as_string () const // where clause str += "\n Where clause: "; if (has_where_clause ()) - { - str += where_clause.as_string (); - } + str += where_clause.as_string (); else - { - str += "none"; - } + str += "none"; return str; } @@ -4060,7 +4054,19 @@ ExternalFunctionItem::as_string () const std::string NamedFunctionParam::as_string () const { - std::string str = name; + std::string str = "outer attributes: "; + + if (!has_outer_attrs ()) + { + str += "none"; + } + else + { + for (const auto &attr : outer_attrs) + str += "\n " + attr.as_string (); + } + + str += "\n" + name; str += "\n Type: " + param_type->as_string (); @@ -4730,7 +4736,8 @@ MacroParser::parse_meta_item_inner () std::vector<MetaNameValueStr> meta_name_value_str_items; for (const auto &item : meta_items) { - std::unique_ptr<MetaNameValueStr> converted_item = item->to_meta_name_value_str (); + std::unique_ptr<MetaNameValueStr> converted_item + = item->to_meta_name_value_str (); if (converted_item == nullptr) { meta_name_value_str_items.clear (); @@ -5048,18 +5055,21 @@ MacroParser::parse_meta_item_lit () bool AttrInputMetaItemContainer::check_cfg_predicate (const Session &session) const { - /* cfg value of container is purely based on cfg of each inner item - all - * must be true */ - for (const auto &inner_item : items) + /* NOTE: assuming that only first item must be true - cfg should only have one + * item, and cfg_attr only has first item as predicate. TODO ensure that this + * is correct. */ + if (items.empty ()) + return false; + + return items[0]->check_cfg_predicate (session); + + /*for (const auto &inner_item : items) { if (!inner_item->check_cfg_predicate (session)) return false; } - /* TODO: as far as I can tell, there should only be a single element to - * check here, so ensure there is only a single element in items too? */ - - return true; + return true;*/ } bool @@ -5346,6 +5356,62 @@ MetaItemPathLit::to_attribute () const new AttrInputLiteral (lit))); } +std::vector<Attribute> +AttrInputMetaItemContainer::separate_cfg_attrs () const +{ + rust_assert (!items.empty ()); + + if (items.size () == 1) + return {}; + + std::vector<Attribute> attrs; + attrs.reserve (items.size () - 1); + + for (auto it = items.begin () + 1; it != items.end (); ++it) + { + Attribute attr = (*it)->to_attribute (); + if (attr.is_empty ()) + { + // TODO should this be an error that causes us to chuck out + // everything? + continue; + } + attrs.push_back (std::move (attr)); + } + + attrs.shrink_to_fit (); + return attrs; +} + +bool +Attribute::check_cfg_predicate (const Session &session) +{ + /* assume that cfg predicate actually can exist, i.e. attribute has cfg or + * cfg_attr path */ + if (!has_attr_input () + || (path.as_string () != "cfg" && path.as_string () != "cfg_attr")) + return false; + + // TODO: maybe replace with storing a "has been parsed" variable? + parse_attr_to_meta_item (); + // can't be const because of this anyway + + return attr_input->check_cfg_predicate (session); +} + +std::vector<Attribute> +Attribute::separate_cfg_attrs () +{ + if (!has_attr_input () || path.as_string () != "cfg_attr") + return {}; + + // TODO: maybe replace with storing a "has been parsed" variable? + parse_attr_to_meta_item (); + // can't be const because of this anyway + + return attr_input->separate_cfg_attrs (); +} + /* Visitor implementations - these are short but inlining can't happen anyway * due to virtual functions and I didn't want to make the ast header includes * any longer than they already are. */ |