Path: blob/main/contrib/llvm-project/llvm/utils/TableGen/DFAEmitter.h
35258 views
//===--------------------- DfaEmitter.h -----------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7// Defines a generic automaton builder. This takes a set of transitions and8// states that represent a nondeterministic finite state automaton (NFA) and9// emits a determinized DFA in a form that include/llvm/Support/Automaton.h can10// drive.11//12// See file llvm/TableGen/Automaton.td for the TableGen API definition.13//14//===----------------------------------------------------------------------===//1516#ifndef LLVM_UTILS_TABLEGEN_DFAEMITTER_H17#define LLVM_UTILS_TABLEGEN_DFAEMITTER_H1819#include "llvm/ADT/SmallVector.h"20#include "llvm/ADT/UniqueVector.h"21#include <map>22#include <set>23#include <utility>24#include <vector>2526namespace llvm {2728class raw_ostream;29class StringRef;3031/// Construct a deterministic finite state automaton from possible32/// nondeterministic state and transition data.33///34/// The state type is a 64-bit unsigned integer. The generated automaton is35/// invariant to the sparsity of the state representation - its size is only36/// a function of the cardinality of the set of states.37///38/// The inputs to this emitter are considered to define a nondeterministic39/// finite state automaton (NFA). This is then converted to a DFA during40/// emission. The emitted tables can be used to by41/// include/llvm/Support/Automaton.h.42class DfaEmitter {43public:44// The type of an NFA state. The initial state is always zero.45using state_type = uint64_t;46// The type of an action.47using action_type = uint64_t;4849DfaEmitter() = default;50virtual ~DfaEmitter() = default;5152void addTransition(state_type From, state_type To, action_type A);53void emit(StringRef Name, raw_ostream &OS);5455protected:56/// Emit the C++ type of an action to OS.57virtual void printActionType(raw_ostream &OS);58/// Emit the C++ value of an action A to OS.59virtual void printActionValue(action_type A, raw_ostream &OS);6061private:62/// The state type of deterministic states. These are only used internally to63/// this class. This is an ID into the DfaStates UniqueVector.64using dfa_state_type = unsigned;6566/// The actual representation of a DFA state, which is a union of one or more67/// NFA states.68using DfaState = SmallVector<state_type, 4>;6970/// A DFA transition consists of a set of NFA states transitioning to a71/// new set of NFA states. The DfaTransitionInfo tracks, for every72/// transitioned-from NFA state, a set of valid transitioned-to states.73///74/// Emission of this transition relation allows algorithmic determination of75/// the possible candidate NFA paths taken under a given input sequence to76/// reach a given DFA state.77using DfaTransitionInfo = SmallVector<std::pair<state_type, state_type>, 4>;7879/// The set of all possible actions.80std::set<action_type> Actions;8182/// The set of nondeterministic transitions. A state-action pair can83/// transition to multiple target states.84std::map<std::pair<state_type, action_type>, std::vector<state_type>>85NfaTransitions;86std::set<state_type> NfaStates;87unsigned NumNfaTransitions = 0;8889/// The set of deterministic states. DfaStates.getId(DfaState) returns an ID,90/// which is dfa_state_type. Note that because UniqueVector reserves state91/// zero, the initial DFA state is always 1.92UniqueVector<DfaState> DfaStates;93/// The set of deterministic transitions. A state-action pair has only a94/// single target state.95std::map<std::pair<dfa_state_type, action_type>,96std::pair<dfa_state_type, DfaTransitionInfo>>97DfaTransitions;9899/// Visit all NFA states and construct the DFA.100void constructDfa();101/// Visit a single DFA state and construct all possible transitions to new DFA102/// states.103void visitDfaState(const DfaState &DS);104};105106} // namespace llvm107108#endif109110111