OLD | NEW |
1 //===- NaClBitcodeParser.h -----------------------------------*- C++ -*-===// | 1 //===- NaClBitcodeParser.h -----------------------------------*- C++ -*-===// |
2 // Low-level bitcode driver to parse PNaCl bitcode files. | 2 // Low-level bitcode driver to parse PNaCl bitcode files. |
3 // | 3 // |
4 // The LLVM Compiler Infrastructure | 4 // The LLVM Compiler Infrastructure |
5 // | 5 // |
6 // This file is distributed under the University of Illinois Open Source | 6 // This file is distributed under the University of Illinois Open Source |
7 // License. See LICENSE.TXT for details. | 7 // License. See LICENSE.TXT for details. |
8 // | 8 // |
9 //===----------------------------------------------------------------------===// | 9 //===----------------------------------------------------------------------===// |
10 // | 10 // |
(...skipping 395 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
406 /// The block ID to use if a global abbreviation. Note: This field is | 406 /// The block ID to use if a global abbreviation. Note: This field is |
407 /// updated by calls to method SetBID. | 407 /// updated by calls to method SetBID. |
408 unsigned GlobalBlockID; | 408 unsigned GlobalBlockID; |
409 }; | 409 }; |
410 | 410 |
411 /// Parses a block in the PNaCl bitcode stream. | 411 /// Parses a block in the PNaCl bitcode stream. |
412 class NaClBitcodeParser { | 412 class NaClBitcodeParser { |
413 // Allow listener privledges, so that it can update/call the parser | 413 // Allow listener privledges, so that it can update/call the parser |
414 // using a clean API. | 414 // using a clean API. |
415 friend class NaClBitcodeParserListener; | 415 friend class NaClBitcodeParserListener; |
| 416 |
| 417 // Implements an error handler for errors in the bitstream reader. |
| 418 // Redirects bitstream reader errors to corresponding parrser error |
| 419 // reporting function. |
| 420 class ErrorHandler : public NaClBitstreamCursor::ErrorHandler { |
| 421 NaClBitcodeParser *Parser; |
| 422 public: |
| 423 ErrorHandler(NaClBitcodeParser *Parser, |
| 424 NaClBitstreamCursor &Cursor): |
| 425 NaClBitstreamCursor::ErrorHandler(Cursor), Parser(Parser) {} |
| 426 LLVM_ATTRIBUTE_NORETURN |
| 427 void Fatal(const std::string &ErrorMessage) const final { |
| 428 Parser->FatalAt(getCurrentBitNo(), ErrorMessage); |
| 429 } |
| 430 ~ErrorHandler() override {} |
| 431 }; |
| 432 |
416 public: | 433 public: |
417 | |
418 // Creates a parser to parse the the block at the given cursor in | 434 // Creates a parser to parse the the block at the given cursor in |
419 // the PNaCl bitcode stream. This instance is a "dummy" instance | 435 // the PNaCl bitcode stream. This instance is a "dummy" instance |
420 // that starts the parser. | 436 // that starts the parser. |
421 explicit NaClBitcodeParser(NaClBitstreamCursor &Cursor) | 437 explicit NaClBitcodeParser(NaClBitstreamCursor &Cursor) |
422 : EnclosingParser(0), | 438 : EnclosingParser(0), |
423 Block(ILLEGAL_BLOCK_ID, Cursor), | 439 Block(ILLEGAL_BLOCK_ID, Cursor), |
424 Record(Block), | 440 Record(Block), |
425 Listener(0), | 441 Listener(0), |
426 ErrStream(&errs()) | 442 ErrStream(&errs()) { |
427 {} | 443 std::unique_ptr<NaClBitstreamCursor::ErrorHandler> |
| 444 ErrHandler(new ErrorHandler(this, Cursor)); |
| 445 Cursor.setErrorHandler(ErrHandler); |
| 446 } |
428 | 447 |
429 virtual ~NaClBitcodeParser(); | 448 virtual ~NaClBitcodeParser(); |
430 | 449 |
431 /// Reads the (top-level) block associated with the given block | 450 /// Reads the (top-level) block associated with the given block |
432 /// record at the stream cursor. Returns true if unable to parse. | 451 /// record at the stream cursor. Returns true if unable to parse. |
433 /// Can be called multiple times to parse multiple blocks. | 452 /// Can be called multiple times to parse multiple blocks. |
434 bool Parse(); | 453 bool Parse(); |
435 | 454 |
436 // Called once the bitstream reader has entered the corresponding | 455 // Called once the bitstream reader has entered the corresponding |
437 // subblock. Argument NumWords is set to the number of words in the | 456 // subblock. Argument NumWords is set to the number of words in the |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
476 // 2) Temporarily modify it for a single error message. In this context, | 495 // 2) Temporarily modify it for a single error message. In this context, |
477 // the method Error() is overridden in the derived class, and | 496 // the method Error() is overridden in the derived class, and |
478 // calls this method twice. Once before calling base method Error(), | 497 // calls this method twice. Once before calling base method Error(), |
479 // and followed by a second call to restore the default error stream. | 498 // and followed by a second call to restore the default error stream. |
480 raw_ostream &setErrStream(raw_ostream &Stream) { | 499 raw_ostream &setErrStream(raw_ostream &Stream) { |
481 raw_ostream &OldErrStream = *ErrStream; | 500 raw_ostream &OldErrStream = *ErrStream; |
482 ErrStream = &Stream; | 501 ErrStream = &Stream; |
483 return OldErrStream; | 502 return OldErrStream; |
484 } | 503 } |
485 | 504 |
486 // Called when error occurs. Message is the error to report. Always | 505 // Called when an error occurs. BitPosition is the bit position the |
| 506 // error was found, and Message is the error to report. Always |
487 // returns true (the error return value of Parse). | 507 // returns true (the error return value of Parse). |
488 virtual bool Error(const std::string &Message) { | 508 virtual bool ErrorAt(uint64_t BitPosition, const std::string &Message); |
489 *ErrStream << "Error: " << Message << "\n"; | 509 |
490 return true; | 510 // Called when an error occurs. Message is the error to |
| 511 // report. Always returns true (the error return value of Parse). |
| 512 bool Error(const std::string &Message) { |
| 513 return ErrorAt(Record.GetStartBit(), Message); |
| 514 } |
| 515 |
| 516 // Called when a fatal error occurs. BitPosition is the bit position |
| 517 // the error was found, and Message is the error to report. Does not |
| 518 // return. |
| 519 LLVM_ATTRIBUTE_NORETURN |
| 520 virtual void FatalAt(uint64_t BitPosition, const std::string &Message); |
| 521 |
| 522 // Called when a fatal error occurs. Message is the error to |
| 523 // report. Does not return. |
| 524 LLVM_ATTRIBUTE_NORETURN |
| 525 void Fatal(const std::string &Message) { |
| 526 FatalAt(Record.GetStartBit(), Message); |
| 527 } |
| 528 |
| 529 // Generates fatal generic error message. |
| 530 LLVM_ATTRIBUTE_NORETURN |
| 531 void Fatal() { |
| 532 Fatal("Fatal error occurred!"); |
491 } | 533 } |
492 | 534 |
493 // Returns the number of bits in this block, including nested blocks. | 535 // Returns the number of bits in this block, including nested blocks. |
494 unsigned GetBlockNumBits() const { | 536 unsigned GetBlockNumBits() const { |
495 return Block.GetNumBits(); | 537 return Block.GetNumBits(); |
496 } | 538 } |
497 | 539 |
498 // Returns the number of bits in this block, excluding nested blocks. | 540 // Returns the number of bits in this block, excluding nested blocks. |
499 unsigned GetBlockLocalNumBits() const { | 541 unsigned GetBlockLocalNumBits() const { |
500 return Block.GetLocalNumBits(); | 542 return Block.GetLocalNumBits(); |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
593 } | 635 } |
594 | 636 |
595 // Parses a BlockInfo block, where processing is handled through | 637 // Parses a BlockInfo block, where processing is handled through |
596 // a listener in the bitstream reader. | 638 // a listener in the bitstream reader. |
597 bool ParseBlockInfoInternal(); | 639 bool ParseBlockInfoInternal(); |
598 | 640 |
599 // Parses the non-BlockInfo block. Returns true if unable to parse the | 641 // Parses the non-BlockInfo block. Returns true if unable to parse the |
600 // block. | 642 // block. |
601 bool ParseBlockInternal(); | 643 bool ParseBlockInternal(); |
602 | 644 |
603 | |
604 void operator=(const NaClBitcodeParser &Parser) LLVM_DELETED_FUNCTION; | 645 void operator=(const NaClBitcodeParser &Parser) LLVM_DELETED_FUNCTION; |
605 NaClBitcodeParser(const NaClBitcodeParser &Parser) LLVM_DELETED_FUNCTION; | 646 NaClBitcodeParser(const NaClBitcodeParser &Parser) LLVM_DELETED_FUNCTION; |
606 | 647 |
607 }; | 648 }; |
608 | 649 |
609 } // namespace llvm | 650 } // namespace llvm |
610 | 651 |
611 #endif | 652 #endif |
OLD | NEW |