1 /* 2 * Copyright 2017 WebAssembly Community Group participants 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 wasm_ir_import_h 18 #define wasm_ir_import_h 19 20 #include "literal.h" 21 #include "wasm.h" 22 23 namespace wasm { 24 25 // Collects info on imports, into a form convenient for summarizing 26 // and searching. 27 struct ImportInfo { 28 Module& wasm; 29 30 std::vector<Global*> importedGlobals; 31 std::vector<Function*> importedFunctions; 32 std::vector<Event*> importedEvents; 33 ImportInfoImportInfo34 ImportInfo(Module& wasm) : wasm(wasm) { 35 for (auto& import : wasm.globals) { 36 if (import->imported()) { 37 importedGlobals.push_back(import.get()); 38 } 39 } 40 for (auto& import : wasm.functions) { 41 if (import->imported()) { 42 importedFunctions.push_back(import.get()); 43 } 44 } 45 for (auto& import : wasm.events) { 46 if (import->imported()) { 47 importedEvents.push_back(import.get()); 48 } 49 } 50 } 51 getImportedGlobalImportInfo52 Global* getImportedGlobal(Name module, Name base) { 53 for (auto* import : importedGlobals) { 54 if (import->module == module && import->base == base) { 55 return import; 56 } 57 } 58 return nullptr; 59 } 60 getImportedFunctionImportInfo61 Function* getImportedFunction(Name module, Name base) { 62 for (auto* import : importedFunctions) { 63 if (import->module == module && import->base == base) { 64 return import; 65 } 66 } 67 return nullptr; 68 } 69 getImportedEventImportInfo70 Event* getImportedEvent(Name module, Name base) { 71 for (auto* import : importedEvents) { 72 if (import->module == module && import->base == base) { 73 return import; 74 } 75 } 76 return nullptr; 77 } 78 getNumImportedGlobalsImportInfo79 Index getNumImportedGlobals() { return importedGlobals.size(); } 80 getNumImportedFunctionsImportInfo81 Index getNumImportedFunctions() { return importedFunctions.size(); } 82 getNumImportedEventsImportInfo83 Index getNumImportedEvents() { return importedEvents.size(); } 84 getNumImportsImportInfo85 Index getNumImports() { 86 return getNumImportedGlobals() + getNumImportedFunctions() + 87 getNumImportedEvents() + (wasm.memory.imported() ? 1 : 0) + 88 (wasm.table.imported() ? 1 : 0); 89 } 90 getNumDefinedGlobalsImportInfo91 Index getNumDefinedGlobals() { 92 return wasm.globals.size() - getNumImportedGlobals(); 93 } 94 getNumDefinedFunctionsImportInfo95 Index getNumDefinedFunctions() { 96 return wasm.functions.size() - getNumImportedFunctions(); 97 } 98 getNumDefinedEventsImportInfo99 Index getNumDefinedEvents() { 100 return wasm.events.size() - getNumImportedEvents(); 101 } 102 }; 103 104 } // namespace wasm 105 106 #endif // wasm_ir_import_h 107