Computer Laboratory

Protocol.h
Go to the documentation of this file.
1 
8 /*
9  * Copyright (c) 2013 Jonathan Anderson
10  * All rights reserved.
11  *
12  * This software was developed by SRI International and the University of
13  * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
14  * ("CTSRD"), as part of the DARPA CRASH research programme.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  * 1. Redistributions of source code must retain the above copyright
20  * notice, this list of conditions and the following disclaimer.
21  * 2. Redistributions in binary form must reproduce the above copyright
22  * notice, this list of conditions and the following disclaimer in the
23  * documentation and/or other materials provided with the distribution.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 
38 #ifndef PROTOCOL_H
39 #define PROTOCOL_H
40 
41 #include "tesla.pb.h"
42 
43 namespace tesla {
44 
45 template<class T>
46 inline bool operator != (const T& x, const T& y) { return !(x == y); }
47 
48 inline bool operator == (const Location& x, const Location& y) {
49  return (
50  // Don't rely on operator==(string&,string&); it might produce unexpected
51  // results depending on the presence of NULL terminators.
52  (strcmp(x.filename().c_str(), y.filename().c_str()) == 0)
53  && (x.line() == y.line())
54  && (x.counter() == y.counter())
55  );
56 }
57 
58 inline bool operator == (const Identifier& x, const Identifier& y) {
59  if (x.has_name())
60  return (x.name() == y.name());
61 
62  return (x.location() == y.location());
63 }
64 
65 inline bool operator==(const Argument &A1, const Argument &A2) {
66  if (A1.type() != A2.type())
67  return false;
68 
69  if (A1.has_index() ^ A2.has_index())
70  return false;
71 
72  if (A1.has_index() && ((A1.index() != A2.index())))
73  return false;
74 
75  if (A1.has_name() ^ A2.has_name())
76  return false;
77 
78  if (A1.has_name() && (A1.name() != A2.name()))
79  return false;
80 
81  if (A1.has_value() ^ A2.has_value())
82  return false;
83 
84  if (A1.has_value() && (A1.value() != A2.value()))
85  return false;
86 
87  return true;
88 }
89 
90 inline bool operator==(const NowEvent &X, const NowEvent &Y) {
91  return X.location() == Y.location();
92 }
93 
94 inline bool operator==(const FunctionRef &X, const FunctionRef &Y) {
95  return (X.name() == Y.name());
96 }
97 
98 inline bool operator==(const FunctionEvent &E1, const FunctionEvent &E2) {
99  if (E1.function() != E2.function())
100  return false;
101 
102  if (E1.has_direction() ^ E2.has_direction())
103  return false;
104 
105  if (E1.has_direction() && (E1.direction() != E2.direction()))
106  return false;
107 
108  if (E1.has_context() ^ E2.has_context())
109  return false;
110 
111  if (E1.has_context() && (E1.context() != E2.context()))
112  return false;
113 
114  if (E1.has_expectedreturnvalue() ^ E2.has_expectedreturnvalue())
115  return false;
116 
117  if (E1.has_expectedreturnvalue()
118  && (E1.expectedreturnvalue() != E2.expectedreturnvalue()))
119  return false;
120 
121  if (E1.argument_size() != E2.argument_size()) return false;
122  for (int i=0 ; i<E1.argument_size() ; i++)
123  if (E1.argument(i) != E2.argument(i)) return false;
124 
125  return true;
126 }
127 
128 inline bool operator==(const StructField &X, const StructField &Y) {
129  return (
130  (X.type() == Y.type())
131  && (X.base() == Y.base())
132  && (X.name() == Y.name())
133  && (X.index() == Y.index())
134  );
135 }
136 
137 inline bool operator==(const FieldAssignment &X, const FieldAssignment &Y) {
138  return (
139  (X.field() == Y.field())
140  && (X.operation() == Y.operation())
141  && (X.value() == Y.value())
142  && (X.strict() == Y.strict())
143  );
144 }
145 
146 
147 inline bool operator < (const Location& x, const Location& y) {
148  // Again, don't trust operator<(string&,string&) because of NULL funniness.
149  int cmp = strcmp(x.filename().c_str(), y.filename().c_str());
150  if (cmp < 0) return true;
151  if (cmp > 0) return false;
152 
153  if (x.line() < y.line()) return true;
154  if (x.line() > y.line()) return false;
155 
156  return (x.counter() < y.counter());
157 }
158 
159 inline bool operator < (const Identifier& x, const Identifier& y) {
160  if (x.has_name()) {
161  if (!y.has_name())
162  return true;
163 
164  return (x.name() < y.name());
165  }
166 
167  return (x.location() < y.location());
168 }
169 
170 } // namespace tesla
171 
172 #endif // TRANSITION_H
173