model/union.spice
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2026 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | // Std imports | ||
| 4 | import "std/data/vector"; | ||
| 5 | import "std/text/stringstream"; | ||
| 6 | import "std/type/types"; | ||
| 7 | |||
| 8 | // Own imports | ||
| 9 | import "@/ast/ast-nodes"; | ||
| 10 | import "@/model/struct-base"; | ||
| 11 | import "@/model/generic-type"; | ||
| 12 | import "@/symboltablebuilder/qual-type"; | ||
| 13 | import "@/symboltablebuilder/symbol-table-entry"; | ||
| 14 | import "@/symboltablebuilder/scope"; | ||
| 15 | import "@/util/common-util"; | ||
| 16 | |||
| 17 | // Constants | ||
| 18 | const string UNION_SCOPE_PREFIX = "union:"; | ||
| 19 | |||
| 20 | 135 | public type Union struct { | |
| 21 | public compose StructBase structBase | ||
| 22 | public QualTypeList fieldTypes | ||
| 23 | 22 | public unsigned long defaultFieldIndex = ULONG_MAX_VALUE | |
| 24 | } | ||
| 25 | |||
| 26 | 22 | public p Union.ctor(const String& name, SymbolTableEntry* entry, Scope* scope, const QualTypeList& fieldTypes, const Vector<GenericType>& templateTypes, ASTNode* declNode) { | |
| 27 | 22 | this.structBase = StructBase(name, entry, scope, templateTypes, declNode); | |
| 28 | 22 | this.fieldTypes = fieldTypes; | |
| 29 | 22 | } | |
| 30 | |||
| 31 | /** | ||
| 32 | * Retrieve the name of the scope, where the union's fields are placed. This is used to navigate to the scope of the | ||
| 33 | * union from the parent scope. | ||
| 34 | * | ||
| 35 | * @return Name of the union scope | ||
| 36 | */ | ||
| 37 | 18 | public f<String> Union.getScopeName() { | |
| 38 | 18 | const String& appendix = this.structBase.isGenericSubstantiation() ? this.structBase.getSignature() : this.name; | |
| 39 | 36 | return String(UNION_SCOPE_PREFIX) + appendix; | |
| 40 | 18 | } | |
| 41 | |||
| 42 | /** | ||
| 43 | * Retrieve the name of the scope, where the union's fields are placed, from just a name and concrete | ||
| 44 | * template types — usable without a live Union instance. See getStructScopeName (struct.spice) for the | ||
| 45 | * same pattern; mirrors StructBase.getSignature's 2-arg overload, which does not read 'this'. | ||
| 46 | * | ||
| 47 | * @param name Union name | ||
| 48 | * @param concreteTemplateTypes Concrete template types | ||
| 49 | * @return Name of the union scope | ||
| 50 | */ | ||
| 51 | 26 | public f<String> getUnionScopeName(const String& name, const QualTypeList& concreteTemplateTypes) { | |
| 52 | 26 | StringStream templateTyStr; | |
| 53 | 26 | if !concreteTemplateTypes.isEmpty() { | |
| 54 | ✗ | templateTyStr << "<"; | |
| 55 | ✗ | foreach unsigned long i, const QualType& templateType : concreteTemplateTypes { | |
| 56 | ✗ | if i > 0ul { templateTyStr << ","; } | |
| 57 | ✗ | templateType.getName(templateTyStr, false, true); | |
| 58 | ✗ | } | |
| 59 | ✗ | templateTyStr << ">"; | |
| 60 | ✗ | } | |
| 61 | 52 | return String(UNION_SCOPE_PREFIX) + getLastFragment(name, SCOPE_ACCESS_TOKEN) + templateTyStr.str(); | |
| 62 | 26 | } | |
| 63 |