|
| 1 | +//===--- TypeCheckRegex.cpp - Regex type checking utilities ---------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "TypeCheckRegex.h" |
| 14 | + |
| 15 | +#include "swift/AST/ASTContext.h" |
| 16 | +#include "swift/AST/Decl.h" |
| 17 | +#include "swift/AST/Expr.h" |
| 18 | +#include "swift/AST/Type.h" |
| 19 | +#include "swift/AST/Types.h" |
| 20 | + |
| 21 | +using namespace swift; |
| 22 | + |
| 23 | +// Encoding rules: |
| 24 | +// encode(〚`T`〛) ==> <version>, 〚`T`〛, .end |
| 25 | +// 〚`T` (atom)〛 ==> .atom |
| 26 | +// 〚`name: T` (atom)〛 ==> .atom, `name`, '\0' |
| 27 | +// 〚`[T]`〛 ==> 〚`T`〛, .formArray |
| 28 | +// 〚`T?`〛 ==> 〚`T`〛, .formOptional |
| 29 | +// 〚`(T0, T1, ...)`〛 ==> .beginTuple, 〚`T0`〛, 〚`T1`〛, ..., .endTuple |
| 30 | +// |
| 31 | +// For details, see apple/swift-experimental-string-processing. |
| 32 | +bool swift::decodeRegexCaptureTypes(ASTContext &ctx, |
| 33 | + ArrayRef<uint8_t> serialization, |
| 34 | + Type atomType, |
| 35 | + SmallVectorImpl<TupleTypeElt> &result) { |
| 36 | + using Version = RegexLiteralExpr::CaptureStructureSerializationVersion; |
| 37 | + static const Version implVersion = 1; |
| 38 | + unsigned size = serialization.size(); |
| 39 | + // A serialization should store a version and `.end` at the very least. |
| 40 | + unsigned minSize = sizeof(Version) + sizeof(RegexCaptureStructureCode); |
| 41 | + if (size < minSize) |
| 42 | + return false; |
| 43 | + // Read version. |
| 44 | + Version version = *reinterpret_cast<const Version *>(serialization.data()); |
| 45 | + if (version != implVersion) |
| 46 | + return true; |
| 47 | + // Read contents. |
| 48 | + SmallVector<SmallVector<TupleTypeElt, 4>, 4> scopes(1); |
| 49 | + unsigned offset = sizeof(Version); |
| 50 | + auto consumeCode = [&]() -> Optional<RegexCaptureStructureCode> { |
| 51 | + auto rawValue = serialization[offset]; |
| 52 | + if (rawValue >= (uint8_t)RegexCaptureStructureCode::CaseCount) |
| 53 | + return None; |
| 54 | + offset += sizeof(RegexCaptureStructureCode); |
| 55 | + return (RegexCaptureStructureCode)rawValue; |
| 56 | + }; |
| 57 | + do { |
| 58 | + auto code = consumeCode(); |
| 59 | + if (!code) |
| 60 | + return false; |
| 61 | + switch (*code) { |
| 62 | + case RegexCaptureStructureCode::End: |
| 63 | + offset = size; |
| 64 | + break; |
| 65 | + case RegexCaptureStructureCode::Atom: |
| 66 | + scopes.back().push_back(atomType); |
| 67 | + break; |
| 68 | + case RegexCaptureStructureCode::NamedAtom: { |
| 69 | + auto *namePtr = reinterpret_cast<const char *>( |
| 70 | + serialization.slice(offset).data()); |
| 71 | + auto length = strnlen(namePtr, size - offset); |
| 72 | + if (length >= size - offset) |
| 73 | + return true; // Unterminated string. |
| 74 | + StringRef name(namePtr, length); |
| 75 | + scopes.back().push_back(TupleTypeElt(atomType, ctx.getIdentifier(name))); |
| 76 | + offset += length + /*NUL*/ 1; |
| 77 | + break; |
| 78 | + } |
| 79 | + case RegexCaptureStructureCode::FormArray: { |
| 80 | + auto &type = scopes.back().back(); |
| 81 | + type = TupleTypeElt(ArraySliceType::get(type.getRawType())); |
| 82 | + break; |
| 83 | + } |
| 84 | + case RegexCaptureStructureCode::FormOptional: { |
| 85 | + auto &type = scopes.back().back(); |
| 86 | + type = TupleTypeElt(OptionalType::get(type.getRawType())); |
| 87 | + break; |
| 88 | + } |
| 89 | + case RegexCaptureStructureCode::BeginTuple: |
| 90 | + scopes.push_back({}); |
| 91 | + break; |
| 92 | + case RegexCaptureStructureCode::EndTuple: { |
| 93 | + auto children = scopes.pop_back_val(); |
| 94 | + scopes.back().push_back(TupleType::get(children, ctx)); |
| 95 | + break; |
| 96 | + } |
| 97 | + case RegexCaptureStructureCode::CaseCount: |
| 98 | + llvm_unreachable("Handled earlier"); |
| 99 | + } |
| 100 | + } while (offset < size); |
| 101 | + if (scopes.size() != 1) |
| 102 | + return true; // Unterminated tuple. |
| 103 | + auto &elements = scopes.back(); |
| 104 | + result.append(elements.begin(), elements.end()); |
| 105 | + return false; |
| 106 | +} |
0 commit comments