Computer Laboratory

State.h
Go to the documentation of this file.
1 
2 /*
3  * Copyright (c) 2013 Jonathan Anderson
4  * All rights reserved.
5  *
6  * This software was developed by SRI International and the University of
7  * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
8  * ("CTSRD"), as part of the DARPA CRASH research programme.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in the
17  * documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #ifndef STATE_H
33 #define STATE_H
34 
35 #include "Transition.h"
36 #include "Types.h"
37 
38 #include <llvm/ADT/ArrayRef.h>
39 #include <llvm/ADT/OwningPtr.h>
40 
41 namespace tesla {
42 
43 class Argument;
44 
45 namespace internal {
46  class DFABuilder;
47 }
48 
50 class State {
51  friend class internal::DFABuilder;
52 public:
53  class Builder {
54  public:
55  Builder& SetName(llvm::StringRef N) { Name = N; return *this; }
56  Builder& SetStartState(bool S = true) { Start = S; return *this; }
57  Builder& SetAccepting(bool A = true) { Accept = A; return *this; }
58  Builder& SetRefCount(int R) { assert(R >= 0); RefCount = R; return *this; }
59 
60  State* Build();
61 
63  : States(S), Start(false), Accept(false), RefCount(-1)
64  {
65  }
66 
67  private:
68  StateVector& States;
69 
70  std::string Name;
71  bool Start;
72  bool Accept;
73  int RefCount;
74  };
75 
76  static Builder NewBuilder(StateVector& S) { return Builder(S); }
77 
78  ~State();
79 
80  void AddTransition(llvm::OwningPtr<Transition>&);
81 
82  size_t ID() const { return id; }
83  std::string Name(bool QuoteNonNumeric = true) const;
84  bool IsStartState() const { return start; }
85  bool IsAcceptingState() const { return accept; }
86 
87  void UpdateReferences(const Transition&);
88  const ReferenceVector References() const { return Refs; }
89  uint32_t Mask() const;
90 
91  std::string String() const;
92  std::string Dot() const;
93  Transition *const*begin() const { return Transitions.begin(); }
94  Transition *const*end() const { return Transitions.end(); }
95 
96 private:
97  State(size_t id, bool start = false, bool accept = false,
98  llvm::StringRef name = "")
99  : id(id), name(name), start(start), accept(accept)
100  {
101  }
102 
103  const size_t id;
104  std::string name;
105  const bool start;
106  const bool accept;
107 
109  llvm::OwningArrayPtr<const Argument*> VariableReferences;
111 
112  llvm::SmallVector<Transition*, 1> Transitions;
113 };
114 
115 } // namespace tesla
116 
117 #endif // STATE_H
118