Skip to content

Commit

Permalink
refactor(zscript): better 'read-only' compile warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilyV99 committed Apr 7, 2024
1 parent ef8ce36 commit a58cf4f
Show file tree
Hide file tree
Showing 28 changed files with 103 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/parser/BuildVisitors.cpp
Expand Up @@ -1192,7 +1192,7 @@ void BuildOpcodes::caseExprArrow(ASTExprArrow& host, void* param)
Function* readfunc = isarray ? host.arrayFunction : host.readFunction;
assert(readfunc->isInternal());

if(readfunc->getFlag(FUNCFLAG_NIL))
if(readfunc->isNil())
{
bool skipptr = readfunc->internal_flags & IFUNCFLAG_SKIPPOINTER;
if (!skipptr)
Expand Down Expand Up @@ -3376,7 +3376,7 @@ void LValBOHelper::caseExprArrow(ASTExprArrow &host, void *param)
int32_t isIndexed = (host.index != NULL);
assert(host.writeFunction->isInternal());

if(host.writeFunction->getFlag(FUNCFLAG_NIL))
if(host.writeFunction->isNil())
{
bool skipptr = host.writeFunction->internal_flags & IFUNCFLAG_SKIPPOINTER;
bool needs_pushpop = isIndexed || !skipptr;
Expand Down
6 changes: 5 additions & 1 deletion src/parser/CompileError.xtable
Expand Up @@ -126,6 +126,10 @@ X( BadAutoType, S, A, E, VOID, VOID, "'auto' must have an initializer with val
X( GroupAuto, S, A, E, VOID, VOID, "'auto' cannot apply to a group declaration." )
X( BadReturnType, S, A, E, STR, VOID, "Cannot return type '%s'." )
X( BadTempVal, T, A, E, VOID, VOID, "Temporary literals cannot be stored!" )

X( MissingReturnWarn, S, A, W, STR, VOID, "Function '%s' is not void, and should return a value!" )
X( MissingReturnError, S, A, E, STR, VOID, "Function '%s' is not void, and must return a value!" )
X( NegSqrt, T, A, E, VOID, VOID, "Attempting to take the square root of a negative number!" )
X( DeprecatedDelete, G, A, W, VOID, VOID, "The 'delete' operator no longer does anything. Objects are freed automatically when they become unreachable" )
X( ReadOnly, C, A, W, STR, VOID, "'%s' is read-only, and cannot be written to!" )

#undef STR
14 changes: 7 additions & 7 deletions src/parser/LibrarySymbols.cpp
Expand Up @@ -187,18 +187,18 @@ void LibrarySymbols::addSymbolsToScope(Scope& scope)
{
AccessorTable& entry = table[i];

DataType const* returnType = typeStore.getType(entry.rettype);
vector<DataType const*> paramTypes;
for (auto& ptype : entry.params)
paramTypes.push_back(typeStore.getType(ptype));

std::string const& name = entry.name;
std::string varName = name;

// Strip out the array at the end.
bool isArray = name.substr(name.size() - 2) == "[]";
if (isArray)
varName = name.substr(0, name.size() - 2);

DataType const* returnType = typeStore.getType(entry.rettype);
vector<DataType const*> paramTypes;
for (auto& ptype : entry.params)
paramTypes.push_back(typeStore.getType(ptype));

// Create function object.
auto setorget = FUNCTION;
Expand Down Expand Up @@ -252,7 +252,7 @@ void LibrarySymbols::addSymbolsToScope(Scope& scope)
}
// Generate function code for getters/setters
int32_t label = function->getLabel();
if(function->getFlag(FUNCFLAG_NIL))
if(function->isNil())
{
handleNil(refVar, function);
}
Expand Down
13 changes: 13 additions & 0 deletions src/parser/Scope.cpp
Expand Up @@ -1070,6 +1070,19 @@ Function* BasicScope::addSetter(
return fun;
}

void BasicScope::addGetter(Function* func)
{
string const& name = func->name;
if(find<Function*>(getters_, name)) return;
getters_[name] = func;
}
void BasicScope::addSetter(Function* func)
{
string const& name = func->name;
if(find<Function*>(setters_, name)) return;
setters_[name] = func;
}

Function* BasicScope::addFunction(
DataType const* returnType, string const& name,
vector<DataType const*> const& paramTypes, vector<string const*> const& paramNames, int32_t flags, ASTFuncDecl* node, CompileErrorHandler* handler)
Expand Down
4 changes: 4 additions & 0 deletions src/parser/Scope.h
Expand Up @@ -136,6 +136,8 @@ namespace ZScript
std::vector<DataType const*> const& paramTypes, std::vector<std::string const*> const& paramNames,
int32_t flags = 0, AST* node = NULL)
= 0;
virtual void addGetter(Function* func) = 0;
virtual void addSetter(Function* func) = 0;
virtual Function* addFunction(
DataType const* returnType, std::string const& name,
std::vector<DataType const*> const& paramTypes, std::vector<std::string const*> const& paramNames,
Expand Down Expand Up @@ -365,6 +367,8 @@ namespace ZScript
DataType const* returnType, std::string const& name,
std::vector<DataType const*> const& paramTypes, std::vector<std::string const*> const& paramNames,
int32_t flags = 0, AST* node = NULL);
virtual void addGetter(Function* func);
virtual void addSetter(Function* func);
virtual Function* addFunction(
DataType const* returnType, std::string const& name,
std::vector<DataType const*> const& paramTypes, std::vector<std::string const*> const& paramNames,
Expand Down
5 changes: 5 additions & 0 deletions src/parser/SemanticAnalyzer.cpp
Expand Up @@ -1367,6 +1367,9 @@ void SemanticAnalyzer::caseExprArrow(ASTExprArrow& host, void* param)
leftType->getName().c_str()));
return;
}
if(host.writeFunction->getFlag(FUNCFLAG_READ_ONLY))
handleError(CompileError::ReadOnly(&host, fmt::format("{}->{}{}",
leftType->getName().c_str(), host.right, (host.index ? "[]" : ""))));
}

if(host.arrayFunction)
Expand Down Expand Up @@ -1714,6 +1717,8 @@ void SemanticAnalyzer::caseExprCall(ASTExprCall& host, void* param)

host.binding = bestFunctions.front();
deprecWarn(host.binding, &host, "Function", host.binding->getSignature().asString());
if(host.binding->getFlag(FUNCFLAG_READ_ONLY))
handleError(CompileError::ReadOnly(&host, host.binding->getSignature().asString()));
}

void SemanticAnalyzer::caseExprNegate(ASTExprNegate& host, void*)
Expand Down
3 changes: 2 additions & 1 deletion src/parser/ZScript.h
Expand Up @@ -471,7 +471,8 @@ namespace ZScript
}
bool getFlag(int32_t flag) const {return (flags & flag) != 0;}

bool isInternal() const {return !node;};
bool isInternal() const {return !node;}
bool isNil() const {return prototype || getFlag(FUNCFLAG_NIL|FUNCFLAG_READ_ONLY);}

// If this is a tracing function (disabled by `#option LOGGING false`)
bool isTracing() const;
Expand Down
25 changes: 14 additions & 11 deletions src/parser/parserDefs.h
Expand Up @@ -9,17 +9,20 @@
using namespace util;

//FUNCFLAG values, for `Function` and `ASTFuncDecl` objects.
#define FUNCFLAG_INLINE 0x00000001
#define FUNCFLAG_INVALID 0x00000002
#define FUNCFLAG_STATIC 0x00000004
#define FUNCFLAG_CONSTRUCTOR 0x00000008
#define FUNCFLAG_DESTRUCTOR 0x00000010
#define FUNCFLAG_CLASSFUNC 0x00000020
#define FUNCFLAG_VARARGS 0x00000040
#define FUNCFLAG_DEPRECATED 0x00000080
#define FUNCFLAG_NOCAST 0x00000100
#define FUNCFLAG_INTARRAY 0x00000200
#define FUNCFLAG_NIL 0x00000400
#define FUNCFLAG_INLINE 0x00000001 //Can be heavily optimized
#define FUNCFLAG_INVALID 0x00000002 //Needs to throw an error, later in compile
#define FUNCFLAG_STATIC 0x00000004 //Is static (not per-object in classes)
#define FUNCFLAG_CONSTRUCTOR 0x00000008 //Is a user class constructor
#define FUNCFLAG_DESTRUCTOR 0x00000010 //Is a user class destructor
#define FUNCFLAG_CLASSFUNC 0x00000020 //Belongs to a user class
#define FUNCFLAG_VARARGS 0x00000040 //Uses variadic arguments
#define FUNCFLAG_DEPRECATED 0x00000080 //Gives a deprecated warning/error on use
#define FUNCFLAG_NOCAST 0x00000100 //Affects function's overloading
#define FUNCFLAG_INTARRAY 0x00000200 //Function represents a special internal array
#define FUNCFLAG_NIL 0x00000400 //Function does 'nothing' (optimizable)
#define FUNCFLAG_EXITS 0x00000800 //Function exits the current script (or game)
#define FUNCFLAG_NEVER_RETURN 0x00001000 //Function never returns
#define FUNCFLAG_READ_ONLY 0x00002000 //Function is read-only

#define IFUNCFLAG_SKIPPOINTER 0x01
#define IFUNCFLAG_REASSIGNPTR 0x02
Expand Down
4 changes: 2 additions & 2 deletions src/parser/symbols/BitmapSymbols.cpp
Expand Up @@ -6,9 +6,9 @@ static AccessorTable BitmapTable[] =
{
// name, tag, rettype, var, funcFlags, params,optparams
{ "getWidth", 0, ZTID_FLOAT, BITMAPWIDTH, 0, { ZTID_BITMAP },{} },
{ "setWidth", 0, ZTID_VOID, BITMAPWIDTH, 0, { ZTID_BITMAP, ZTID_FLOAT },{} },
{ "setWidth", 0, ZTID_VOID, BITMAPWIDTH, FL_RDONLY, { ZTID_BITMAP, ZTID_FLOAT },{} },
{ "getHeight", 0, ZTID_FLOAT, BITMAPHEIGHT, 0, { ZTID_BITMAP },{} },
{ "setHeight", 0, ZTID_VOID, BITMAPHEIGHT, 0, { ZTID_BITMAP, ZTID_FLOAT },{} },
{ "setHeight", 0, ZTID_VOID, BITMAPHEIGHT,FL_RDONLY, { ZTID_BITMAP, ZTID_FLOAT },{} },

{ "GetPixel", 0, ZTID_FLOAT, -1, 0, { ZTID_BITMAP, ZTID_FLOAT, ZTID_FLOAT },{} },
{ "CountColor", 0, ZTID_FLOAT, -1, 0, { ZTID_BITMAP, ZTID_BITMAP, ZTID_FLOAT, ZTID_FLOAT, ZTID_FLOAT, ZTID_FLOAT },{ -10000 } },
Expand Down
9 changes: 7 additions & 2 deletions src/parser/symbols/CombosPtrSymbols.cpp
Expand Up @@ -7,13 +7,18 @@ static AccessorTable CombosTable[] =
//name, tag, rettype, var, funcFlags, params,optparams
//Only as 'this->' in combo scripts
{ "getX", 0, ZTID_FLOAT, COMBOXR, 0, { ZTID_COMBOS },{} },
{ "setX", 0, ZTID_VOID, COMBOXR, FL_RDONLY, { ZTID_COMBOS, ZTID_FLOAT },{} },
{ "getY", 0, ZTID_FLOAT, COMBOYR, 0, { ZTID_COMBOS },{} },
{ "setY", 0, ZTID_VOID, COMBOYR, FL_RDONLY, { ZTID_COMBOS, ZTID_FLOAT },{} },
{ "getPos", 0, ZTID_FLOAT, COMBOPOSR, 0, { ZTID_COMBOS },{} },
{ "setPos", 0, ZTID_VOID, COMBOPOSR, FL_RDONLY, { ZTID_COMBOS, ZTID_FLOAT },{} },
{ "getLayer", 0, ZTID_FLOAT, COMBOLAYERR, 0, { ZTID_COMBOS },{} },
{ "setLayer", 0, ZTID_VOID, COMBOLAYERR, FL_RDONLY, { ZTID_COMBOS, ZTID_FLOAT },{} },
//
{ "getInitD[]", 0, ZTID_UNTYPED, COMBODATAINITD, 0, { ZTID_COMBOS, ZTID_FLOAT },{} },
{ "setInitD[]", 0, ZTID_VOID, COMBODATAINITD, 0, { ZTID_COMBOS, ZTID_FLOAT, ZTID_UNTYPED },{} },
{ "getID", 0, ZTID_FLOAT, COMBODATAID, 0, { ZTID_COMBOS },{} },
{ "setID", 0, ZTID_VOID, COMBODATAID, FL_RDONLY, { ZTID_COMBOS, ZTID_FLOAT },{} },
{ "getOriginalTile", 0, ZTID_FLOAT, COMBODOTILE, 0, { ZTID_COMBOS },{} },
{ "setOriginalTile", 0, ZTID_VOID, COMBODOTILE, 0, { ZTID_COMBOS, ZTID_FLOAT },{} },
{ "getFrame", 0, ZTID_FLOAT, COMBODFRAME, 0, { ZTID_COMBOS },{} },
Expand All @@ -34,8 +39,8 @@ static AccessorTable CombosTable[] =
{ "setEffect", 0, ZTID_VOID, COMBODEFFECT, 0, { ZTID_COMBOS, ZTID_FLOAT },{} },
{ "getType", 0, ZTID_FLOAT, COMBODTYPE, 0, { ZTID_COMBOS },{} },
{ "setType", 0, ZTID_VOID, COMBODTYPE, 0, { ZTID_COMBOS, ZTID_FLOAT },{} },
{ "getCSet", 0, ZTID_FLOAT, COMBODCSET, 0, { ZTID_COMBOS },{},0,"Use ->CSet2 instead!" },
{ "setCSet", 0, ZTID_VOID, COMBODCSET, 0, { ZTID_COMBOS, ZTID_FLOAT },{},0,"Use ->CSet2 instead!" },
{ "getCSet", 0, ZTID_FLOAT, COMBODCSET, FL_DEPR, { ZTID_COMBOS },{},0,"Use ->CSet2 instead!" },
{ "setCSet", 0, ZTID_VOID, COMBODCSET, FL_DEPR, { ZTID_COMBOS, ZTID_FLOAT },{},0,"Use ->CSet2 instead!" },
{ "getCSet2", 0, ZTID_FLOAT, COMBODCSET, 0, { ZTID_COMBOS },{} },
{ "setCSet2", 0, ZTID_VOID, COMBODCSET, 0, { ZTID_COMBOS, ZTID_FLOAT },{} },
{ "getCSet2Flags", 0, ZTID_FLOAT, COMBODCSET2FLAGS, 0, { ZTID_COMBOS },{} },
Expand Down
1 change: 1 addition & 0 deletions src/parser/symbols/DMapDataSymbols.cpp
Expand Up @@ -15,6 +15,7 @@ static AccessorTable DMapDataTable[] =
{ "SetMusic", 0, ZTID_VOID, -1, 0, { ZTID_DMAPDATA, ZTID_CHAR },{} },
//
{ "getID", 0, ZTID_FLOAT, DMAPDATAID, 0, { ZTID_DMAPDATA },{} },
{ "setID", 0, ZTID_VOID, DMAPDATAID, FL_RDONLY, { ZTID_DMAPDATA, ZTID_FLOAT },{} },
{ "getMap", 0, ZTID_FLOAT, DMAPDATAMAP, 0, { ZTID_DMAPDATA },{} },
{ "setMap", 0, ZTID_VOID, DMAPDATAMAP, 0, { ZTID_DMAPDATA, ZTID_FLOAT },{} },
{ "getLevel", 0, ZTID_FLOAT, DMAPDATALEVEL, 0, { ZTID_DMAPDATA },{} },
Expand Down
4 changes: 4 additions & 0 deletions src/parser/symbols/DebugSymbols.cpp
Expand Up @@ -43,6 +43,7 @@ static AccessorTable DebugTable[] =
{ "SetItemdataPointer", 0, ZTID_ITEMCLASS, -1, 0, { ZTID_DEBUG, ZTID_FLOAT },{} },

{ "getPlayfieldOffset", 0, ZTID_FLOAT, GAMEPLAYFIELDOFS, 0, { ZTID_DEBUG },{} },
{ "setPlayfieldOffset", 0, ZTID_VOID, GAMEPLAYFIELDOFS, FL_RDONLY, { ZTID_DEBUG, ZTID_FLOAT },{} },
{ "TriggerSecret", 0, ZTID_VOID, -1, 0, { ZTID_DEBUG, ZTID_FLOAT },{} },
{ "ChangeFFCScript", 0, ZTID_VOID, -1, 0, { ZTID_DEBUG, ZTID_FLOAT },{} },

Expand All @@ -51,9 +52,12 @@ static AccessorTable DebugTable[] =
{ "_getNULL", 0, ZTID_UNTYPED, DONULL, 0, { ZTID_DEBUG },{} },
{ "_getNull", 0, ZTID_UNTYPED, DONULL, 0, { ZTID_DEBUG },{} },
{ "getNULL", 0, ZTID_UNTYPED, DONULL, 0, { ZTID_DEBUG },{} },
{ "setNULL", 0, ZTID_VOID, DONULL, FL_RDONLY, { ZTID_DEBUG, ZTID_UNTYPED },{} },
{ "getNull", 0, ZTID_UNTYPED, DONULL, 0, { ZTID_DEBUG },{} },
{ "setNull", 0, ZTID_VOID, DONULL, FL_RDONLY, { ZTID_DEBUG, ZTID_UNTYPED },{} },
{ "Breakpoint", 0, ZTID_VOID, -1, FL_INL, { ZTID_DEBUG, ZTID_CHAR },{ 0 } },
{ "getTesting", 0, ZTID_BOOL, DEBUGTESTING, 0, { ZTID_DEBUG },{} },
{ "setTesting", 0, ZTID_VOID, DEBUGTESTING, FL_RDONLY, { ZTID_DEBUG, ZTID_BOOL },{} },

{ "", 0, ZTID_VOID, -1, 0, {},{} }
};
Expand Down
1 change: 1 addition & 0 deletions src/parser/symbols/DirectorySymbols.cpp
Expand Up @@ -6,6 +6,7 @@ static AccessorTable DirectoryTable[] =
{
//name, tag, rettype, var, funcFlags, params,optparams
{ "getSize", 0, ZTID_FLOAT, DIRECTORYSIZE, 0, { ZTID_DIRECTORY },{} },
{ "setSize", 0, ZTID_VOID, DIRECTORYSIZE, FL_RDONLY, { ZTID_DIRECTORY, ZTID_FLOAT },{} },
{ "GetFilename", 0, ZTID_BOOL, -1, 0, { ZTID_DIRECTORY, ZTID_FLOAT, ZTID_CHAR },{} },
{ "Reload", 0, ZTID_VOID, -1, 0, { ZTID_DIRECTORY },{} },
{ "Free", 0, ZTID_VOID, -1, 0, { ZTID_DIRECTORY },{} },
Expand Down
1 change: 1 addition & 0 deletions src/parser/symbols/EnemyWeaponSymbols.cpp
Expand Up @@ -179,6 +179,7 @@ static AccessorTable ewpnTable[] =

//Intentionally undocumented
{ "getUID", 0, ZTID_FLOAT, EWEAPONSCRIPTUID, FL_DEPR, { ZTID_EWPN },{} },
{ "setUID", 0, ZTID_VOID, EWEAPONSCRIPTUID, FL_DEPR|FL_RDONLY, { ZTID_EWPN, ZTID_FLOAT },{} },
{ "getParentUID", 0, ZTID_FLOAT, EWPNPARENTUID, FL_DEPR, { ZTID_EWPN },{} },
{ "setParentUID", 0, ZTID_VOID, EWPNPARENTUID, FL_DEPR, { ZTID_EWPN, ZTID_FLOAT },{} },

Expand Down
3 changes: 3 additions & 0 deletions src/parser/symbols/FileSymbols.cpp
Expand Up @@ -28,8 +28,11 @@ static AccessorTable FileTable[] =
{ "Rewind", 0, ZTID_VOID, -1, FL_INL, { ZTID_FILE },{} },
{ "ClearError", 0, ZTID_VOID, -1, FL_INL, { ZTID_FILE },{} },
{ "getPos", 0, ZTID_LONG, FILEPOS, 0, { ZTID_FILE },{} },
{ "setPos", 0, ZTID_VOID, FILEPOS, FL_RDONLY, { ZTID_FILE, ZTID_LONG },{} },
{ "getEOF", 0, ZTID_FLOAT, FILEEOF, 0, { ZTID_FILE },{} },
{ "setEOF", 0, ZTID_VOID, FILEEOF, FL_RDONLY, { ZTID_FILE, ZTID_FLOAT },{} },
{ "getError", 0, ZTID_FLOAT, FILEERR, 0, { ZTID_FILE },{} },
{ "setError", 0, ZTID_VOID, FILEERR, FL_RDONLY, { ZTID_FILE, ZTID_FLOAT },{} },
{ "GetError", 0, ZTID_VOID, -1, FL_INL, { ZTID_FILE, ZTID_CHAR },{} },
{ "Remove", 0, ZTID_BOOL, -1, FL_INL, { ZTID_FILE },{} },
{ "WriteBytes", 0, ZTID_FLOAT, -1, 0, { ZTID_FILE, ZTID_CHAR, ZTID_FLOAT, ZTID_FLOAT },{ -10000, 0 } },
Expand Down

0 comments on commit a58cf4f

Please sign in to comment.