aboutsummaryrefslogtreecommitdiffstats
path: root/libsolidity/parsing/Parser.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'libsolidity/parsing/Parser.cpp')
-rw-r--r--libsolidity/parsing/Parser.cpp28
1 files changed, 2 insertions, 26 deletions
diff --git a/libsolidity/parsing/Parser.cpp b/libsolidity/parsing/Parser.cpp
index b78c58c6..3f4a015b 100644
--- a/libsolidity/parsing/Parser.cpp
+++ b/libsolidity/parsing/Parser.cpp
@@ -1555,32 +1555,8 @@ ASTPointer<Expression> Parser::parsePrimaryExpression()
expression = nodeFactory.createNode<TupleExpression>(components, isArray);
break;
}
- case Token::IllegalHex:
- fatalParserError("Expected even number of hex-nibbles within double-quotes.");
- break;
- case Token::IllegalCommentTerminator:
- fatalParserError("Expected multi-line comment-terminator.");
- break;
- case Token::IllegalStringEscape:
- fatalParserError("Invalid String Escape.");
- break;
- case Token::IllegalStringEndQuote:
- fatalParserError("Expected String end-quote.");
- break;
- case Token::IllegalNumberSeparator:
- fatalParserError("Invalid use of Number Separator '_'.");
- break;
- case Token::IllegalHexDigit:
- fatalParserError("Hex Digit missing or invalid.");
- break;
- case Token::IllegalOctalNotAllowed:
- fatalParserError("Octal Numbers not allowed.");
- break;
- case Token::IllegalExponent:
- fatalParserError("Invalid Exponent.");
- break;
- case Token::IllegalNumberEnd:
- fatalParserError("Digit or Identifier-Start not allowed at end of Number.");
+ case Token::Illegal:
+ fatalParserError(to_string(m_scanner->currentError()));
break;
default:
if (TokenTraits::isElementaryTypeName(token))
='#n138'>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
/*
    This file is part of solidity.

    solidity is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    solidity is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with solidity.  If not, see <http://www.gnu.org/licenses/>.
*/
/**
 * @author Christian <c@ethdev.com>
 * @author Gav Wood <g@ethdev.com>
 * @date 2014
 * Full-stack compiler that converts a source code string to bytecode.
 */


#include <libsolidity/interface/CompilerStack.h>

#include <libsolidity/interface/Version.h>
#include <libsolidity/analysis/SemVerHandler.h>
#include <libsolidity/ast/AST.h>
#include <libsolidity/parsing/Scanner.h>
#include <libsolidity/parsing/Parser.h>
#include <libsolidity/analysis/GlobalContext.h>
#include <libsolidity/analysis/NameAndTypeResolver.h>
#include <libsolidity/analysis/TypeChecker.h>
#include <libsolidity/analysis/DocStringAnalyser.h>
#include <libsolidity/analysis/StaticAnalyzer.h>
#include <libsolidity/analysis/PostTypeChecker.h>
#include <libsolidity/analysis/SyntaxChecker.h>
#include <libsolidity/codegen/Compiler.h>
#include <libsolidity/formal/SMTChecker.h>
#include <libsolidity/interface/ABI.h>
#include <libsolidity/interface/Natspec.h>
#include <libsolidity/interface/GasEstimator.h>

#include <libevmasm/Exceptions.h>

#include <libdevcore/SwarmHash.h>
#include <libdevcore/JSON.h>

#include <json/json.h>

#include <boost/algorithm/string.hpp>

using namespace std;
using namespace dev;
using namespace dev::solidity;

void CompilerStack::setRemappings(vector<string> const& _remappings)
{
    vector<Remapping> remappings;
    for (auto const& remapping: _remappings)
    {
        auto eq = find(remapping.begin(), remapping.end(), '=');
        if (eq == remapping.end())
            continue; // ignore
        auto colon = find(remapping.begin(), eq, ':');
        Remapping r;
        r.context = colon == eq ? string() : string(remapping.begin(), colon);
        r.prefix = colon == eq ? string(remapping.begin(), eq) : string(colon + 1, eq);
        r.target = string(eq + 1, remapping.end());
        remappings.push_back(r);
    }
    swap(m_remappings, remappings);
}

void CompilerStack::reset(bool _keepSources)
{
    if (_keepSources)
    {
        m_stackState = SourcesSet;
        for (auto sourcePair: m_sources)
            sourcePair.second.reset();
    }
    else
    {
        m_stackState = Empty;
        m_sources.clear();
    }
    m_libraries.clear();
    m_optimize = false;
    m_optimizeRuns = 200;
    m_globalContext.reset();
    m_scopes.clear();
    m_sourceOrder.clear();
    m_contracts.clear();
    m_errorReporter.clear();
}

bool CompilerStack::addSource(string const& _name, string const& _content, bool _isLibrary)
{
    bool existed = m_sources.count(_name) != 0;
    reset(true);
    m_sources[_name].scanner = make_shared<Scanner>(CharStream(_content), _name);
    m_sources[_name].isLibrary = _isLibrary;
    m_stackState = SourcesSet;
    return existed;
}

bool CompilerStack::parse()
{
    //reset
    if(m_stackState != SourcesSet)
        return false;
    m_errorReporter.clear();
    ASTNode::resetID();

    if (SemVerVersion{string(VersionString)}.isPrerelease())
        m_errorReporter.warning("This is a pre-release compiler version, please do not use it in production.");

    vector<string> sourcesToParse;
    for (auto const& s: m_sources)
        sourcesToParse.push_back(s.first);
    for (size_t i = 0; i < sourcesToParse.size(); ++i)
    {
        string const& path = sourcesToParse[i];
        Source& source = m_sources[path];
        source.scanner->reset();
        source.ast = Parser(m_errorReporter).parse(source.scanner);
        if (!source.ast)
            solAssert(!Error::containsOnlyWarnings(m_errorReporter.errors()), "Parser returned null but did not report error.");
        else
        {
            source.ast->annotation().path = path;
            for (auto const& newSource: loadMissingSources(*source.ast, path))
            {
                string const& newPath = newSource.first;
                string const& newContents = newSource.second;
                m_sources[newPath].scanner = make_shared<Scanner>(CharStream(newContents), newPath);
                sourcesToParse.push_back(newPath);
            }
        }
    }
    if (Error::containsOnlyWarnings(m_errorReporter.errors()))
    {
        m_stackState = ParsingSuccessful;
        return true;
    }
    else
        return false;
}

bool CompilerStack::analyze()
{
    if (m_stackState != ParsingSuccessful)
        return false;
    resolveImports();

    bool noErrors = true;
    SyntaxChecker syntaxChecker(m_errorReporter);
    for (Source const* source: m_sourceOrder)
        if (!syntaxChecker.checkSyntax(*source->ast))
            noErrors = false;

    DocStringAnalyser docStringAnalyser(m_errorReporter);
    for (Source const* source: m_sourceOrder)
        if (!docStringAnalyser.analyseDocStrings(*source->ast))
            noErrors = false;

    m_globalContext = make_shared<GlobalContext>();
    NameAndTypeResolver resolver(m_globalContext->declarations(), m_scopes, m_errorReporter);
    for (Source const* source: m_sourceOrder)
        if (!resolver.registerDeclarations(*source->ast))
            return false;

    map<string, SourceUnit const*> sourceUnitsByName;
    for (auto& source: m_sources)
        sourceUnitsByName[source.first] = source.second.ast.get();
    for (Source const* source: m_sourceOrder)
        if (!resolver.performImports(*source->ast, sourceUnitsByName))
            return false;

    for (Source const* source: m_sourceOrder)
        for (ASTPointer<ASTNode> const& node: source->ast->nodes())
            if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
            {
                m_globalContext->setCurrentContract(*contract);
                if (!resolver.updateDeclaration(*m_globalContext->currentThis())) return false;
                if (!resolver.updateDeclaration(*m_globalContext->currentSuper())) return false;
                if (!resolver.resolveNamesAndTypes(*contract)) return false;

                contract->setDevDocumentation(Natspec::devDocumentation(*contract));
                contract->setUserDocumentation(Natspec::userDocumentation(*contract));

                // Note that we now reference contracts by their fully qualified names, and
                // thus contracts can only conflict if declared in the same source file.  This
                // already causes a double-declaration error elsewhere, so we do not report
                // an error here and instead silently drop any additional contracts we find.

                if (m_contracts.find(contract->fullyQualifiedName()) == m_contracts.end())
                    m_contracts[contract->fullyQualifiedName()].contract = contract;
            }

    TypeChecker typeChecker(m_errorReporter);
    for (Source const* source: m_sourceOrder)
        for (ASTPointer<ASTNode> const& node: source->ast->nodes())
            if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
                if (!typeChecker.checkTypeRequirements(*contract))
                    noErrors = false;

    if (noErrors)
    {
        PostTypeChecker postTypeChecker(m_errorReporter);
        for (Source const* source: m_sourceOrder)
            if (!postTypeChecker.check(*source->ast))
                noErrors = false;
    }

    if (noErrors)
    {
        StaticAnalyzer staticAnalyzer(m_errorReporter);
        for (Source const* source: m_sourceOrder)
            if (!staticAnalyzer.analyze(*source->ast))
                noErrors = false;
    }

    if (noErrors)
    {
        SMTChecker smtChecker(m_errorReporter, m_smtQuery);
        for (Source const* source: m_sourceOrder)
            smtChecker.analyze(*source->ast);
    }

    if (noErrors)
    {
        m_stackState = AnalysisSuccessful;
        return true;
    }
    else
        return false;
}

bool CompilerStack::parseAndAnalyze()
{
    return parse() && analyze();
}

bool CompilerStack::compile()
{
    if (m_stackState < AnalysisSuccessful)
        if (!parseAndAnalyze())
            return false;

    map<ContractDefinition const*, eth::Assembly const*> compiledContracts;
    for (Source const* source: m_sourceOrder)
        for (ASTPointer<ASTNode> const& node: source->ast->nodes())
            if (auto contract = dynamic_cast<ContractDefinition const*>(node.get()))
                compileContract(*contract, compiledContracts);
    this->link();
    m_stackState = CompilationSuccessful;
    return true;
}

void CompilerStack::link()
{
    for (auto& contract: m_contracts)
    {
        contract.second.object.link(m_libraries);
        contract.second.runtimeObject.link(m_libraries);
        contract.second.cloneObject.link(m_libraries);
    }
}

vector<string> CompilerStack::contractNames() const
{
    if (m_stackState < AnalysisSuccessful)
        BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Parsing was not successful."));
    vector<string> contractNames;
    for (auto const& contract: m_contracts)
        contractNames.push_back(contract.first);
    return contractNames;
}

eth::AssemblyItems const* CompilerStack::assemblyItems(string const& _contractName) const
{
    Contract const& currentContract = contract(_contractName);
    return currentContract.compiler ? &contract(_contractName).compiler->assemblyItems() : nullptr;
}

eth::AssemblyItems const* CompilerStack::runtimeAssemblyItems(string const& _contractName) const
{
    Contract const& currentContract = contract(_contractName);
    return currentContract.compiler ? &contract(_contractName).compiler->runtimeAssemblyItems() : nullptr;
}

string const* CompilerStack::sourceMapping(string const& _contractName) const
{
    Contract const& c = contract(_contractName);
    if (!c.sourceMapping)
    {
        if (auto items = assemblyItems(_contractName))
            c.sourceMapping.reset(new string(computeSourceMapping(*items)));
    }
    return c.sourceMapping.get();
}

string const* CompilerStack::runtimeSourceMapping(string const& _contractName) const
{
    Contract const& c = contract(_contractName);
    if (!c.runtimeSourceMapping)
    {
        if (auto items = runtimeAssemblyItems(_contractName))
            c.runtimeSourceMapping.reset(new string(computeSourceMapping(*items)));
    }
    return c.runtimeSourceMapping.get();
}

std::string const CompilerStack::filesystemFriendlyName(string const& _contractName) const
{
    // Look up the contract (by its fully-qualified name)
    Contract const& matchContract = m_contracts.at(_contractName);
    // Check to see if it could collide on name
    for (auto const& contract: m_contracts)
    {
        if (contract.second.contract->name() == matchContract.contract->name() &&
                contract.second.contract != matchContract.contract)
        {
            // If it does, then return its fully-qualified name, made fs-friendly
            std::string friendlyName = boost::algorithm::replace_all_copy(_contractName, "/", "_");