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 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999
|
/******************************************************************************
* Copyright (c) 2000-2016 Ericsson Telecom AB
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
*
* Baji, Laszlo
* Balasko, Jeno
* Baranyi, Botond
* Beres, Szabolcs
* Cserveni, Akos
* Czerman, Oliver
* Delic, Adam
* Feher, Csaba
* Forstner, Matyas
* Gecse, Roland
* Kovacs, Ferenc
* Raduly, Csaba
* Szabados, Kristof
* Szabo, Bence Janos
* Szabo, Janos Zoltan – initial implementation
* Szalai, Gabor
* Zalanyi, Balazs Andor
* Pandi, Krisztian
*
******************************************************************************/
#include <set>
#include <string>
#include <sstream>
#include "../common/dbgnew.hh"
#include "AST.hh"
#include "asn1/AST_asn1.hh"
#include "Identifier.hh"
#include "Type.hh"
#include "TypeCompat.hh"
#include "Value.hh"
#include "ustring.hh"
#include "main.hh"
#include "asn1/Object0.hh"
#include "PredefFunc.hh"
#include "../common/version.h"
#include "CodeGenHelper.hh"
#include <limits.h>
#include "ttcn3/profiler.h"
reffer::reffer(const char*) {}
namespace Common {
// =================================
// ===== Modules
// =================================
vector<Modules::type_enc_t> Modules::delayed_type_enc_v;
Modules::Modules()
: Node(), mods_v(), mods_m()
{
set_fullname(string('@'));
}
Modules::~Modules()
{
for(size_t i = 0; i < mods_v.size(); i++) delete mods_v[i];
mods_v.clear();
mods_m.clear();
}
Modules *Modules::clone() const
{
FATAL_ERROR("Modules::clone()");
return 0;
}
void Modules::add_mod(Module *p_mod)
{
if (!p_mod) FATAL_ERROR("NULL parameter: Common::Modules::add_mod()");
p_mod->set_fullname("@"+p_mod->get_modid().get_dispname());
p_mod->set_scope_name(p_mod->get_modid().get_dispname());
mods_v.add(p_mod);
}
bool Modules::has_mod_withId(const Identifier& p_modid)
{
return mods_m.has_key(p_modid.get_name());
}
Module* Modules::get_mod_byId(const Identifier& p_modid)
{
const string& name = p_modid.get_name();
return mods_m.has_key(name)?mods_m[name]:0;
}
Assignment* Modules::get_ass_bySRef(Ref_simple *p_ref)
{
if(!p_ref)
FATAL_ERROR("NULL parameter: Common::Modules::get_ass_bySRef()");
const Identifier *modid=p_ref->get_modid();
if(modid) {
if(has_mod_withId(*modid))
return get_mod_byId(*modid)->get_ass_bySRef(p_ref);
else {
p_ref->error("There is no module with identifier `%s'",
modid->get_dispname().c_str());
return 0;
}
}
else {
p_ref->error("`%s' entity not found in global scope",
p_ref->get_dispname().c_str());
return 0;
}
}
void Modules::chk_uniq()
{
for(size_t i = 0; i < mods_v.size(); i++) {
Module *m = mods_v[i];
const Identifier& id = m->get_modid();
const string& name = id.get_name();
if (mods_m.has_key(name)) {
Module *m2 = mods_m[name];
m->error("A module with identifier `%s' already exists",
id.get_dispname().c_str());
m2->error("This is the first module with the same name");
if (m->get_moduletype() == m2->get_moduletype() &&
!strcmp(m->get_filename(), m2->get_filename())) {
// the same file was given twice -> drop the entire module
delete m;
mods_v.replace(i, 1);
i--;
}
} else mods_m.add(name, m);
}
}
void Modules::chk()
{
// first check the uniqueness of module names
chk_uniq();
// check the import chains
size_t nof_mods = mods_v.size();
for (size_t i = 0; i < nof_mods; i++) {
Module *m = mods_v[i];
ReferenceChain refch(m, "While checking import chains");
vector<Common::Module> modules;
m->chk_imp(refch, modules);
modules.clear();
//clear the reference chain, get a fresh start
refch.reset();
}
// check the modules
Module::module_set_t checked_modules;
if (nof_top_level_pdus > 0) {
chk_top_level_pdus();
// do not check ASN.1 modules, but assume they are already checked
for (size_t i = 0; i < nof_mods; i++) {
Module *module = mods_v[i];
if (module->get_moduletype() == Module::MOD_ASN)
checked_modules.add(module, 0);
}
for (size_t i = 0; i < nof_mods; i++) {
Module *module = mods_v[i];
if (module->get_moduletype() != Module::MOD_ASN)
module->chk_recursive(checked_modules);
}
} else {
// walk through all modules in bottom-up order
for (size_t i = 0; i < nof_mods; i++)
mods_v[i]->chk_recursive(checked_modules);
}
checked_modules.clear();
// run delayed Type::chk_coding() calls
if (!delayed_type_enc_v.empty()) {
for (size_t i = 0; i < delayed_type_enc_v.size(); ++i) {
delayed_type_enc_v[i]->t->chk_coding(delayed_type_enc_v[i]->enc, true);
delete delayed_type_enc_v[i];
}
delayed_type_enc_v.clear();
}
}
void Modules::chk_top_level_pdus()
{
Location loc("<command line>");
for(size_t i=0; i<nof_top_level_pdus; i++) {
string pduname(top_level_pdu[i]);
size_t dotpos=pduname.find('.');
if(dotpos>=pduname.size()) {
loc.error("While searching top-level pdu `%s': "
"Please use the `modulename.identifier' format",
pduname.c_str());
continue;
}
Module *module=0;
Identifier *pdu_id=0;
{ // searching the module
const string& pduname_mod = pduname.substr(0, dotpos);
const string& pduname_id = pduname.substr(dotpos + 1);
{ // ASN
Identifier modid(Identifier::ID_ASN, pduname_mod, true);
module = get_mod_byId(modid);
}
if (module && module->get_moduletype() == Module::MOD_ASN) {
pdu_id = new Identifier(Identifier::ID_ASN, pduname_id, true);
goto mod_ok;
}
{ // TTCN
Identifier modid(Identifier::ID_TTCN, pduname_mod, true);
module = get_mod_byId(modid);
}
if (module && module->get_moduletype() == Module::MOD_TTCN) {
pdu_id = new Identifier(Identifier::ID_TTCN, pduname_id, true);
goto mod_ok;
}
{ // C++
Identifier modid(Identifier::ID_NAME, pduname_mod, true);
module = get_mod_byId(modid);
}
if(module) {
pdu_id = new Identifier(Identifier::ID_NAME, pduname_id, true);
goto mod_ok;
}
// error - no such module
loc.error("While searching top-level pdu `%s': "
"No module with name `%s'",
pduname.c_str(), pduname_mod.c_str());
continue;
}
mod_ok:
Assignments *asss=module->get_asss();
if(asss->has_local_ass_withId(*pdu_id)) {
Assignment *ass=asss->get_local_ass_byId(*pdu_id);
ass->chk();
}
else {
loc.error("While searching top-level pdu `%s': "
"No assignment with identifier `%s'",
pduname.c_str(), pdu_id->get_dispname().c_str());
}
delete pdu_id;
} // for top-level pdus
}
void Modules::write_checksums()
{
fputs("Module name Language MD5 checksum Version\n"
"---------------------------------------------------------------------------\n", stderr);
size_t nof_mods = mods_v.size();
for (size_t i = 0; i < nof_mods; i++) {
mods_v[i]->write_checksum();
}
}
void Modules::generate_code(CodeGenHelper& cgh)
{
Common::Module::rename_default_namespace(); // if needed
/*
The White Rabbit put on his spectacles.
"Where shall I begin, please your Majesty ?" he asked.
"Begin at the beginning,", the King said, very gravely, "and go on
till you come to the end: then stop."
-- Lewis Carroll
*/
for (size_t i = 0; i < mods_v.size(); i++) {
mods_v[i]->generate_code(cgh);
if (tcov_file_name && in_tcov_files(mods_v[i]->get_filename())) {
Free(effective_module_lines);
Free(effective_module_functions);
effective_module_lines = effective_module_functions = NULL;
}
}
cgh.write_output();
}
void Modules::dump(unsigned level) const
{
DEBUG(level, "Modules (%lu pcs.)", (unsigned long) mods_v.size());
for(size_t i = 0; i < mods_v.size(); i++) mods_v[i]->dump(level);
}
std::set<ModuleVersion> Modules::getVersionsWithProductNumber() const {
std::set<ModuleVersion> versions;
for (size_t i = 0; i < mods_v.size(); ++i) {
const ModuleVersion version = mods_v[i]->getVersion();
if (version.hasProductNumber()) {
versions.insert(version);
}
}
return versions;
}
void Modules::generate_json_schema(JSON_Tokenizer& json, map<Type*, JSON_Tokenizer>& json_refs)
{
for(size_t i = 0; i < mods_v.size(); ++i) {
mods_v[i]->generate_json_schema(json, json_refs);
}
}
void Modules::delay_type_encode_check(Type* p_type, bool p_encode)
{
type_enc_t* elem = new type_enc_t;
elem->t = p_type;
elem->enc = p_encode;
delayed_type_enc_v.add(elem);
}
// =================================
// ===== Module
// =================================
ModuleVersion Module::getVersion() const {
return ModuleVersion(product_number, suffix, release, patch, build, extra);
}
void Module::generate_literals(output_struct *target)
{
char *src = NULL;
char *hdr = NULL;
generate_bs_literals(src, hdr); // implementations follow directly below
generate_bp_literals(src, hdr);
generate_hs_literals(src, hdr);
generate_hp_literals(src, hdr);
generate_os_literals(src, hdr);
generate_op_literals(src, hdr);
generate_cs_literals(src, hdr);
generate_us_literals(src, hdr);
generate_oid_literals(src, hdr);
generate_pp_literals(src, hdr);
generate_mp_literals(src, hdr);
target->source.string_literals =
mputstr(target->source.string_literals, src);
if (CodeGenHelper::GetInstance().get_split_mode() != CodeGenHelper::SPLIT_NONE) {
target->header.global_vars = mputstr(target->header.global_vars, hdr);
}
Free(src);
Free(hdr);
}
void Module::generate_bs_literals(char *&src, char *&hdr)
{
if (bs_literals.size() == 0) return;
// indicates whether we have found at least one non-empty bitstring
bool is_nonempty = false;
bool splitting =
CodeGenHelper::GetInstance().get_split_mode() != CodeGenHelper::SPLIT_NONE;
for (size_t i = 0; i < bs_literals.size(); i++) {
const string& str = bs_literals.get_nth_key(i);
size_t bits = str.size();
if (bits == 0) continue;
if (is_nonempty) src = mputstr(src, ",\n");
else {
src = mputprintf(src, "%sconst unsigned char ", split_to_slices ? "" : "static ");
is_nonempty = true;
}
src = mputprintf(src, "%s_bits[] = { ",
bs_literals.get_nth_elem(i)->c_str());
// Filling up the octets one-by-one
for (size_t j = 0; j < (bits + 7) / 8; j++) {
size_t offset = 8 * j;
unsigned char value = 0;
for (size_t k = 0; k < 8 && k < bits - offset; k++) {
if (str[offset + k] == '1') value |= (1 << k);
}
if (j > 0) src = mputstr(src, ", ");
src = mputprintf(src, "%d", value);
}
src = mputstr(src, " }");
}
if (is_nonempty) src = mputstr(src, ";\n");
for (size_t i = 0; i < bs_literals.size(); i++) {
if (i > 0) {
src = mputstr(src, ",\n");
if (splitting) hdr = mputstr(hdr, ",\n");
}
else {
src = mputprintf(src, "%s const BITSTRING ",
splitting ? "extern" : "static");
if (splitting) hdr = mputstr(hdr, "extern const BITSTRING ");
}
size_t bits = bs_literals.get_nth_key(i).size();
const char *object_name = bs_literals.get_nth_elem(i)->c_str();
if (bits > 0) src = mputprintf(src, "%s(%lu, %s_bits)",
object_name, (unsigned long) bits, object_name);
else src = mputprintf(src, "%s(0, NULL)", object_name);
if (splitting) hdr = mputstr(hdr, object_name);
}
src = mputstr(src, ";\n");
if (splitting) hdr = mputstr(hdr, ";\n");
}
void Module::generate_bp_literals(char *&src, char *& hdr)
{
if (bp_literals.size() == 0) return;
for (size_t i = 0; i < bp_literals.size(); i++) {
if (i > 0) src = mputstr(src, ",\n");
else {
src = mputprintf(src, "%sconst unsigned char ", split_to_slices ? "" : "static ");
}
src = mputprintf(src, "%s_elements[] = { ",
bp_literals.get_nth_elem(i)->c_str());
const string& str = bp_literals.get_nth_key(i);
for (size_t j = 0; j < str.size(); j++) {
if (j > 0) src = mputstr(src, ", ");
switch (str[j]) {
case '0':
src = mputc(src, '0');
break;
case '1':
src = mputc(src, '1');
break;
case '?':
src = mputc(src, '2');
break;
case '*':
src = mputc(src, '3');
break;
default:
FATAL_ERROR("Invalid character in bitstring pattern.");
}
}
src = mputstr(src, " }");
}
src = mputstr(src, ";\n");
for (size_t i = 0; i < bp_literals.size(); i++) {
if (i > 0) {
src = mputstr(src, ",\n");
if (split_to_slices) {
hdr = mputstr(hdr, ",\n");
}
}
else {
src = mputprintf(src, "%sconst BITSTRING_template ", split_to_slices ? "" : "static ");
if (split_to_slices) {
hdr = mputprintf(hdr, "extern const BITSTRING_template ");
}
}
const char *name = bp_literals.get_nth_elem(i)->c_str();
src = mputprintf(src, "%s(%lu, %s_elements)",
name, (unsigned long) bp_literals.get_nth_key(i).size(), name);
if (split_to_slices) {
hdr = mputstr(hdr, name);
}
}
src = mputstr(src, ";\n");
if (split_to_slices) {
hdr = mputstr(hdr, ";\n");
}
}
void Module::generate_hs_literals(char *&src, char *&hdr)
{
if (hs_literals.size() == 0) return;
// indicates whether we have found at least one non-empty hexstring
bool is_nonempty = false;
bool splitting =
CodeGenHelper::GetInstance().get_split_mode() != CodeGenHelper::SPLIT_NONE;
for (size_t i = 0; i < hs_literals.size(); i++) {
const string& str = hs_literals.get_nth_key(i);
size_t nibbles = str.size();
if (nibbles == 0) continue;
size_t octets = (nibbles + 1) / 2;
const char *str_ptr = str.c_str();
if (is_nonempty) src = mputstr(src, ",\n");
else {
src = mputprintf(src, "%sconst unsigned char ", split_to_slices ? "" : "static ");
is_nonempty = true;
}
src = mputprintf(src, "%s_nibbles[] = { ",
hs_literals.get_nth_elem(i)->c_str());
for (size_t j = 0; j < octets; j++) {
// Hex digit with even index always goes to the least significant
// 4 bits of the octet.
unsigned char value = char_to_hexdigit(str_ptr[2 * j]);
if (2 * j + 1 < nibbles) {
// Hex digit with odd index always goes to the most significant
// 4 bits of the octet.
// This digit is not present (bits are set to zero) if the length
// of hexstring is odd.
value += 16 * char_to_hexdigit(str_ptr[2 * j + 1]);
}
if (j > 0) src = mputstr(src, ", ");
src = mputprintf(src, "%u", value);
}
src = mputstr(src, " }");
}
if (is_nonempty) src = mputstr(src, ";\n");
for (size_t i = 0; i < hs_literals.size(); i++) {
if (i > 0) {
src = mputstr(src, ",\n");
if (splitting) hdr = mputstr(hdr, ",\n");
}
else {
src = mputprintf(src, "%s const HEXSTRING ",
splitting ? "extern" : "static");
if (splitting) hdr = mputstr(hdr, "extern const HEXSTRING ");
}
size_t nibbles = hs_literals.get_nth_key(i).size();
const char *object_name = hs_literals.get_nth_elem(i)->c_str();
if (nibbles > 0) src = mputprintf(src, "%s(%lu, %s_nibbles)",
object_name, (unsigned long) nibbles, object_name);
else src = mputprintf(src, "%s(0, NULL)", object_name);
if (splitting) hdr = mputstr(hdr, object_name);
}
src = mputstr(src, ";\n");
if (splitting) hdr = mputstr(hdr, ";\n");
}
void Module::generate_hp_literals(char *&src, char *& hdr)
{
if (hp_literals.size() == 0) return;
for (size_t i = 0; i < hp_literals.size(); i++) {
if (i > 0) {
src = mputstr(src, ",\n");
} else {
src = mputprintf(src, "%sconst unsigned char ", split_to_slices ? "" : "static ");
}
src = mputprintf(src, "%s_elements[] = { ",
hp_literals.get_nth_elem(i)->c_str());
const string& str = hp_literals.get_nth_key(i);
size_t size = str.size();
const char *str_ptr = str.c_str();
for (size_t j = 0; j < size; j++) {
if (j > 0) src = mputstr(src, ", ");
unsigned char num;
if (str_ptr[j] == '?') num = 16;
else if (str_ptr[j] == '*') num = 17;
else num = char_to_hexdigit(str_ptr[j]);
src = mputprintf(src, "%u", num);
}
src = mputstr(src, " }");
}
src = mputstr(src, ";\n");
for (size_t i = 0; i < hp_literals.size(); i++) {
if (i > 0) {
src = mputstr(src, ",\n");
if (split_to_slices) {
hdr = mputstr(hdr, ",\n");
}
} else {
src = mputprintf(src, "%sconst HEXSTRING_template ", split_to_slices ? "" : "static ");
if (split_to_slices) {
hdr = mputprintf(hdr, "extern const HEXSTRING_template ");
}
}
const char *name = hp_literals.get_nth_elem(i)->c_str();
src = mputprintf(src, "%s(%lu, %s_elements)",
name, (unsigned long) hp_literals.get_nth_key(i).size(), name);
if (split_to_slices) {
hdr = mputstr(hdr, name);
}
}
src = mputstr(src, ";\n");
if (split_to_slices) {
hdr = mputstr(hdr, ";\n");
}
}
void Module::generate_os_literals(char *&src, char *&hdr)
{
if (os_literals.size() == 0) return;
// indicates whether we have found at least one non-empty octetstring
bool is_nonempty = false;
bool splitting =
CodeGenHelper::GetInstance().get_split_mode() != CodeGenHelper::SPLIT_NONE;
for (size_t i = 0; i < os_literals.size(); i++) {
const string& str = os_literals.get_nth_key(i);
size_t size = str.size();
if (size % 2) FATAL_ERROR("Invalid length for an octetstring.");
size_t octets = size / 2;
if (octets == 0) continue;
const char *str_ptr = str.c_str();
if (is_nonempty) src = mputstr(src, ",\n");
else {
src = mputprintf(src, "%sconst unsigned char ", split_to_slices ? "" : "static ");
is_nonempty = true;
}
src = mputprintf(src, "%s_octets[] = { ",
os_literals.get_nth_elem(i)->c_str());
for (size_t j = 0; j < octets; j++) {
if (j > 0) src = mputstr(src, ", ");
src = mputprintf(src, "%u", 16 * char_to_hexdigit(str_ptr[2 * j]) +
char_to_hexdigit(str_ptr[2 * j + 1]));
}
src = mputstr(src, " }");
}
if (is_nonempty) src = mputstr(src, ";\n");
for (size_t i = 0; i < os_literals.size(); i++) {
if (i > 0) {
src = mputstr(src, ",\n");
if (splitting) hdr = mputstr(hdr, ",\n");
}
else {
src = mputprintf(src, "%s const OCTETSTRING ",
splitting ? "extern" : "static");
if (splitting) hdr = mputstr(hdr, "extern const OCTETSTRING ");
}
size_t octets = os_literals.get_nth_key(i).size() / 2;
const char *object_name = os_literals.get_nth_elem(i)->c_str();
if (octets > 0) src = mputprintf(src, "%s(%lu, %s_octets)",
object_name, (unsigned long) octets, object_name);
else src = mputprintf(src, "%s(0, NULL)", object_name);
if (splitting) hdr = mputstr(hdr, object_name);
}
src = mputstr(src, ";\n");
if (splitting) hdr = mputstr(hdr, ";\n");
}
void Module::generate_op_literals(char *&src, char *& hdr)
{
if (op_literals.size() == 0) return;
vector<size_t> pattern_lens;
for(size_t i = 0; i < op_literals.size(); i++) {
if (i > 0) {
src = mputstr(src, ",\n");
} else {
src = mputprintf(src, "%sconst unsigned short ", split_to_slices ? "" : "static ");
}
src = mputprintf(src, "%s_elements[] = { ",
op_literals.get_nth_elem(i)->c_str());
const string& str = op_literals.get_nth_key(i);
size_t size = str.size();
size_t pattern_len = 0;
const char *str_ptr = str.c_str();
for (size_t j = 0; j < size; j++) {
if (j > 0) src = mputstr(src, ", ");
unsigned short num;
if (str_ptr[j] == '?') num = 256;
else if (str_ptr[j] == '*') num = 257;
else {
// first digit
num = 16 * char_to_hexdigit(str_ptr[j]);
j++;
if (j >= size) FATAL_ERROR("Unexpected end of octetstring pattern.");
// second digit
num += char_to_hexdigit(str_ptr[j]);
}
src = mputprintf(src, "%u", num);
pattern_len++;
}
src = mputstr(src, " }");
pattern_lens.add(new size_t(pattern_len));
}
src = mputstr(src, ";\n");
for (size_t i = 0; i < op_literals.size(); i++) {
if (i > 0) {
src = mputstr(src, ",\n");
if (split_to_slices) {
hdr = mputstr(hdr, ",\n");
}
}
else {
src = mputprintf(src, "%sconst OCTETSTRING_template ", split_to_slices ? "" : "static ");
if (split_to_slices) {
hdr = mputprintf(hdr, "extern const OCTETSTRING_template ");
}
}
const char *name = op_literals.get_nth_elem(i)->c_str();
src = mputprintf(src, "%s(%lu, %s_elements)",
name, (unsigned long) *pattern_lens[i], name);
if (split_to_slices) {
hdr = mputstr(hdr, name);
}
}
src = mputstr(src, ";\n");
if (split_to_slices) {
hdr = mputstr(hdr, ";\n");
}
for (size_t i = 0; i < pattern_lens.size(); i++) delete pattern_lens[i];
pattern_lens.clear();
}
void Module::generate_cs_literals(char *&src, char *&hdr)
{
if (cs_literals.size() == 0) return;
bool splitting =
CodeGenHelper::GetInstance().get_split_mode() != CodeGenHelper::SPLIT_NONE;
for (size_t i = 0; i < cs_literals.size(); i++) {
const string& str = cs_literals.get_nth_key(i);
size_t str_len = str.size();
const char *str_ptr = str.c_str();
const char *str_name = cs_literals.get_nth_elem(i)->c_str();
if (i > 0) {
src = mputstr(src, ",\n");
if (splitting) hdr = mputstr(hdr, ",\n");
}
else {
src = mputprintf(src, "%s const CHARSTRING ",
splitting ? "extern" : "static");
if (splitting) hdr = mputstr(hdr, "extern const CHARSTRING ");
}
switch (str_len) {
case 0:
src = mputprintf(src, "%s(0, NULL)", str_name);
break;
case 1:
src = mputprintf(src, "%s('", str_name);
src = Code::translate_character(src, *str_ptr, false);
src = mputstr(src, "')");
break;
default:
src = mputprintf(src, "%s(%lu, \"", str_name, (unsigned long) str_len);
// Note: Code::translate_string() is not suitable because the string
// may contain NUL characters at which translate_string() stops
// immediately
for (size_t j = 0; j < str_len; j++)
src = Code::translate_character(src, str_ptr[j], true);
src = mputstr(src, "\")");
break;
} // switch
if (splitting) hdr = mputstr(hdr, str_name);
}
src = mputstr(src, ";\n");
if (splitting) hdr = mputstr(hdr, ";\n");
}
void Module::generate_pp_literals(char *&src, char *&) // padding patterns
{
if (pp_literals.size() == 0) return;
for (size_t i = 0; i < pp_literals.size(); i++) {
const string& pattern = pp_literals.get_nth_key(i);
size_t pattern_len = pattern.size();
const char *pattern_ptr = pattern.c_str();
if (i > 0) {
src = mputstr(src, ",\n");
}
else {
src = mputprintf(src, "%sconst unsigned char ", split_to_slices ? "" : "static ");
}
src = mputprintf(src, "%s[] = { ", pp_literals.get_nth_elem(i)->c_str());
if (pattern_len % 8 != 0) FATAL_ERROR("Module::generate_pp_literals()");
size_t nof_octets = pattern_len / 8;
for (size_t j = 0; j < nof_octets; j++) {
if (j > 0) src = mputstr(src, ", ");
unsigned char octet = 0;
for (size_t k = 0; k < 8; k++) {
// take the octets in reverse order
// MSB is the first character of the string
octet <<= 1;
if (pattern_ptr[8 * (nof_octets - j - 1) + k] == '1') octet |= 0x01;
}
src = mputprintf(src, "0x%02x", octet);
}
src = mputstr(src, " }");
}
src = mputstr(src, ";\n");
}
void Module::generate_mp_literals(char *&src, char *&) // matching patt.
{
if (mp_literals.size() == 0) return;
for (size_t i = 0; i < mp_literals.size(); i++) {
const string& str = mp_literals.get_nth_key(i);
if (str.size() < 1) FATAL_ERROR("Module::generate_mp_literals()");
const char *str_ptr = str.c_str();
if (i > 0) src = mputstr(src, ",\n");
else src = mputprintf(src, "%sconst Token_Match ", split_to_slices ? "" : "static ");
src = mputprintf(src, "%s(\"", mp_literals.get_nth_elem(i)->c_str());
src = Code::translate_string(src, str_ptr + 1);
// The first character of the string is case-sensitiveness flag:
// 'I' for yes, 'N' for no,
// 'F' for fixed string matching which is always case sensitive.
src = mputprintf(src, "\", %s%s)", (str_ptr[0]!='N') ? "TRUE" : "FALSE",
(str_ptr[0] == 'F') ? ", TRUE" : "");
}
src = mputstr(src, ";\n");
}
void Module::generate_us_literals(char *&src, char *&hdr) // univ.cs
{
size_t n_literals = us_literals.size();
if (n_literals == 0) return;
bool array_needed = false;
bool splitting =
CodeGenHelper::GetInstance().get_split_mode() != CodeGenHelper::SPLIT_NONE;
for (size_t i = 0; i < n_literals; i++) {
const ustring& value = us_literals.get_nth_key(i);
size_t value_size = value.size();
if (value_size < 2) continue;
if (array_needed) src = mputstr(src, ",\n");
else {
src = mputprintf(src, "%sconst universal_char ", split_to_slices ? "" : "static ");
array_needed = true;
}
src = mputprintf(src, "%s_uchars[] = { ",
us_literals.get_nth_elem(i)->c_str());
const ustring::universal_char *uchars_ptr = value.u_str();
for (size_t j = 0; j < value_size; j++) {
if (j > 0) src = mputstr(src, ", ");
src = mputprintf(src, "{ %u, %u, %u, %u }", uchars_ptr[j].group,
uchars_ptr[j].plane, uchars_ptr[j].row, uchars_ptr[j].cell);
}
src = mputstr(src, " }");
}
if (array_needed) src = mputstr(src, ";\n");
for (size_t i = 0; i < n_literals; i++) {
if (i > 0) {
src = mputstr(src, ",\n");
if (splitting) hdr = mputstr(hdr, ",\n");
}
else {
src = mputprintf(src, "%s const UNIVERSAL_CHARSTRING ",
splitting ? "extern" : "static");
if (splitting) hdr = mputstr(hdr, "extern const UNIVERSAL_CHARSTRING ");
}
const char *value_name = us_literals.get_nth_elem(i)->c_str();
const ustring& value = us_literals.get_nth_key(i);
size_t value_size = value.size();
switch (value_size) {
case 0:
src = mputprintf(src, "%s(0, (const universal_char*)NULL)", value_name);
break;
case 1: {
const ustring::universal_char& uchar = value.u_str()[0];
src = mputprintf(src, "%s(%u, %u, %u, %u)", value_name,
uchar.group, uchar.plane, uchar.row, uchar.cell);
break; }
default:
src = mputprintf(src, "%s(%lu, %s_uchars)", value_name,
(unsigned long) value_size, value_name);
break;
}
if (splitting) hdr = mputstr(hdr, value_name);
}
src = mputstr(src, ";\n");
if (splitting) hdr = mputstr(hdr, ";\n");
}
void Module::generate_oid_literals(char *&src, char *& hdr)
{
if (oid_literals.size() == 0) return;
for (size_t i = 0; i < oid_literals.size(); i++) {
if (i > 0) src = mputstr(src, ",\n");
else src = mputprintf(src, "%sconst OBJID::objid_element ", split_to_slices ? "" : "static ");
src = mputprintf(src, "%s_comps[] = { %s }",
oid_literals.get_nth_elem(i)->oid_id.c_str(),
oid_literals.get_nth_key(i).c_str());
}
src = mputstr(src, ";\n");
for(size_t i = 0; i < oid_literals.size(); i++) {
const OID_literal *litstruct = oid_literals.get_nth_elem(i);
if (i > 0) {
src = mputstr(src, ",\n");
if (split_to_slices) {
hdr = mputstr(hdr, ",\n");
}
}
else {
src = mputprintf(src, "%sconst OBJID ", split_to_slices ? "" : "static ");
if (split_to_slices) {
hdr = mputstr(hdr, "extern const OBJID ");
}
}
src = mputprintf(src, "%s(%lu, %s_comps)",
litstruct->oid_id.c_str(), (unsigned long) litstruct->nof_elems,
litstruct->oid_id.c_str());
if (split_to_slices) {
hdr = mputstr(hdr, litstruct->oid_id.c_str());
}
}
src = mputstr(src, ";\n");
if (split_to_slices) {
hdr = mputstr(hdr, ";\n");
}
}
void Module::generate_functions(output_struct *output)
{
bool tcov_enabled = tcov_file_name && in_tcov_files(get_filename());
bool has_pre_init_before_tcov = output->functions.pre_init != NULL;
if (tcov_enabled) {
output->functions.pre_init = mputprintf(output->functions.pre_init,
"TTCN_Location_Statistics::init_file_lines(\"%s\", effective_module_lines, sizeof(effective_module_lines) / sizeof(int));\n" \
"TTCN_Location_Statistics::init_file_functions(\"%s\", effective_module_functions, sizeof(effective_module_functions) / sizeof(char *));\n",
get_tcov_file_name(get_filename()), get_tcov_file_name(get_filename()));
}
// pre_init function
bool has_pre_init = false;
bool profiled = MOD_TTCN == get_moduletype() && is_file_profiled(get_filename());
bool debugged = debugger_active && MOD_TTCN == get_moduletype();
// always generate pre_init_module if the file is profiled
if (output->functions.pre_init || profiled || debugged) {
output->source.static_function_prototypes =
mputprintf(output->source.static_function_prototypes,
"%svoid pre_init_module();\n", split_to_slices ? "extern " : "static ");
output->source.static_function_bodies = mputprintf(output->source.static_function_bodies,
"%svoid pre_init_module()\n"
"{\n", split_to_slices ? "" : "static ");
if (include_location_info) {
output->source.static_function_bodies =
mputstr(output->source.static_function_bodies,
(tcov_enabled && has_pre_init_before_tcov) ? "TTCN_Location_Statistics current_location(\""
: "TTCN_Location current_location(\"");
output->source.static_function_bodies =
Code::translate_string(output->source.static_function_bodies, (tcov_enabled && has_pre_init_before_tcov) ? get_tcov_file_name(get_filename()) : get_filename());
output->source.static_function_bodies =
mputprintf(output->source.static_function_bodies,
(tcov_enabled && has_pre_init_before_tcov) ? "\", 0, TTCN_Location_Statistics::LOCATION_UNKNOWN, \"%s\");\n"
: "\", 0, TTCN_Location::LOCATION_UNKNOWN, \"%s\");\n", get_modid().get_dispname().c_str());
if (tcov_enabled && has_pre_init_before_tcov) {
effective_module_lines =
mputprintf(effective_module_lines, "%s0",
(effective_module_lines ? ", " : ""));
effective_module_functions =
mputprintf(effective_module_functions, "%s\"%s\"",
(effective_module_functions ? ", " : ""), get_modid().get_dispname().c_str());
}
if (profiled) {
output->source.static_function_bodies = mputprintf(output->source.static_function_bodies,
"%s::init_ttcn3_profiler();\n"
"TTCN3_Stack_Depth stack_depth;\n"
"ttcn3_prof.execute_line(\"%s\", 0);\n", get_modid().get_name().c_str(), get_filename());
}
if (debugged) {
output->source.static_function_bodies = mputprintf(output->source.static_function_bodies,
"%s::init_ttcn3_debugger();\n", get_modid().get_name().c_str());
}
}
output->source.static_function_bodies =
mputstr(output->source.static_function_bodies, output->functions.pre_init);
output->source.static_function_bodies =
mputstr(output->source.static_function_bodies, "}\n\n");
Free(output->functions.pre_init);
output->functions.pre_init = NULL;
has_pre_init = true;
}
bool has_post_init_before_tcov = output->functions.post_init != NULL;
// post_init function
bool has_post_init = false;
if (output->functions.post_init) {
output->source.static_function_prototypes = mputprintf(output->source.static_function_prototypes,
"%svoid post_init_module();\n", split_to_slices ? "extern " : "static ");
output->source.static_function_bodies = mputprintf(output->source.static_function_bodies,
"%svoid post_init_module()\n"
"{\n", split_to_slices ? "" : "static ");
if (include_location_info) {
output->source.static_function_bodies =
mputstr(output->source.static_function_bodies,
(tcov_enabled && has_post_init_before_tcov) ? "TTCN_Location_Statistics current_location(\""
: "TTCN_Location current_location(\"");
output->source.static_function_bodies =
Code::translate_string(output->source.static_function_bodies, (tcov_enabled && has_post_init_before_tcov) ? get_tcov_file_name(get_filename()) : get_filename());
output->source.static_function_bodies =
mputprintf(output->source.static_function_bodies,
(tcov_enabled && has_post_init_before_tcov) ? "\", 0, TTCN_Location_Statistics::LOCATION_UNKNOWN, \"%s\");\n"
: "\", 0, TTCN_Location::LOCATION_UNKNOWN, \"%s\");\n", get_modid().get_dispname().c_str());
if (tcov_enabled && has_post_init_before_tcov) {
effective_module_lines =
mputprintf(effective_module_lines, "%s0",
(effective_module_lines ? ", " : ""));
effective_module_functions =
mputprintf(effective_module_functions, "%s\"%s\"",
(effective_module_functions ? ", " : ""), get_modid().get_dispname().c_str());
}
if (MOD_TTCN == get_moduletype() && is_file_profiled(get_filename())) {
output->source.static_function_bodies = mputprintf(output->source.static_function_bodies,
"TTCN3_Stack_Depth stack_depth;\n"
"ttcn3_prof.execute_line(\"%s\", 0);\n", get_filename());
}
}
output->source.static_function_bodies =
mputstr(output->source.static_function_bodies, output->functions.post_init);
output->source.static_function_bodies =
mputstr(output->source.static_function_bodies, "}\n\n");
Free(output->functions.post_init);
output->functions.post_init = NULL;
has_post_init = true;
}
// set_param function
bool has_set_param;
if (output->functions.set_param) {
output->source.static_function_prototypes = mputprintf(output->source.static_function_prototypes,
"%sboolean set_module_param(Module_Param& param);\n", split_to_slices ? "extern " : "static ");
output->source.static_function_bodies = mputprintf(output->source.static_function_bodies,
"%sboolean set_module_param(Module_Param& param)\n"
"{\n"
"const char* const par_name = param.get_id()->get_current_name();\n", split_to_slices ? "" : "static ");
output->source.static_function_bodies =
mputstr(output->source.static_function_bodies, output->functions.set_param);
output->source.static_function_bodies =
mputstr(output->source.static_function_bodies, "return FALSE;\n"
"}\n\n");
Free(output->functions.set_param);
output->functions.set_param = NULL;
has_set_param = true;
} else has_set_param = false;
// get_param function
bool has_get_param;
if (use_runtime_2 && output->functions.get_param) {
output->source.static_function_prototypes = mputprintf(output->source.static_function_prototypes,
"%sModule_Param* get_module_param(Module_Param_Name& param_name);\n", split_to_slices ? "extern " : "static ");
output->source.static_function_bodies = mputprintf(output->source.static_function_bodies,
"%sModule_Param* get_module_param(Module_Param_Name& param_name)\n"
"{\n"
"const char* const par_name = param_name.get_current_name();\n", split_to_slices ? "" : "static ");
output->source.static_function_bodies =
mputstr(output->source.static_function_bodies, output->functions.get_param);
output->source.static_function_bodies =
mputstr(output->source.static_function_bodies, "return NULL;\n"
"}\n\n");
Free(output->functions.get_param);
output->functions.get_param = NULL;
has_get_param = true;
} else has_get_param = false;
// log_param function
bool has_log_param;
if (output->functions.log_param) {
output->source.static_function_prototypes = mputprintf(output->source.static_function_prototypes,
"%svoid log_module_param();\n", split_to_slices ? "extern " : "static ");
output->source.static_function_bodies = mputprintf(output->source.static_function_bodies,
"%svoid log_module_param()\n"
"{\n", split_to_slices ? "" : "static ");
output->source.static_function_bodies =
mputstr(output->source.static_function_bodies, output->functions.log_param);
output->source.static_function_bodies = mputstr(output->source.static_function_bodies,
"}\n\n");
Free(output->functions.log_param);
output->functions.log_param = NULL;
has_log_param = true;
} else has_log_param = false;
// init_comp function
bool has_init_comp;
if (output->functions.init_comp) {
output->source.static_function_prototypes =
mputprintf(output->source.static_function_prototypes,
"%sboolean init_comp_type("
"const char *component_type, boolean init_base_comps);\n", split_to_slices ? "extern " : "static ");
output->source.static_function_bodies =
mputprintf(output->source.static_function_bodies,
"%sboolean init_comp_type(const char *component_type, "
"boolean init_base_comps)\n"
"{\n(void)init_base_comps;\n", split_to_slices ? "" : "static ");
output->source.static_function_bodies =
mputstr(output->source.static_function_bodies,
output->functions.init_comp);
output->source.static_function_bodies =
mputstr(output->source.static_function_bodies, "return FALSE;\n"
"}\n\n");
Free(output->functions.init_comp);
output->functions.init_comp = NULL;
has_init_comp = true;
} else has_init_comp = false;
// start function
bool has_start;
if (output->functions.start) {
output->source.static_function_prototypes = mputprintf(output->source.static_function_prototypes,
"%sboolean start_ptc_function(const char *function_name, "
"Text_Buf& function_arguments);\n", split_to_slices ? "extern " : "static ");
output->source.static_function_bodies = mputprintf(output->source.static_function_bodies,
"%sboolean start_ptc_function(const char *function_name, "
"Text_Buf& function_arguments)\n"
"{\n", split_to_slices ? "" : "static ");
output->source.static_function_bodies =
mputstr(output->source.static_function_bodies, output->functions.start);
output->source.static_function_bodies =
mputstr(output->source.static_function_bodies, "return FALSE;\n"
"}\n\n");
Free(output->functions.start);
output->functions.start = NULL;
has_start = true;
} else has_start = false;
// control part
bool has_control;
if (output->functions.control) {
output->source.static_function_prototypes = mputprintf(output->source.static_function_prototypes,
"%svoid module_control_part();\n", split_to_slices ? "extern " : "static ");
output->source.static_function_bodies = mputprintf(output->source.static_function_bodies,
"%svoid module_control_part()\n"
"{\n", split_to_slices ? "" : "static ");
output->source.static_function_bodies =
mputstr(output->source.static_function_bodies, output->functions.control);
output->source.static_function_bodies =
mputstr(output->source.static_function_bodies, "}\n\n");
Free(output->functions.control);
output->functions.control = NULL;
has_control = true;
} else has_control = false;
// module checksum
if (has_checksum) {
if (split_to_slices) {
output->header.global_vars = mputprintf(output->header.global_vars, "extern const unsigned char module_checksum[];\n");
}
output->source.string_literals = mputprintf(output->source.string_literals,
"%sconst unsigned char module_checksum[] = {", split_to_slices ? "" : "static ");
for (size_t i = 0; i < sizeof(module_checksum); i++) {
if (i > 0) output->source.string_literals =
mputc(output->source.string_literals, ',');
output->source.string_literals =
mputprintf(output->source.string_literals, " 0x%02x",
module_checksum[i]);
}
output->source.string_literals = mputstr(output->source.string_literals,
" };\n");
}
const char *module_name = modid->get_name().c_str();
// XML namespaces. Written in the order they are stored:
// sorted ASCIIbetically by the prefix.
// Not all namespaces are used by every module. Unfortunately, the array
// (which has the same size in all modules) cannot be compacted, because
// the indexes have already been used when the XER descriptors were written.
// All we can do is store NULLs for the unused namespaces.
size_t num_xml_namespaces = namespaces.size();
if (moduletype == MOD_TTCN) { //TODO remove this when ASN.1 gets EXER
#ifndef NDEBUG
output->source.global_vars = mputprintf(output->source.global_vars,
"// written by %s in " __FILE__ " at %d\n"
, __FUNCTION__, __LINE__
);
#endif
if (num_xml_namespaces != 0 || (control_ns && control_ns_prefix)) {
output->source.global_vars = mputprintf(output->source.global_vars,
"%sconst size_t num_namespaces = %lu;\n"
"%sconst namespace_t xml_namespaces[num_namespaces+1] = {\n"
, split_to_slices ? "" : "static ", (unsigned long)num_xml_namespaces
, split_to_slices ? "" : "static "
);
if (split_to_slices) {
output->header.global_vars = mputprintf(output->header.global_vars,
"extern const size_t num_namespaces;\n");
output->header.global_vars = mputprintf(output->header.global_vars,
"extern const namespace_t xml_namespaces[];\n");
}
for (size_t i=0; i < namespaces.size(); ++i) {
if (used_namespaces.has_key(i)) {
output->source.global_vars = mputprintf(output->source.global_vars,
" { \"%s\", \"%s\" },\n", // ns, then prefix
namespaces.get_nth_elem(i), namespaces.get_nth_key(i).c_str());
}
else {
output->source.global_vars = mputstr(output->source.global_vars,
" { NULL, NULL },\n"); // not used in this module
}
} // next namespace
output->source.global_vars = mputprintf(output->source.global_vars,
" { \"%s\", \"%s\" }\n};\n\n",
(control_ns ? control_ns : ""),
(control_ns_prefix ? control_ns_prefix : ""));
} // if there are namespaces
} // if TTCN
// module object
output->header.global_vars = mputprintf(output->header.global_vars,
"extern TTCN_Module %s;\n",
"module_object");
output->source.global_vars = mputprintf(output->source.global_vars,
"TTCN_Module %s(\"%s\", __DATE__, __TIME__, %s, %s",
"module_object",
modid->get_dispname().c_str(),
(has_checksum ? "module_checksum" : "NULL"),
has_pre_init ? "pre_init_module" : "NULL");
if (moduletype == MOD_TTCN) {
// TTCN-3 specific function pointers
if (product_number == NULL) {
output->source.global_vars = mputstr(output->source.global_vars, ", NULL");
} else {
output->source.global_vars = mputprintf(output->source.global_vars, ", \"%s\"", product_number);
}
string extra_str = extra ? ( string('"') + extra + string('"') ) : string("NULL");
output->source.global_vars = mputprintf(output->source.global_vars,
", %uU, %uU, %uU, %uU, %s, %luLU, %s, %s, %s, %s, %s, %s, %s, %s",
suffix, release, patch, build, extra_str.c_str(),
(unsigned long)num_xml_namespaces,
((num_xml_namespaces || (control_ns && control_ns_prefix)) ? "xml_namespaces" : "0"),
has_post_init ? "post_init_module" : "NULL",
has_set_param ? "set_module_param" : "NULL",
has_get_param ? "get_module_param" : "NULL",
has_log_param ? "log_module_param" : "NULL",
has_init_comp ? "init_comp_type" : "NULL",
has_start ? "start_ptc_function" : "NULL",
has_control ? "module_control_part" : "NULL");
} else {
// self checks for ASN.1 modules
if (has_post_init)
FATAL_ERROR("Module::generate_functions(): post_init function in ASN.1 module");
if (has_set_param)
FATAL_ERROR("Module::generate_functions(): set_param function in ASN.1 module");
if (has_get_param)
FATAL_ERROR("Module::generate_functions(): get_param function in ASN.1 module");
if (has_log_param)
FATAL_ERROR("Module::generate_functions(): log_param function in ASN.1 module");
if (has_init_comp)
FATAL_ERROR("Module::generate_functions(): init_comp function in ASN.1 module");
if (has_start)
FATAL_ERROR("Module::generate_functions(): startable function in ASN.1 module");
if (has_control)
FATAL_ERROR("Module::generate_functions(): control part in ASN.1 module");
}
output->source.global_vars = mputstr(output->source.global_vars, ");\n");
// #include into the source file
output->source.includes = mputprintf(output->source.includes,
"#include \"%s.hh\"\n",
duplicate_underscores ? module_name : modid->get_ttcnname().c_str());
output->source.global_vars = mputprintf(output->source.global_vars,
"\n%sconst RuntimeVersionChecker ver_checker("
" current_runtime_version.requires_major_version_%d,\n"
" current_runtime_version.requires_minor_version_%d,\n"
" current_runtime_version.requires_patch_level_%d,"
" current_runtime_version.requires_runtime_%d);\n",
split_to_slices ? "" : "static ",
TTCN3_MAJOR, TTCN3_MINOR, TTCN3_PATCHLEVEL, use_runtime_2 ? 2 : 1
);
if (split_to_slices) {
output->header.global_vars = mputprintf(output->header.global_vars,
"extern const RuntimeVersionChecker ver_checker;\n");
}
if (tcov_enabled) {
output->source.global_vars = mputprintf(output->source.global_vars,
"\n%sconst int effective_module_lines[] = { %s };\n" \
"%sconst char *effective_module_functions[] = { %s };\n",
split_to_slices ? "" : "static ",
effective_module_lines ? static_cast<const char *>(effective_module_lines) : "",
split_to_slices ? "" : "static ",
effective_module_functions ? static_cast<const char *>(effective_module_functions) : "");
if (split_to_slices) {
output->header.global_vars = mputprintf(output->header.global_vars,
"extern const int effective_module_lines[];\n" \
"extern const char *effective_module_functions[];\n");
}
}
}
void Module::generate_conversion_functions(output_struct *output)
{
for (size_t i = 0; i < type_conv_v.size(); i++)
type_conv_v[i]
->gen_conv_func(&output->source.static_conversion_function_prototypes,
&output->source.static_conversion_function_bodies,
this);
}
string Module::add_literal(map<string, string>& literals, const string& str,
const char *prefix)
{
if (literals.has_key(str)) return *literals[str];
else {
string *literal = new string(prefix+Int2string(literals.size()));
literals.add(str, literal);
return *literal;
}
}
void Module::clear_literals(map<string, string>& literals)
{
for (size_t i = 0; i < literals.size(); i++)
delete literals.get_nth_elem(i);
literals.clear();
}
map<string, const char> Module::namespaces;
map<string, const char> Module::invented_prefixes;
size_t Module::default_namespace_attempt = 0;
size_t Module::replacement_for_empty_prefix = (size_t)-1;
Module::Module(moduletype_t p_mt, Identifier *p_modid)
: Scope(), moduletype(p_mt), modid(p_modid),
imp_checked(false), gen_code(false), has_checksum(false),
visible_mods(), module_checksum(),
bs_literals(), bp_literals(), hs_literals(), hp_literals(), os_literals(),
op_literals(), cs_literals(), us_literals(), pp_literals(), mp_literals(),
oid_literals(), tmp_id_count(0),
control_ns(p_mt == MOD_ASN ? mcopystr("urn:oid:2.1.5.2.0.1") : NULL),
control_ns_prefix(p_mt == MOD_ASN ? mcopystr("asn1") : NULL),
// only ASN.1 modules have default control namespace (X.693 amd1, 16.9)
used_namespaces(), type_conv_v(), product_number(NULL),
suffix(0), release(UINT_MAX), patch(UINT_MAX), build(UINT_MAX), extra(NULL)
{
if(!p_modid)
FATAL_ERROR("NULL parameter: Common::Module::Module()");
memset(module_checksum, 0, sizeof(module_checksum));
set_scopeMacro_name(modid->get_dispname());
}
Module::~Module()
{
delete modid;
visible_mods.clear();
clear_literals(bs_literals);
clear_literals(bp_literals);
clear_literals(hs_literals);
clear_literals(hp_literals);
clear_literals(os_literals);
clear_literals(op_literals);
clear_literals(cs_literals);
clear_literals(pp_literals);
clear_literals(mp_literals);
for (size_t i = 0; i < us_literals.size(); i++)
delete us_literals.get_nth_elem(i);
us_literals.clear();
for (size_t i = 0; i < oid_literals.size(); i++)
delete oid_literals.get_nth_elem(i);
oid_literals.clear();
for (size_t i = 0; i < type_conv_v.size(); i++)
delete type_conv_v[i];
type_conv_v.clear();
Free(control_ns);
Free(control_ns_prefix);
used_namespaces.clear(); // all the values are NULL, no need to free
// static members below; repeated clear()s are redundant but harmless
namespaces.clear();
invented_prefixes.clear();
Free(product_number);
Free(extra);
}
Type *Module::get_address_type()
{
FATAL_ERROR("Common::Module::get_address_type()");
return 0;
}
string Module::add_ustring_literal(const ustring& ustr)
{
if (us_literals.has_key(ustr)) return *us_literals[ustr];
else {
string *literal = new string("us_" + Int2string(us_literals.size()));
us_literals.add(ustr, literal);
return *literal;
}
}
string Module::add_objid_literal(const string& oi_str, const size_t nof_elems)
{
if(oid_literals.has_key(oi_str)) return oid_literals[oi_str]->oid_id;
else {
OID_literal *oi_struct = new OID_literal;
oi_struct->nof_elems = nof_elems;
oi_struct->oid_id = "oi_" + Int2string(oid_literals.size());
oid_literals.add(oi_str, oi_struct);
return oi_struct->oid_id;
}
}
void Module::add_type_conv(TypeConv *p_conv)
{
if (p_conv == NULL) FATAL_ERROR("Module::add_type_conv()");
Type *p_from_type = p_conv->get_from_type();
Type *p_to_type = p_conv->get_to_type();
if (!p_from_type->is_structured_type()
|| !p_to_type->is_structured_type())
FATAL_ERROR("Module::add_type_conv()");
if (p_from_type == p_to_type) {
// Never add the same types.
delete p_conv;
return;
}
for (size_t i = 0; i < type_conv_v.size(); i++) {
TypeConv *conv = type_conv_v[i];
if (conv->get_from_type() == p_from_type
&& conv->get_to_type() == p_to_type
&& conv->is_temp() == p_conv->is_temp()) {
// Same pair of types, both for values or templates. We're the
// owners, deallocate.
delete p_conv;
return;
}
}
type_conv_v.add(p_conv);
}
bool Module::needs_type_conv(Type *p_from_type, Type *p_to_type) const
{
for (size_t i = 0; i < type_conv_v.size(); i++) {
TypeConv *conv = type_conv_v[i];
if (conv->get_from_type() == p_from_type
&& conv->get_to_type() == p_to_type)
return true;
}
return false;
}
void Module::chk_recursive(module_set_t& checked_modules)
{
if (checked_modules.has_key(this)) return;
// this must be added to the set at the beginning
// in order to deal with circular imports
checked_modules.add(this, 0);
// check the imported modules first
size_t nof_visible_mods = visible_mods.size();
for (size_t i = 0; i < nof_visible_mods; i++) {
Module *m = visible_mods.get_nth_key(i);
if (m != this) m->chk_recursive(checked_modules);
}
// then check the module itself
chk(); // this is the only virtual call
}
bool Module::is_visible(Module *m)
{
collect_visible_mods();
return visible_mods.has_key(m);
}
void Module::get_visible_mods(module_set_t& p_visible_mods)
{
if (visible_mods.has_key(this)) {
size_t nof_visible_mods = visible_mods.size();
for (size_t i = 0; i < nof_visible_mods; i++) {
Module *m = visible_mods.get_nth_key(i);
if (!p_visible_mods.has_key(m)) p_visible_mods.add(m, 0);
}
} else {
get_imported_mods(p_visible_mods);
}
}
void Module::write_checksum()
{
fprintf(stderr, "%-18s ", modid->get_dispname().c_str());
switch (moduletype) {
case MOD_TTCN: fprintf(stderr, "%-15s ", "TTCN-3"); break;
case MOD_ASN: fprintf(stderr, "%-15s ", "ASN.1"); break;
case MOD_UNKNOWN: fprintf(stderr, "%-15s ", "OTHER"); break;
}
if (!has_checksum) {
fputc('\n', stderr);
return;
}
size_t nof_checksum = sizeof(module_checksum);
for (size_t i = 0; i < nof_checksum; i++) {
fprintf(stderr, "%02x", module_checksum[i]);
}
if (release <= 999999 && patch < 30 && build < 100) {
char *product_identifier =
get_product_identifier(product_number, suffix, release, patch, build, extra);
fprintf(stderr, " %s", product_identifier);
Free(product_identifier);
}
fputc('\n', stderr);
}
char* Module::get_product_identifier(const char* product_number,
const unsigned int suffix, unsigned int release, unsigned int patch,
unsigned int build, const char* extra)
{
expstring_t ret_val = memptystr();
if ( product_number == NULL
&& suffix == UINT_MAX
&& release == UINT_MAX
&& patch == UINT_MAX
&& build == UINT_MAX) {
ret_val = mputstr(ret_val, "<RnXnn>");
return ret_val;
}
if (product_number != NULL) {
ret_val = mputstr(ret_val, product_number);
if (suffix != 0) {
ret_val = mputprintf(ret_val, "/%d", suffix);
}
ret_val = mputc(ret_val, ' ');
}
char* build_str = buildstr(build);
ret_val = mputprintf(ret_val, "R%u%c%s%s", release, eri(patch), build_str, extra ? extra : "");
Free(build_str);
return ret_val;
}
void Module::collect_visible_mods()
{
if (!visible_mods.has_key(this)) {
get_imported_mods(visible_mods);
if (!visible_mods.has_key(this)) visible_mods.add(this, 0);
}
}
void Module::set_checksum(size_t checksum_len,
const unsigned char* checksum_ptr)
{
if (checksum_len != sizeof(module_checksum))
FATAL_ERROR("Module::set_checksum(): invalid length");
memcpy(module_checksum, checksum_ptr, sizeof(module_checksum));
has_checksum = true;
}
void Module::set_controlns(char *ns, char *prefix)
{
Free(control_ns);
control_ns = ns;
Free(control_ns_prefix);
control_ns_prefix = prefix;
}
void Module::get_controlns(const char *&ns, const char *&prefix)
{
ns = control_ns;
prefix = control_ns_prefix;
}
const size_t max_invented_prefixes = 10000;
void Module::add_namespace(const char *new_uri, char *&new_prefix)
{
const bool prefix_is_empty = new_prefix && !*new_prefix;
const string key(new_prefix ? new_prefix : "");
if (new_prefix && !namespaces.has_key(key)) {
namespaces.add(key, new_uri);
if (*new_prefix == 0) { // first add of default namespace
++default_namespace_attempt;
}
return; // newly added
}
else { // prefix already present (or we are required to invent one)
if (new_prefix) {
const char *uri_value = namespaces[key];
if (!strcmp(uri_value, new_uri)) return; // same URI, same prefix: no-op
}
// prefix already present but different URI,
// or prefix is NULL (which means we must invent a prefix)
if (new_prefix && *new_prefix) {
Free(new_prefix); // prefix is not empty, discard it and start fresh
new_prefix = memptystr();
}
const string uri_key(new_uri);
if (invented_prefixes.has_key(uri_key)) {
// we already made one up for this URI
new_prefix = mputstr(new_prefix, invented_prefixes[uri_key]);
return; // already there
}
else {
// make one up on the spot
size_t iidx = invented_prefixes.size(); // "invented index"
new_prefix = mputprintf(new_prefix, "tq%04lu", (unsigned long)iidx++);
string made_up_prefix(new_prefix);
for (; iidx < max_invented_prefixes; ++iidx) {
if (namespaces.has_key(made_up_prefix)) {
// Some pervert wrote an XSD with a namespace prefix like tq0007!
// Make up another one in the same memory spot.
sprintf(new_prefix, "tq%04lu", (unsigned long)iidx);
made_up_prefix = new_prefix;
}
else break;
}
if (iidx >= max_invented_prefixes) {
Location loc; // no particular location
loc.error("Internal limit: too many assigned prefixes");
return; // not added
}
invented_prefixes.add(uri_key, new_prefix);
namespaces.add(made_up_prefix, new_uri);
// Search for the newly added prefix and remember it.
replacement_for_empty_prefix = namespaces.find_key(made_up_prefix);
if (prefix_is_empty) {
++default_namespace_attempt;
}
return; // newly added
}
} // if (present)
}
static const string empty_prefix;
void Module::rename_default_namespace()
{
if (default_namespace_attempt < 2) return;
// There was more than one default namespace. However, all but the first
// are already renamed to tq%d.
size_t di = namespaces.find_key(empty_prefix); // keys are prefixes
if (di < namespaces.size()) { // found it
const char *last_remaining_def_namespace = namespaces.get_nth_elem(di);
// we can't change a key, we can only remove and re-add it
namespaces.erase(empty_prefix);
expstring_t empty_prefix_string = NULL; // force a made-up prefix
add_namespace(last_remaining_def_namespace, empty_prefix_string);
Free(empty_prefix_string);
}
else FATAL_ERROR("Module::rename_default_namespace");
}
size_t Module::get_ns_index(const char *prefix)
{
size_t idx = namespaces.find_key(string(prefix));
if (idx >= namespaces.size()) { // not found
// If the the caller asked for the empty prefix and it wasn't found
// because it has been replaced, use the replacement.
if (*prefix == '\0' && replacement_for_empty_prefix != (size_t)-1) {
idx = replacement_for_empty_prefix;
}
else FATAL_ERROR("Module::get_ns_index()");
}
// Remember that the index is used by this module
if (!used_namespaces.has_key(idx)) {
used_namespaces.add(idx, NULL);
}
return idx;
}
string Module::get_temporary_id()
{
static const string tmp_prefix("tmp_");
return tmp_prefix + Int2string(tmp_id_count++);
}
void Module::generate_code(CodeGenHelper& cgh)
{
if (!gen_code) {
nof_notupdated_files += 2;
DEBUG(1, "Code not generated for module `%s'.",
modid->get_dispname().c_str());
return;
}
DEBUG(1, "Generating code for module `%s'.",
modid->get_dispname().c_str());
// TODO: Always assume to have circular imports until
// full program optimization is available,
// this increases the size of the generated code,
// but otherwise it is possible to create uncompilable code:
// 1) let the project of module A and B refer to each other,
// 2) A refers B, and compile A
// 3) edit B to refer to A and compile it ...
// As the code for A can not be rewritten the code will not compile
cgh.add_module(modid->get_name(), modid->get_ttcnname(),
moduletype == MOD_TTCN, true);
cgh.set_current_module(modid->get_ttcnname());
// language specific parts (definitions, imports, etc.)
//generate_code_internal(&target); <- needed to pass cgh
generate_code_internal(cgh);
output_struct* output = cgh.get_current_outputstruct();
CodeGenHelper::update_intervals(output);
// string literals
generate_literals(output);
// module level entry points
generate_functions(output);
CodeGenHelper::update_intervals(output); // maybe deeper in generate_functions
// type conversion functions for type compatibility
generate_conversion_functions(output);
CodeGenHelper::update_intervals(output); // maybe deeper in conv_funcs
/* generate the initializer function for the TTCN-3 profiler
* (this is done at the end of the code generation, to make sure all code
* lines have been added to the profiler database) */
if (is_file_profiled(get_filename())) {
output->source.global_vars = mputstr(output->source.global_vars,
"\n/* Initializing TTCN-3 profiler */\n"
"void init_ttcn3_profiler()\n"
"{\n");
char* function_name = 0;
int line_no = -1;
while (get_profiler_code_line(get_filename(), &function_name, &line_no)) {
output->source.global_vars = mputprintf(output->source.global_vars,
" ttcn3_prof.create_line(ttcn3_prof.get_element(\"%s\"), %d);\n",
get_filename(), line_no);
if (0 != function_name) {
output->source.global_vars = mputprintf(output->source.global_vars,
" ttcn3_prof.create_function(ttcn3_prof.get_element(\"%s\"), %d, \"%s\");\n",
get_filename(), line_no, function_name);
}
}
output->source.global_vars = mputstr(output->source.global_vars, "}\n");
if (split_to_slices) {
output->header.global_vars = mputstr(output->header.global_vars,
"extern void init_ttcn3_profiler();\n");
}
}
/* TTCN-3 debugger:
generate the printing function for the types defined in this module
and initialize the debugger with this module's global variables,
component types and the components' variables */
if (debugger_active) {
generate_debugger_functions(output);
generate_debugger_init(output);
}
}
void Module::dump(unsigned level) const
{
DEBUG(level, "Module: %s", get_modid().get_dispname().c_str());
}
// =================================
// ===== Assignments
// =================================
Assignments *Assignments::get_scope_asss()
{
return this;
}
Assignment *Assignments::get_ass_bySRef(Ref_simple *p_ref)
{
if (!p_ref || !parent_scope)
FATAL_ERROR("NULL parameter: Common::Assignments::get_ass_bySRef()");
if (!p_ref->get_modid()) {
const Identifier *id = p_ref->get_id();
if (id && has_local_ass_withId(*id)) return get_local_ass_byId(*id);
}
return parent_scope->get_ass_bySRef(p_ref);
}
bool Assignments::has_ass_withId(const Identifier& p_id)
{
if (has_local_ass_withId(p_id)) return true;
else if (parent_scope) return parent_scope->has_ass_withId(p_id);
else return false;
}
// =================================
// ===== Assignment
// =================================
Assignment::Assignment(asstype_t p_asstype, Identifier *p_id)
: asstype(p_asstype), id(p_id), my_scope(0), checked(false),
visibilitytype(PUBLIC)
{
if (!id) FATAL_ERROR("Assignment::Assignment(): NULL parameter");
}
Assignment::Assignment(const Assignment& p)
: Node(p), Location(p), asstype(p.asstype), id(p.id->clone()), my_scope(0),
checked(false), visibilitytype(p.visibilitytype)
{
}
Assignment::~Assignment()
{
delete id;
}
Assignment::asstype_t Assignment::get_asstype() const
{
return asstype;
}
const char *Assignment::get_assname() const
{
switch (get_asstype()) {
case A_TYPE:
return "type";
case A_CONST:
if (my_scope && my_scope->get_scope_mod()->get_moduletype() ==
Module::MOD_ASN) return "value";
else return "constant";
case A_ERROR:
return "erroneous assignment";
case A_OC:
return "information object class";
case A_OBJECT:
return "information object";
case A_OS:
return "information object set";
case A_VS:
return "value set";
case A_EXT_CONST:
return "external constant";
case A_MODULEPAR:
return "module parameter";
case A_MODULEPAR_TEMP:
return "template module parameter";
case A_TEMPLATE:
return "template";
case A_VAR:
return "variable";
case A_VAR_TEMPLATE:
return "template variable";
case A_TIMER:
return "timer";
case A_PORT:
return "port";
case A_FUNCTION:
case A_FUNCTION_RVAL:
case A_FUNCTION_RTEMP:
return "function";
case A_EXT_FUNCTION:
case A_EXT_FUNCTION_RVAL:
case A_EXT_FUNCTION_RTEMP:
return "external function";
case A_ALTSTEP:
return "altstep";
case A_TESTCASE:
return "testcase";
case A_PAR_VAL:
case A_PAR_VAL_IN:
return "value parameter";
case A_PAR_VAL_OUT:
return "`out' value parameter";
case A_PAR_VAL_INOUT:
return "`inout' value parameter";
case A_PAR_TEMPL_IN:
return "template parameter";
case A_PAR_TEMPL_OUT:
return "`out' template parameter";
case A_PAR_TEMPL_INOUT:
return "`inout' template parameter";
case A_PAR_TIMER:
return "timer parameter";
case A_PAR_PORT:
return "port parameter";
default:
return "<unknown>";
}
}
string Assignment::get_description()
{
string ret_val(get_assname());
ret_val += " `";
switch (asstype) {
case A_PAR_VAL:
case A_PAR_VAL_IN:
case A_PAR_VAL_OUT:
case A_PAR_VAL_INOUT:
case A_PAR_TEMPL_IN:
case A_PAR_TEMPL_OUT:
case A_PAR_TEMPL_INOUT:
case A_PAR_TIMER:
case A_PAR_PORT:
// parameter is identified using its id
ret_val += id->get_dispname();
break;
case A_CONST:
case A_TEMPLATE:
case A_VAR:
case A_VAR_TEMPLATE:
case A_TIMER:
// these can be both local and global
if (is_local()) ret_val += id->get_dispname();
else ret_val += get_fullname();
break;
default:
// the rest is always global
ret_val += get_fullname();
}
ret_val += "'";
return ret_val;
}
void Assignment::set_my_scope(Scope *p_scope)
{
my_scope=p_scope;
}
bool Assignment::is_local() const
{
return false;
}
Setting *Assignment::get_Setting()
{
FATAL_ERROR("Common::Assignment::get_Setting()");
return 0;
}
Type *Assignment::get_Type()
{
FATAL_ERROR("Common::Assignment::get_Type()");
return 0;
}
Value *Assignment::get_Value()
{
FATAL_ERROR("Common::Assignment::get_Value()");
return 0;
}
Ttcn::Template *Assignment::get_Template()
{
FATAL_ERROR("Common::Assignment::get_Template()");
return 0;
}
bool Assignment::get_lazy_eval() const
{
FATAL_ERROR("Common::Assignment::get_lazy_eval()");
return false;
}
Ttcn::FormalParList *Assignment::get_FormalParList()
{
return 0;
}
Ttcn::ArrayDimensions *Assignment::get_Dimensions()
{
return 0;
}
Type *Assignment::get_RunsOnType()
{
return 0;
}
void Assignment::chk_ttcn_id()
{
if(!my_scope) return;
if(!get_id().get_has_valid(Identifier::ID_TTCN)
&& my_scope->get_parent_scope()==my_scope->get_scope_mod()
// <internal> or <error> ...
&& my_scope->get_scope_mod()->get_modid().get_dispname()[0]!='<')
warning("The identifier `%s' is not reachable from TTCN-3",
get_id().get_dispname().c_str());
}
// *this is the (var/const/modulepar/etc.) definition we want to access.
// p_scope is the location from where we want to reach the definition.
string Assignment::get_genname_from_scope(Scope *p_scope,
const char *p_prefix)
{
if (!p_scope || !my_scope)
FATAL_ERROR("Assignment::get_genname_from_scope()");
string ret_val;
Module *my_mod = my_scope->get_scope_mod_gen();
if ((my_mod != p_scope->get_scope_mod_gen()) &&
!Asn::Assignments::is_spec_asss(my_mod)) {
// when the definition is referred from another module
// the reference shall be qualified with the namespace of my module
ret_val = my_mod->get_modid().get_name();
ret_val += "::";
}
if (p_prefix) ret_val += p_prefix;
ret_val += get_genname();
// add the cast to real type if its a lazy formal paramter
switch (asstype) {
case A_PAR_VAL:
case A_PAR_VAL_IN:
case A_PAR_TEMPL_IN:
if (get_lazy_eval() && p_prefix==NULL) {
Type* type = get_Type();
string type_genname = (asstype==A_PAR_TEMPL_IN) ? type->get_genname_template(p_scope) : type->get_genname_value(p_scope);
ret_val = string("((") + type_genname + string("&)") + ret_val + string(")");
}
break;
default:
// nothing to do
break;
}
return ret_val;
}
const char *Assignment::get_module_object_name()
{
if (!my_scope) FATAL_ERROR("Assignment::get_module_object_name()");
return "module_object";
}
void Assignment::use_as_lvalue(const Location&)
{
FATAL_ERROR("Common::Assignment::use_as_lvalue()");
}
void Assignment::generate_code(output_struct *, bool)
{
}
void Assignment::generate_code(CodeGenHelper&)
{
}
void Assignment::dump(unsigned level) const
{
DEBUG(level, "Assignment: %s (%d)",
id->get_dispname().c_str(), asstype);
}
Ttcn::Group* Assignment::get_parent_group()
{
return NULL;
}
} // namespace Common
|