symboltablebuilder/capture.spice
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2026 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | // Std imports | ||
| 4 | import "std/text/json/json-value"; | ||
| 5 | |||
| 6 | // Own imports | ||
| 7 | import "@/symboltablebuilder/super-type"; | ||
| 8 | import "@/symboltablebuilder/symbol-table-entry"; | ||
| 9 | import "@/util/common-util"; | ||
| 10 | |||
| 11 | public type CapturePassMode enum { | ||
| 12 | BY_VALUE, | ||
| 13 | BY_REFERENCE | ||
| 14 | } | ||
| 15 | |||
| 16 | public type CaptureAccessType enum { | ||
| 17 | READ_ONLY, | ||
| 18 | READ_WRITE | ||
| 19 | } | ||
| 20 | |||
| 21 | public type Capture struct { | ||
| 22 | public SymbolTableEntry* capturedSymbol | ||
| 23 | 94 | CaptureAccessType accessType = CaptureAccessType::READ_ONLY | |
| 24 | 94 | CapturePassMode captureMode = CapturePassMode::BY_VALUE | |
| 25 | } | ||
| 26 | |||
| 27 | 94 | public p Capture.ctor(SymbolTableEntry* capturedSymbol) { | |
| 28 | 94 | this.capturedSymbol = capturedSymbol; | |
| 29 | // Set the capture mode depending on the symbol type | ||
| 30 | // All types with guaranteed size <= 64 bit are captured by value, all others by reference. | ||
| 31 | 94 | const QualType capturedType = capturedSymbol.getQualType(); | |
| 32 | 94 | const dyn over64Options = [SuperType::TY_STRUCT, SuperType::TY_INTERFACE, SuperType::TY_UNION]; | |
| 33 | 94 | const bool canBeOver64Bit = capturedType.isOneOf(over64Options, len(over64Options)); | |
| 34 | 94 | this.captureMode = canBeOver64Bit ? CapturePassMode::BY_REFERENCE : CapturePassMode::BY_VALUE; | |
| 35 | 94 | } | |
| 36 | |||
| 37 | /** | ||
| 38 | * Retrieve the name of the capture | ||
| 39 | * | ||
| 40 | * @return Capture name or symbol name if no capture name was set | ||
| 41 | */ | ||
| 42 | 92 | public f<String> Capture.getName() { | |
| 43 | 92 | return this.capturedSymbol.name; | |
| 44 | } | ||
| 45 | |||
| 46 | /** | ||
| 47 | * Set the access type of this capture. | ||
| 48 | * Possible values are READ_ONLY and READ_WRITE | ||
| 49 | * | ||
| 50 | * @param captureAccessType Capture access type | ||
| 51 | */ | ||
| 52 | 31 | public p Capture.setAccessType(CaptureAccessType captureAccessType) { | |
| 53 | 31 | this.accessType = captureAccessType; | |
| 54 | // If we write to the captured symbol, we need to set the symbol to be a reference | ||
| 55 | 31 | if captureAccessType == CaptureAccessType::READ_WRITE { | |
| 56 | 31 | this.captureMode = CapturePassMode::BY_REFERENCE; | |
| 57 | 31 | } | |
| 58 | 31 | } | |
| 59 | |||
| 60 | /** | ||
| 61 | * Retrieve the mode of this capture. | ||
| 62 | * Possible values are BY_VALUE and BY_REFERENCE | ||
| 63 | * | ||
| 64 | * @return Capture mode | ||
| 65 | */ | ||
| 66 | 439 | public f<CapturePassMode> Capture.getMode() { | |
| 67 | 439 | return this.captureMode; | |
| 68 | } | ||
| 69 | |||
| 70 | /** | ||
| 71 | * Stringify the current capture to a human-readable form. Used to dump whole symbol tables with their contents. | ||
| 72 | * The returned heap-allocated JSON value is owned by the caller. | ||
| 73 | * | ||
| 74 | * Example: | ||
| 75 | * { | ||
| 76 | * "name": "testIdentifier", | ||
| 77 | * "accessType": "READ_ONLY", | ||
| 78 | * "mode": "BY_VALUE" | ||
| 79 | * } | ||
| 80 | * | ||
| 81 | * @return Capture as a JSON object | ||
| 82 | */ | ||
| 83 | 92 | public f<JsonValue*> Capture.toJSON() { | |
| 84 | 92 | result = __new<JsonValue>(JsonValueKind::JSON_OBJECT); | |
| 85 | 92 | setJsonField(result, String("name"), __new<JsonValue>(this.capturedSymbol.name)); | |
| 86 | 92 | const string accessTypeStr = this.accessType == CaptureAccessType::READ_ONLY ? "READ_ONLY" : "READ_WRITE"; | |
| 87 | 92 | setJsonField(result, String("accessType"), __new<JsonValue>(String(accessTypeStr))); | |
| 88 | 92 | const string mode = this.captureMode == CapturePassMode::BY_VALUE ? "BY_VALUE" : "BY_REFERENCE"; | |
| 89 | 92 | setJsonField(result, String("mode"), __new<JsonValue>(String(mode))); | |
| 90 | 92 | } | |
| 91 |