//===- Token.cpp - MLIR Token Implementation ------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // This file implements the Token class for the MLIR textual form. // //===----------------------------------------------------------------------===// #include "Token.h" #include "mlir/Support/LLVM.h" #include "llvm/ADT/StringExtras.h" #include "llvm/Support/ErrorHandling.h" #include <cassert> #include <cstdint> #include <optional> #include <string> usingnamespacemlir; SMLoc Token::getLoc() const { … } SMLoc Token::getEndLoc() const { … } SMRange Token::getLocRange() const { … } /// For an integer token, return its value as an unsigned. If it doesn't fit, /// return std::nullopt. std::optional<unsigned> Token::getUnsignedIntegerValue() const { … } /// For an integer token, return its value as a uint64_t. If it doesn't fit, /// return std::nullopt. std::optional<uint64_t> Token::getUInt64IntegerValue(StringRef spelling) { … } /// For a floatliteral, return its value as a double. Return std::nullopt if the /// value underflows or overflows. std::optional<double> Token::getFloatingPointValue() const { … } /// For an inttype token, return its bitwidth. std::optional<unsigned> Token::getIntTypeBitwidth() const { … } std::optional<bool> Token::getIntTypeSignedness() const { … } /// Given a token containing a string literal, return its value, including /// removing the quote characters and unescaping the contents of the string. The /// lexer has already verified that this token is valid. std::string Token::getStringValue() const { … } /// Given a token containing a hex string literal, return its value or /// std::nullopt if the token does not contain a valid hex string. std::optional<std::string> Token::getHexStringValue() const { … } /// Given a token containing a symbol reference, return the unescaped string /// value. std::string Token::getSymbolReference() const { … } /// Given a hash_identifier token like #123, try to parse the number out of /// the identifier, returning std::nullopt if it is a named identifier like #x /// or if the integer doesn't fit. std::optional<unsigned> Token::getHashIdentifierNumber() const { … } /// Given a punctuation or keyword token kind, return the spelling of the /// token as a string. Warning: This will abort on markers, identifiers and /// literal tokens since they have no fixed spelling. StringRef Token::getTokenSpelling(Kind kind) { … } /// Return true if this is one of the keyword token kinds (e.g. kw_if). bool Token::isKeyword() const { … } bool Token::isCodeCompletionFor(Kind kind) const { … }