1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
|
//===--- SILParser.h ------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_SIL_PARSER_SILPARSER_H
#define SWIFT_SIL_PARSER_SILPARSER_H
#include "SILParserState.h"
#include "swift/Parse/Parser.h"
#include "swift/SIL/SILCoverageMap.h"
#include "swift/Sema/SILTypeResolutionContext.h"
namespace swift {
struct ParsedSubstitution {
SourceLoc loc;
Type replacement;
};
struct ParsedSpecAttr {
ArrayRef<RequirementRepr> requirements;
bool exported;
SILSpecializeAttr::SpecializationKind kind;
SILFunction *target = nullptr;
Identifier spiGroupID;
ModuleDecl *spiModule;
AvailabilityContext availability = AvailabilityContext::alwaysAvailable();
};
/// The parser for an individual SIL function.
class SILParser {
friend SILParserState;
public:
Parser &P;
SILModule &SILMod;
SILParserState &TUState;
SILFunction *F = nullptr;
GenericSignature ContextGenericSig;
GenericParamList *ContextGenericParams = nullptr;
private:
/// HadError - Have we seen an error parsing this function?
bool HadError = false;
/// Transient state for parsing a multiple optional attributes
/// in parseSpecificSILInstruction:
/// <regular syntax>[, keyword1][, keyword2]
bool parsedComma = false;
/// Data structures used to perform name lookup of basic blocks.
llvm::DenseMap<Identifier, SILBasicBlock *> BlocksByName;
llvm::DenseMap<SILBasicBlock *, Located<Identifier>> UndefinedBlocks;
/// The set of opened packs in the function, indexed by UUID.
/// Note that we don't currently support parsing references to
/// opened packs prior to their instruction, although this is
/// theoretically possible if basic blocks are not sorted in
/// dominance order.
SILTypeResolutionContext::OpenedPackElementsMap OpenedPackElements;
/// Data structures used to perform name lookup for local values.
llvm::StringMap<ValueBase *> LocalValues;
llvm::StringMap<llvm::SmallVector<SpecifyTestInst *>> TestSpecsWithRefs;
llvm::StringMap<SourceLoc> ForwardRefLocalValues;
Type performTypeResolution(TypeRepr *TyR, bool IsSILType,
GenericSignature GenericSig,
GenericParamList *GenericParams);
void convertRequirements(ArrayRef<RequirementRepr> From,
SmallVectorImpl<Requirement> &To,
SmallVectorImpl<Type> &typeErasedParams);
ProtocolConformanceRef
parseProtocolConformanceHelper(ProtocolDecl *&proto,
GenericSignature GenericSig,
GenericParamList *WitnessParams);
public:
SILParser(Parser &P)
: P(P), SILMod(static_cast<SILParserState *>(P.SIL)->M),
TUState(*static_cast<SILParserState *>(P.SIL)) {}
~SILParser();
/// diagnoseProblems - After a function is fully parse, emit any diagnostics
/// for errors and return true if there were any.
bool diagnoseProblems();
/// getGlobalNameForReference - Given a reference to a global name, look it
/// up and return an appropriate SIL function.
SILFunction *getGlobalNameForReference(Identifier Name, CanSILFunctionType Ty,
SourceLoc Loc,
bool IgnoreFwdRef = false);
/// getGlobalNameForDefinition - Given a definition of a global name, look
/// it up and return an appropriate SIL function.
SILFunction *getGlobalNameForDefinition(Identifier Name,
CanSILFunctionType Ty, SourceLoc Loc);
/// getBBForDefinition - Return the SILBasicBlock for a definition of the
/// specified block.
SILBasicBlock *getBBForDefinition(Identifier Name, SourceLoc Loc);
/// getBBForReference - return the SILBasicBlock of the specified name. The
/// source location is used to diagnose a failure if the block ends up never
/// being defined.
SILBasicBlock *getBBForReference(Identifier Name, SourceLoc Loc);
struct UnresolvedValueName {
StringRef Name;
SourceLoc NameLoc;
bool isUndef() const { return Name == "undef"; }
};
/// getLocalValue - Get a reference to a local value with the specified name
/// and type.
SILValue getLocalValue(UnresolvedValueName Name, SILType Type, SILLocation L,
SILBuilder &B);
/// setLocalValue - When an instruction or block argument is defined, this
/// method is used to register it and update our symbol table.
void setLocalValue(ValueBase *Value, StringRef Name, SourceLoc NameLoc);
SILDebugLocation getDebugLoc(SILBuilder &B, SILLocation Loc) {
return SILDebugLocation(Loc, F->getDebugScope());
}
/// @{ Primitive parsing.
/// \verbatim
/// sil-identifier ::= [A-Za-z_0-9]+
/// \endverbatim
bool parseSILIdentifier(Identifier &Result, SourceLoc &Loc, DiagRef D);
template <typename... DiagArgTypes, typename... ArgTypes>
bool parseSILIdentifier(Identifier &Result, Diag<DiagArgTypes...> ID,
ArgTypes... Args) {
SourceLoc L;
return parseSILIdentifier(Result, L, {ID, {Args...}});
}
template <typename T, typename... DiagArgTypes, typename... ArgTypes>
bool parseSILIdentifierSwitch(T &Result, ArrayRef<StringRef> Strings,
Diag<DiagArgTypes...> ID, ArgTypes... Args) {
Identifier TmpResult;
SourceLoc L;
if (parseSILIdentifier(TmpResult, L, {ID, {Args...}})) {
return true;
}
auto Iter = std::find(Strings.begin(), Strings.end(), TmpResult.str());
if (Iter == Strings.end()) {
P.diagnose(P.Tok, ID, Args...);
return true;
}
Result = T(*Iter);
return false;
}
template <typename... DiagArgTypes, typename... ArgTypes>
bool parseSILIdentifier(Identifier &Result, SourceLoc &L,
Diag<DiagArgTypes...> ID, ArgTypes... Args) {
return parseSILIdentifier(Result, L, {ID, {Args...}});
}
template <typename T>
bool
parseSILQualifier(std::optional<T> &result,
llvm::function_ref<std::optional<T>(StringRef)> parseName);
bool parseVerbatim(StringRef identifier);
template <typename T>
bool parseInteger(T &Result, DiagRef D) {
if (!P.Tok.is(tok::integer_literal)) {
P.diagnose(P.Tok, D);
return true;
}
bool error = parseIntegerLiteral(P.Tok.getText(), 0, Result);
P.consumeToken(tok::integer_literal);
return error;
}
template <typename T>
bool parseIntegerLiteral(StringRef text, unsigned radix, T &result) {
text = prepareIntegerLiteralForParsing(text);
return text.getAsInteger(radix, result);
}
StringRef prepareIntegerLiteralForParsing(StringRef text) {
// tok::integer_literal can contain characters that the library
// parsing routines don't expect.
if (text.contains('_'))
text = P.copyAndStripUnderscores(text);
return text;
}
/// @}
/// @{ Type parsing.
bool parseASTType(CanType &result,
GenericSignature genericSig = GenericSignature(),
GenericParamList *genericParams = nullptr,
bool forceContextualType = false);
bool parseASTType(CanType &result, SourceLoc &TypeLoc,
GenericSignature genericSig = GenericSignature(),
GenericParamList *genericParams = nullptr,
bool forceContextualType = false) {
TypeLoc = P.Tok.getLoc();
return parseASTType(result, genericSig, genericParams, forceContextualType);
}
bool parseASTPackType(CanPackType &result) {
SourceLoc loc;
CanType rawType;
if (parseASTType(rawType, loc))
return true;
result = dyn_cast<PackType>(rawType);
if (!result) {
P.diagnose(loc, diag::expected_sil_type_kind, "match $Pack{...}");
return true;
}
return false;
}
std::optional<StringRef>
parseOptionalAttribute(ArrayRef<StringRef> expected) {
// We parse here @ <identifier>.
if (P.Tok.getKind() != tok::at_sign)
return std::nullopt;
auto name = P.peekToken().getText();
if (!is_contained(expected, name))
return std::nullopt;
// Ok, we can do this.
P.consumeToken(tok::at_sign);
P.consumeToken(tok::identifier);
return name;
}
bool parseSILOwnership(ValueOwnershipKind &OwnershipKind) {
// We parse here @ <identifier>.
if (!P.consumeIf(tok::at_sign)) {
// If we fail, we must have @any ownership. We check elsewhere in the
// parser that this matches what the function signature wants.
OwnershipKind = OwnershipKind::None;
return false;
}
StringRef AllOwnershipKinds[4] = {"unowned", "owned", "guaranteed", "none"};
return parseSILIdentifierSwitch(OwnershipKind, AllOwnershipKinds,
diag::expected_sil_value_ownership_kind);
}
void bindSILGenericParams(TypeRepr *TyR);
bool parseSILType(SILType &Result, GenericSignature &parsedGenericSig,
GenericParamList *&parsedGenericParams,
bool IsFuncDecl = false,
GenericSignature parentGenericSig = GenericSignature(),
GenericParamList *parentGenericParams = nullptr);
bool parseSILType(SILType &Result) {
GenericSignature IgnoredSig;
GenericParamList *IgnoredParams = nullptr;
return parseSILType(Result, IgnoredSig, IgnoredParams);
}
bool parseSILType(SILType &Result, SourceLoc &TypeLoc) {
TypeLoc = P.Tok.getLoc();
return parseSILType(Result);
}
bool parseSILType(SILType &Result, SourceLoc &TypeLoc,
GenericSignature &parsedGenericSig,
GenericParamList *&parsedGenericParams,
GenericSignature parentGenericSig = GenericSignature(),
GenericParamList *parentGenericParams = nullptr) {
TypeLoc = P.Tok.getLoc();
return parseSILType(Result, parsedGenericSig, parsedGenericParams, false,
parentGenericSig, parentGenericParams);
}
/// @}
bool parseSILDottedPath(ValueDecl *&Decl,
SmallVectorImpl<ValueDecl *> &values);
bool parseSILDottedPath(ValueDecl *&Decl) {
SmallVector<ValueDecl *, 4> values;
return parseSILDottedPath(Decl, values);
}
bool parseSILDottedPathWithoutPound(ValueDecl *&Decl,
SmallVectorImpl<ValueDecl *> &values);
bool parseSILDottedPathWithoutPound(ValueDecl *&Decl) {
SmallVector<ValueDecl *, 4> values;
return parseSILDottedPathWithoutPound(Decl, values);
}
/// At the time of calling this function, we may not have the type of the
/// Decl yet. So we return a SILDeclRef on the first lookup result and also
/// return all the lookup results. After parsing the expected type, the
/// caller of this function can choose the one that has the expected type.
bool parseSILDeclRef(SILDeclRef &Result,
SmallVectorImpl<ValueDecl *> &values);
bool parseSILDeclRef(SILDeclRef &Result) {
SmallVector<ValueDecl *, 4> values;
return parseSILDeclRef(Result, values);
}
bool parseSILDeclRef(SILDeclRef &Member, bool FnTypeRequired);
bool parseGlobalName(Identifier &Name);
bool parseValueName(UnresolvedValueName &Name);
bool parseValueRef(SILValue &Result, SILType Ty, SILLocation Loc,
SILBuilder &B);
bool parseTypedValueRef(SILValue &Result, SourceLoc &Loc, SILBuilder &B);
bool parseTypedValueRef(SILValue &Result, SILBuilder &B) {
SourceLoc Tmp;
return parseTypedValueRef(Result, Tmp, B);
}
bool parseSILOpcode(SILInstructionKind &Opcode, SourceLoc &OpcodeLoc,
StringRef &OpcodeName);
bool parseSILDebugVar(SILDebugVariable &Var);
bool parseSILDebugInfoExpression(SILDebugInfoExpression &DIExpr);
/// Parses the basic block arguments as part of branch instruction.
bool parseSILBBArgsAtBranch(SmallVector<SILValue, 6> &Args, SILBuilder &B);
bool parseSILLocation(SILLocation &L);
bool parseScopeRef(SILDebugScope *&DS);
bool parseForwardingOwnershipKind(ValueOwnershipKind &forwardingKind);
bool parseSILDebugLocation(SILLocation &L, SILBuilder &B);
bool parseSpecificSILInstruction(SILBuilder &B, SILInstructionKind Opcode,
SourceLoc OpcodeLoc, StringRef OpcodeName,
SILInstruction *&ResultVal);
bool parseSILInstruction(SILBuilder &B);
bool parseCallInstruction(SILLocation InstLoc, SILInstructionKind Opcode,
SILBuilder &B, SILInstruction *&ResultVal);
bool parseSILFunctionRef(SILLocation InstLoc, SILFunction *&ResultFn);
bool parseSILBasicBlock(SILBuilder &B);
bool parseKeyPathPatternComponent(KeyPathPatternComponent &component,
SmallVectorImpl<SILType> &operandTypes,
SourceLoc componentLoc,
Identifier componentKind,
SILLocation InstLoc,
GenericSignature patternSig,
GenericParamList *patternParams);
bool isStartOfSILInstruction();
bool parseSubstitutions(SmallVectorImpl<ParsedSubstitution> &parsed,
GenericSignature GenericSig = GenericSignature(),
GenericParamList *GenericParams = nullptr);
ProtocolConformanceRef
parseProtocolConformance(ProtocolDecl *&proto, GenericSignature &genericSig,
GenericParamList *&genericParams);
ProtocolConformanceRef parseProtocolConformance() {
ProtocolDecl *dummy = nullptr;
GenericSignature genericSig;
GenericParamList *genericParams = nullptr;
return parseProtocolConformance(dummy, genericSig, genericParams);
}
std::optional<llvm::coverage::Counter>
parseSILCoverageExpr(llvm::coverage::CounterExpressionBuilder &Builder);
template <class T>
struct ParsedEnum {
std::optional<T> Value;
StringRef Name;
SourceLoc Loc;
bool isSet() const { return Value.has_value(); }
T operator*() const { return *Value; }
};
template <class T>
void setEnum(ParsedEnum<T> &existing, T value, StringRef name,
SourceLoc loc) {
if (existing.Value) {
if (*existing.Value == value) {
P.diagnose(loc, diag::duplicate_attribute, /*modifier*/ 1);
} else {
P.diagnose(loc, diag::mutually_exclusive_attrs, name, existing.Name,
/*modifier*/ 1);
}
P.diagnose(existing.Loc, diag::previous_attribute, /*modifier*/ 1);
}
existing.Value = value;
existing.Name = name;
existing.Loc = loc;
}
template <class T>
void maybeSetEnum(bool allowed, ParsedEnum<T> &existing, T value,
StringRef name, SourceLoc loc) {
if (allowed)
setEnum(existing, value, name, loc);
else
P.diagnose(loc, diag::unknown_attribute, name);
}
};
} // namespace swift
#endif
|