Changeset 28f8f15 for src/Parser
- Timestamp:
- Apr 27, 2023, 3:13:24 PM (2 years ago)
- Branches:
- ADT
- Children:
- 561354f
- Parents:
- b110bcc
- Location:
- src/Parser
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Parser/DeclarationNode.cc
rb110bcc r28f8f15 279 279 } // DeclarationNode::newEnum 280 280 281 DeclarationNode * DeclarationNode::newADT( const string * name, DeclarationNode * constructors ) { 282 DeclarationNode * newnode = newEnum( name, nullptr, true, false ); 283 newnode->type->enumeration.isData = true; 284 newnode->type->enumeration.data_constructors = constructors; 285 return newnode; 286 } 287 288 281 289 DeclarationNode * DeclarationNode::newName( const string * name ) { 282 290 DeclarationNode * newnode = new DeclarationNode; … … 305 313 } // if 306 314 } // DeclarationNode::newEnumValueGeneric 315 316 DeclarationNode * DeclarationNode::newDataConstructor( const string * name ) { 317 DeclarationNode * newnode = newName(name); 318 return newnode; 319 } 307 320 308 321 DeclarationNode * DeclarationNode::newEnumInLine( const string name ) { … … 1083 1096 } 1084 1097 return nullptr; 1098 } 1099 1100 void buildDataConstructors( DeclarationNode * firstNode, std::vector<ast::ptr<ast::StructDecl>> & outputList ) { 1101 std::back_insert_iterator<std::vector<ast::ptr<ast::StructDecl>>> out( outputList ); 1102 for ( const DeclarationNode * cur = firstNode; cur; cur = strict_next( cur ) ) { 1103 // td->kind == TypeData::Symbolic 1104 assert( cur->type->kind == TypeData::Symbolic ); 1105 const std::string * name = cur->name; 1106 auto ctor = new ast::StructDecl( cur->location, 1107 std::string(*name), 1108 ast::AggregateDecl::Aggregate::Struct 1109 ); 1110 ctor->set_body(true); 1111 TypeData * td = cur->type; 1112 TypeData::Symbolic_t st = td->symbolic; 1113 DeclarationNode * params = st.params; 1114 1115 if ( params ) { 1116 buildList( params, ctor->members ); 1117 } 1118 1119 for ( std::size_t i = 0; i < ctor->members.size(); ++i ) { 1120 assert(ctor->members[i]->name == ""); 1121 ast::Decl * member = ctor->members[i].get_and_mutate(); 1122 member->name = "field_" + std::to_string(i); 1123 } 1124 *out++ = ctor; 1125 } 1126 } 1127 1128 ast::UnionDecl * buildDataUnion( ast::EnumDecl * data, const std::vector<ast::ptr<ast::StructDecl>> & typeList ) { 1129 ast::UnionDecl * out = new ast::UnionDecl( data->location, "temp_data_union" ); 1130 // size_t index = 0; 1131 if ( typeList.size() > 0 ) out->set_body( true ); 1132 size_t i = 0; 1133 for (const ast::ptr<ast::StructDecl> structDecl : typeList ) { 1134 ast::StructInstType * inst = new ast::StructInstType(structDecl); 1135 ast::ObjectDecl * instObj = new ast::ObjectDecl( 1136 structDecl->location, 1137 "option_" + std::to_string(i), 1138 inst 1139 ); 1140 i++; 1141 out->members.push_back( instObj ); 1142 1143 } 1144 return out; 1145 } 1146 1147 ast::EnumDecl * buildTag( ast::EnumDecl * data, const std::vector<ast::ptr<ast::StructDecl>> & typeList ) { 1148 ast::EnumDecl * out = new ast::EnumDecl( data->location, "temp_data_tag" ); 1149 if ( typeList.size() > 0 ) out->set_body( true ); 1150 for ( const ast::ptr<ast::StructDecl> structDecl : typeList ) { 1151 ast::EnumInstType * inst = new ast::EnumInstType( out ); 1152 assert( inst->base != nullptr ); 1153 ast::ObjectDecl * instObj = new ast::ObjectDecl( 1154 structDecl->location, 1155 structDecl->name, 1156 inst 1157 ); 1158 out->members.push_back( instObj ); 1159 } 1160 return out; 1161 } 1162 1163 ast::StructDecl * buildTaggedUnions( const ast::EnumDecl * data, const ast::EnumDecl * tags, const ast::UnionDecl * data_union ) { 1164 assert( tags->members.size() == data_union->members.size() ); 1165 ast::StructDecl * out = new ast::StructDecl( data->location, data->name ); 1166 out->kind = ast::AggregateDecl::ADT; 1167 1168 out->set_body( true ); 1169 1170 ast::EnumInstType * tag = new ast::EnumInstType( tags ); 1171 ast::ObjectDecl * tag_obj = new ast::ObjectDecl( 1172 data->location, 1173 "tag", 1174 tag 1175 ); 1176 ast::UnionInstType * value = new ast::UnionInstType( data_union ); 1177 ast::ObjectDecl * value_obj = new ast::ObjectDecl( 1178 data->location, 1179 "value", 1180 value 1181 ); 1182 1183 out->members.push_back( value_obj ); 1184 out->members.push_back( tag_obj ); 1185 return out; 1085 1186 } 1086 1187 -
src/Parser/DeclarationNode.h
rb110bcc r28f8f15 76 76 static DeclarationNode * newStaticAssert( ExpressionNode * condition, ast::Expr * message ); 77 77 78 // Experimental algebric data type 79 static DeclarationNode * newADT( const std::string * name, DeclarationNode * constructors ); 80 static DeclarationNode * newDataConstructor( const std::string * name ); 81 // static DeclarationNode * newDataConstructor( const std::string * name, DeclarationNode * typeSpecifiers ); 82 78 83 DeclarationNode(); 79 84 ~DeclarationNode(); … … 156 161 ExpressionNode * bitfieldWidth = nullptr; 157 162 std::unique_ptr<ExpressionNode> enumeratorValue; 163 158 164 bool hasEllipsis = false; 159 165 ast::Linkage::Spec linkage; … … 210 216 void buildList( DeclarationNode * firstNode, std::vector<ast::ptr<ast::DeclWithType>> & outputList ); 211 217 void buildTypeList( const DeclarationNode * firstNode, std::vector<ast::ptr<ast::Type>> & outputList ); 218 void buildDataConstructors( DeclarationNode * firstNode, std::vector<ast::ptr<ast::StructDecl>> & outputList ); 219 ast::UnionDecl * buildDataUnion( ast::EnumDecl * data, const std::vector<ast::ptr<ast::StructDecl>> & typeList ); 220 ast::EnumDecl * buildTag( ast::EnumDecl * data, const std::vector<ast::ptr<ast::StructDecl>> & typeList ); 221 ast::StructDecl * buildTaggedUnions( const ast::EnumDecl * data, const ast::EnumDecl * tags, const ast::UnionDecl * data_union ); 212 222 213 223 template<typename AstType, typename NodeType, -
src/Parser/TypeData.cc
rb110bcc r28f8f15 1260 1260 ); 1261 1261 buildList( td->enumeration.constants, ret->members ); 1262 if ( td->enumeration.data_constructors != nullptr ) { 1263 buildDataConstructors( td->enumeration.data_constructors, ret->data_constructors ); 1264 ret->data_union = buildDataUnion( ret, ret->data_constructors ); 1265 ret->tag = buildTag( ret, ret->data_constructors ); 1266 ret->tag_union = buildTaggedUnions( ret, ret->tag.get(), ret->data_union.get() ); 1267 } 1268 1269 if ( ret->data_constructors.size() > 0 ) ret->isData = true; 1262 1270 auto members = ret->members.begin(); 1263 1271 ret->hide = td->enumeration.hiding == EnumHiding::Hide ? ast::EnumDecl::EnumHiding::Hide : ast::EnumDecl::EnumHiding::Visible; -
src/Parser/TypeData.h
rb110bcc r28f8f15 25 25 struct TypeData { 26 26 enum Kind { Basic, Pointer, Reference, Array, Function, Aggregate, AggregateInst, Enum, EnumConstant, Symbolic, 27 SymbolicInst, Tuple, Basetypeof, Typeof, Vtable, Builtin, GlobalScope, Qualified, Unknown };27 SymbolicInst, Tuple, Basetypeof, Typeof, Vtable, Builtin, GlobalScope, Qualified, ADT, Ctor, Unknown }; 28 28 29 29 struct Aggregate_t { … … 58 58 bool typed; 59 59 EnumHiding hiding; 60 bool isData = false; 61 62 DeclarationNode * data_constructors = nullptr; 63 }; 64 65 struct ADT_t { 66 const std::string * name = nullptr; 67 DeclarationNode * constructors; 68 }; 69 70 struct Constructor_t { 71 const std::string * name; 72 DeclarationNode * type; // types? 60 73 }; 61 74 … … 98 111 Array_t array; 99 112 Enumeration_t enumeration; 113 ADT_t adt; 114 Constructor_t data_constructor; 115 100 116 Function_t function; 101 117 Symbolic_t symbolic; -
src/Parser/lex.ll
rb110bcc r28f8f15 353 353 with { KEYWORD_RETURN(WITH); } // CFA 354 354 zero_t { NUMERIC_RETURN(ZERO_T); } // CFA 355 _DATA_ { KEYWORD_RETURN(DATA); } // Experimental 355 356 356 357 /* identifier */ -
src/Parser/parser.yy
rb110bcc r28f8f15 339 339 %token SIZEOF TYPEOF VA_LIST VA_ARG AUTO_TYPE // GCC 340 340 %token OFFSETOF BASETYPEOF TYPEID // CFA 341 %token ENUM STRUCT UNION 341 %token ENUM STRUCT UNION DATA 342 342 %token EXCEPTION // CFA 343 343 %token GENERATOR COROUTINE MONITOR THREAD // CFA … … 453 453 %type<decl> enumerator_list enum_type enum_type_nobody 454 454 %type<init> enumerator_value_opt 455 456 %type<decl> value_list 457 %type<decl> data_constructor type_specifier_list 455 458 456 459 %type<decl> external_definition external_definition_list external_definition_list_opt … … 2441 2444 } 2442 2445 | enum_type 2446 /* | algebric_data_type */ 2443 2447 ; 2444 2448 … … 2694 2698 } 2695 2699 | enum_type_nobody 2696 ; 2700 | DATA identifier 2701 { typedefTable.makeTypedef( *$2 ); } 2702 '{' value_list '}' 2703 { 2704 $$ = DeclarationNode::newADT( $2, $5 ); 2705 } 2706 ; 2707 2708 value_list: 2709 data_constructor 2710 { 2711 $$ = $1; 2712 } 2713 /* | identifier_or_type_name '(' type_specifier ')' 2714 { 2715 $$ = DeclarationNode::newEnumValueGeneric( $1, nullptr ); 2716 } */ 2717 /* | data_constructor '|' value_list */ 2718 | value_list '|' data_constructor 2719 { 2720 { $$ = $1->appendList( $3 ); } 2721 } 2722 ; 2723 2724 data_constructor: 2725 identifier_or_type_name 2726 { 2727 typedefTable.makeTypedef( *$1 ); 2728 $$ = DeclarationNode::newTypeDecl( $1, nullptr );; 2729 } 2730 | identifier_or_type_name '(' type_specifier_list ')' 2731 { 2732 typedefTable.makeTypedef( *$1 ); 2733 $$ = DeclarationNode::newTypeDecl( $1, $3 ); 2734 } 2735 2736 type_specifier_list: 2737 type_specifier 2738 /* | type_specifier ',' type_specifier_list */ 2739 | type_specifier_list ',' type_specifier 2740 { 2741 $$ = $1->appendList($3); 2742 } 2743 ; 2744 2697 2745 2698 2746 hide_opt:
Note:
See TracChangeset
for help on using the changeset viewer.