2018-06-23 01:39:19 +08:00
|
|
|
//===- Token.h - MLIR Token Interface ---------------------------*- C++ -*-===//
|
|
|
|
//
|
2020-01-26 11:58:30 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
2019-12-24 01:35:36 +08:00
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-06-23 01:39:19 +08:00
|
|
|
//
|
2019-12-24 01:35:36 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-06-23 01:39:19 +08:00
|
|
|
|
|
|
|
#ifndef MLIR_LIB_PARSER_TOKEN_H
|
|
|
|
#define MLIR_LIB_PARSER_TOKEN_H
|
|
|
|
|
|
|
|
#include "mlir/Support/LLVM.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/Support/SMLoc.h"
|
|
|
|
|
|
|
|
namespace mlir {
|
|
|
|
|
|
|
|
/// This represents a token in the MLIR syntax.
|
|
|
|
class Token {
|
|
|
|
public:
|
2018-06-30 02:15:56 +08:00
|
|
|
enum Kind {
|
|
|
|
#define TOK_MARKER(NAME) NAME,
|
|
|
|
#define TOK_IDENTIFIER(NAME) NAME,
|
|
|
|
#define TOK_LITERAL(NAME) NAME,
|
|
|
|
#define TOK_PUNCTUATION(NAME, SPELLING) NAME,
|
|
|
|
#define TOK_KEYWORD(SPELLING) kw_##SPELLING,
|
|
|
|
#include "TokenKinds.def"
|
2018-06-23 01:39:19 +08:00
|
|
|
};
|
|
|
|
|
2019-07-23 01:51:40 +08:00
|
|
|
Token(Kind kind, StringRef spelling) : kind(kind), spelling(spelling) {}
|
2018-06-23 01:39:19 +08:00
|
|
|
|
|
|
|
// Return the bytes that make up this token.
|
|
|
|
StringRef getSpelling() const { return spelling; }
|
|
|
|
|
|
|
|
// Token classification.
|
2018-06-30 02:15:56 +08:00
|
|
|
Kind getKind() const { return kind; }
|
|
|
|
bool is(Kind K) const { return kind == K; }
|
2018-06-23 01:39:19 +08:00
|
|
|
|
2019-07-23 01:51:40 +08:00
|
|
|
bool isAny(Kind k1, Kind k2) const { return is(k1) || is(k2); }
|
2018-06-23 01:39:19 +08:00
|
|
|
|
|
|
|
/// Return true if this token is one of the specified kinds.
|
2019-07-23 01:51:40 +08:00
|
|
|
template <typename... T>
|
2018-06-30 02:15:56 +08:00
|
|
|
bool isAny(Kind k1, Kind k2, Kind k3, T... others) const {
|
2018-06-23 01:39:19 +08:00
|
|
|
if (is(k1))
|
|
|
|
return true;
|
|
|
|
return isAny(k2, k3, others...);
|
|
|
|
}
|
|
|
|
|
2018-06-30 02:15:56 +08:00
|
|
|
bool isNot(Kind k) const { return kind != k; }
|
2018-06-23 01:39:19 +08:00
|
|
|
|
|
|
|
/// Return true if this token isn't one of the specified kinds.
|
Fix a bug in the .mlir lexer, where a \0 character in a file is treated as a colon (due to an accidental fall through) instead of whitespace.
Summary:
While here, simplify the lexer a bit by eliminating the unneeded 'operator'
classification of certain sigils, they can just be treated as 'punctuation'.
Reviewers: rriddle!
Subscribers: mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, liufengdb, Joonsoo, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D76647
2020-03-24 06:39:32 +08:00
|
|
|
template <typename... T>
|
|
|
|
bool isNot(Kind k1, Kind k2, T... others) const {
|
2018-06-23 01:39:19 +08:00
|
|
|
return !isAny(k1, k2, others...);
|
|
|
|
}
|
|
|
|
|
2018-07-05 11:45:39 +08:00
|
|
|
/// Return true if this is one of the keyword token kinds (e.g. kw_if).
|
|
|
|
bool isKeyword() const;
|
|
|
|
|
2018-06-23 06:52:02 +08:00
|
|
|
// Helpers to decode specific sorts of tokens.
|
|
|
|
|
|
|
|
/// For an integer token, return its value as an unsigned. If it doesn't fit,
|
|
|
|
/// return None.
|
2018-06-29 11:45:33 +08:00
|
|
|
Optional<unsigned> getUnsignedIntegerValue() const;
|
|
|
|
|
2018-07-20 00:52:39 +08:00
|
|
|
/// For an integer token, return its value as an uint64_t. If it doesn't fit,
|
2018-07-05 11:45:39 +08:00
|
|
|
/// return None.
|
Fix the MLIR integer attribute parser to be correct in the face of large integer attributes, it was previously artificially limited to 64 bits.
Reviewers: rriddle!
Subscribers: mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, liufengdb, Joonsoo, grosul1, frgossen, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D78065
2020-04-14 07:37:00 +08:00
|
|
|
static Optional<uint64_t> getUInt64IntegerValue(StringRef spelling);
|
|
|
|
Optional<uint64_t> getUInt64IntegerValue() const {
|
|
|
|
return getUInt64IntegerValue(getSpelling());
|
|
|
|
}
|
2018-07-05 11:45:39 +08:00
|
|
|
|
2018-08-01 08:15:15 +08:00
|
|
|
/// For a floatliteral token, return its value as a double. Returns None in
|
|
|
|
/// the case of underflow or overflow.
|
|
|
|
Optional<double> getFloatingPointValue() const;
|
|
|
|
|
2018-06-30 13:08:05 +08:00
|
|
|
/// For an inttype token, return its bitwidth.
|
|
|
|
Optional<unsigned> getIntTypeBitwidth() const;
|
|
|
|
|
2020-01-11 03:48:24 +08:00
|
|
|
/// For an inttype token, return its signedness semantics: llvm::None means no
|
|
|
|
/// signedness semantics; true means signed integer type; false means unsigned
|
|
|
|
/// integer type.
|
|
|
|
Optional<bool> getIntTypeSignedness() const;
|
|
|
|
|
2018-07-21 09:41:34 +08:00
|
|
|
/// Given a hash_identifier token like #123, try to parse the number out of
|
|
|
|
/// the identifier, returning None if it is a named identifier like #x or
|
|
|
|
/// if the integer doesn't fit.
|
|
|
|
Optional<unsigned> getHashIdentifierNumber() const;
|
|
|
|
|
2019-10-09 08:44:39 +08:00
|
|
|
/// Given a token containing a string literal, return its value, including
|
|
|
|
/// removing the quote characters and unescaping the contents of the string.
|
2018-06-29 11:45:33 +08:00
|
|
|
std::string getStringValue() const;
|
2018-06-23 01:39:19 +08:00
|
|
|
|
2018-06-23 06:52:02 +08:00
|
|
|
// Location processing.
|
2018-06-23 01:39:19 +08:00
|
|
|
llvm::SMLoc getLoc() const;
|
|
|
|
llvm::SMLoc getEndLoc() const;
|
|
|
|
llvm::SMRange getLocRange() const;
|
|
|
|
|
2018-06-30 02:15:56 +08:00
|
|
|
/// 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.
|
|
|
|
static StringRef getTokenSpelling(Kind kind);
|
|
|
|
|
2018-06-23 01:39:19 +08:00
|
|
|
private:
|
|
|
|
/// Discriminator that indicates the sort of token this is.
|
2018-06-30 02:15:56 +08:00
|
|
|
Kind kind;
|
2018-06-23 01:39:19 +08:00
|
|
|
|
|
|
|
/// A reference to the entire token contents; this is always a pointer into
|
|
|
|
/// a memory buffer owned by the source manager.
|
|
|
|
StringRef spelling;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace mlir
|
|
|
|
|
2019-07-23 01:51:40 +08:00
|
|
|
#endif // MLIR_LIB_PARSER_TOKEN_H
|