/src/icu/icu4c/source/i18n/messageformat2_checker.cpp
Line | Count | Source |
1 | | // © 2024 and later: Unicode, Inc. and others. |
2 | | // License & terms of use: http://www.unicode.org/copyright.html |
3 | | |
4 | | #include "unicode/utypes.h" |
5 | | |
6 | | #if !UCONFIG_NO_NORMALIZATION |
7 | | |
8 | | #if !UCONFIG_NO_FORMATTING |
9 | | |
10 | | #if !UCONFIG_NO_MF2 |
11 | | |
12 | | #include "unicode/messageformat2.h" |
13 | | #include "messageformat2_allocation.h" |
14 | | #include "messageformat2_checker.h" |
15 | | #include "messageformat2_evaluation.h" |
16 | | #include "messageformat2_function_registry_internal.h" |
17 | | #include "messageformat2_macros.h" |
18 | | #include "uvector.h" // U_ASSERT |
19 | | |
20 | | U_NAMESPACE_BEGIN |
21 | | |
22 | | namespace message2 { |
23 | | |
24 | | /* |
25 | | Checks data model errors |
26 | | (see https://github.com/unicode-org/message-format-wg/blob/main/spec/formatting.md#error-handling ) |
27 | | |
28 | | The following are checked here: |
29 | | Variant Key Mismatch |
30 | | Duplicate Variant |
31 | | Missing Fallback Variant (called NonexhaustivePattern here) |
32 | | Missing Selector Annotation |
33 | | Duplicate Declaration |
34 | | - Most duplicate declaration errors are checked by the parser, |
35 | | but the checker checks for declarations of input variables |
36 | | that were previously implicitly declared |
37 | | (Duplicate option names and duplicate declarations are checked by the parser) |
38 | | */ |
39 | | |
40 | | // Type environments |
41 | | // ----------------- |
42 | | |
43 | 483 | TypeEnvironment::TypeEnvironment(UErrorCode& status) { |
44 | 483 | CHECK_ERROR(status); |
45 | | |
46 | 483 | UVector* temp; |
47 | 483 | temp = createStringVectorNoAdopt(status); |
48 | 483 | CHECK_ERROR(status); |
49 | 483 | annotated.adoptInstead(temp); |
50 | 483 | temp = createStringVectorNoAdopt(status); |
51 | 483 | CHECK_ERROR(status); |
52 | 483 | unannotated.adoptInstead(temp); |
53 | 483 | temp = createStringVectorNoAdopt(status); |
54 | 483 | CHECK_ERROR(status); |
55 | 483 | freeVars.adoptInstead(temp); |
56 | 483 | } |
57 | | |
58 | 0 | static bool has(const UVector& v, const VariableName& var) { |
59 | 0 | return v.contains(const_cast<void*>(static_cast<const void*>(&var))); |
60 | 0 | } |
61 | | |
62 | | // Returns true if `var` was either previously used (implicit declaration), |
63 | | // or is in scope by an explicit declaration |
64 | 0 | bool TypeEnvironment::known(const VariableName& var) const { |
65 | 0 | return has(*annotated, var) || has(*unannotated, var) || has(*freeVars, var); |
66 | 0 | } |
67 | | |
68 | 0 | TypeEnvironment::Type TypeEnvironment::get(const VariableName& var) const { |
69 | 0 | U_ASSERT(annotated.isValid()); |
70 | 0 | if (has(*annotated, var)) { |
71 | 0 | return Annotated; |
72 | 0 | } |
73 | 0 | U_ASSERT(unannotated.isValid()); |
74 | 0 | if (has(*unannotated, var)) { |
75 | 0 | return Unannotated; |
76 | 0 | } |
77 | 0 | U_ASSERT(freeVars.isValid()); |
78 | 0 | if (has(*freeVars, var)) { |
79 | 0 | return FreeVariable; |
80 | 0 | } |
81 | | // This case is a "free variable without an implicit declaration", |
82 | | // i.e. one used only in a selector expression and not in a declaration RHS |
83 | 0 | return Unannotated; |
84 | 0 | } |
85 | | |
86 | 0 | void TypeEnvironment::extend(const VariableName& var, TypeEnvironment::Type t, UErrorCode& status) { |
87 | 0 | if (t == Unannotated) { |
88 | 0 | U_ASSERT(unannotated.isValid()); |
89 | | // See comment below |
90 | 0 | unannotated->addElement(const_cast<void*>(static_cast<const void*>(&var)), status); |
91 | 0 | return; |
92 | 0 | } |
93 | | |
94 | 0 | if (t == FreeVariable) { |
95 | 0 | U_ASSERT(freeVars.isValid()); |
96 | | // See comment below |
97 | 0 | freeVars->addElement(const_cast<void*>(static_cast<const void*>(&var)), status); |
98 | 0 | return; |
99 | 0 | } |
100 | | |
101 | 0 | U_ASSERT(annotated.isValid()); |
102 | | // This is safe because elements of `annotated` are never written |
103 | | // and the lifetime of `var` is guaranteed to include the lifetime of |
104 | | // `annotated` |
105 | 0 | annotated->addElement(const_cast<void*>(static_cast<const void*>(&var)), status); |
106 | 0 | } |
107 | | |
108 | 483 | TypeEnvironment::~TypeEnvironment() {} |
109 | | |
110 | | // --------------------- |
111 | | |
112 | 0 | Key Checker::normalizeNFC(const Key& k) const { |
113 | 0 | if (k.isWildcard()) { |
114 | 0 | return k; |
115 | 0 | } |
116 | 0 | return Key(Literal(k.asLiteral().isQuoted(), |
117 | 0 | StandardFunctions::normalizeNFC(k.asLiteral().unquoted()))); |
118 | 0 | } |
119 | | |
120 | 0 | static bool areDefaultKeys(const Key* keys, int32_t len) { |
121 | 0 | U_ASSERT(len > 0); |
122 | 0 | for (int32_t i = 0; i < len; i++) { |
123 | 0 | if (!keys[i].isWildcard()) { |
124 | 0 | return false; |
125 | 0 | } |
126 | 0 | } |
127 | 0 | return true; |
128 | 0 | } |
129 | | |
130 | 0 | void Checker::addFreeVars(TypeEnvironment& t, const Operand& rand, UErrorCode& status) { |
131 | 0 | CHECK_ERROR(status); |
132 | |
|
133 | 0 | if (rand.isVariable()) { |
134 | 0 | const VariableName& v = rand.asVariable(); |
135 | 0 | if (!t.known(v)) { |
136 | 0 | t.extend(v, TypeEnvironment::Type::FreeVariable, status); |
137 | 0 | } |
138 | 0 | } |
139 | 0 | } |
140 | | |
141 | 0 | void Checker::addFreeVars(TypeEnvironment& t, const OptionMap& opts, UErrorCode& status) { |
142 | 0 | for (int32_t i = 0; i < opts.size(); i++) { |
143 | 0 | const Option& o = opts.getOption(i, status); |
144 | 0 | CHECK_ERROR(status); |
145 | 0 | addFreeVars(t, o.getValue(), status); |
146 | 0 | } |
147 | 0 | } |
148 | | |
149 | 0 | void Checker::addFreeVars(TypeEnvironment& t, const Operator& rator, UErrorCode& status) { |
150 | 0 | CHECK_ERROR(status); |
151 | |
|
152 | 0 | addFreeVars(t, rator.getOptionsInternal(), status); |
153 | 0 | } |
154 | | |
155 | 0 | void Checker::addFreeVars(TypeEnvironment& t, const Expression& rhs, UErrorCode& status) { |
156 | 0 | CHECK_ERROR(status); |
157 | |
|
158 | 0 | if (rhs.isFunctionCall()) { |
159 | 0 | const Operator* rator = rhs.getOperator(status); |
160 | 0 | U_ASSERT(U_SUCCESS(status)); |
161 | 0 | addFreeVars(t, *rator, status); |
162 | 0 | } |
163 | 0 | addFreeVars(t, rhs.getOperand(), status); |
164 | 0 | } |
165 | | |
166 | 0 | void Checker::checkVariants(UErrorCode& status) { |
167 | 0 | CHECK_ERROR(status); |
168 | |
|
169 | 0 | U_ASSERT(!dataModel.hasPattern()); |
170 | | |
171 | | // Check that each variant has a key list with size |
172 | | // equal to the number of selectors |
173 | 0 | const Variant* variants = dataModel.getVariantsInternal(); |
174 | | |
175 | | // Check that one variant includes only wildcards |
176 | 0 | bool defaultExists = false; |
177 | 0 | bool duplicatesExist = false; |
178 | |
|
179 | 0 | for (int32_t i = 0; i < dataModel.numVariants(); i++) { |
180 | 0 | const SelectorKeys& k = variants[i].getKeys(); |
181 | 0 | const Key* keys = k.getKeysInternal(); |
182 | 0 | int32_t len = k.len; |
183 | 0 | if (len != dataModel.numSelectors()) { |
184 | | // Variant key mismatch |
185 | 0 | errors.addError(StaticErrorType::VariantKeyMismatchError, status); |
186 | 0 | return; |
187 | 0 | } |
188 | 0 | defaultExists |= areDefaultKeys(keys, len); |
189 | | |
190 | | // Check if this variant's keys are duplicated by any other variant's keys |
191 | 0 | if (!duplicatesExist) { |
192 | | // This check takes quadratic time, but it can be optimized if checking |
193 | | // this property turns out to be a bottleneck. |
194 | 0 | for (int32_t j = 0; j < i; j++) { |
195 | 0 | const SelectorKeys& k1 = variants[j].getKeys(); |
196 | 0 | const Key* keys1 = k1.getKeysInternal(); |
197 | 0 | bool allEqual = true; |
198 | | // This variant was already checked, |
199 | | // so we know keys1.len == len |
200 | 0 | for (int32_t kk = 0; kk < len; kk++) { |
201 | 0 | if (!(normalizeNFC(keys[kk]) == normalizeNFC(keys1[kk]))) { |
202 | 0 | allEqual = false; |
203 | 0 | break; |
204 | 0 | } |
205 | 0 | } |
206 | 0 | if (allEqual) { |
207 | 0 | duplicatesExist = true; |
208 | 0 | } |
209 | 0 | } |
210 | 0 | } |
211 | 0 | } |
212 | | |
213 | 0 | if (duplicatesExist) { |
214 | 0 | errors.addError(StaticErrorType::DuplicateVariant, status); |
215 | 0 | } |
216 | 0 | if (!defaultExists) { |
217 | 0 | errors.addError(StaticErrorType::NonexhaustivePattern, status); |
218 | 0 | } |
219 | 0 | } |
220 | | |
221 | | void Checker::requireAnnotated(const TypeEnvironment& t, |
222 | | const VariableName& selectorVar, |
223 | 0 | UErrorCode& status) { |
224 | 0 | CHECK_ERROR(status); |
225 | |
|
226 | 0 | if (t.get(selectorVar) == TypeEnvironment::Type::Annotated) { |
227 | 0 | return; // No error |
228 | 0 | } |
229 | | // If this code is reached, an error was detected |
230 | 0 | errors.addError(StaticErrorType::MissingSelectorAnnotation, status); |
231 | 0 | } |
232 | | |
233 | 0 | void Checker::checkSelectors(const TypeEnvironment& t, UErrorCode& status) { |
234 | 0 | U_ASSERT(!dataModel.hasPattern()); |
235 | | |
236 | | // Check each selector; if it's not annotated, emit a |
237 | | // "missing selector annotation" error |
238 | 0 | const VariableName* selectors = dataModel.getSelectorsInternal(); |
239 | 0 | for (int32_t i = 0; i < dataModel.numSelectors(); i++) { |
240 | 0 | requireAnnotated(t, selectors[i], status); |
241 | 0 | } |
242 | 0 | } |
243 | | |
244 | 0 | TypeEnvironment::Type typeOf(TypeEnvironment& t, const Expression& expr) { |
245 | 0 | if (expr.isFunctionCall()) { |
246 | 0 | return TypeEnvironment::Type::Annotated; |
247 | 0 | } |
248 | 0 | const Operand& rand = expr.getOperand(); |
249 | 0 | U_ASSERT(!rand.isNull()); |
250 | 0 | if (rand.isLiteral()) { |
251 | 0 | return TypeEnvironment::Type::Unannotated; |
252 | 0 | } |
253 | 0 | U_ASSERT(rand.isVariable()); |
254 | 0 | return t.get(rand.asVariable()); |
255 | 0 | } |
256 | | |
257 | 483 | void Checker::checkDeclarations(TypeEnvironment& t, UErrorCode& status) { |
258 | 483 | CHECK_ERROR(status); |
259 | | |
260 | | // For each declaration, extend the type environment with its type |
261 | | // Only a very simple type system is necessary: variables |
262 | | // have the type "annotated", "unannotated", or "free". |
263 | | // For "missing selector annotation" checking, free variables |
264 | | // (message arguments) are treated as unannotated. |
265 | | // Free variables are also used for checking duplicate declarations. |
266 | 483 | const Binding* env = dataModel.getLocalVariablesInternal(); |
267 | 483 | for (int32_t i = 0; i < dataModel.bindingsLen; i++) { |
268 | 0 | const Binding& b = env[i]; |
269 | 0 | const VariableName& lhs = b.getVariable(); |
270 | 0 | const Expression& rhs = b.getValue(); |
271 | | |
272 | | // First, add free variables from the RHS of b |
273 | | // This must be done first so we can catch: |
274 | | // .local $foo = {$foo} |
275 | | // (where the RHS is the first use of $foo) |
276 | 0 | if (b.isLocal()) { |
277 | 0 | addFreeVars(t, rhs, status); |
278 | | |
279 | | // Next, check if the LHS equals any free variables |
280 | | // whose implicit declarations are in scope |
281 | 0 | if (t.known(lhs) && t.get(lhs) == TypeEnvironment::Type::FreeVariable) { |
282 | 0 | errors.addError(StaticErrorType::DuplicateDeclarationError, status); |
283 | 0 | } |
284 | 0 | } else { |
285 | | // Input declaration; if b has no annotation, there's nothing to check |
286 | 0 | if (!b.isLocal() && b.hasAnnotation()) { |
287 | 0 | const OptionMap& opts = b.getOptionsInternal(); |
288 | | // For .input declarations, we just need to add any variables |
289 | | // referenced in the options |
290 | 0 | addFreeVars(t, opts, status); |
291 | 0 | } |
292 | | // Next, check if the LHS equals any free variables |
293 | | // whose implicit declarations are in scope |
294 | 0 | if (t.known(lhs) && t.get(lhs) == TypeEnvironment::Type::FreeVariable) { |
295 | 0 | errors.addError(StaticErrorType::DuplicateDeclarationError, status); |
296 | 0 | } |
297 | 0 | } |
298 | | // Next, extend the type environment with a binding from lhs to its type |
299 | 0 | t.extend(lhs, typeOf(t, rhs), status); |
300 | 0 | } |
301 | 483 | } |
302 | | |
303 | 483 | void Checker::check(UErrorCode& status) { |
304 | 483 | CHECK_ERROR(status); |
305 | | |
306 | 483 | TypeEnvironment typeEnv(status); |
307 | 483 | checkDeclarations(typeEnv, status); |
308 | | // Pattern message |
309 | 483 | if (dataModel.hasPattern()) { |
310 | 483 | return; |
311 | 483 | } else { |
312 | | // Selectors message |
313 | 0 | checkSelectors(typeEnv, status); |
314 | 0 | checkVariants(status); |
315 | 0 | } |
316 | 483 | } |
317 | | |
318 | | } // namespace message2 |
319 | | U_NAMESPACE_END |
320 | | |
321 | | #endif /* #if !UCONFIG_NO_MF2 */ |
322 | | |
323 | | #endif /* #if !UCONFIG_NO_FORMATTING */ |
324 | | |
325 | | #endif /* #if !UCONFIG_NO_NORMALIZATION */ |