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
|
#include "stdafx.h"
#include "FnPtr.h"
#include "Compiler/Engine.h"
#include "Compiler/Exception.h"
#include "Compiler/Lib/Fn.h"
#include "Compiler/Lib/Maybe.h"
#include "Core/Join.h"
namespace storm {
namespace bs {
static void checkDot(Expr *dotExpr) {
Value dotResult = dotExpr->result().type();
if (dotResult.isValue())
throw new (dotExpr) SyntaxError(dotExpr->pos,
S("Only classes and actors can be bound to a function pointer. Not values."));
}
FnPtr::FnPtr(Block *block, SrcName *name, Array<SrcName *> *formal) : Expr(name->pos) {
findTarget(block->scope, name, formal, null);
}
FnPtr::FnPtr(Block *block, Expr *dot, syntax::SStr *name, Array<SrcName *>*formal)
: Expr(name->pos), dotExpr(dot) {
checkDot(dotExpr);
SrcName *tn = new (this) SrcName(name->pos);
tn->add(new (this) SimplePart(name->v));
findTarget(block->scope, tn, formal, dot);
}
FnPtr::FnPtr(Block *block, SrcName *name) : Expr(name->pos), parent(block), name(name->simplify(block->scope)) {
if (!this->name) {
Str *msg = TO_S(engine(), S("The parameters of the name ") << name <<
S(" can not be resolved to proper types."));
throw new (this) SyntaxError(name->pos, msg);
}
}
FnPtr::FnPtr(Block *block, Expr *dot, syntax::SStr *name) : Expr(name->pos), dotExpr(dot), parent(block) {
checkDot(dotExpr);
SimpleName *tn = new (this) SimpleName();
tn->add(new (this) SimplePart(name->v));
this->name = tn;
}
FnPtr::FnPtr(Function *target, SrcPos pos) : Expr(pos), target(target) {
Array<Value> *formals = clone(target->params);
formals->insert(0, target->result);
ptrType = thisPtr(fnType(formals));
}
FnPtr::FnPtr(Expr *dot, Function *target, SrcPos pos) : Expr(pos), dotExpr(dot), target(target) {
checkDot(dotExpr);
if (target->params->empty() || !target->params->at(0).mayReferTo(dot->result().type()))
throw new (this) SyntaxError(dotExpr->pos, S("The first parameter of the specified function does not match the type of the provided expression."));
Array<Value> *formals = clone(target->params);
formals->at(0) = target->result;
ptrType = thisPtr(fnType(formals));
}
// Note: We don't support implicit this pointer.
void FnPtr::findTarget(const Scope &scope, SrcName *name, Array<SrcName *> *formal, MAYBE(Expr *) dot) {
SimpleName *resolved = name->simplify(scope);
if (!resolved) {
Str *msg = TO_S(engine(), S("The parameters of the name ") << name
<< S(" can not be resolved to proper types."));
throw new (this) SyntaxError(name->pos, msg);
}
Array<Value> *params = new (this) Array<Value>();
Array<Value> *formals = new (this) Array<Value>();
if (dot)
params->push(dot->result().type());
for (Nat i = 0; i < formal->count(); i++) {
Value v = scope.value(formal->at(i));
params->push(v);
formals->push(v);
}
SimplePart *last = new (this) SimplePart(resolved->last()->name, params);
SimplePart *oldLast = resolved->last();
resolved->last() = last;
// Try to find a function:
Named *found = scope.find(resolved);
target = as<Function>(found);
if (target) {
formals->insert(0, target->result);
ptrType = thisPtr(fnType(formals));
return;
}
// Try to find a type...
resolved->last() = oldLast;
Type *foundType = as<Type>(scope.find(resolved));
// ...with a constructor.
if (foundType && !dot) {
params->insert(0, Value(foundType, true));
target = as<Function>(foundType->find(Type::CTOR, params, scope));
if (target) {
formals->insert(0, Value(foundType));
ptrType = thisPtr(fnType(formals));
return;
}
}
// All failed. Produce a sensible error message:
if (!found && !foundType) {
StrBuf *msg = new (this) StrBuf();
*msg << S("Could not find a function ");
resolved->last() = last;
*msg << resolved << S(" nor a type ");
resolved->last() = oldLast;
*msg << resolved << S(".");
throw new (this) SyntaxError(name->pos, msg->toS());
}
if (foundType) {
Str *msg = TO_S(engine(), S("Failed to find a suitable constructor for ") << foundType << S("."));
throw new (this) SyntaxError(name->pos, msg);
}
if (found) {
throw new (this) SyntaxError(name->pos, S("Can not take the pointer of anything other than functions or constructors."));
}
throw new (this) SyntaxError(name->pos, S("Unknown error."));
}
Function *FnPtr::acceptableFn(Value &t) {
FnType *ptrType = as<FnType>(t.type);
// Check if wrapped in a maybe type?
if (!ptrType) {
if (MaybeType *maybeType = as<MaybeType>(t.type)) {
t = maybeType->param();
ptrType = as<FnType>(t.type);
}
}
if (!ptrType)
return null;
if (ptrType->params->empty())
return null;
Array<Value> *params = clone(ptrType->params);
Value result = params->at(0);
if (dotExpr)
params->at(0) = dotExpr->result().type();
else
params->remove(0);
// Regular member function:
SimplePart *last = new (this) SimplePart(this->name->last()->name, params);
SimpleName *name = clone(this->name);
name->last() = last;
if (Function *found = as<Function>(parent->scope.find(name))) {
if (!result.mayReferTo(found->result))
return null;
return found;
}
// Constructor of a type:
if (!dotExpr) {
if (Type *foundType = as<Type>(parent->scope.find(this->name))) {
if (!result.mayReferTo(foundType))
return null;
params->insert(0, Value(foundType, true));
return as<Function>(foundType->find(Type::CTOR, params, parent->scope));
}
}
return null;
}
ExprResult FnPtr::result() {
return ptrType;
}
Int FnPtr::castPenalty(Value to) {
if (ptrType != Value()) {
// If types were specified, no automatic cast.
return to.asRef(false) == ptrType ? 0 : -1;
} else if (acceptableFn(to)) {
return 10;
} else {
return -1;
}
}
void FnPtr::code(CodeGen *to, CodeResult *r) {
Value type = result().type();
if (!type.type) {
// Types were deduced automatically, we need to check some more...
type = r->type();
if (!type.type) {
throw new (this) SyntaxError(pos, S("Unable to deduce parameter types for a function pointer ")
S("in this context. Please specify parameter types explicitly."));
}
Function *target = acceptableFn(type);
if (!target) {
Str *msg = TO_S(engine(), S("Failed to find a suitable function for the function pointer type ")
<< type << S(". Please specify explicit parameters."));
throw new (this) SyntaxError(pos, msg);
}
code(to, r, type, target);
} else {
assert(target);
code(to, r, type, target);
}
}
void FnPtr::code(CodeGen *to, CodeResult *r, Value type, Function *target) {
using namespace code;
// Note: initialized to zero if not needed.
VarInfo thisPtr = to->createVar(type);
if (dotExpr) {
CodeResult *result = new (this) CodeResult(type, thisPtr);
dotExpr->code(to, result);
}
if (!r->needed())
return;
code::Var z = r->location(to);
if (!dotExpr) {
// Create the object once and store it.
FnBase *obj = pointer(target);
*to->l << mov(z, objPtr(obj));
} else {
// We need to create a new object each time since the 'dotExpr' might change.
RunOn runOn = target->runOn();
bool memberFn = target->isMember();
code::TypeDesc *ptr = engine().ptrDesc();
*to->l << lea(ptrA, target->ref());
*to->l << fnParam(ptr, type.type->typeRef());
*to->l << fnParam(ptr, ptrA);
if (runOn.state == RunOn::named)
*to->l << fnParam(ptr, runOn.thread->ref());
else
*to->l << fnParam(ptr, ptrConst(Offset()));
*to->l << fnParam(ptr, thisPtr.v);
*to->l << fnParam(byteDesc(engine()), byteConst(memberFn ? 1 : 0));
*to->l << fnCall(engine().ref(builtin::fnCreate), false, ptr, ptrA);
*to->l << mov(z, ptrA);
}
r->created(to);
}
void FnPtr::toS(StrBuf *to) const {
*to << S("&");
if (target && ptrType.type) {
if (dotExpr) {
*to << dotExpr << S(".") << target->name;
} else {
SimpleName *p = target->path();
p->last()->params = new (this) Array<Value>();
*to << p;
}
*to << S("(");
for (Nat i = 1; i < ptrType.type->params->count(); i++) {
if (i != 1)
*to << S(", ");
*to << ptrType.type->params->at(i);
}
*to << S(")");
} else if (name) {
*to << name;
} else {
*to << S("<invalid>");
}
}
}
}
|