1 /*
2  * Copyright (c) 2015-2018, Intel Corporation
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *  * Redistributions of source code must retain the above copyright notice,
8  *    this list of conditions and the following disclaimer.
9  *  * Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *  * Neither the name of Intel Corporation nor the names of its contributors
13  *    may be used to endorse or promote products derived from this software
14  *    without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /** \file
30  * \brief NG declaration.
31  */
32 
33 #ifndef NG_H
34 #define NG_H
35 
36 #include "ng_holder.h"
37 #include "ue2common.h"
38 #include "parser/position.h"
39 #include "som/slot_manager.h"
40 #include "som/som.h"
41 #include "util/boundary_reports.h"
42 #include "util/compile_context.h"
43 #include "util/depth.h"
44 #include "util/graph.h"
45 #include "util/noncopyable.h"
46 #include "util/report_manager.h"
47 
48 #include <deque>
49 #include <map>
50 #include <memory>
51 #include <utility>
52 #include <vector>
53 
54 namespace ue2 {
55 
56 struct CompileContext;
57 struct ue2_literal;
58 
59 class ExpressionInfo;
60 class RoseBuild;
61 class SmallWriteBuild;
62 
63 class NG : noncopyable {
64 public:
65     NG(const CompileContext &in_cc, size_t num_patterns,
66        unsigned in_somPrecision);
67     ~NG();
68 
69     /** \brief Consumes a pattern, returns false or throws a CompileError
70      * exception if the graph cannot be consumed. */
71     bool addGraph(ExpressionInfo &expr, std::unique_ptr<NGHolder> g_ptr);
72 
73     /** \brief Consumes a graph, cut-down version of addGraph for use by SOM
74      * processing. */
75     bool addHolder(NGHolder &h);
76 
77     /** \brief Adds a literal to Rose, used by literal shortcut passes (instead
78      * of using \ref addGraph) */
79     bool addLiteral(const ue2_literal &lit, u32 expr_index, u32 external_report,
80                     bool highlander, som_type som, bool quiet);
81 
82     /** \brief Maximum history in bytes available for use by SOM reverse NFAs,
83      * a hack for pattern support (see UE-1903). This is always set to the max
84      * "lookbehind" length. */
85     const u32 maxSomRevHistoryAvailable;
86 
87     /** \brief The length of the shortest corpus which can match a pattern
88      * contained in the NG (excluding the boundary reports used by vacuous
89      * patterns, which give an effective minWidth of zero). */
90     depth minWidth;
91 
92     ReportManager rm;
93     SomSlotManager ssm;
94     BoundaryReports boundary;
95     const CompileContext cc;
96 
97     const std::unique_ptr<SmallWriteBuild> smwr; //!< SmallWrite builder.
98     const std::unique_ptr<RoseBuild> rose; //!< Rose builder.
99 };
100 
101 /** \brief Run graph reduction passes.
102  *
103  * Shared with the small write compiler.
104  */
105 void reduceGraph(NGHolder &g, som_type som, bool utf8,
106                  const CompileContext &cc);
107 
108 } // namespace ue2
109 
110 #endif
111