1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921
|
// statements.h -- Go frontend statements. -*- C++ -*-
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
#ifndef GO_STATEMENTS_H
#define GO_STATEMENTS_H
#include "operator.h"
class Gogo;
class Traverse;
class Statement_inserter;
class Block;
class Function;
class Unnamed_label;
class Assignment_statement;
class Temporary_statement;
class Variable_declaration_statement;
class Expression_statement;
class Block_statement;
class Return_statement;
class Thunk_statement;
class Goto_statement;
class Goto_unnamed_statement;
class Label_statement;
class Unnamed_label_statement;
class If_statement;
class For_statement;
class For_range_statement;
class Switch_statement;
class Type_switch_statement;
class Send_statement;
class Select_statement;
class Variable;
class Named_object;
class Label;
class Translate_context;
class Expression;
class Expression_list;
class Struct_type;
class Call_expression;
class Map_index_expression;
class Receive_expression;
class Case_clauses;
class Type_case_clauses;
class Select_clauses;
class Typed_identifier_list;
class Bexpression;
class Bstatement;
class Bvariable;
class Ast_dump_context;
// This class is used to traverse assignments made by a statement
// which makes assignments.
class Traverse_assignments
{
public:
Traverse_assignments()
{ }
virtual ~Traverse_assignments()
{ }
// This is called for a variable initialization.
virtual void
initialize_variable(Named_object*) = 0;
// This is called for each assignment made by the statement. PLHS
// points to the left hand side, and PRHS points to the right hand
// side. PRHS may be NULL if there is no associated expression, as
// in the bool set by a non-blocking receive.
virtual void
assignment(Expression** plhs, Expression** prhs) = 0;
// This is called for each expression which is not passed to the
// assignment function. This is used for some of the statements
// which assign two values, for which there is no expression which
// describes the value. For ++ and -- the value is passed to both
// the assignment method and the rhs method. IS_STORED is true if
// this value is being stored directly. It is false if the value is
// computed but not stored. IS_LOCAL is true if the value is being
// stored in a local variable or this is being called by a return
// statement.
virtual void
value(Expression**, bool is_stored, bool is_local) = 0;
};
// A single statement.
class Statement
{
public:
// The types of statements.
enum Statement_classification
{
STATEMENT_ERROR,
STATEMENT_VARIABLE_DECLARATION,
STATEMENT_TEMPORARY,
STATEMENT_ASSIGNMENT,
STATEMENT_EXPRESSION,
STATEMENT_BLOCK,
STATEMENT_GO,
STATEMENT_DEFER,
STATEMENT_RETURN,
STATEMENT_BREAK_OR_CONTINUE,
STATEMENT_GOTO,
STATEMENT_GOTO_UNNAMED,
STATEMENT_LABEL,
STATEMENT_UNNAMED_LABEL,
STATEMENT_IF,
STATEMENT_CONSTANT_SWITCH,
STATEMENT_SEND,
STATEMENT_SELECT,
// These statements types are created by the parser, but they
// disappear during the lowering pass.
STATEMENT_ASSIGNMENT_OPERATION,
STATEMENT_TUPLE_ASSIGNMENT,
STATEMENT_TUPLE_MAP_ASSIGNMENT,
STATEMENT_TUPLE_RECEIVE_ASSIGNMENT,
STATEMENT_TUPLE_TYPE_GUARD_ASSIGNMENT,
STATEMENT_INCDEC,
STATEMENT_FOR,
STATEMENT_FOR_RANGE,
STATEMENT_SWITCH,
STATEMENT_TYPE_SWITCH
};
Statement(Statement_classification, Location);
virtual ~Statement();
// Make a variable declaration.
static Statement*
make_variable_declaration(Named_object*);
// Make a statement which creates a temporary variable and
// initializes it to an expression. The block is used if the
// temporary variable has to be explicitly destroyed; the variable
// must still be added to the block. References to the temporary
// variable may be constructed using make_temporary_reference.
// Either the type or the initialization expression may be NULL, but
// not both.
static Temporary_statement*
make_temporary(Type*, Expression*, Location);
// Make an assignment statement.
static Statement*
make_assignment(Expression*, Expression*, Location);
// Make an assignment operation (+=, etc.).
static Statement*
make_assignment_operation(Operator, Expression*, Expression*,
Location);
// Make a tuple assignment statement.
static Statement*
make_tuple_assignment(Expression_list*, Expression_list*, Location);
// Make an assignment from a map index to a pair of variables.
static Statement*
make_tuple_map_assignment(Expression* val, Expression* present,
Expression*, Location);
// Make an assignment from a nonblocking receive to a pair of
// variables.
static Statement*
make_tuple_receive_assignment(Expression* val, Expression* closed,
Expression* channel, Location);
// Make an assignment from a type guard to a pair of variables.
static Statement*
make_tuple_type_guard_assignment(Expression* val, Expression* ok,
Expression* expr, Type* type,
Location);
// Make an expression statement from an Expression. IS_IGNORED is
// true if the value is being explicitly ignored, as in an
// assignment to _.
static Statement*
make_statement(Expression*, bool is_ignored);
// Make a block statement from a Block. This is an embedded list of
// statements which may also include variable definitions.
static Statement*
make_block_statement(Block*, Location);
// Make an increment statement.
static Statement*
make_inc_statement(Expression*);
// Make a decrement statement.
static Statement*
make_dec_statement(Expression*);
// Make a go statement.
static Statement*
make_go_statement(Call_expression* call, Location);
// Make a defer statement.
static Statement*
make_defer_statement(Call_expression* call, Location);
// Make a return statement.
static Return_statement*
make_return_statement(Expression_list*, Location);
// Make a statement that returns the result of a call expression.
// If the call does not return any results, this just returns the
// call expression as a statement, assuming that the function will
// end immediately afterward.
static Statement*
make_return_from_call(Call_expression*, Location);
// Make a break statement.
static Statement*
make_break_statement(Unnamed_label* label, Location);
// Make a continue statement.
static Statement*
make_continue_statement(Unnamed_label* label, Location);
// Make a goto statement.
static Statement*
make_goto_statement(Label* label, Location);
// Make a goto statement to an unnamed label.
static Statement*
make_goto_unnamed_statement(Unnamed_label* label, Location);
// Make a label statement--where the label is defined.
static Statement*
make_label_statement(Label* label, Location);
// Make an unnamed label statement--where the label is defined.
static Statement*
make_unnamed_label_statement(Unnamed_label* label);
// Make an if statement.
static Statement*
make_if_statement(Expression* cond, Block* then_block, Block* else_block,
Location);
// Make a switch statement.
static Switch_statement*
make_switch_statement(Expression* switch_val, Location);
// Make a type switch statement.
static Type_switch_statement*
make_type_switch_statement(const std::string&, Expression*, Location);
// Make a send statement.
static Send_statement*
make_send_statement(Expression* channel, Expression* val, Location);
// Make a select statement.
static Select_statement*
make_select_statement(Location);
// Make a for statement.
static For_statement*
make_for_statement(Block* init, Expression* cond, Block* post,
Location location);
// Make a for statement with a range clause.
static For_range_statement*
make_for_range_statement(Expression* index_var, Expression* value_var,
Expression* range, Location);
// Return the statement classification.
Statement_classification
classification() const
{ return this->classification_; }
// Get the statement location.
Location
location() const
{ return this->location_; }
// Traverse the tree.
int
traverse(Block*, size_t* index, Traverse*);
// Traverse the contents of this statement--the expressions and
// statements which it contains.
int
traverse_contents(Traverse*);
// If this statement assigns some values, it calls a function for
// each value to which this statement assigns a value, and returns
// true. If this statement does not assign any values, it returns
// false.
bool
traverse_assignments(Traverse_assignments* tassign);
// Lower a statement. This is called immediately after parsing to
// simplify statements for further processing. It returns the same
// Statement or a new one. FUNCTION is the function containing this
// statement. BLOCK is the block containing this statement.
// INSERTER can be used to insert new statements before this one.
Statement*
lower(Gogo* gogo, Named_object* function, Block* block,
Statement_inserter* inserter)
{ return this->do_lower(gogo, function, block, inserter); }
// Flatten a statement. This is called immediately after the order of
// evaluation rules are applied to statements. It returns the same
// Statement or a new one. FUNCTION is the function containing this
// statement. BLOCK is the block containing this statement.
// INSERTER can be used to insert new statements before this one.
Statement*
flatten(Gogo* gogo, Named_object* function, Block* block,
Statement_inserter* inserter)
{ return this->do_flatten(gogo, function, block, inserter); }
// Set type information for unnamed constants.
void
determine_types();
// Check types in a statement. This simply checks that any
// expressions used by the statement have the right type.
void
check_types(Gogo* gogo)
{ this->do_check_types(gogo); }
// Return whether this is a block statement.
bool
is_block_statement() const
{ return this->classification_ == STATEMENT_BLOCK; }
// If this is an assignment statement, return it. Otherwise return
// NULL.
Assignment_statement*
assignment_statement()
{
return this->convert<Assignment_statement, STATEMENT_ASSIGNMENT>();
}
// If this is an temporary statement, return it. Otherwise return
// NULL.
Temporary_statement*
temporary_statement()
{
return this->convert<Temporary_statement, STATEMENT_TEMPORARY>();
}
// If this is a variable declaration statement, return it.
// Otherwise return NULL.
Variable_declaration_statement*
variable_declaration_statement()
{
return this->convert<Variable_declaration_statement,
STATEMENT_VARIABLE_DECLARATION>();
}
// If this is an expression statement, return it. Otherwise return
// NULL.
Expression_statement*
expression_statement()
{
return this->convert<Expression_statement, STATEMENT_EXPRESSION>();
}
// If this is an block statement, return it. Otherwise return
// NULL.
Block_statement*
block_statement()
{ return this->convert<Block_statement, STATEMENT_BLOCK>(); }
// If this is a return statement, return it. Otherwise return NULL.
Return_statement*
return_statement()
{ return this->convert<Return_statement, STATEMENT_RETURN>(); }
// If this is a thunk statement (a go or defer statement), return
// it. Otherwise return NULL.
Thunk_statement*
thunk_statement();
// If this is a goto statement, return it. Otherwise return NULL.
Goto_statement*
goto_statement()
{ return this->convert<Goto_statement, STATEMENT_GOTO>(); }
// If this is a goto_unnamed statement, return it. Otherwise return NULL.
Goto_unnamed_statement*
goto_unnamed_statement()
{ return this->convert<Goto_unnamed_statement, STATEMENT_GOTO_UNNAMED>(); }
// If this is a label statement, return it. Otherwise return NULL.
Label_statement*
label_statement()
{ return this->convert<Label_statement, STATEMENT_LABEL>(); }
// If this is an unnamed_label statement, return it. Otherwise return NULL.
Unnamed_label_statement*
unnamed_label_statement()
{ return this->convert<Unnamed_label_statement, STATEMENT_UNNAMED_LABEL>(); }
// If this is an if statement, return it. Otherwise return NULL.
If_statement*
if_statement()
{ return this->convert<If_statement, STATEMENT_IF>(); }
// If this is a for statement, return it. Otherwise return NULL.
For_statement*
for_statement()
{ return this->convert<For_statement, STATEMENT_FOR>(); }
// If this is a for statement over a range clause, return it.
// Otherwise return NULL.
For_range_statement*
for_range_statement()
{ return this->convert<For_range_statement, STATEMENT_FOR_RANGE>(); }
// If this is a switch statement, return it. Otherwise return NULL.
Switch_statement*
switch_statement()
{ return this->convert<Switch_statement, STATEMENT_SWITCH>(); }
// If this is a type switch statement, return it. Otherwise return
// NULL.
Type_switch_statement*
type_switch_statement()
{ return this->convert<Type_switch_statement, STATEMENT_TYPE_SWITCH>(); }
// If this is a send statement, return it. Otherwise return NULL.
Send_statement*
send_statement()
{ return this->convert<Send_statement, STATEMENT_SEND>(); }
// If this is a select statement, return it. Otherwise return NULL.
Select_statement*
select_statement()
{ return this->convert<Select_statement, STATEMENT_SELECT>(); }
// Return true if this statement may fall through--if after
// executing this statement we may go on to execute the following
// statement, if any.
bool
may_fall_through() const
{ return this->do_may_fall_through(); }
// Convert the statement to the backend representation.
Bstatement*
get_backend(Translate_context*);
// Dump AST representation of a statement to a dump context.
void
dump_statement(Ast_dump_context*) const;
protected:
// Implemented by child class: traverse the tree.
virtual int
do_traverse(Traverse*) = 0;
// Implemented by child class: traverse assignments. Any statement
// which includes an assignment should implement this.
virtual bool
do_traverse_assignments(Traverse_assignments*)
{ return false; }
// Implemented by the child class: lower this statement to a simpler
// one.
virtual Statement*
do_lower(Gogo*, Named_object*, Block*, Statement_inserter*)
{ return this; }
// Implemented by the child class: lower this statement to a simpler
// one.
virtual Statement*
do_flatten(Gogo*, Named_object*, Block*, Statement_inserter*)
{ return this; }
// Implemented by child class: set type information for unnamed
// constants. Any statement which includes an expression needs to
// implement this.
virtual void
do_determine_types()
{ }
// Implemented by child class: check types of expressions used in a
// statement.
virtual void
do_check_types(Gogo*)
{ }
// Implemented by child class: return true if this statement may
// fall through.
virtual bool
do_may_fall_through() const
{ return true; }
// Implemented by child class: convert to backend representation.
virtual Bstatement*
do_get_backend(Translate_context*) = 0;
// Implemented by child class: dump ast representation.
virtual void
do_dump_statement(Ast_dump_context*) const = 0;
// Traverse an expression in a statement.
int
traverse_expression(Traverse*, Expression**);
// Traverse an expression list in a statement. The Expression_list
// may be NULL.
int
traverse_expression_list(Traverse*, Expression_list*);
// Traverse a type in a statement.
int
traverse_type(Traverse*, Type*);
// For children to call when they detect that they are in error.
void
set_is_error();
// For children to call to report an error conveniently.
void
report_error(const char*);
// For children to return an error statement from lower().
static Statement*
make_error_statement(Location);
private:
// Convert to the desired statement classification, or return NULL.
// This is a controlled dynamic cast.
template<typename Statement_class, Statement_classification sc>
Statement_class*
convert()
{
return (this->classification_ == sc
? static_cast<Statement_class*>(this)
: NULL);
}
template<typename Statement_class, Statement_classification sc>
const Statement_class*
convert() const
{
return (this->classification_ == sc
? static_cast<const Statement_class*>(this)
: NULL);
}
// The statement classification.
Statement_classification classification_;
// The location in the input file of the start of this statement.
Location location_;
};
// An assignment statement.
class Assignment_statement : public Statement
{
public:
Assignment_statement(Expression* lhs, Expression* rhs,
Location location)
: Statement(STATEMENT_ASSIGNMENT, location),
lhs_(lhs), rhs_(rhs)
{ }
Expression*
lhs() const
{ return this->lhs_; }
Expression*
rhs() const
{ return this->rhs_; }
protected:
int
do_traverse(Traverse* traverse);
bool
do_traverse_assignments(Traverse_assignments*);
virtual Statement*
do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
void
do_determine_types();
void
do_check_types(Gogo*);
Statement*
do_flatten(Gogo*, Named_object*, Block*, Statement_inserter*);
Bstatement*
do_get_backend(Translate_context*);
void
do_dump_statement(Ast_dump_context*) const;
private:
// Left hand side--the lvalue.
Expression* lhs_;
// Right hand side--the rvalue.
Expression* rhs_;
};
// A statement which creates and initializes a temporary variable.
class Temporary_statement : public Statement
{
public:
Temporary_statement(Type* type, Expression* init, Location location)
: Statement(STATEMENT_TEMPORARY, location),
type_(type), init_(init), bvariable_(NULL), is_address_taken_(false)
{ }
// Return the type of the temporary variable.
Type*
type() const;
// Return the initializer if there is one.
Expression*
init() const
{ return this->init_; }
// Record that something takes the address of this temporary
// variable.
void
set_is_address_taken()
{ this->is_address_taken_ = true; }
// Return the temporary variable. This should not be called until
// after the statement itself has been converted.
Bvariable*
get_backend_variable(Translate_context*) const;
protected:
int
do_traverse(Traverse*);
bool
do_traverse_assignments(Traverse_assignments*);
void
do_determine_types();
void
do_check_types(Gogo*);
Statement*
do_flatten(Gogo*, Named_object*, Block*, Statement_inserter*);
Bstatement*
do_get_backend(Translate_context*);
void
do_dump_statement(Ast_dump_context*) const;
private:
// The type of the temporary variable.
Type* type_;
// The initial value of the temporary variable. This may be NULL.
Expression* init_;
// The backend representation of the temporary variable.
Bvariable* bvariable_;
// True if something takes the address of this temporary variable.
bool is_address_taken_;
};
// A variable declaration. This marks the point in the code where a
// variable is declared. The Variable is also attached to a Block.
class Variable_declaration_statement : public Statement
{
public:
Variable_declaration_statement(Named_object* var);
// The variable being declared.
Named_object*
var()
{ return this->var_; }
protected:
int
do_traverse(Traverse*);
bool
do_traverse_assignments(Traverse_assignments*);
Statement*
do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
Statement*
do_flatten(Gogo*, Named_object*, Block*, Statement_inserter*);
Bstatement*
do_get_backend(Translate_context*);
void
do_dump_statement(Ast_dump_context*) const;
private:
Named_object* var_;
};
// A return statement.
class Return_statement : public Statement
{
public:
Return_statement(Expression_list* vals, Location location)
: Statement(STATEMENT_RETURN, location),
vals_(vals), is_lowered_(false)
{ }
// The list of values being returned. This may be NULL.
const Expression_list*
vals() const
{ return this->vals_; }
protected:
int
do_traverse(Traverse* traverse)
{ return this->traverse_expression_list(traverse, this->vals_); }
bool
do_traverse_assignments(Traverse_assignments*);
Statement*
do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
bool
do_may_fall_through() const
{ return false; }
Bstatement*
do_get_backend(Translate_context*);
void
do_dump_statement(Ast_dump_context*) const;
private:
// Return values. This may be NULL.
Expression_list* vals_;
// True if this statement has been lowered.
bool is_lowered_;
};
// An expression statement.
class Expression_statement : public Statement
{
public:
Expression_statement(Expression* expr, bool is_ignored);
Expression*
expr()
{ return this->expr_; }
protected:
int
do_traverse(Traverse* traverse)
{ return this->traverse_expression(traverse, &this->expr_); }
void
do_determine_types();
void
do_check_types(Gogo*);
bool
do_may_fall_through() const;
Bstatement*
do_get_backend(Translate_context* context);
void
do_dump_statement(Ast_dump_context*) const;
private:
Expression* expr_;
// Whether the value of this expression is being explicitly ignored.
bool is_ignored_;
};
// A block statement--a list of statements which may include variable
// definitions.
class Block_statement : public Statement
{
public:
Block_statement(Block* block, Location location)
: Statement(STATEMENT_BLOCK, location),
block_(block), is_lowered_for_statement_(false)
{ }
void
set_is_lowered_for_statement()
{ this->is_lowered_for_statement_ = true; }
bool
is_lowered_for_statement()
{ return this->is_lowered_for_statement_; }
protected:
int
do_traverse(Traverse* traverse)
{ return this->block_->traverse(traverse); }
void
do_determine_types()
{ this->block_->determine_types(); }
bool
do_may_fall_through() const
{ return this->block_->may_fall_through(); }
Bstatement*
do_get_backend(Translate_context* context);
void
do_dump_statement(Ast_dump_context*) const;
private:
Block* block_;
// True if this block statement represents a lowered for statement.
bool is_lowered_for_statement_;
};
// A send statement.
class Send_statement : public Statement
{
public:
Send_statement(Expression* channel, Expression* val,
Location location)
: Statement(STATEMENT_SEND, location),
channel_(channel), val_(val)
{ }
Expression*
channel()
{ return this->channel_; }
Expression*
val()
{ return this->val_; }
protected:
int
do_traverse(Traverse* traverse);
void
do_determine_types();
void
do_check_types(Gogo*);
Statement*
do_flatten(Gogo*, Named_object*, Block*, Statement_inserter*);
Bstatement*
do_get_backend(Translate_context*);
void
do_dump_statement(Ast_dump_context*) const;
private:
// The channel on which to send the value.
Expression* channel_;
// The value to send.
Expression* val_;
};
// Select_clauses holds the clauses of a select statement. This is
// built by the parser.
class Select_clauses
{
public:
Select_clauses()
: clauses_()
{ }
// Add a new clause. IS_SEND is true if this is a send clause,
// false for a receive clause. For a send clause CHANNEL is the
// channel and VAL is the value to send. For a receive clause
// CHANNEL is the channel, VAL is either NULL or a Var_expression
// for the variable to set, and CLOSED is either NULL or a
// Var_expression to set to whether the channel is closed. If VAL
// is NULL, VAR may be a variable to be initialized with the
// received value, and CLOSEDVAR ma be a variable to be initialized
// with whether the channel is closed. IS_DEFAULT is true if this
// is the default clause. STATEMENTS is the list of statements to
// execute.
void
add(bool is_send, Expression* channel, Expression* val, Expression* closed,
Named_object* var, Named_object* closedvar, bool is_default,
Block* statements, Location location)
{
this->clauses_.push_back(Select_clause(is_send, channel, val, closed, var,
closedvar, is_default, statements,
location));
}
size_t
size() const
{ return this->clauses_.size(); }
// Traverse the select clauses.
int
traverse(Traverse*);
// Lower statements.
void
lower(Gogo*, Named_object*, Block*, Temporary_statement*);
// Determine types.
void
determine_types();
// Check types.
void
check_types();
// Whether the select clauses may fall through to the statement
// which follows the overall select statement.
bool
may_fall_through() const;
// Convert to the backend representation.
Bstatement*
get_backend(Translate_context*, Temporary_statement* sel,
Unnamed_label* break_label, Location);
// Dump AST representation.
void
dump_clauses(Ast_dump_context*) const;
private:
// A single clause.
class Select_clause
{
public:
Select_clause()
: channel_(NULL), val_(NULL), closed_(NULL), var_(NULL),
closedvar_(NULL), statements_(NULL), is_send_(false),
is_default_(false)
{ }
Select_clause(bool is_send, Expression* channel, Expression* val,
Expression* closed, Named_object* var,
Named_object* closedvar, bool is_default, Block* statements,
Location location)
: channel_(channel), val_(val), closed_(closed), var_(var),
closedvar_(closedvar), statements_(statements), location_(location),
is_send_(is_send), is_default_(is_default), is_lowered_(false)
{ go_assert(is_default ? channel == NULL : channel != NULL); }
// Traverse the select clause.
int
traverse(Traverse*);
// Lower statements.
void
lower(Gogo*, Named_object*, Block*, Temporary_statement*);
// Determine types.
void
determine_types();
// Check types.
void
check_types();
// Return true if this is the default clause.
bool
is_default() const
{ return this->is_default_; }
// Return the channel. This will return NULL for the default
// clause.
Expression*
channel() const
{ return this->channel_; }
// Return true for a send, false for a receive.
bool
is_send() const
{
go_assert(!this->is_default_);
return this->is_send_;
}
// Return the statements.
const Block*
statements() const
{ return this->statements_; }
// Return the location.
Location
location() const
{ return this->location_; }
// Whether this clause may fall through to the statement which
// follows the overall select statement.
bool
may_fall_through() const;
// Convert the statements to the backend representation.
Bstatement*
get_statements_backend(Translate_context*);
// Dump AST representation.
void
dump_clause(Ast_dump_context*) const;
private:
void
lower_default(Block*, Expression*);
void
lower_send(Block*, Expression*, Expression*);
void
lower_recv(Gogo*, Named_object*, Block*, Expression*, Expression*);
// The channel.
Expression* channel_;
// The value to send or the lvalue to receive into.
Expression* val_;
// The lvalue to set to whether the channel is closed on a
// receive.
Expression* closed_;
// The variable to initialize, for "case a := <-ch".
Named_object* var_;
// The variable to initialize to whether the channel is closed,
// for "case a, c := <-ch".
Named_object* closedvar_;
// The statements to execute.
Block* statements_;
// The location of this clause.
Location location_;
// Whether this is a send or a receive.
bool is_send_;
// Whether this is the default.
bool is_default_;
// Whether this has been lowered.
bool is_lowered_;
};
typedef std::vector<Select_clause> Clauses;
Clauses clauses_;
};
// A select statement.
class Select_statement : public Statement
{
public:
Select_statement(Location location)
: Statement(STATEMENT_SELECT, location),
clauses_(NULL), sel_(NULL), break_label_(NULL), is_lowered_(false)
{ }
// Add the clauses.
void
add_clauses(Select_clauses* clauses)
{
go_assert(this->clauses_ == NULL);
this->clauses_ = clauses;
}
// Return the break label for this select statement.
Unnamed_label*
break_label();
protected:
int
do_traverse(Traverse* traverse)
{ return this->clauses_->traverse(traverse); }
Statement*
do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
void
do_determine_types()
{ this->clauses_->determine_types(); }
void
do_check_types(Gogo*)
{ this->clauses_->check_types(); }
bool
do_may_fall_through() const;
Bstatement*
do_get_backend(Translate_context*);
void
do_dump_statement(Ast_dump_context*) const;
private:
// The select clauses.
Select_clauses* clauses_;
// A temporary which holds the select structure we build up at runtime.
Temporary_statement* sel_;
// The break label.
Unnamed_label* break_label_;
// Whether this statement has been lowered.
bool is_lowered_;
};
// A statement which requires a thunk: go or defer.
class Thunk_statement : public Statement
{
public:
Thunk_statement(Statement_classification, Call_expression*,
Location);
// Return the call expression.
Expression*
call() const
{ return this->call_; }
// Simplify a go or defer statement so that it only uses a single
// parameter.
bool
simplify_statement(Gogo*, Named_object*, Block*);
protected:
int
do_traverse(Traverse* traverse);
bool
do_traverse_assignments(Traverse_assignments*);
void
do_determine_types();
void
do_check_types(Gogo*);
// Return the function and argument for the call.
bool
get_fn_and_arg(Expression** pfn, Expression** parg);
private:
// Return whether this is a simple go statement.
bool
is_simple(Function_type*) const;
// Return whether the thunk function is a constant.
bool
is_constant_function() const;
// Build the struct to use for a complex case.
Struct_type*
build_struct(Function_type* fntype);
// Build the thunk.
void
build_thunk(Gogo*, const std::string&);
// Set the name to use for thunk field N.
void
thunk_field_param(int n, char* buf, size_t buflen);
// The function call to be executed in a separate thread (go) or
// later (defer).
Expression* call_;
// The type used for a struct to pass to a thunk, if this is not a
// simple call.
Struct_type* struct_type_;
};
// A go statement.
class Go_statement : public Thunk_statement
{
public:
Go_statement(Call_expression* call, Location location)
: Thunk_statement(STATEMENT_GO, call, location)
{ }
protected:
Bstatement*
do_get_backend(Translate_context*);
void
do_dump_statement(Ast_dump_context*) const;
};
// A defer statement.
class Defer_statement : public Thunk_statement
{
public:
Defer_statement(Call_expression* call, Location location)
: Thunk_statement(STATEMENT_DEFER, call, location)
{ }
protected:
Bstatement*
do_get_backend(Translate_context*);
void
do_dump_statement(Ast_dump_context*) const;
};
// A goto statement.
class Goto_statement : public Statement
{
public:
Goto_statement(Label* label, Location location)
: Statement(STATEMENT_GOTO, location),
label_(label)
{ }
// Return the label being jumped to.
Label*
label() const
{ return this->label_; }
protected:
int
do_traverse(Traverse*);
void
do_check_types(Gogo*);
bool
do_may_fall_through() const
{ return false; }
Bstatement*
do_get_backend(Translate_context*);
void
do_dump_statement(Ast_dump_context*) const;
private:
Label* label_;
};
// A goto statement to an unnamed label.
class Goto_unnamed_statement : public Statement
{
public:
Goto_unnamed_statement(Unnamed_label* label, Location location)
: Statement(STATEMENT_GOTO_UNNAMED, location),
label_(label)
{ }
Unnamed_label*
unnamed_label() const
{ return this->label_; }
protected:
int
do_traverse(Traverse*);
bool
do_may_fall_through() const
{ return false; }
Bstatement*
do_get_backend(Translate_context* context);
void
do_dump_statement(Ast_dump_context*) const;
private:
Unnamed_label* label_;
};
// A label statement.
class Label_statement : public Statement
{
public:
Label_statement(Label* label, Location location)
: Statement(STATEMENT_LABEL, location),
label_(label)
{ }
// Return the label itself.
Label*
label() const
{ return this->label_; }
protected:
int
do_traverse(Traverse*);
Bstatement*
do_get_backend(Translate_context*);
void
do_dump_statement(Ast_dump_context*) const;
private:
// The label.
Label* label_;
};
// An unnamed label statement.
class Unnamed_label_statement : public Statement
{
public:
Unnamed_label_statement(Unnamed_label* label);
protected:
int
do_traverse(Traverse*);
Bstatement*
do_get_backend(Translate_context* context);
void
do_dump_statement(Ast_dump_context*) const;
private:
// The label.
Unnamed_label* label_;
};
// An if statement.
class If_statement : public Statement
{
public:
If_statement(Expression* cond, Block* then_block, Block* else_block,
Location location)
: Statement(STATEMENT_IF, location),
cond_(cond), then_block_(then_block), else_block_(else_block)
{ }
Expression*
condition() const
{ return this->cond_; }
protected:
int
do_traverse(Traverse*);
void
do_determine_types();
void
do_check_types(Gogo*);
bool
do_may_fall_through() const;
Bstatement*
do_get_backend(Translate_context*);
void
do_dump_statement(Ast_dump_context*) const;
private:
Expression* cond_;
Block* then_block_;
Block* else_block_;
};
// A for statement.
class For_statement : public Statement
{
public:
For_statement(Block* init, Expression* cond, Block* post,
Location location)
: Statement(STATEMENT_FOR, location),
init_(init), cond_(cond), post_(post), statements_(NULL),
break_label_(NULL), continue_label_(NULL)
{ }
// Add the statements.
void
add_statements(Block* statements)
{
go_assert(this->statements_ == NULL);
this->statements_ = statements;
}
// Return the break label for this for statement.
Unnamed_label*
break_label();
// Return the continue label for this for statement.
Unnamed_label*
continue_label();
// Set the break and continue labels for this statement.
void
set_break_continue_labels(Unnamed_label* break_label,
Unnamed_label* continue_label);
protected:
int
do_traverse(Traverse*);
bool
do_traverse_assignments(Traverse_assignments*)
{ go_unreachable(); }
Statement*
do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
bool
do_may_fall_through() const;
Bstatement*
do_get_backend(Translate_context*)
{ go_unreachable(); }
void
do_dump_statement(Ast_dump_context*) const;
private:
// The initialization statements. This may be NULL.
Block* init_;
// The condition. This may be NULL.
Expression* cond_;
// The statements to run after each iteration. This may be NULL.
Block* post_;
// The statements in the loop itself.
Block* statements_;
// The break label, if needed.
Unnamed_label* break_label_;
// The continue label, if needed.
Unnamed_label* continue_label_;
};
// A for statement over a range clause.
class For_range_statement : public Statement
{
public:
For_range_statement(Expression* index_var, Expression* value_var,
Expression* range, Location location)
: Statement(STATEMENT_FOR_RANGE, location),
index_var_(index_var), value_var_(value_var), range_(range),
statements_(NULL), break_label_(NULL), continue_label_(NULL)
{ }
// Add the statements.
void
add_statements(Block* statements)
{
go_assert(this->statements_ == NULL);
this->statements_ = statements;
}
// Return the break label for this for statement.
Unnamed_label*
break_label();
// Return the continue label for this for statement.
Unnamed_label*
continue_label();
protected:
int
do_traverse(Traverse*);
bool
do_traverse_assignments(Traverse_assignments*)
{ go_unreachable(); }
Statement*
do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
Bstatement*
do_get_backend(Translate_context*)
{ go_unreachable(); }
void
do_dump_statement(Ast_dump_context*) const;
private:
Expression*
make_range_ref(Named_object*, Temporary_statement*, Location);
Call_expression*
call_builtin(Gogo*, const char* funcname, Expression* arg, Location);
void
lower_range_array(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
Temporary_statement*, Temporary_statement*,
Block**, Expression**, Block**, Block**);
void
lower_range_slice(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
Temporary_statement*, Temporary_statement*,
Block**, Expression**, Block**, Block**);
void
lower_range_string(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
Temporary_statement*, Temporary_statement*,
Block**, Expression**, Block**, Block**);
void
lower_range_map(Gogo*, Map_type*, Block*, Block*, Named_object*,
Temporary_statement*, Temporary_statement*,
Temporary_statement*, Block**, Expression**, Block**,
Block**);
void
lower_range_channel(Gogo*, Block*, Block*, Named_object*,
Temporary_statement*, Temporary_statement*,
Temporary_statement*, Block**, Expression**, Block**,
Block**);
// The variable which is set to the index value.
Expression* index_var_;
// The variable which is set to the element value. This may be
// NULL.
Expression* value_var_;
// The expression we are ranging over.
Expression* range_;
// The statements in the block.
Block* statements_;
// The break label, if needed.
Unnamed_label* break_label_;
// The continue label, if needed.
Unnamed_label* continue_label_;
};
// Class Case_clauses holds the clauses of a switch statement. This
// is built by the parser.
class Case_clauses
{
public:
Case_clauses()
: clauses_()
{ }
// Add a new clause. CASES is a list of case expressions; it may be
// NULL. IS_DEFAULT is true if this is the default case.
// STATEMENTS is a block of statements. IS_FALLTHROUGH is true if
// after the statements the case clause should fall through to the
// next clause.
void
add(Expression_list* cases, bool is_default, Block* statements,
bool is_fallthrough, Location location)
{
this->clauses_.push_back(Case_clause(cases, is_default, statements,
is_fallthrough, location));
}
// Return whether there are no clauses.
bool
empty() const
{ return this->clauses_.empty(); }
// Traverse the case clauses.
int
traverse(Traverse*);
// Lower for a nonconstant switch.
void
lower(Block*, Temporary_statement*, Unnamed_label*) const;
// Determine types of expressions. The Type parameter is the type
// of the switch value.
void
determine_types(Type*);
// Check types. The Type parameter is the type of the switch value.
bool
check_types(Type*);
// Return true if all the clauses are constant values.
bool
is_constant() const;
// Return true if these clauses may fall through to the statements
// following the switch statement.
bool
may_fall_through() const;
// Return the body of a SWITCH_EXPR when all the clauses are
// constants.
void
get_backend(Translate_context*, Unnamed_label* break_label,
std::vector<std::vector<Bexpression*> >* all_cases,
std::vector<Bstatement*>* all_statements) const;
// Dump the AST representation to a dump context.
void
dump_clauses(Ast_dump_context*) const;
private:
// For a constant switch we need to keep a record of constants we
// have already seen.
class Hash_integer_value;
class Eq_integer_value;
typedef Unordered_set_hash(Expression*, Hash_integer_value,
Eq_integer_value) Case_constants;
// One case clause.
class Case_clause
{
public:
Case_clause()
: cases_(NULL), statements_(NULL), is_default_(false),
is_fallthrough_(false), location_(Linemap::unknown_location())
{ }
Case_clause(Expression_list* cases, bool is_default, Block* statements,
bool is_fallthrough, Location location)
: cases_(cases), statements_(statements), is_default_(is_default),
is_fallthrough_(is_fallthrough), location_(location)
{ }
// Whether this clause falls through to the next clause.
bool
is_fallthrough() const
{ return this->is_fallthrough_; }
// Whether this is the default.
bool
is_default() const
{ return this->is_default_; }
// The location of this clause.
Location
location() const
{ return this->location_; }
// Traversal.
int
traverse(Traverse*);
// Lower for a nonconstant switch.
void
lower(Block*, Temporary_statement*, Unnamed_label*, Unnamed_label*) const;
// Determine types.
void
determine_types(Type*);
// Check types.
bool
check_types(Type*);
// Return true if all the case expressions are constant.
bool
is_constant() const;
// Return true if this clause may fall through to execute the
// statements following the switch statement. This is not the
// same as whether this clause falls through to the next clause.
bool
may_fall_through() const;
// Convert the case values and statements to the backend
// representation.
Bstatement*
get_backend(Translate_context*, Unnamed_label* break_label,
Case_constants*, std::vector<Bexpression*>* cases) const;
// Dump the AST representation to a dump context.
void
dump_clause(Ast_dump_context*) const;
private:
// The list of case expressions.
Expression_list* cases_;
// The statements to execute.
Block* statements_;
// Whether this is the default case.
bool is_default_;
// Whether this falls through after the statements.
bool is_fallthrough_;
// The location of this case clause.
Location location_;
};
friend class Case_clause;
// The type of the list of clauses.
typedef std::vector<Case_clause> Clauses;
// All the case clauses.
Clauses clauses_;
};
// A switch statement.
class Switch_statement : public Statement
{
public:
Switch_statement(Expression* val, Location location)
: Statement(STATEMENT_SWITCH, location),
val_(val), clauses_(NULL), break_label_(NULL)
{ }
// Add the clauses.
void
add_clauses(Case_clauses* clauses)
{
go_assert(this->clauses_ == NULL);
this->clauses_ = clauses;
}
// Return the break label for this switch statement.
Unnamed_label*
break_label();
protected:
int
do_traverse(Traverse*);
Statement*
do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
Bstatement*
do_get_backend(Translate_context*)
{ go_unreachable(); }
void
do_dump_statement(Ast_dump_context*) const;
bool
do_may_fall_through() const;
private:
// The value to switch on. This may be NULL.
Expression* val_;
// The case clauses.
Case_clauses* clauses_;
// The break label, if needed.
Unnamed_label* break_label_;
};
// Class Type_case_clauses holds the clauses of a type switch
// statement. This is built by the parser.
class Type_case_clauses
{
public:
Type_case_clauses()
: clauses_()
{ }
// Add a new clause. TYPE is the type for this clause; it may be
// NULL. IS_FALLTHROUGH is true if this falls through to the next
// clause; in this case STATEMENTS will be NULL. IS_DEFAULT is true
// if this is the default case. STATEMENTS is a block of
// statements; it may be NULL.
void
add(Type* type, bool is_fallthrough, bool is_default, Block* statements,
Location location)
{
this->clauses_.push_back(Type_case_clause(type, is_fallthrough, is_default,
statements, location));
}
// Return whether there are no clauses.
bool
empty() const
{ return this->clauses_.empty(); }
// Traverse the type case clauses.
int
traverse(Traverse*);
// Check for duplicates.
void
check_duplicates() const;
// Lower to if and goto statements.
void
lower(Type*, Block*, Temporary_statement* descriptor_temp,
Unnamed_label* break_label) const;
// Return true if these clauses may fall through to the statements
// following the switch statement.
bool
may_fall_through() const;
// Dump the AST representation to a dump context.
void
dump_clauses(Ast_dump_context*) const;
private:
// One type case clause.
class Type_case_clause
{
public:
Type_case_clause()
: type_(NULL), statements_(NULL), is_default_(false),
location_(Linemap::unknown_location())
{ }
Type_case_clause(Type* type, bool is_fallthrough, bool is_default,
Block* statements, Location location)
: type_(type), statements_(statements), is_fallthrough_(is_fallthrough),
is_default_(is_default), location_(location)
{ }
// The type.
Type*
type() const
{ return this->type_; }
// Whether this is the default.
bool
is_default() const
{ return this->is_default_; }
// The location of this type clause.
Location
location() const
{ return this->location_; }
// Traversal.
int
traverse(Traverse*);
// Lower to if and goto statements.
void
lower(Type*, Block*, Temporary_statement* descriptor_temp,
Unnamed_label* break_label, Unnamed_label** stmts_label) const;
// Return true if this clause may fall through to execute the
// statements following the switch statement. This is not the
// same as whether this clause falls through to the next clause.
bool
may_fall_through() const;
// Dump the AST representation to a dump context.
void
dump_clause(Ast_dump_context*) const;
private:
// The type for this type clause.
Type* type_;
// The statements to execute.
Block* statements_;
// Whether this falls through--this is true for "case T1, T2".
bool is_fallthrough_;
// Whether this is the default case.
bool is_default_;
// The location of this type case clause.
Location location_;
};
friend class Type_case_clause;
// The type of the list of type clauses.
typedef std::vector<Type_case_clause> Type_clauses;
// All the type case clauses.
Type_clauses clauses_;
};
// A type switch statement.
class Type_switch_statement : public Statement
{
public:
Type_switch_statement(const std::string& name, Expression* expr,
Location location)
: Statement(STATEMENT_TYPE_SWITCH, location),
name_(name), expr_(expr), clauses_(NULL), break_label_(NULL)
{ }
// Add the clauses.
void
add_clauses(Type_case_clauses* clauses)
{
go_assert(this->clauses_ == NULL);
this->clauses_ = clauses;
}
// Return the break label for this type switch statement.
Unnamed_label*
break_label();
protected:
int
do_traverse(Traverse*);
Statement*
do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
Bstatement*
do_get_backend(Translate_context*)
{ go_unreachable(); }
void
do_dump_statement(Ast_dump_context*) const;
bool
do_may_fall_through() const;
private:
// The name of the variable declared in the type switch guard. Empty if there
// is no variable declared.
std::string name_;
// The expression we are switching on if there is no variable.
Expression* expr_;
// The type case clauses.
Type_case_clauses* clauses_;
// The break label, if needed.
Unnamed_label* break_label_;
};
#endif // !defined(GO_STATEMENTS_H)
|