1 /* 2 * Copyright 2017, The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef WORD_STREAM_H 18 #define WORD_STREAM_H 19 20 #include "core_defs.h" 21 #include "types_generated.h" 22 23 #include <stdint.h> 24 25 #include <string> 26 #include <vector> 27 28 namespace android { 29 namespace spirit { 30 31 struct IdRef; 32 class Instruction; 33 34 class InputWordStream { 35 public: 36 static InputWordStream *Create(); 37 static InputWordStream *Create(std::vector<uint32_t> &&words); 38 static InputWordStream *Create(const std::vector<uint32_t> &words); 39 static InputWordStream *Create(const std::vector<uint8_t> &bytes); 40 static InputWordStream *Create(const char *fileName); 41 ~InputWordStream()42 virtual ~InputWordStream() {} 43 44 virtual bool empty() const = 0; 45 virtual uint32_t operator*() = 0; 46 47 virtual InputWordStream &operator>>(uint32_t *RHS) = 0; 48 virtual InputWordStream &operator>>(LiteralContextDependentNumber *num) = 0; 49 virtual InputWordStream &operator>>(std::string *str) = 0; 50 51 InputWordStream &operator>>(int32_t *RHS) { return *this >> (uint32_t *)RHS; } 52 53 InputWordStream &operator>>(OpCodeAndWordCount *codeCount) { 54 uint32_t word; 55 *this >> &word; 56 *codeCount = word; 57 return *this; 58 } 59 60 InputWordStream &operator>>(IdRef *RHS) { 61 // The referred instruction will be resolved later towards the end of the 62 // deserialization of the module after all instructions have been 63 // deserialized. 64 // It cannot be resolved here because it may be a forward reference. 65 RHS->mInstruction = nullptr; 66 return *this >> &RHS->mId; 67 ; 68 } 69 70 InputWordStream &operator>>(PairLiteralIntegerIdRef *RHS) { 71 return *this >> &RHS->mField0 >> &RHS->mField1; 72 } 73 74 InputWordStream &operator>>(PairIdRefLiteralInteger *RHS) { 75 return *this >> &RHS->mField0 >> &RHS->mField1; 76 } 77 78 InputWordStream &operator>>(PairIdRefIdRef *RHS) { 79 return *this >> &RHS->mField0 >> &RHS->mField1; 80 } 81 82 #define HANDLE_ENUM(Enum) \ 83 InputWordStream &operator>>(Enum *RHS) { return *this >> (uint32_t *)RHS; } 84 #include "enum_dispatches_generated.h" 85 #undef HANDLE_ENUM 86 }; 87 88 class OutputWordStream { 89 public: 90 static OutputWordStream *Create(); 91 ~OutputWordStream()92 virtual ~OutputWordStream() {} 93 94 virtual std::vector<uint32_t> getWords() = 0; 95 96 virtual OutputWordStream &operator<<(const uint32_t RHS) = 0; 97 virtual OutputWordStream & 98 operator<<(const LiteralContextDependentNumber &RHS) = 0; 99 virtual OutputWordStream &operator<<(const std::string &str) = 0; 100 101 OutputWordStream &operator<<(const int32_t RHS) { 102 return *this << (uint32_t)RHS; 103 } 104 105 OutputWordStream &operator<<(const OpCodeAndWordCount codeCount) { 106 return *this << (uint32_t)codeCount; 107 } 108 109 OutputWordStream &operator<<(const IdRef &RHS) { 110 return *this << RHS.mId; 111 } 112 113 OutputWordStream &operator<<(const PairLiteralIntegerIdRef &RHS) { 114 return *this << RHS.mField0 << RHS.mField1; 115 } 116 117 OutputWordStream &operator<<(const PairIdRefLiteralInteger &RHS) { 118 return *this << RHS.mField0 << RHS.mField1; 119 } 120 121 OutputWordStream &operator<<(const PairIdRefIdRef &RHS) { 122 return *this << RHS.mField0 << RHS.mField1; 123 } 124 125 #define HANDLE_ENUM(Enum) \ 126 OutputWordStream &operator<<(const Enum RHS) { \ 127 return *this << static_cast<uint32_t>(RHS); \ 128 } 129 #include "enum_dispatches_generated.h" 130 #undef HANDLE_ENUM 131 }; 132 133 class WordStream : public InputWordStream, public OutputWordStream { 134 public: 135 static WordStream *Create(); 136 static WordStream *Create(const std::vector<uint32_t> &words); 137 ~WordStream()138 virtual ~WordStream() {} 139 }; 140 141 } // namespace spirit 142 } // namespace android 143 144 #endif // WORD_STREAM_H 145