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