forked from runtimeverification/k
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAttribute.java
More file actions
230 lines (198 loc) · 7.54 KB
/
Attribute.java
File metadata and controls
230 lines (198 loc) · 7.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Copyright (c) 2014-2019 K Team. All Rights Reserved.
package org.kframework.kil;
import com.google.common.reflect.TypeToken;
import com.google.inject.name.Named;
import com.google.inject.name.Names;
import org.kframework.kil.loader.Constants;
import java.io.Serializable;
import java.lang.annotation.Annotation;
/**
* Represents either an explicit attribute on a {@link Rule} or {@link Production},
* or node metadata like location.
* The inherited member attributes is used for location information
* if this represents an explicitly written attribute.
*/
public class Attribute<T> extends ASTNode {
public static final String FUNCTION_KEY = "function";
public static final String SIMPLIFICATION_KEY = "simplification";
public static final String ASSOCIATIVE_KEY = "assoc";
public static final String COMMUTATIVE_KEY = "comm";
public static final String IDEMPOTENT_KEY = "idem";
public static final String PROJECTION_KEY = "proj";
public static final String UNIT_KEY = "unit";
public static final String PREDICATE_KEY = "predicate";
public static final String KORE_KEY = "kore";
public static final String ML_BINDER_KEY = "mlBinder";
public static final String ANYWHERE_KEY = Constants.ANYWHERE;
public static final String PATTERN_KEY = "pattern";
public static final String PATTERN_FOLDING_KEY = "pattern-folding";
public static final String HOOK_KEY = "hook";
public static final String MACRO_KEY = "macro";
public static final String ALIAS_KEY = "alias";
public static final String MACRO_REC_KEY = "macro-rec";
public static final String ALIAS_REC_KEY = "alias-rec";
public static final String LEMMA_KEY = "lemma";
public static final String TRUSTED_KEY = "trusted";
public static final String MATCHING_KEY = "matching";
public static final String INJECTIVE_KEY = "injective";
public static final String BITWIDTH_KEY = "bitwidth";
public static final String EXPONENT_KEY = "exponent";
public static final String SIGNIFICAND_KEY = "significand";
public static final String SMTHOOK_KEY = "smt-hook";
public static final String SMTLIB_KEY = "smtlib";
public static final String SMT_LEMMA_KEY = "smt-lemma";
public static final String SMT_PRELUDE_KEY = "smt-prelude";
// Used to direct configuration abstraction,
// generated when translating configuration declaration to productions.
public static final String CELL_KEY = "cell";
public static final String CELL_FRAGMENT_KEY = "cellFragment";
public static final String CELL_OPT_ABSENT_KEY = "cellOptAbsent";
public static final String IMPURE_KEY = "impure";
public static final String STRICT_KEY = "strict";
public static final String SEQSTRICT_KEY = "seqstrict";
public static final String CONCRETE_FUNCTION_KEY = "concrete";
public static final Attribute<String> PATTERN = Attribute.of(PATTERN_KEY, "");
private Key<T> key;
private T value;
public static class Key<T> implements Serializable {
private final TypeToken<T> typeToken;
private final Annotation annotation;
protected Key(TypeToken<T> typeToken, Annotation annotation) {
this.typeToken = typeToken;
this.annotation = annotation;
}
public TypeToken<T> getTypeToken() {
return typeToken;
}
public Annotation getAnnotation() {
return annotation;
}
public static <T> Key<T> get(Class<T> cls, Annotation annotation) {
return new Key<T>(TypeToken.of(cls), annotation);
}
public static <T> Key<T> get(Class<T> cls) {
return new Key<T>(TypeToken.of(cls), null);
}
public static <T> Key<T> get(TypeToken<T> type) {
return new Key<T>(type, null);
}
public static <T> Key<T> get(TypeToken<T> type, Annotation annotation) {
return new Key<T>(type, annotation);
}
@Override
public String toString() {
if (getTypeToken().equals(TypeToken.of(String.class))) {
return toString(getAnnotation());
}
String annotation = toString(getAnnotation());
if (annotation != null) {
return "@" + getTypeToken().toString() + "." + annotation;
} else {
return "@" + getTypeToken().toString();
}
}
public static String toString(Annotation annotation) {
if (annotation == null) return null;
if (annotation instanceof Named) {
return ((Named)annotation).value();
}
return annotation.toString();
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((annotation == null) ? 0 : annotation.hashCode());
result = prime * result
+ ((typeToken == null) ? 0 : typeToken.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Key<?> other = (Key<?>) obj;
if (annotation == null) {
if (other.annotation != null)
return false;
} else if (!annotation.equals(other.annotation))
return false;
if (typeToken == null) {
if (other.typeToken != null)
return false;
} else if (!typeToken.equals(other.typeToken))
return false;
return true;
}
}
public static Attribute<String> of(String key, String value) {
return new Attribute<String>(keyOf(key), value);
}
public static Key<String> keyOf(String key) {
return Key.get(String.class, Names.named(key));
}
public Attribute(Key<T> key, T value) {
super();
this.key = key;
this.value = value;
}
public Attribute(Attribute<T> attribute) {
super(attribute);
key = attribute.key;
value = attribute.value;
}
@Override
public String toString() {
return " " + this.getKey() + "(" + this.getValue() + ")";
}
public void setValue(T value) {
this.value = value;
}
public T getValue() {
return value;
}
public Key<T> getKey() {
return key;
}
public void setKey(Key<T> key) {
this.key = key;
}
@Override
public Attribute<T> shallowCopy() {
return new Attribute<T>(this);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((key == null) ? 0 : key.hashCode());
result = prime * result + ((value == null) ? 0 : value.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Attribute<?> other = (Attribute<?>) obj;
if (key == null) {
if (other.key != null)
return false;
} else if (!key.equals(other.key))
return false;
if (value == null) {
if (other.value != null)
return false;
} else if (!value.equals(other.value))
return false;
return true;
}
}