1# Copyright 2019 Google LLC 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15-- Tests for virtual fields: 16-- 17-- * `let` constructs 18-- * TODO(bolms@): `transform` annotations 19-- * TODO(bolms@): `read` and `write` annotations 20 21[$default byte_order: "LittleEndian"] 22[(cpp) namespace: "emboss::test"] 23 24 25struct StructureWithConstants: 26 let ten = 10 27 let twenty = 20 28 let four_billion = 4_000_000_000 29 let ten_billion = 10_000_000_000 30 let minus_ten_billion = -10_000_000_000 31 0 [+4] UInt value 32 let alias_of_value = value 33 let alias_of_alias_of_value = alias_of_value 34 let alias_of_ten = ten 35 let alias_of_alias_of_ten = alias_of_ten 36 37 38struct StructureWithComputedValues: 39 0 [+4] UInt value 40 let doubled = value * 2 41 let plus_ten = value + 10 42 let signed_doubled = value2 * 2 43 let signed_plus_ten = value2 + 10 44 let product = value * value2 45 4 [+4] Int value2 46 47 48struct StructureWithConditionalValue: 49 0 [+4] UInt x 50 if x < 0x8000_0000: 51 let two_x = x * 2 52 let x_plus_one = x + 1 53 54 55struct StructureWithValueInCondition: 56 let two_x = x * 2 57 0 [+4] UInt x 58 if two_x < 100: 59 4 [+4] UInt if_two_x_lt_100 60 61 62struct StructureWithValuesInLocation: 63 let two_x = x * 2 64 0 [+4] UInt x 65 two_x [+4] UInt offset_two_x 66 4 [+two_x] UInt:32 size_two_x 67 68 69struct StructureWithBoolValue: 70 let x_is_ten = x == 10 71 0 [+4] UInt x 72 73 74struct StructureWithEnumValue: 75 enum Category: 76 SMALL = 1 77 LARGE = 2 78 let x_size = x < 100 ? Category.SMALL : Category.LARGE 79 0 [+4] UInt x 80 81 82struct StructureWithBitsWithValue: 83 0 [+4] BitsWithValue b 84 let alias_of_b_sum = b.sum 85 let alias_of_b_a = b.a 86 87 88bits BitsWithValue: 89 0 [+16] UInt a 90 16 [+16] UInt b 91 let sum = a + b 92 93 94struct StructureUsingForeignConstants: 95 StructureWithConstants.ten [+4] UInt x 96 let one_hundred = StructureWithConstants.twenty * 5 97 98 99struct SubfieldOfAlias: 100 0 [+4] struct header: 101 0 [+2] UInt size 102 2 [+2] UInt message_id 103 let h = header 104 let size = h.size 105 106 107struct RestrictedAlias: 108 0 [+4] BitsWithValue a_b 109 4 [+1] UInt alias_switch 110 if alias_switch > 10: 111 let a_b_alias = a_b 112 113 114struct HasField: 115 0 [+1] UInt z 116 if $present(x.y): 117 let y = x.y 118 if z > 10: 119 1 [+2] struct x: 120 0 [+1] UInt v 121 if v > 10: 122 1 [+1] UInt y 123 if $present(x): 124 let x_has_y = $present(x.y) 125 126 127struct VirtualUnconditionallyUsesConditional: 128 0 [+1] UInt x 129 if x == 0: 130 1 [+1] UInt xc 131 132 let x_nor_xc = x == 0 && xc == 0 133 134 135struct UsesSize: 136 0 [+1] bits r: 137 0 [+8] UInt q 138 let q_plus_bit_size = q + $size_in_bits 139 let r_q_plus_byte_size = r.q + $size_in_bytes 140 141 142struct UsesExternalSize: 143 0 [+4] StructureWithConstants x 144 x.$size_in_bytes [+StructureWithConstants.$size_in_bytes] StructureWithConstants y 145 146 147struct ImplicitWriteBack: 148 0 [+1] UInt x 149 let x_plus_ten = x + 10 150 let ten_plus_x = 10 + x 151 let x_minus_ten = x - 10 152 let ten_minus_x = 10 - x 153 let ten_minus_x_plus_ten = (10 - x) + 10 154