- no more simplify in state parameters.

- resolving constants with the backend requires a few more error checks.
This commit is contained in:
Christoph Oelckers 2017-01-22 23:53:50 +01:00
parent 2880f56080
commit 061ba48dc1
5 changed files with 119 additions and 72 deletions

View file

@ -1054,6 +1054,8 @@ void FScriptPosition::Message (int severity, const char *message, ...) const
{ {
severity = StrictErrors || strictdecorate ? MSG_ERROR : MSG_WARNING; severity = StrictErrors || strictdecorate ? MSG_ERROR : MSG_WARNING;
} }
// This is mainly for catching the error with an exception handler.
if (severity == MSG_ERROR && errorout) severity = MSG_FATAL;
if (message == NULL) if (message == NULL)
{ {

View file

@ -146,6 +146,7 @@ struct FScriptPosition
static bool StrictErrors; static bool StrictErrors;
FString FileName; FString FileName;
int ScriptLine; int ScriptLine;
bool errorout = false;
FScriptPosition() FScriptPosition()
{ {

View file

@ -5653,6 +5653,12 @@ FxExpression *FxIdentifier::Resolve(FCompileContext& ctx)
if (Identifier == NAME_Default) if (Identifier == NAME_Default)
{ {
if (ctx.Function == nullptr)
{
ScriptPosition.Message(MSG_ERROR, "Unable to access class defaults from constant declaration");
delete this;
return nullptr;
}
if (ctx.Function->Variants[0].SelfClass == nullptr) if (ctx.Function->Variants[0].SelfClass == nullptr)
{ {
ScriptPosition.Message(MSG_ERROR, "Unable to access class defaults from static function"); ScriptPosition.Message(MSG_ERROR, "Unable to access class defaults from static function");
@ -5680,6 +5686,13 @@ FxExpression *FxIdentifier::Resolve(FCompileContext& ctx)
{ {
if (sym->IsKindOf(RUNTIME_CLASS(PField))) if (sym->IsKindOf(RUNTIME_CLASS(PField)))
{ {
if (ctx.Function == nullptr)
{
ScriptPosition.Message(MSG_ERROR, "Unable to access class member %s from constant declaration", sym->SymbolName.GetChars());
delete this;
return nullptr;
}
FxExpression *self = new FxSelf(ScriptPosition); FxExpression *self = new FxSelf(ScriptPosition);
self = self->Resolve(ctx); self = self->Resolve(ctx);
newex = ResolveMember(ctx, ctx.Function->Variants[0].SelfClass, self, ctx.Function->Variants[0].SelfClass); newex = ResolveMember(ctx, ctx.Function->Variants[0].SelfClass, self, ctx.Function->Variants[0].SelfClass);
@ -5697,6 +5710,12 @@ FxExpression *FxIdentifier::Resolve(FCompileContext& ctx)
newex = FxConstant::MakeConstant(sym, ScriptPosition); newex = FxConstant::MakeConstant(sym, ScriptPosition);
goto foundit; goto foundit;
} }
else if (ctx.Function == nullptr)
{
ScriptPosition.Message(MSG_ERROR, "Unable to access class member %s from constant declaration", sym->SymbolName.GetChars());
delete this;
return nullptr;
}
// Do this check for ZScript as well, so that a clearer error message can be printed. MSG_OPTERROR will default to MSG_ERROR there. // Do this check for ZScript as well, so that a clearer error message can be printed. MSG_OPTERROR will default to MSG_ERROR there.
else if (ctx.Function->Variants[0].SelfClass != ctx.Class && sym->IsKindOf(RUNTIME_CLASS(PField))) else if (ctx.Function->Variants[0].SelfClass != ctx.Class && sym->IsKindOf(RUNTIME_CLASS(PField)))
{ {
@ -7166,6 +7185,13 @@ FxExpression *FxFunctionCall::Resolve(FCompileContext& ctx)
if (afd != nullptr) if (afd != nullptr)
{ {
if (ctx.Function == nullptr)
{
ScriptPosition.Message(MSG_ERROR, "Unable to call function %s from constant declaration", MethodName.GetChars());
delete this;
return nullptr;
}
if (!CheckFunctionCompatiblity(ScriptPosition, ctx.Function, afd)) if (!CheckFunctionCompatiblity(ScriptPosition, ctx.Function, afd))
{ {
delete this; delete this;
@ -7202,7 +7228,13 @@ FxExpression *FxFunctionCall::Resolve(FCompileContext& ctx)
if (special != 0 && min >= 0) if (special != 0 && min >= 0)
{ {
int paramcount = ArgList.Size(); int paramcount = ArgList.Size();
if (paramcount < min) if (ctx.Function == nullptr)
{
ScriptPosition.Message(MSG_ERROR, "Unable to call action special %s from constant declaration", MethodName.GetChars());
delete this;
return nullptr;
}
else if (paramcount < min)
{ {
ScriptPosition.Message(MSG_ERROR, "Not enough parameters for '%s' (expected %d, got %d)", ScriptPosition.Message(MSG_ERROR, "Not enough parameters for '%s' (expected %d, got %d)",
MethodName.GetChars(), min, paramcount); MethodName.GetChars(), min, paramcount);
@ -7467,6 +7499,12 @@ FxExpression *FxMemberFunctionCall::Resolve(FCompileContext& ctx)
staticonly = true; staticonly = true;
if (ccls->IsKindOf(RUNTIME_CLASS(PClass))) if (ccls->IsKindOf(RUNTIME_CLASS(PClass)))
{ {
if (ctx.Function == nullptr)
{
ScriptPosition.Message(MSG_ERROR, "Unable to call %s from constant declaration", MethodName.GetChars());
delete this;
return nullptr;
}
auto clstype = dyn_cast<PClass>(ctx.Function->Variants[0].SelfClass); auto clstype = dyn_cast<PClass>(ctx.Function->Variants[0].SelfClass);
if (clstype != nullptr) if (clstype != nullptr)
{ {
@ -7493,6 +7531,12 @@ FxExpression *FxMemberFunctionCall::Resolve(FCompileContext& ctx)
if (Self->ExprType == EFX_Super) if (Self->ExprType == EFX_Super)
{ {
if (ctx.Function == nullptr)
{
ScriptPosition.Message(MSG_ERROR, "Unable to call %s from constant declaration", MethodName.GetChars());
delete this;
return nullptr;
}
auto clstype = dyn_cast<PClass>(ctx.Function->Variants[0].SelfClass); auto clstype = dyn_cast<PClass>(ctx.Function->Variants[0].SelfClass);
if (clstype != nullptr) if (clstype != nullptr)
{ {
@ -7743,6 +7787,12 @@ isresolved:
if (afd->Variants[0].Flags & VARF_Method) if (afd->Variants[0].Flags & VARF_Method)
{ {
if (ctx.Function == nullptr)
{
ScriptPosition.Message(MSG_ERROR, "Unable to call %s from constant declaration", MethodName.GetChars());
delete this;
return nullptr;
}
if (Self->ExprType == EFX_Self) if (Self->ExprType == EFX_Self)
{ {
if (!CheckFunctionCompatiblity(ScriptPosition, ctx.Function, afd)) if (!CheckFunctionCompatiblity(ScriptPosition, ctx.Function, afd))

View file

@ -52,6 +52,42 @@
#include "vmbuilder.h" #include "vmbuilder.h"
#include "version.h" #include "version.h"
static int GetIntConst(FxExpression *ex, FCompileContext &ctx)
{
ex = new FxIntCast(ex, false);
ex = ex->Resolve(ctx);
return ex ? static_cast<FxConstant*>(ex)->GetValue().GetInt() : 0;
}
static double GetFloatConst(FxExpression *ex, FCompileContext &ctx)
{
ex = new FxFloatCast(ex);
ex = ex->Resolve(ctx);
return ex ? static_cast<FxConstant*>(ex)->GetValue().GetFloat() : 0;
}
static FString GetStringConst(FxExpression *ex, FCompileContext &ctx)
{
ex = new FxStringCast(ex);
ex = ex->Resolve(ctx);
return static_cast<FxConstant*>(ex)->GetValue().GetString();
}
int ZCCCompiler::IntConstFromNode(ZCC_TreeNode *node, PStruct *cls)
{
FCompileContext ctx(cls, false);
FxExpression *ex = new FxIntCast(ConvertNode(node), false);
ex = ex->Resolve(ctx);
if (ex == nullptr) return 0;
if (!ex->isConstant())
{
ex->ScriptPosition.Message(MSG_ERROR, "Expression is not constant");
return 0;
}
return static_cast<FxConstant*>(ex)->GetValue().GetInt();
}
//========================================================================== //==========================================================================
// //
// ZCCCompiler :: ProcessClass // ZCCCompiler :: ProcessClass
@ -1293,7 +1329,7 @@ bool ZCCCompiler::CompileFields(PStruct *type, TArray<ZCC_VarDeclarator *> &Fiel
if (field->Type->ArraySize != nullptr) if (field->Type->ArraySize != nullptr)
{ {
fieldtype = ResolveArraySize(fieldtype, field->Type->ArraySize, &type->Symbols); fieldtype = ResolveArraySize(fieldtype, field->Type->ArraySize, type);
} }
auto name = field->Names; auto name = field->Names;
@ -1304,7 +1340,7 @@ bool ZCCCompiler::CompileFields(PStruct *type, TArray<ZCC_VarDeclarator *> &Fiel
auto thisfieldtype = fieldtype; auto thisfieldtype = fieldtype;
if (name->ArraySize != nullptr) if (name->ArraySize != nullptr)
{ {
thisfieldtype = ResolveArraySize(thisfieldtype, name->ArraySize, &type->Symbols); thisfieldtype = ResolveArraySize(thisfieldtype, name->ArraySize, type);
} }
if (varflags & VARF_Native) if (varflags & VARF_Native)
@ -1662,7 +1698,7 @@ PType *ZCCCompiler::ResolveUserType(ZCC_BasicType *type, PSymbolTable *symt)
// //
//========================================================================== //==========================================================================
PType *ZCCCompiler::ResolveArraySize(PType *baseType, ZCC_Expression *arraysize, PSymbolTable *sym) PType *ZCCCompiler::ResolveArraySize(PType *baseType, ZCC_Expression *arraysize, PStruct *cls)
{ {
TArray<ZCC_Expression *> indices; TArray<ZCC_Expression *> indices;
@ -1674,15 +1710,21 @@ PType *ZCCCompiler::ResolveArraySize(PType *baseType, ZCC_Expression *arraysize,
node = static_cast<ZCC_Expression*>(node->SiblingNext); node = static_cast<ZCC_Expression*>(node->SiblingNext);
} while (node != arraysize); } while (node != arraysize);
FCompileContext ctx(cls, false);
for (auto node : indices) for (auto node : indices)
{ {
auto val = Simplify(node, sym, true); // There is no float->int casting here.
if (val->Operation != PEX_ConstValue || !val->Type->IsA(RUNTIME_CLASS(PInt))) FxExpression *ex = ConvertNode(node);
ex = ex->Resolve(ctx);
if (ex == nullptr) return TypeError;
if (!ex->isConstant() || !ex->ValueType->IsA(RUNTIME_CLASS(PInt)))
{ {
Error(arraysize, "Array index must be an integer constant"); Error(arraysize, "Array index must be an integer constant");
return TypeError; return TypeError;
} }
int size = static_cast<ZCC_ExprConstant *>(val)->IntVal; int size = static_cast<FxConstant*>(ex)->GetValue().GetInt();
if (size < 1) if (size < 1)
{ {
Error(arraysize, "Array size must be positive"); Error(arraysize, "Array size must be positive");
@ -1765,27 +1807,6 @@ const char *ZCCCompiler::GetString(ZCC_Expression *expr, bool silent)
} }
} }
static int GetIntConst(FxExpression *ex, FCompileContext &ctx)
{
ex = new FxIntCast(ex, false);
ex = ex->Resolve(ctx);
return ex ? static_cast<FxConstant*>(ex)->GetValue().GetInt() : 0;
}
static double GetFloatConst(FxExpression *ex, FCompileContext &ctx)
{
ex = new FxFloatCast(ex);
ex = ex->Resolve(ctx);
return ex ? static_cast<FxConstant*>(ex)->GetValue().GetFloat() : 0;
}
static FString GetStringConst(FxExpression *ex, FCompileContext &ctx)
{
ex = new FxStringCast(ex);
ex = ex->Resolve(ctx);
return static_cast<FxConstant*>(ex)->GetValue().GetString();
}
//========================================================================== //==========================================================================
// //
// Parses an actor property's parameters and calls the handler // Parses an actor property's parameters and calls the handler
@ -2812,7 +2833,7 @@ void ZCCCompiler::CompileStates()
{ {
state.sprite = GetSpriteIndex(sl->Sprite->GetChars()); state.sprite = GetSpriteIndex(sl->Sprite->GetChars());
} }
// It is important to call CheckRandom before Simplify, because Simplify will resolve the function's name to nonsense FCompileContext ctx(c->Type(), false);
if (CheckRandom(sl->Duration)) if (CheckRandom(sl->Duration))
{ {
auto func = static_cast<ZCC_ExprFuncCall *>(sl->Duration); auto func = static_cast<ZCC_ExprFuncCall *>(sl->Duration);
@ -2820,26 +2841,16 @@ void ZCCCompiler::CompileStates()
{ {
Error(sl, "Random duration requires exactly 2 parameters"); Error(sl, "Random duration requires exactly 2 parameters");
} }
auto p1 = Simplify(func->Parameters->Value, &c->Type()->Symbols, true); int v1 = IntConstFromNode(func->Parameters->Value, c->Type());
auto p2 = Simplify(static_cast<ZCC_FuncParm *>(func->Parameters->SiblingNext)->Value, &c->Type()->Symbols, true); int v2 = IntConstFromNode(static_cast<ZCC_FuncParm *>(func->Parameters->SiblingNext)->Value, c->Type());
int v1 = GetInt(p1);
int v2 = GetInt(p2);
if (v1 > v2) std::swap(v1, v2); if (v1 > v2) std::swap(v1, v2);
state.Tics = (int16_t)clamp<int>(v1, 0, INT16_MAX); state.Tics = (int16_t)clamp<int>(v1, 0, INT16_MAX);
state.TicRange = (uint16_t)clamp<int>(v2 - v1, 0, UINT16_MAX); state.TicRange = (uint16_t)clamp<int>(v2 - v1, 0, UINT16_MAX);
} }
else else
{ {
auto duration = Simplify(sl->Duration, &c->Type()->Symbols, true); state.Tics = (int16_t)IntConstFromNode(sl->Duration, c->Type());
if (duration->Operation == PEX_ConstValue) state.TicRange = 0;
{
state.Tics = (int16_t)clamp<int>(GetInt(duration), -1, INT16_MAX);
state.TicRange = 0;
}
else
{
Error(sl, "Duration is not a constant");
}
} }
if (sl->bBright) state.StateFlags |= STF_FULLBRIGHT; if (sl->bBright) state.StateFlags |= STF_FULLBRIGHT;
if (sl->bFast) state.StateFlags |= STF_FAST; if (sl->bFast) state.StateFlags |= STF_FAST;
@ -2855,18 +2866,8 @@ void ZCCCompiler::CompileStates()
} }
if (sl->Offset != nullptr) if (sl->Offset != nullptr)
{ {
auto o1 = static_cast<ZCC_Expression *>(Simplify(sl->Offset, &c->Type()->Symbols, true)); state.Misc1 = IntConstFromNode(sl->Offset, c->Type());
auto o2 = static_cast<ZCC_Expression *>(Simplify(static_cast<ZCC_Expression *>(o1->SiblingNext), &c->Type()->Symbols, true)); state.Misc2 = IntConstFromNode(static_cast<ZCC_Expression *>(sl->Offset->SiblingNext), c->Type());
if (o1->Operation != PEX_ConstValue || o2->Operation != PEX_ConstValue)
{
Error(o1, "State offsets must be constant");
}
else
{
state.Misc1 = GetInt(o1);
state.Misc2 = GetInt(o2);
}
} }
#ifdef DYNLIGHT #ifdef DYNLIGHT
if (sl->Lights != nullptr) if (sl->Lights != nullptr)
@ -2915,23 +2916,15 @@ void ZCCCompiler::CompileStates()
statename.Truncate((long)statename.Len() - 1); // remove the last '.' in the label name statename.Truncate((long)statename.Len() - 1); // remove the last '.' in the label name
if (sg->Offset != nullptr) if (sg->Offset != nullptr)
{ {
auto ofs = Simplify(sg->Offset, &c->Type()->Symbols, true); int offset = IntConstFromNode(sg->Offset, c->Type());
if (ofs->Operation != PEX_ConstValue) if (offset < 0)
{ {
Error(sg, "Constant offset expected for GOTO"); Error(sg, "GOTO offset must be positive");
offset = 0;
} }
else if (offset > 0)
{ {
int offset = GetInt(ofs); statename.AppendFormat("+%d", offset);
if (offset < 0)
{
Error(sg, "GOTO offset must be positive");
offset = 0;
}
if (offset > 0)
{
statename.AppendFormat("+%d", offset);
}
} }
} }
if (!statedef.SetGotoLabel(statename)) if (!statedef.SetGotoLabel(statename))
@ -3332,7 +3325,7 @@ FxExpression *ZCCCompiler::ConvertNode(ZCC_TreeNode *ast)
if (loc->Type->ArraySize != nullptr) if (loc->Type->ArraySize != nullptr)
{ {
ztype = ResolveArraySize(ztype, loc->Type->ArraySize, &ConvertClass->Symbols); ztype = ResolveArraySize(ztype, loc->Type->ArraySize, ConvertClass);
} }
do do
@ -3341,7 +3334,7 @@ FxExpression *ZCCCompiler::ConvertNode(ZCC_TreeNode *ast)
if (node->ArraySize != nullptr) if (node->ArraySize != nullptr)
{ {
type = ResolveArraySize(ztype, node->ArraySize, &ConvertClass->Symbols); type = ResolveArraySize(ztype, node->ArraySize, ConvertClass);
} }
else else
{ {

View file

@ -88,6 +88,7 @@ public:
int Compile(); int Compile();
private: private:
int ZCCCompiler::IntConstFromNode(ZCC_TreeNode *node, PStruct *cls);
void ProcessClass(ZCC_Class *node, PSymbolTreeNode *tnode); void ProcessClass(ZCC_Class *node, PSymbolTreeNode *tnode);
void ProcessStruct(ZCC_Struct *node, PSymbolTreeNode *tnode, ZCC_Class *outer); void ProcessStruct(ZCC_Struct *node, PSymbolTreeNode *tnode, ZCC_Class *outer);
void CreateStructTypes(); void CreateStructTypes();
@ -103,7 +104,7 @@ private:
bool CompileProperties(PClass *type, TArray<ZCC_Property *> &Properties, FName prefix); bool CompileProperties(PClass *type, TArray<ZCC_Property *> &Properties, FName prefix);
FString FlagsToString(uint32_t flags); FString FlagsToString(uint32_t flags);
PType *DetermineType(PType *outertype, ZCC_TreeNode *field, FName name, ZCC_Type *ztype, bool allowarraytypes, bool formember); PType *DetermineType(PType *outertype, ZCC_TreeNode *field, FName name, ZCC_Type *ztype, bool allowarraytypes, bool formember);
PType *ResolveArraySize(PType *baseType, ZCC_Expression *arraysize, PSymbolTable *sym); PType *ResolveArraySize(PType *baseType, ZCC_Expression *arraysize, PStruct *cls);
PType *ResolveUserType(ZCC_BasicType *type, PSymbolTable *sym); PType *ResolveUserType(ZCC_BasicType *type, PSymbolTable *sym);
void InitDefaults(); void InitDefaults();