/
methodInstance.h
325 lines (290 loc) · 13.1 KB
/
methodInstance.h
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
Copyright 2013-present Barefoot Networks, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef _FRONTENDS_P4_METHODINSTANCE_H_
#define _FRONTENDS_P4_METHODINSTANCE_H_
#include "ir/ir.h"
#include "frontends/common/resolveReferences/referenceMap.h"
#include "frontends/p4/typeMap.h"
#include "frontends/p4/parameterSubstitution.h"
#include "frontends/p4/methodInstance.h"
namespace P4 {
class InstanceBase {
public:
template<typename T> bool is() const { return to<T>() != nullptr; }
template<typename T> const T* to() const { return dynamic_cast<const T*>(this); }
protected:
virtual ~InstanceBase() {}
};
/**
This class is very useful for extracting information out of
MethodCallExpressions. Since there are no function pointers in P4,
methods can completely be resolved at compilation time. The static
method 'resolve' will categorize each method call into one of several
kinds:
- apply method, could be of a table, control or parser
- extern function
- extern method (method of an extern object)
- action call
- built-in method (there are five of these: setValid, setInvalid, isValid, push, pop
See also the ConstructorCall class and the MethodCallDescription class
below.
*/
class MethodInstance : public InstanceBase {
protected:
MethodInstance(const IR::MethodCallExpression* mce,
const IR::IDeclaration* decl,
const IR::Type_MethodBase* originalMethodType,
const IR::Type_MethodBase* actualMethodType) :
expr(mce), object(decl), originalMethodType(originalMethodType),
actualMethodType(actualMethodType)
{ CHECK_NULL(mce); CHECK_NULL(originalMethodType); CHECK_NULL(actualMethodType); }
void bindParameters() {
auto params = getActualParameters();
substitution.populate(params, expr->arguments);
}
public:
const IR::MethodCallExpression* expr;
/** Declaration of object that method is applied to.
May be null for plain functions. */
const IR::IDeclaration* object;
/** The type of the *original* called method,
without instantiated type parameters. */
const IR::Type_MethodBase* originalMethodType;
/** Type of called method,
with instantiated type parameters. */
const IR::Type_MethodBase* actualMethodType;
/// For each callee parameter the corresponding argument
ParameterSubstitution substitution;
virtual bool isApply() const { return false; }
/** @param useExpressionType If true, the typeMap can be nullptr,
and then mce->type is used. For some technical reasons
neither the refMap or the typeMap are const here. */
static MethodInstance* resolve(const IR::MethodCallExpression* mce,
ReferenceMap* refMap, TypeMap* typeMap,
bool useExpressionType = false);
static MethodInstance* resolve(const IR::MethodCallStatement* mcs,
ReferenceMap* refMap, TypeMap* typeMap)
{ return resolve(mcs->methodCall, refMap, typeMap); }
const IR::ParameterList* getOriginalParameters() const
{ return originalMethodType->parameters; }
const IR::ParameterList* getActualParameters() const
{ return actualMethodType->parameters; }
};
/** Represents the call of an Apply method on an object that implements IApply:
a table, control or parser */
class ApplyMethod final : public MethodInstance {
ApplyMethod(const IR::MethodCallExpression* expr, const IR::IDeclaration* decl,
const IR::IApply* applyObject) :
MethodInstance(expr, decl, applyObject->getApplyMethodType(),
applyObject->getApplyMethodType()),
applyObject(applyObject)
{ CHECK_NULL(applyObject); bindParameters(); }
friend class MethodInstance;
public:
const IR::IApply* applyObject;
bool isApply() const { return true; }
bool isTableApply() const { return object->is<IR::P4Table>(); }
};
/** Represents a method call on an extern object */
class ExternMethod final : public MethodInstance {
ExternMethod(const IR::MethodCallExpression* expr, const IR::IDeclaration* decl,
const IR::Method* method,
const IR::Type_Extern* originalExternType,
const IR::Type_Method* originalMethodType,
const IR::Type_Extern* actualExternType,
const IR::Type_Method* actualMethodType) :
MethodInstance(expr, decl, originalMethodType, actualMethodType), method(method),
originalExternType(originalExternType), actualExternType(actualExternType) {
CHECK_NULL(method); CHECK_NULL(originalExternType); CHECK_NULL(actualExternType);
bindParameters();
}
friend class MethodInstance;
public:
const IR::Method* method;
const IR::Type_Extern* originalExternType; // type of object method is applied to
const IR::Type_Extern* actualExternType; // with type variables substituted
/// Set of IR::Method and IR::Function objects that may be called by this method.
// If this method is abstract, will consist of (just) the concrete implementation,
// otherwise will consist of those methods that are @synchronous with this
std::vector<const IR::IDeclaration *> mayCall() const;
};
/** Represents the call of an extern function */
class ExternFunction final : public MethodInstance {
ExternFunction(const IR::MethodCallExpression* expr,
const IR::Method* method,
const IR::Type_Method* originalMethodType,
const IR::Type_Method* actualMethodType) :
MethodInstance(expr, nullptr, originalMethodType, actualMethodType), method(method)
{ CHECK_NULL(method); bindParameters(); }
friend class MethodInstance;
public:
const IR::Method* method;
};
/** Represents the direct call of an action; This also works for
correctly for actions declared in a table 'actions' list, and for
action instantiations such as the default_action or the list of
table entries */
class ActionCall final : public MethodInstance {
ActionCall(const IR::MethodCallExpression* expr,
const IR::P4Action* action,
const IR::Type_Action* actionType) :
// Actions are never generic
MethodInstance(expr, nullptr, actionType, actionType), action(action)
{ CHECK_NULL(action); bindParameters(); }
friend class MethodInstance;
public:
const IR::P4Action* action;
};
/**
Represents the call of a function.
*/
class FunctionCall final : public MethodInstance {
FunctionCall(const IR::MethodCallExpression* expr,
const IR::Function* function,
const IR::Type_Method* originalMethodType,
const IR::Type_Method* actualMethodType) :
MethodInstance(expr, nullptr, originalMethodType, actualMethodType), function(function)
{ CHECK_NULL(function); bindParameters(); }
friend class MethodInstance;
public:
const IR::Function* function;
};
/** This class represents the call of a built-in method:
These methods are:
- header.setValid(),
- header.setInvalid(),
- header.isValid(),
- union.isValid(),
- stack.push_front(int),
- stack.pop_front(int)
*/
class BuiltInMethod final : public MethodInstance {
friend class MethodInstance;
BuiltInMethod(const IR::MethodCallExpression* expr, IR::ID name,
const IR::Expression* appliedTo, const IR::Type_Method* methodType) :
MethodInstance(expr, nullptr, methodType, methodType), name(name), appliedTo(appliedTo)
{ CHECK_NULL(appliedTo); bindParameters(); }
public:
const IR::ID name;
const IR::Expression* appliedTo; // object is an expression
};
////////////////////////////////////////////////////
/** This class is used to disambiguate constructor calls.
The core method is the static method 'resolve', which will categorize a
constructor as one of
- Extern constructor
- Container constructor (parser, control or package)
*/
class ConstructorCall : public InstanceBase {
protected:
virtual ~ConstructorCall() {}
explicit ConstructorCall(const IR::ConstructorCallExpression* cce): cce(cce)
{ CHECK_NULL(cce); }
public:
/// For each callee parameter the corresponding argument
ParameterSubstitution substitution;
const IR::ConstructorCallExpression* cce;
const IR::Vector<IR::Type>* typeArguments;
const IR::ParameterList* constructorParameters;
static ConstructorCall* resolve(const IR::ConstructorCallExpression* cce,
ReferenceMap* refMap,
TypeMap* typeMap);
};
/** Represents a constructor call that allocates an Extern object */
class ExternConstructorCall : public ConstructorCall {
explicit ExternConstructorCall(const IR::ConstructorCallExpression* cce,
const IR::Type_Extern* type,
const IR::Method* constructor) :
ConstructorCall(cce), type(type), constructor(constructor)
{ CHECK_NULL(type); CHECK_NULL(constructor); }
friend class ConstructorCall;
public:
const IR::Type_Extern* type; // actual extern declaration in program IR
const IR::Method* constructor; // that is being invoked
};
/** Represents a constructor call that allocates an object that implements IContainer.
These can be package, control or parser */
class ContainerConstructorCall : public ConstructorCall {
explicit ContainerConstructorCall(const IR::ConstructorCallExpression* cce,
const IR::IContainer* cont) :
ConstructorCall(cce), container(cont) { CHECK_NULL(cont); }
friend class ConstructorCall;
public:
const IR::IContainer* container; // actual container in program IR
};
/////////////////////////////////////////////
/// Used to resolve a Declaration_Instance
class Instantiation : public InstanceBase {
protected:
void substitute() {
substitution.populate(constructorParameters, constructorArguments);
}
public:
Instantiation(const IR::Declaration_Instance* instance,
const IR::Vector<IR::Type>* typeArguments):
instance(instance), typeArguments(typeArguments)
{ CHECK_NULL(instance); constructorArguments = instance->arguments; }
const IR::Declaration_Instance* instance;
const IR::Vector<IR::Type>* typeArguments;
const IR::Vector<IR::Argument>*constructorArguments;
const IR::ParameterList* constructorParameters;
ParameterSubstitution substitution;
static Instantiation* resolve(const IR::Declaration_Instance* instance,
ReferenceMap* refMap,
TypeMap* typeMap);
};
class ExternInstantiation : public Instantiation {
public:
ExternInstantiation(const IR::Declaration_Instance* instance,
const IR::Vector<IR::Type>* typeArguments,
const IR::Type_Extern* type) :
Instantiation(instance, typeArguments), type(type) {
auto constructor = type->lookupConstructor(constructorArguments);
BUG_CHECK(constructor, "%1%: could not find constructor", type);
constructorParameters = constructor->type->parameters;
substitute();
}
const IR::Type_Extern* type;
};
class PackageInstantiation : public Instantiation {
public:
PackageInstantiation(const IR::Declaration_Instance* instance,
const IR::Vector<IR::Type>* typeArguments,
const IR::Type_Package* package) :
Instantiation(instance, typeArguments), package(package) {
constructorParameters = package->getConstructorParameters();
substitute(); }
const IR::Type_Package* package;
};
class ParserInstantiation : public Instantiation {
public:
ParserInstantiation(const IR::Declaration_Instance* instance,
const IR::Vector<IR::Type>* typeArguments,
const IR::P4Parser* parser) :
Instantiation(instance, typeArguments), parser(parser) {
constructorParameters = parser->getConstructorParameters();
substitute(); }
const IR::P4Parser* parser;
};
class ControlInstantiation : public Instantiation {
public:
ControlInstantiation(const IR::Declaration_Instance* instance,
const IR::Vector<IR::Type>* typeArguments,
const IR::P4Control* control) :
Instantiation(instance, typeArguments), control(control) {
constructorParameters = control->getConstructorParameters();
substitute(); }
const IR::P4Control* control;
};
} // namespace P4
#endif /* _FRONTENDS_P4_METHODINSTANCE_H_ */