OMCompiler/Compiler/NFFrontEnd/NFImport.mo
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file is part of OpenModelica. | ||
| 3 | * | ||
| 4 | * Copyright (c) 1998-2026, Open Source Modelica Consortium (OSMC), | ||
| 5 | * c/o Linköpings universitet, Department of Computer and Information Science, | ||
| 6 | * SE-58183 Linköping, Sweden. | ||
| 7 | * | ||
| 8 | * All rights reserved. | ||
| 9 | * | ||
| 10 | * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF AGPL VERSION 3 LICENSE OR | ||
| 11 | * THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.8. | ||
| 12 | * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES | ||
| 13 | * RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GNU AGPL | ||
| 14 | * VERSION 3, ACCORDING TO RECIPIENTS CHOICE. | ||
| 15 | * | ||
| 16 | * The OpenModelica software and the OSMC (Open Source Modelica Consortium) | ||
| 17 | * Public License (OSMC-PL) are obtained from OSMC, either from the above | ||
| 18 | * address, from the URLs: | ||
| 19 | * http://www.openmodelica.org or | ||
| 20 | * https://github.com/OpenModelica/ or | ||
| 21 | * http://www.ida.liu.se/projects/OpenModelica, | ||
| 22 | * and in the OpenModelica distribution. | ||
| 23 | * | ||
| 24 | * GNU AGPL version 3 is obtained from: | ||
| 25 | * https://www.gnu.org/licenses/licenses.html#GPL | ||
| 26 | * | ||
| 27 | * This program is distributed WITHOUT ANY WARRANTY; without | ||
| 28 | * even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 29 | * FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH | ||
| 30 | * IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL. | ||
| 31 | * | ||
| 32 | * See the full OSMC Public License conditions for more details. | ||
| 33 | * | ||
| 34 | */ | ||
| 35 | |||
| 36 | encapsulated uniontype NFImport | ||
| 37 | import NFInstNode.InstNode; | ||
| 38 | import NFInstNode; | ||
| 39 | |||
| 40 | protected | ||
| 41 | import Absyn; | ||
| 42 | import Class = NFClass; | ||
| 43 | import Inst = NFInst; | ||
| 44 | import Lookup = NFLookup; | ||
| 45 | import NFClassTree.ClassTree; | ||
| 46 | |||
| 47 | import Import = NFImport; | ||
| 48 | import Error; | ||
| 49 | import ErrorTypes; | ||
| 50 | |||
| 51 | public | ||
| 52 | record UNRESOLVED_IMPORT | ||
| 53 | Absyn.Import imp; | ||
| 54 | NFInstNode.ScopeRef scope "Weakly: the class tree owns the scope this | ||
| 55 | import sits in."; | ||
| 56 | SourceInfo info; | ||
| 57 | end UNRESOLVED_IMPORT; | ||
| 58 | |||
| 59 | record RESOLVED_IMPORT | ||
| 60 | NFInstNode.ScopeRef node "Weakly: the class tree owns the imported node."; | ||
| 61 | String shortName; | ||
| 62 | SourceInfo info; | ||
| 63 | end RESOLVED_IMPORT; | ||
| 64 | |||
| 65 | record CONFLICTING_IMPORT | ||
| 66 | Import imp1; | ||
| 67 | Import imp2; | ||
| 68 | end CONFLICTING_IMPORT; | ||
| 69 | |||
| 70 | function name | ||
| 71 | input Import imp; | ||
| 72 | output String name; | ||
| 73 | algorithm | ||
| 74 | name := match imp | ||
| 75 | 12539 | case UNRESOLVED_IMPORT() then AbsynUtil.importName(imp.imp); | |
| 76 | 1652 | case RESOLVED_IMPORT() then InstNode.name(InstNode.borrow(imp.node)); | |
| 77 | end match; | ||
| 78 | end name; | ||
| 79 | |||
| 80 | function info | ||
| 81 | input Import imp; | ||
| 82 | output SourceInfo info; | ||
| 83 | algorithm | ||
| 84 | info := match imp | ||
| 85 | ✗ | case UNRESOLVED_IMPORT() then imp.info; | |
| 86 | ✗ | case RESOLVED_IMPORT() then imp.info; | |
| 87 | end match; | ||
| 88 | end info; | ||
| 89 | |||
| 90 | function resolve | ||
| 91 | input Import imp; | ||
| 92 | output InstNode node; | ||
| 93 | output Boolean changed; | ||
| 94 | output Import outImport; | ||
| 95 | algorithm | ||
| 96 | (outImport, node, changed) := match imp | ||
| 97 | case UNRESOLVED_IMPORT() | ||
| 98 | algorithm | ||
| 99 | 10977 | (outImport, node) := instQualified(imp.imp, InstNode.fromCell(imp.scope), imp.info); | |
| 100 | 10976 | then | |
| 101 | (outImport, node, true); | ||
| 102 | |||
| 103 | case RESOLVED_IMPORT() | ||
| 104 | 401693 | then (imp, InstNode.fromCell(imp.node), false); | |
| 105 | |||
| 106 | case CONFLICTING_IMPORT() | ||
| 107 | algorithm | ||
| 108 | ✗ | printImportError(imp.imp1, imp.imp2); | |
| 109 | ✗ | then | |
| 110 | fail(); | ||
| 111 | |||
| 112 | end match; | ||
| 113 | end resolve; | ||
| 114 | |||
| 115 | function resolveList | ||
| 116 | input array<Import> imps; | ||
| 117 | output list<Import> resolvedImps = {}; | ||
| 118 | algorithm | ||
| 119 |
3/4✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✓ Branch 3 taken 5 times.
✓ Branch 4 taken 3 times.
|
11 | for imp in imps loop |
| 120 | try | ||
| 121 | 5 | (_, _, imp) := resolve(imp); | |
| 122 | |||
| 123 | resolvedImps := match imp | ||
| 124 | ✗ | case UNRESOLVED_IMPORT(imp = Absyn.Import.UNQUAL_IMPORT()) then instUnqualified(imp, resolvedImps); | |
| 125 | else imp :: resolvedImps; | ||
| 126 | end match; | ||
| 127 | else | ||
| 128 | end try; | ||
| 129 | end for; | ||
| 130 | end resolveList; | ||
| 131 | |||
| 132 | function instQualified | ||
| 133 | input Absyn.Import imp; | ||
| 134 | input InstNode scope; | ||
| 135 | input SourceInfo info; | ||
| 136 | output Import outImport; | ||
| 137 | output InstNode node; | ||
| 138 | protected | ||
| 139 | String short_name; | ||
| 140 | algorithm | ||
| 141 | node := match imp | ||
| 142 | case Absyn.Import.NAMED_IMPORT() | ||
| 143 | 473 | then Lookup.lookupImport(imp.path, scope, info); | |
| 144 | |||
| 145 | case Absyn.Import.QUAL_IMPORT() | ||
| 146 | 10504 | then Lookup.lookupImport(imp.path, scope, info); | |
| 147 | end match; | ||
| 148 | |||
| 149 | short_name := match imp | ||
| 150 | 473 | case Absyn.Import.NAMED_IMPORT() then imp.name; | |
| 151 | else ""; | ||
| 152 | end match; | ||
| 153 | |||
| 154 | 10976 | outImport := RESOLVED_IMPORT(InstNode.scopeRef(node), short_name, info); | |
| 155 | end instQualified; | ||
| 156 | |||
| 157 | function instUnqualified | ||
| 158 | input Import imp; | ||
| 159 | input output list<Import> imps = {}; | ||
| 160 | protected | ||
| 161 | Absyn.Path path; | ||
| 162 | InstNode node; | ||
| 163 | NFInstNode.ScopeRef scope; | ||
| 164 | ClassTree tree; | ||
| 165 | SourceInfo info; | ||
| 166 | algorithm | ||
| 167 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 15 times.
|
15 | UNRESOLVED_IMPORT(imp = Absyn.Import.UNQUAL_IMPORT(path = path), scope = scope, info = info) := imp; |
| 168 | |||
| 169 | 15 | node := Lookup.lookupImport(path, InstNode.fromCell(scope), info); | |
| 170 | 15 | node := Inst.instPackage(node, NFInstContext.NO_CONTEXT); | |
| 171 | 15 | tree := Class.classTree(InstNode.getClass(node)); | |
| 172 | |||
| 173 | () := match tree | ||
| 174 | case ClassTree.FLAT_TREE() | ||
| 175 | algorithm | ||
| 176 |
3/4✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
✓ Branch 3 taken 1646 times.
✓ Branch 4 taken 15 times.
|
1676 | for cls in tree.classes loop |
| 177 | 1646 | imps := RESOLVED_IMPORT(InstNode.scopeRef(cls), "", info) :: imps; | |
| 178 | end for; | ||
| 179 | |||
| 180 |
3/4✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 15 times.
|
36 | for comp in tree.components loop |
| 181 | 6 | imps := RESOLVED_IMPORT(InstNode.scopeRef(comp), "", info) :: imps; | |
| 182 | end for; | ||
| 183 | then | ||
| 184 | (); | ||
| 185 | |||
| 186 | else | ||
| 187 | algorithm | ||
| 188 | ✗ | Error.terminate(getInstanceName() + " got invalid class tree", sourceInfo()); | |
| 189 | then | ||
| 190 | (); | ||
| 191 | |||
| 192 | end match; | ||
| 193 | end instUnqualified; | ||
| 194 | |||
| 195 | function printImportError | ||
| 196 | input Import imp1; | ||
| 197 | input Import imp2; | ||
| 198 | protected | ||
| 199 | ErrorTypes.Message err_msg; | ||
| 200 | algorithm | ||
| 201 | ✗ | Error.addSourceMessage(Error.ERROR_FROM_HERE, {}, info(imp1)); | |
| 202 | |||
| 203 | err_msg := match imp2 | ||
| 204 | case UNRESOLVED_IMPORT() then Error.MULTIPLE_QUALIFIED_IMPORTS_WITH_SAME_NAME; | ||
| 205 | case RESOLVED_IMPORT() then Error.IMPORT_SEVERAL_NAMES; | ||
| 206 | end match; | ||
| 207 | |||
| 208 | ✗ | Error.addSourceMessage(err_msg, {name(imp2)}, info(imp2)); | |
| 209 | end printImportError; | ||
| 210 | |||
| 211 | annotation(__OpenModelica_Interface="nf_frontend"); | ||
| 212 | end NFImport; | ||
| 213 |