MCPcopy Create free account
hub / github.com/Whiley/WhileyCompiler / Lambda

Class Lambda

src/main/java/wyil/lang/WyilFile.java:1185–1251  ·  view source on GitHub ↗

Represents a lambda declaration within a Whiley source file. Sometimes also known as closures, these are anonymous function or method declarations declared within an expression. The following illustrates: type func is function(int)->int function g() -> func: return &(int x -> x +

Source from the content-addressed store, hash-verified

1183 *
1184 */
1185 public static class Lambda extends Callable implements Expr {
1186
1187 public Lambda(Tuple<Modifier> modifiers, Identifier name, Tuple<Decl.Variable> parameters, Expr body,
1188 WyilFile.Type.Callable signature) {
1189 this(modifiers, name, new Tuple<>(), parameters, new Tuple<Decl.Variable>(), body, signature);
1190 }
1191
1192
1193 public Lambda(Tuple<Modifier> modifiers, Identifier name, Tuple<Template.Variable> template,
1194 Tuple<Decl.Variable> parameters, Tuple<Decl.Variable> returns, Expr body, WyilFile.Type.Callable signature) {
1195 super(DECL_lambda, modifiers, name, template, parameters, returns, body, signature);
1196 }
1197
1198 public Set<Decl.Variable> getCapturedVariables() {
1199 UsedVariableExtractor usedVariableExtractor = new UsedVariableExtractor();
1200 HashSet<Decl.Variable> captured = new HashSet<>();
1201 usedVariableExtractor.visitExpression(getBody(), captured);
1202 Tuple<Decl.Variable> parameters = getParameters();
1203 for (int i = 0; i != parameters.size(); ++i) {
1204 captured.remove(parameters.get(i));
1205 }
1206 return captured;
1207 }
1208
1209 @Override
1210 public Expr getBody() {
1211 return (Expr) get(5);
1212 }
1213
1214 @Override
1215 public WyilFile.Type.Callable getType() {
1216 return (WyilFile.Type.Callable) super.get(6);
1217 }
1218
1219 @Override
1220 public void setType(WyilFile.Type type) {
1221 // FIXME: this is a hack which should be removed.
1222 type = type.as(WyilFile.Type.Callable.class);
1223 Syntactic.Heap heap = getHeap();
1224 //
1225 if (type instanceof WyilFile.Type.Callable) {
1226 // Set the type
1227 operands[6] = heap.allocate(type);
1228 } else {
1229 throw new IllegalArgumentException();
1230 }
1231 }
1232
1233 @SuppressWarnings("unchecked")
1234 @Override
1235 public Syntactic.Item clone(Syntactic.Item[] operands) {
1236 return new Lambda((Tuple<Modifier>) operands[0], (Identifier) operands[1],
1237 (Tuple<Template.Variable>) operands[2], (Tuple<Decl.Variable>) operands[3],
1238 (Tuple<Decl.Variable>) operands[4], (Expr) operands[5], (WyilFile.Type.Callable) operands[6]);
1239 }
1240
1241 public static final Descriptor DESCRIPTOR_0 = new Descriptor(Operands.SEVEN, Data.ZERO, "DECL_lambda") {
1242 @SuppressWarnings("unchecked")

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected