Appearance
| 1 | @@IF.dotnet() | |
| 2 | ||
| 3 | namespace IR.Emitter is | |
| 4 | use Semantic.Types.Type | |
| 5 | ||
| 6 | use IR.Values.Value | |
| 7 | ||
| 8 | // The extent of one `.try` block, shared by every handler attached | |
| 9 | // to it — a `try` with three catches is three regions over a single | |
| 10 | // extent. | |
| 11 | // | |
| 12 | // SRM delimits a region by four labels and the handler's type, | |
| 13 | // added to the body's control-flow builder once every one of them | |
| 14 | // is known — rather than by a nesting whose extents fall out of | |
| 15 | // the walk. So the boundaries have to be recorded as the walk | |
| 16 | // passes them and held somewhere until each region closes, which | |
| 17 | // is what this and PROTECTED_REGION are for. | |
| 18 | class TRY_EXTENT is | |
| 19 | start: int public | |
| 20 | end: int public | |
| 21 | ||
| 22 | init() is | |
| 23 | start = -1 | |
| 24 | end = -1 | |
| 25 | si | |
| 26 | si | |
| 27 | ||
| 28 | // One handler over an extent: a catch of `catch_type`, or — when | |
| 29 | // that is null — a finally. | |
| 30 | class PROTECTED_REGION is | |
| 31 | extent: TRY_EXTENT | |
| 32 | catch_type: Type? | |
| 33 | ||
| 34 | handler_start: int public | |
| 35 | handler_end: int public | |
| 36 | ||
| 37 | init(extent: TRY_EXTENT, catch_type: Type?) is | |
| 38 | self.extent = extent | |
| 39 | self.catch_type = catch_type | |
| 40 | ||
| 41 | handler_start = -1 | |
| 42 | handler_end = -1 | |
| 43 | si | |
| 44 | ||
| 45 | is_catch: bool => catch_type? | |
| 46 | ||
| 47 | // The caught type as it reads in this value's description. | |
| 48 | catch_type_il: string => | |
| 49 | if let type = catch_type then | |
| 50 | "{type}" | |
| 51 | else | |
| 52 | "" | |
| 53 | fi | |
| 54 | si | |
| 55 | ||
| 56 | // `.try {` | |
| 57 | class TRY_START: CilValue is | |
| 58 | _extent: TRY_EXTENT | |
| 59 | ||
| 60 | init(extent: TRY_EXTENT) is | |
| 61 | super.init() | |
| 62 | ||
| 63 | _extent = extent | |
| 64 | si | |
| 65 | ||
| 66 | gen(context: IR.Emitter.CIL_CONTEXT) is | |
| 67 | let body = context.current_srm_body_emitter! | |
| 68 | _extent.start = body.mark_region_boundary() | |
| 69 | si | |
| 70 | ||
| 71 | to_string() -> string => ".try {{" | |
| 72 | si | |
| 73 | ||
| 74 | // The `}` closing a `.try` block. | |
| 75 | class TRY_END: CilValue is | |
| 76 | _extent: TRY_EXTENT | |
| 77 | ||
| 78 | init(extent: TRY_EXTENT) is | |
| 79 | super.init() | |
| 80 | ||
| 81 | _extent = extent | |
| 82 | si | |
| 83 | ||
| 84 | gen(context: IR.Emitter.CIL_CONTEXT) is | |
| 85 | let body = context.current_srm_body_emitter! | |
| 86 | _extent.end = body.mark_region_boundary() | |
| 87 | si | |
| 88 | ||
| 89 | to_string() -> string => "}} // try" | |
| 90 | si | |
| 91 | ||
| 92 | // `catch T {` or `finally {` | |
| 93 | class HANDLER_START: CilValue is | |
| 94 | _region: PROTECTED_REGION | |
| 95 | ||
| 96 | init(region: PROTECTED_REGION) is | |
| 97 | super.init() | |
| 98 | ||
| 99 | _region = region | |
| 100 | si | |
| 101 | ||
| 102 | gen(context: IR.Emitter.CIL_CONTEXT) is | |
| 103 | let body = context.current_srm_body_emitter! | |
| 104 | _region.handler_start = body.mark_region_boundary() | |
| 105 | si | |
| 106 | ||
| 107 | to_string() -> string => | |
| 108 | if _region.is_catch then | |
| 109 | "catch {_region.catch_type_il} {{" | |
| 110 | else | |
| 111 | "finally {{" | |
| 112 | fi | |
| 113 | si | |
| 114 | ||
| 115 | // The `}` closing a handler, and the point at which every boundary | |
| 116 | // of the region is known — so this is where the region is added. | |
| 117 | // | |
| 118 | // Adding it here rather than in some later sweep is what keeps the | |
| 119 | // ordering right: ECMA-335 requires regions be listed innermost | |
| 120 | // first, and a region nested inside another closes before it does. | |
| 121 | // Nothing validates the order, and getting it wrong produces an | |
| 122 | // assembly that loads and then runs the wrong handler, so the | |
| 123 | // ordering wants to fall out of the structure rather than be | |
| 124 | // arranged separately. | |
| 125 | class HANDLER_END: CilValue is | |
| 126 | _region: PROTECTED_REGION | |
| 127 | ||
| 128 | init(region: PROTECTED_REGION) is | |
| 129 | super.init() | |
| 130 | ||
| 131 | _region = region | |
| 132 | si | |
| 133 | ||
| 134 | gen(context: IR.Emitter.CIL_CONTEXT) is | |
| 135 | let body = context.current_srm_body_emitter! | |
| 136 | _region.handler_end = body.mark_region_boundary() | |
| 137 | ||
| 138 | if _region.is_catch then | |
| 139 | let token = context.resolve_type_token(_region.catch_type!) | |
| 140 | ||
| 141 | body.add_catch_region( | |
| 142 | _region.extent.start, | |
| 143 | _region.extent.end, | |
| 144 | _region.handler_start, | |
| 145 | _region.handler_end, | |
| 146 | token) | |
| 147 | else | |
| 148 | body.add_finally_region( | |
| 149 | _region.extent.start, | |
| 150 | _region.extent.end, | |
| 151 | _region.handler_start, | |
| 152 | _region.handler_end) | |
| 153 | fi | |
| 154 | si | |
| 155 | ||
| 156 | to_string() -> string => "}} // handler" | |
| 157 | si | |
| 158 | si |