Skip to content

Commit a58cf4f

Browse files
committed
refactor(zscript): better 'read-only' compile warnings
1 parent ef8ce36 commit a58cf4f

28 files changed

+103
-31
lines changed

src/parser/BuildVisitors.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,7 +1192,7 @@ void BuildOpcodes::caseExprArrow(ASTExprArrow& host, void* param)
11921192
Function* readfunc = isarray ? host.arrayFunction : host.readFunction;
11931193
assert(readfunc->isInternal());
11941194

1195-
if(readfunc->getFlag(FUNCFLAG_NIL))
1195+
if(readfunc->isNil())
11961196
{
11971197
bool skipptr = readfunc->internal_flags & IFUNCFLAG_SKIPPOINTER;
11981198
if (!skipptr)
@@ -3376,7 +3376,7 @@ void LValBOHelper::caseExprArrow(ASTExprArrow &host, void *param)
33763376
int32_t isIndexed = (host.index != NULL);
33773377
assert(host.writeFunction->isInternal());
33783378

3379-
if(host.writeFunction->getFlag(FUNCFLAG_NIL))
3379+
if(host.writeFunction->isNil())
33803380
{
33813381
bool skipptr = host.writeFunction->internal_flags & IFUNCFLAG_SKIPPOINTER;
33823382
bool needs_pushpop = isIndexed || !skipptr;

src/parser/CompileError.xtable

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ X( BadAutoType, S, A, E, VOID, VOID, "'auto' must have an initializer with val
126126
X( GroupAuto, S, A, E, VOID, VOID, "'auto' cannot apply to a group declaration." )
127127
X( BadReturnType, S, A, E, STR, VOID, "Cannot return type '%s'." )
128128
X( BadTempVal, T, A, E, VOID, VOID, "Temporary literals cannot be stored!" )
129-
129+
X( MissingReturnWarn, S, A, W, STR, VOID, "Function '%s' is not void, and should return a value!" )
130+
X( MissingReturnError, S, A, E, STR, VOID, "Function '%s' is not void, and must return a value!" )
131+
X( NegSqrt, T, A, E, VOID, VOID, "Attempting to take the square root of a negative number!" )
132+
X( DeprecatedDelete, G, A, W, VOID, VOID, "The 'delete' operator no longer does anything. Objects are freed automatically when they become unreachable" )
133+
X( ReadOnly, C, A, W, STR, VOID, "'%s' is read-only, and cannot be written to!" )
130134

131135
#undef STR

src/parser/LibrarySymbols.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,18 +187,18 @@ void LibrarySymbols::addSymbolsToScope(Scope& scope)
187187
{
188188
AccessorTable& entry = table[i];
189189

190-
DataType const* returnType = typeStore.getType(entry.rettype);
191-
vector<DataType const*> paramTypes;
192-
for (auto& ptype : entry.params)
193-
paramTypes.push_back(typeStore.getType(ptype));
194-
195190
std::string const& name = entry.name;
196191
std::string varName = name;
197-
192+
198193
// Strip out the array at the end.
199194
bool isArray = name.substr(name.size() - 2) == "[]";
200195
if (isArray)
201196
varName = name.substr(0, name.size() - 2);
197+
198+
DataType const* returnType = typeStore.getType(entry.rettype);
199+
vector<DataType const*> paramTypes;
200+
for (auto& ptype : entry.params)
201+
paramTypes.push_back(typeStore.getType(ptype));
202202

203203
// Create function object.
204204
auto setorget = FUNCTION;
@@ -252,7 +252,7 @@ void LibrarySymbols::addSymbolsToScope(Scope& scope)
252252
}
253253
// Generate function code for getters/setters
254254
int32_t label = function->getLabel();
255-
if(function->getFlag(FUNCFLAG_NIL))
255+
if(function->isNil())
256256
{
257257
handleNil(refVar, function);
258258
}

src/parser/Scope.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,6 +1070,19 @@ Function* BasicScope::addSetter(
10701070
return fun;
10711071
}
10721072

1073+
void BasicScope::addGetter(Function* func)
1074+
{
1075+
string const& name = func->name;
1076+
if(find<Function*>(getters_, name)) return;
1077+
getters_[name] = func;
1078+
}
1079+
void BasicScope::addSetter(Function* func)
1080+
{
1081+
string const& name = func->name;
1082+
if(find<Function*>(setters_, name)) return;
1083+
setters_[name] = func;
1084+
}
1085+
10731086
Function* BasicScope::addFunction(
10741087
DataType const* returnType, string const& name,
10751088
vector<DataType const*> const& paramTypes, vector<string const*> const& paramNames, int32_t flags, ASTFuncDecl* node, CompileErrorHandler* handler)

src/parser/Scope.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ namespace ZScript
136136
std::vector<DataType const*> const& paramTypes, std::vector<std::string const*> const& paramNames,
137137
int32_t flags = 0, AST* node = NULL)
138138
= 0;
139+
virtual void addGetter(Function* func) = 0;
140+
virtual void addSetter(Function* func) = 0;
139141
virtual Function* addFunction(
140142
DataType const* returnType, std::string const& name,
141143
std::vector<DataType const*> const& paramTypes, std::vector<std::string const*> const& paramNames,
@@ -365,6 +367,8 @@ namespace ZScript
365367
DataType const* returnType, std::string const& name,
366368
std::vector<DataType const*> const& paramTypes, std::vector<std::string const*> const& paramNames,
367369
int32_t flags = 0, AST* node = NULL);
370+
virtual void addGetter(Function* func);
371+
virtual void addSetter(Function* func);
368372
virtual Function* addFunction(
369373
DataType const* returnType, std::string const& name,
370374
std::vector<DataType const*> const& paramTypes, std::vector<std::string const*> const& paramNames,

src/parser/SemanticAnalyzer.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,6 +1367,9 @@ void SemanticAnalyzer::caseExprArrow(ASTExprArrow& host, void* param)
13671367
leftType->getName().c_str()));
13681368
return;
13691369
}
1370+
if(host.writeFunction->getFlag(FUNCFLAG_READ_ONLY))
1371+
handleError(CompileError::ReadOnly(&host, fmt::format("{}->{}{}",
1372+
leftType->getName().c_str(), host.right, (host.index ? "[]" : ""))));
13701373
}
13711374

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

17151718
host.binding = bestFunctions.front();
17161719
deprecWarn(host.binding, &host, "Function", host.binding->getSignature().asString());
1720+
if(host.binding->getFlag(FUNCFLAG_READ_ONLY))
1721+
handleError(CompileError::ReadOnly(&host, host.binding->getSignature().asString()));
17171722
}
17181723

17191724
void SemanticAnalyzer::caseExprNegate(ASTExprNegate& host, void*)

src/parser/ZScript.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,8 @@ namespace ZScript
471471
}
472472
bool getFlag(int32_t flag) const {return (flags & flag) != 0;}
473473

474-
bool isInternal() const {return !node;};
474+
bool isInternal() const {return !node;}
475+
bool isNil() const {return prototype || getFlag(FUNCFLAG_NIL|FUNCFLAG_READ_ONLY);}
475476

476477
// If this is a tracing function (disabled by `#option LOGGING false`)
477478
bool isTracing() const;

src/parser/parserDefs.h

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,20 @@
99
using namespace util;
1010

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

2427
#define IFUNCFLAG_SKIPPOINTER 0x01
2528
#define IFUNCFLAG_REASSIGNPTR 0x02

src/parser/symbols/BitmapSymbols.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ static AccessorTable BitmapTable[] =
66
{
77
// name, tag, rettype, var, funcFlags, params,optparams
88
{ "getWidth", 0, ZTID_FLOAT, BITMAPWIDTH, 0, { ZTID_BITMAP },{} },
9-
{ "setWidth", 0, ZTID_VOID, BITMAPWIDTH, 0, { ZTID_BITMAP, ZTID_FLOAT },{} },
9+
{ "setWidth", 0, ZTID_VOID, BITMAPWIDTH, FL_RDONLY, { ZTID_BITMAP, ZTID_FLOAT },{} },
1010
{ "getHeight", 0, ZTID_FLOAT, BITMAPHEIGHT, 0, { ZTID_BITMAP },{} },
11-
{ "setHeight", 0, ZTID_VOID, BITMAPHEIGHT, 0, { ZTID_BITMAP, ZTID_FLOAT },{} },
11+
{ "setHeight", 0, ZTID_VOID, BITMAPHEIGHT,FL_RDONLY, { ZTID_BITMAP, ZTID_FLOAT },{} },
1212

1313
{ "GetPixel", 0, ZTID_FLOAT, -1, 0, { ZTID_BITMAP, ZTID_FLOAT, ZTID_FLOAT },{} },
1414
{ "CountColor", 0, ZTID_FLOAT, -1, 0, { ZTID_BITMAP, ZTID_BITMAP, ZTID_FLOAT, ZTID_FLOAT, ZTID_FLOAT, ZTID_FLOAT },{ -10000 } },

src/parser/symbols/CombosPtrSymbols.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,18 @@ static AccessorTable CombosTable[] =
77
//name, tag, rettype, var, funcFlags, params,optparams
88
//Only as 'this->' in combo scripts
99
{ "getX", 0, ZTID_FLOAT, COMBOXR, 0, { ZTID_COMBOS },{} },
10+
{ "setX", 0, ZTID_VOID, COMBOXR, FL_RDONLY, { ZTID_COMBOS, ZTID_FLOAT },{} },
1011
{ "getY", 0, ZTID_FLOAT, COMBOYR, 0, { ZTID_COMBOS },{} },
12+
{ "setY", 0, ZTID_VOID, COMBOYR, FL_RDONLY, { ZTID_COMBOS, ZTID_FLOAT },{} },
1113
{ "getPos", 0, ZTID_FLOAT, COMBOPOSR, 0, { ZTID_COMBOS },{} },
14+
{ "setPos", 0, ZTID_VOID, COMBOPOSR, FL_RDONLY, { ZTID_COMBOS, ZTID_FLOAT },{} },
1215
{ "getLayer", 0, ZTID_FLOAT, COMBOLAYERR, 0, { ZTID_COMBOS },{} },
16+
{ "setLayer", 0, ZTID_VOID, COMBOLAYERR, FL_RDONLY, { ZTID_COMBOS, ZTID_FLOAT },{} },
1317
//
1418
{ "getInitD[]", 0, ZTID_UNTYPED, COMBODATAINITD, 0, { ZTID_COMBOS, ZTID_FLOAT },{} },
1519
{ "setInitD[]", 0, ZTID_VOID, COMBODATAINITD, 0, { ZTID_COMBOS, ZTID_FLOAT, ZTID_UNTYPED },{} },
1620
{ "getID", 0, ZTID_FLOAT, COMBODATAID, 0, { ZTID_COMBOS },{} },
21+
{ "setID", 0, ZTID_VOID, COMBODATAID, FL_RDONLY, { ZTID_COMBOS, ZTID_FLOAT },{} },
1722
{ "getOriginalTile", 0, ZTID_FLOAT, COMBODOTILE, 0, { ZTID_COMBOS },{} },
1823
{ "setOriginalTile", 0, ZTID_VOID, COMBODOTILE, 0, { ZTID_COMBOS, ZTID_FLOAT },{} },
1924
{ "getFrame", 0, ZTID_FLOAT, COMBODFRAME, 0, { ZTID_COMBOS },{} },
@@ -34,8 +39,8 @@ static AccessorTable CombosTable[] =
3439
{ "setEffect", 0, ZTID_VOID, COMBODEFFECT, 0, { ZTID_COMBOS, ZTID_FLOAT },{} },
3540
{ "getType", 0, ZTID_FLOAT, COMBODTYPE, 0, { ZTID_COMBOS },{} },
3641
{ "setType", 0, ZTID_VOID, COMBODTYPE, 0, { ZTID_COMBOS, ZTID_FLOAT },{} },
37-
{ "getCSet", 0, ZTID_FLOAT, COMBODCSET, 0, { ZTID_COMBOS },{},0,"Use ->CSet2 instead!" },
38-
{ "setCSet", 0, ZTID_VOID, COMBODCSET, 0, { ZTID_COMBOS, ZTID_FLOAT },{},0,"Use ->CSet2 instead!" },
42+
{ "getCSet", 0, ZTID_FLOAT, COMBODCSET, FL_DEPR, { ZTID_COMBOS },{},0,"Use ->CSet2 instead!" },
43+
{ "setCSet", 0, ZTID_VOID, COMBODCSET, FL_DEPR, { ZTID_COMBOS, ZTID_FLOAT },{},0,"Use ->CSet2 instead!" },
3944
{ "getCSet2", 0, ZTID_FLOAT, COMBODCSET, 0, { ZTID_COMBOS },{} },
4045
{ "setCSet2", 0, ZTID_VOID, COMBODCSET, 0, { ZTID_COMBOS, ZTID_FLOAT },{} },
4146
{ "getCSet2Flags", 0, ZTID_FLOAT, COMBODCSET2FLAGS, 0, { ZTID_COMBOS },{} },

0 commit comments

Comments
 (0)