Debian Codegraph @

intl/icu/source/i18n/messageformat2_evaluation.cpp

raw
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 "messageformat2_allocation.h"
13 #include "messageformat2_evaluation.h"
14 #include "messageformat2_macros.h"
15 #include "uvector.h" // U_ASSERT
16
17 U_NAMESPACE_BEGIN
18
19 // Auxiliary data structures used during formatting a message
20
21 namespace message2 {
22
23 using namespace data_model;
24
25 // Functions
26 // -------------
27
28 ResolvedFunctionOption::ResolvedFunctionOption(ResolvedFunctionOption&& other) {
29 name = std::move(other.name);
30 value = std::move(other.value);
31 }
32
33 ResolvedFunctionOption::~ResolvedFunctionOption() {}
34
35
36 const ResolvedFunctionOption* FunctionOptions::getResolvedFunctionOptions(int32_t& len) const {
37 len = functionOptionsLen;
38 U_ASSERT(len == 0 || options != nullptr);
39 return options;
40 }
41
42 FunctionOptions::FunctionOptions(UVector&& optionsVector, UErrorCode& status) {
43 CHECK_ERROR(status);
44
45 functionOptionsLen = optionsVector.size();
46 options = moveVectorToArray<ResolvedFunctionOption>(optionsVector, status);
47 }
48
49 UBool FunctionOptions::getFunctionOption(const UnicodeString& key, Formattable& option) const {
50 if (options == nullptr) {
51 U_ASSERT(functionOptionsLen == 0);
52 }
53 for (int32_t i = 0; i < functionOptionsLen; i++) {
54 const ResolvedFunctionOption& opt = options[i];
55 if (opt.getName() == key) {
56 option = opt.getValue();
57 return true;
58 }
59 }
60 return false;
61 }
62
63 UnicodeString FunctionOptions::getStringFunctionOption(const UnicodeString& key) const {
64 Formattable option;
65 if (getFunctionOption(key, option)) {
66 if (option.getType() == UFMT_STRING) {
67 UErrorCode localErrorCode = U_ZERO_ERROR;
68 UnicodeString val = option.getString(localErrorCode);
69 U_ASSERT(U_SUCCESS(localErrorCode));
70 return val;
71 }
72 }
73 // For anything else, including non-string values, return "".
74 // Alternately, could try to stringify the non-string option.
75 // (Currently, no tests require that.)
76 return {};
77 }
78
79 FunctionOptions& FunctionOptions::operator=(FunctionOptions&& other) noexcept {
80 functionOptionsLen = other.functionOptionsLen;
81 options = other.options;
82 other.functionOptionsLen = 0;
83 other.options = nullptr;
84 return *this;
85 }
86
87 FunctionOptions::FunctionOptions(FunctionOptions&& other) {
88 *this = std::move(other);
89 }
90
91 FunctionOptions::~FunctionOptions() {
92 if (options != nullptr) {
93 delete[] options;
94 options = nullptr;
95 }
96 }
97
98 static bool containsOption(const UVector& opts, const ResolvedFunctionOption& opt) {
99 for (int32_t i = 0; i < opts.size(); i++) {
100 if (static_cast<ResolvedFunctionOption*>(opts[i])->getName()
101 == opt.getName()) {
102 return true;
103 }
104 }
105 return false;
106 }
107
108 // Options in `this` take precedence
109 // `this` can't be used after mergeOptions is called
110 FunctionOptions FunctionOptions::mergeOptions(FunctionOptions&& other,
111 UErrorCode& status) {
112 UVector mergedOptions(status);
113 mergedOptions.setDeleter(uprv_deleteUObject);
114
115 if (U_FAILURE(status)) {
116 return {};
117 }
118
119 // Create a new vector consisting of the options from this `FunctionOptions`
120 for (int32_t i = 0; i < functionOptionsLen; i++) {
121 mergedOptions.adoptElement(create<ResolvedFunctionOption>(std::move(options[i]), status),
122 status);
123 }
124
125 // Add each option from `other` that doesn't appear in this `FunctionOptions`
126 for (int i = 0; i < other.functionOptionsLen; i++) {
127 // Note: this is quadratic in the length of `options`
128 if (!containsOption(mergedOptions, other.options[i])) {
129 mergedOptions.adoptElement(create<ResolvedFunctionOption>(std::move(other.options[i]),
130 status),
131 status);
132 }
133 }
134
135 delete[] options;
136 options = nullptr;
137 functionOptionsLen = 0;
138
139 return FunctionOptions(std::move(mergedOptions), status);
140 }
141
142 // PrioritizedVariant
143 // ------------------
144
145 UBool PrioritizedVariant::operator<(const PrioritizedVariant& other) const {
146 if (priority < other.priority) {
147 return true;
148 }
149 return false;
150 }
151
152 PrioritizedVariant::~PrioritizedVariant() {}
153
154 // ---------------- Environments and closures
155
156 Environment* Environment::create(const VariableName& var, Closure&& c, Environment* parent, UErrorCode& errorCode) {
157 NULL_ON_ERROR(errorCode);
158 Environment* result = new NonEmptyEnvironment(var, std::move(c), parent);
159 if (result == nullptr) {
160 errorCode = U_MEMORY_ALLOCATION_ERROR;
161 return nullptr;
162 }
163 return result;
164 }
165
166 Environment* Environment::create(UErrorCode& errorCode) {
167 NULL_ON_ERROR(errorCode);
168 Environment* result = new EmptyEnvironment();
169 if (result == nullptr) {
170 errorCode = U_MEMORY_ALLOCATION_ERROR;
171 return nullptr;
172 }
173 return result;
174 }
175
176 const Closure& EmptyEnvironment::lookup(const VariableName& v) const {
177 (void) v;
178 U_ASSERT(false);
179 UPRV_UNREACHABLE_EXIT;
180 }
181
182 const Closure& NonEmptyEnvironment::lookup(const VariableName& v) const {
183 if (v == var) {
184 return rhs;
185 }
186 return parent->lookup(v);
187 }
188
189 bool EmptyEnvironment::has(const VariableName& v) const {
190 (void) v;
191 return false;
192 }
193
194 bool NonEmptyEnvironment::has(const VariableName& v) const {
195 if (v == var) {
196 return true;
197 }
198 return parent->has(v);
199 }
200
201 Environment::~Environment() {}
202 NonEmptyEnvironment::~NonEmptyEnvironment() {}
203 EmptyEnvironment::~EmptyEnvironment() {}
204
205 Closure::~Closure() {}
206
207 // MessageContext methods
208
209 void MessageContext::checkErrors(UErrorCode& status) const {
210 CHECK_ERROR(status);
211 errors.checkErrors(status);
212 }
213
214 const Formattable* MessageContext::getGlobal(const MessageFormatter& context,
215 const VariableName& v,
216 UErrorCode& errorCode) const {
217 return arguments.getArgument(context, v, errorCode);
218 }
219
220 MessageContext::MessageContext(const MessageArguments& args,
221 const StaticErrors& e,
222 UErrorCode& status) : arguments(args), errors(e, status) {}
223
224 MessageContext::~MessageContext() {}
225
226 // InternalValue
227 // -------------
228
229 bool InternalValue::isFallback() const {
230 return std::holds_alternative<FormattedPlaceholder>(argument)
231 && std::get_if<FormattedPlaceholder>(&argument)->isFallback();
232 }
233
234 bool InternalValue::hasNullOperand() const {
235 return std::holds_alternative<FormattedPlaceholder>(argument)
236 && std::get_if<FormattedPlaceholder>(&argument)->isNullOperand();
237 }
238
239 FormattedPlaceholder InternalValue::takeArgument(UErrorCode& errorCode) {
240 if (U_FAILURE(errorCode)) {
241 return {};
242 }
243
244 if (std::holds_alternative<FormattedPlaceholder>(argument)) {
245 return std::move(*std::get_if<FormattedPlaceholder>(&argument));
246 }
247 errorCode = U_ILLEGAL_ARGUMENT_ERROR;
248 return {};
249 }
250
251 const UnicodeString& InternalValue::getFallback() const {
252 if (std::holds_alternative<FormattedPlaceholder>(argument)) {
253 return std::get_if<FormattedPlaceholder>(&argument)->getFallback();
254 }
255 return (*std::get_if<InternalValue*>(&argument))->getFallback();
256 }
257
258 const Selector* InternalValue::getSelector(UErrorCode& errorCode) const {
259 if (U_FAILURE(errorCode)) {
260 return nullptr;
261 }
262
263 if (selector == nullptr) {
264 errorCode = U_ILLEGAL_ARGUMENT_ERROR;
265 }
266 return selector;
267 }
268
269 InternalValue::InternalValue(FormattedPlaceholder&& arg) {
270 argument = std::move(arg);
271 selector = nullptr;
272 formatter = nullptr;
273 }
274
275 InternalValue::InternalValue(InternalValue* operand,
276 FunctionOptions&& opts,
277 const FunctionName& functionName,
278 const Formatter* f,
279 const Selector* s) {
280 argument = operand;
281 options = std::move(opts);
282 name = functionName;
283 selector = s;
284 formatter = f;
285 U_ASSERT(selector != nullptr || formatter != nullptr);
286 }
287
288 // `this` cannot be used after calling this method
289 void InternalValue::forceSelection(DynamicErrors& errs,
290 const UnicodeString* keys,
291 int32_t keysLen,
292 UnicodeString* prefs,
293 int32_t& prefsLen,
294 UErrorCode& errorCode) {
295 if (U_FAILURE(errorCode)) {
296 return;
297 }
298
299 if (!canSelect()) {
300 errorCode = U_ILLEGAL_ARGUMENT_ERROR;
301 return;
302 }
303 // Find the argument and complete set of options by traversing `argument`
304 FunctionOptions opts;
305 InternalValue* p = this;
306 FunctionName selectorName = name;
307 while (std::holds_alternative<InternalValue*>(p->argument)) {
308 if (p->name != selectorName) {
309 // Can only compose calls to the same selector
310 errorCode = U_ILLEGAL_ARGUMENT_ERROR;
311 return;
312 }
313 // First argument to mergeOptions takes precedence
314 opts = opts.mergeOptions(std::move(p->options), errorCode);
315 if (U_FAILURE(errorCode)) {
316 return;
317 }
318 InternalValue* next = *std::get_if<InternalValue*>(&p->argument);
319 p = next;
320 }
321 FormattedPlaceholder arg = std::move(*std::get_if<FormattedPlaceholder>(&p->argument));
322
323 selector->selectKey(std::move(arg), std::move(opts),
324 keys, keysLen,
325 prefs, prefsLen, errorCode);
326 if (U_FAILURE(errorCode)) {
327 errorCode = U_ZERO_ERROR;
328 errs.setSelectorError(selectorName, errorCode);
329 }
330 }
331
332 FormattedPlaceholder InternalValue::forceFormatting(DynamicErrors& errs, UErrorCode& errorCode) {
333 if (U_FAILURE(errorCode)) {
334 return {};
335 }
336
337 if (formatter == nullptr && selector == nullptr) {
338 U_ASSERT(std::holds_alternative<FormattedPlaceholder>(argument));
339 return std::move(*std::get_if<FormattedPlaceholder>(&argument));
340 }
341 if (formatter == nullptr) {
342 errorCode = U_ILLEGAL_ARGUMENT_ERROR;
343 return {};
344 }
345
346 FormattedPlaceholder arg;
347
348 if (std::holds_alternative<FormattedPlaceholder>(argument)) {
349 arg = std::move(*std::get_if<FormattedPlaceholder>(&argument));
350 } else {
351 arg = (*std::get_if<InternalValue*>(&argument))->forceFormatting(errs,
352 errorCode);
353 }
354
355 if (U_FAILURE(errorCode)) {
356 return {};
357 }
358
359 // The fallback for a nullary function call is the function name
360 UnicodeString fallback;
361 if (arg.isNullOperand()) {
362 fallback = u":";
363 fallback += name;
364 } else {
365 fallback = arg.getFallback();
366 }
367
368 // Call the function with the argument
369 FormattedPlaceholder result = formatter->format(std::move(arg), std::move(options), errorCode);
370 if (U_FAILURE(errorCode)) {
371 if (errorCode == U_MF_OPERAND_MISMATCH_ERROR) {
372 errorCode = U_ZERO_ERROR;
373 errs.setOperandMismatchError(name, errorCode);
374 } else {
375 errorCode = U_ZERO_ERROR;
376 // Convey any error generated by the formatter
377 // as a formatting error, except for operand mismatch errors
378 errs.setFormattingError(name, errorCode);
379 }
380 }
381 // Ignore the output if any error occurred
382 if (errs.hasFormattingError()) {
383 return FormattedPlaceholder(fallback);
384 }
385
386 return result;
387 }
388
389 InternalValue& InternalValue::operator=(InternalValue&& other) noexcept {
390 argument = std::move(other.argument);
391 other.argument = nullptr;
392 options = std::move(other.options);
393 name = other.name;
394 selector = other.selector;
395 formatter = other.formatter;
396 other.selector = nullptr;
397 other.formatter = nullptr;
398
399 return *this;
400 }
401
402 InternalValue::~InternalValue() {
403 delete selector;
404 selector = nullptr;
405 delete formatter;
406 formatter = nullptr;
407 if (std::holds_alternative<InternalValue*>(argument)) {
408 delete *std::get_if<InternalValue*>(&argument);
409 argument = nullptr;
410 }
411 }
412
413 } // namespace message2
414 U_NAMESPACE_END
415
416 #endif /* #if !UCONFIG_NO_MF2 */
417
418 #endif /* #if !UCONFIG_NO_FORMATTING */
419
420 #endif /* #if !UCONFIG_NO_NORMALIZATION */