Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions de.peeeq.wurstscript/parserspec/wurstscript.parseq
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,6 @@ NameDef = NamedScope | VarDef | TypeDef | ModuleDef | ModuleInstanciation | Func
VisibilityModifier =
VisibilityPublic(de.peeeq.wurstscript.parser.WPos source)
| VisibilityPrivate(de.peeeq.wurstscript.parser.WPos source)
| VisibilityPublicread(de.peeeq.wurstscript.parser.WPos source)
| VisibilityProtected(de.peeeq.wurstscript.parser.WPos source)
| VisibilityDefault(de.peeeq.wurstscript.parser.WPos source)

Expand All @@ -310,6 +309,7 @@ Modifier =
| ModOverride(de.peeeq.wurstscript.parser.WPos source)
| ModAbstract(de.peeeq.wurstscript.parser.WPos source)
| ModConstant(de.peeeq.wurstscript.parser.WPos source)
| ModReadonly(de.peeeq.wurstscript.parser.WPos source)
| WurstDoc(@ignoreForEquality de.peeeq.wurstscript.parser.WPos source, String rawComment)
| ModVararg(de.peeeq.wurstscript.parser.WPos source)

Expand Down Expand Up @@ -582,9 +582,9 @@ HasModifier.attrIsPublic()
returns boolean
implemented by de.peeeq.wurstscript.attributes.ModifiersHelper.isPublic

HasModifier.attrIsPublicRead()
HasModifier.attrIsReadonly()
returns boolean
implemented by de.peeeq.wurstscript.attributes.ModifiersHelper.isPublicRead
implemented by de.peeeq.wurstscript.attributes.ModifiersHelper.isReadonly

HasModifier.attrIsPrivate()
returns boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ modifier:
'public'
| 'private'
| 'protected'
| 'publicread'
| 'readonly'
| 'static'
| 'override'
| 'abstract'
Expand Down Expand Up @@ -491,7 +491,7 @@ ENDPACKAGE: 'endpackage';
FUNCTION: 'function';
RETURNS: 'returns';
PUBLIC: 'public';
PULBICREAD: 'publicread';
READONLY: 'readonly';
PRIVATE: 'private';
PROTECTED: 'protected';
IMPORT: 'import';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ modifier:
'public'
| 'private'
| 'protected'
| 'publicread'
| 'readonly'
| 'static'
| 'override'
Expand Down Expand Up @@ -488,7 +487,6 @@ ENDPACKAGE: 'endpackage';
FUNCTION: 'function';
RETURNS: 'returns';
PUBLIC: 'public';
PULBICREAD: 'publicread';
READONLY: 'readonly';
DELEGATE: 'delegate';
STUB: 'stub';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -804,8 +804,8 @@ public List<Either<String, MarkedString>> case_Indexes(Indexes indexes) {
}

@Override
public List<Either<String, MarkedString>> case_VisibilityPublicread(VisibilityPublicread visibilityPublicread) {
return string("This variable can be read from everywhere but only written to in this scope.");
public List<Either<String, MarkedString>> case_ModReadonly(ModReadonly readonly) {
return string("This variable can only be written from its declaring scope.");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,8 @@ public static String description(VisibilityPublic visibilityPublic) {
return "public: can be used in other packages";
}

public static @Nullable String description(VisibilityPublicread visibilityPublicread) {
return null;
public static String description(ModReadonly readonly) {
return "readonly: can only be written from its declaring scope";
}

public static @Nullable String description(WBlock wBlock) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public static boolean isProtected(HasModifier e) {
return containsType(e.getModifiers(), VisibilityProtected.class);
}

public static boolean isPublicRead(HasModifier e) {
return containsType(e.getModifiers(), VisibilityPublicread.class);
public static boolean isReadonly(HasModifier e) {
return containsType(e.getModifiers(), ModReadonly.class);
}

public static boolean isPrivate(HasModifier e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1295,8 +1295,8 @@ public static void prettyPrint(VisibilityPublic e, Spacer spacer, StringBuilder
sb.append("public");
}

public static void prettyPrint(VisibilityPublicread e, Spacer spacer, StringBuilder sb, int indent) {
sb.append("publicread");
public static void prettyPrint(ModReadonly e, Spacer spacer, StringBuilder sb, int indent) {
sb.append("readonly");
}

public static void prettyPrint(WBlock wBlock, Spacer spacer, StringBuilder sb, int indent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,8 @@ private Modifier transformModifier(ModifierContext m) {
return Ast.VisibilityPrivate(src);
case JurstParser.PROTECTED:
return Ast.VisibilityProtected(src);
case JurstParser.PULBICREAD:
case JurstParser.READONLY:
return Ast.VisibilityPublicread(src);
return Ast.ModReadonly(src);
case JurstParser.STATIC:
return Ast.ModStatic(src);
case JurstParser.OVERRIDE:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,8 @@ private Modifier transformModifier(ModifierContext m) {
return Ast.VisibilityPrivate(src);
case WurstParser.PROTECTED:
return Ast.VisibilityProtected(src);
case WurstParser.PULBICREAD:
return Ast.VisibilityPublicread(src);
case WurstParser.READONLY:
return Ast.ModReadonly(src);
case WurstParser.STATIC:
return Ast.ModStatic(src);
case WurstParser.OVERRIDE:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1240,15 +1240,15 @@ private void checkStmtSet(StmtSet s) {
return;
}
checkAssignment(Utils.isJassCode(s), s, setOverload.getParameterType(1), rightType);
checkIfAssigningToConstant(s.getUpdatedExpr());
checkIfAssigningToRestrictedVariable(s.getUpdatedExpr());
checkIfNoEffectAssignment(s);
return;
}
}
WurstType leftType = s.getUpdatedExpr().attrTyp();
checkAssignment(Utils.isJassCode(s), s, leftType, rightType);

checkIfAssigningToConstant(s.getUpdatedExpr());
checkIfAssigningToRestrictedVariable(s.getUpdatedExpr());

checkIfNoEffectAssignment(s);
}
Expand Down Expand Up @@ -1294,17 +1294,18 @@ && refersToSameVar(va.attrImplicitParameter(), vb.attrImplicitParameter())) {
return false;
}

private void checkIfAssigningToConstant(final LExpr left) {
private void checkIfAssigningToRestrictedVariable(final LExpr left) {
left.match(new LExpr.MatcherVoid() {

@Override
public void case_ExprVarArrayAccess(ExprVarArrayAccess e) {

checkVarNotReadonly(e, e.attrNameLink());
}

@Override
public void case_ExprVarAccess(ExprVarAccess e) {
checkVarNotConstant(e, e.attrNameLink());
checkVarNotReadonly(e, e.attrNameLink());
}

@Override
Expand All @@ -1315,18 +1316,19 @@ public void case_ExprMemberVarDot(ExprMemberVarDot e) {
if (e.getLeft() instanceof ExprThis) {
e.addError("Cannot change 'this'. Tuples are not classes.");
} else if (e.getLeft() instanceof NameRef) {
checkIfAssigningToConstant((NameRef) e.getLeft());
checkIfAssigningToRestrictedVariable((NameRef) e.getLeft());
} else {
e.addError(
"Ok, so you are trying to assign something to the return value of a function. This wont do nothing. Tuples are not classes.");
}
}
checkVarNotConstant(e, e.attrNameLink());
checkVarNotReadonly(e, e.attrNameLink());
}

@Override
public void case_ExprMemberArrayVarDot(ExprMemberArrayVarDot e) {

checkVarNotReadonly(e, e.attrNameLink());
}

@Override
Expand All @@ -1347,6 +1349,47 @@ public void case_ExprMemberVarQuestionDot(ExprMemberVarQuestionDot e) {
});
}

private void checkVarNotReadonly(NameRef left, @Nullable NameLink link) {
if (link == null || !(link.getDef() instanceof GlobalVarDef)) {
return;
}
GlobalVarDef var = (GlobalVarDef) link.getDef();
if (!var.attrIsReadonly()) {
return;
}

ModuleInstanciation moduleOwner = nearestModuleInstanciation(var);
if (moduleOwner != null) {
if (nearestModuleInstanciation(left) != moduleOwner) {
left.addError("Readonly member " + var.getName()
+ " can only be assigned from its declaring module.");
}
return;
}

ClassOrModule owner = var.attrNearestClassOrModule();
Comment thread
Frotty marked this conversation as resolved.
if (owner != null) {
if (left.attrNearestClassOrModule() != owner) {
left.addError("Readonly member " + var.getName()
+ " can only be assigned from its declaring "
+ (owner instanceof ClassDef ? "class" : "module") + ".");
}
} else if (left.attrNearestPackage() != var.attrNearestPackage()) {
left.addError("Readonly variable " + var.getName()
+ " can only be assigned from its declaring package.");
}
}

private @Nullable ModuleInstanciation nearestModuleInstanciation(Element element) {
while (element != null) {
if (element instanceof ModuleInstanciation) {
return (ModuleInstanciation) element;
}
element = element.getParent();
}
return null;
}

private void checkVarNotConstant(NameRef left, @Nullable NameLink link) {
if (link == null) {
return;
Expand Down Expand Up @@ -2589,10 +2632,10 @@ public void case_LocalVarDef(LocalVarDef localVarDef) {
@Override
public void case_GlobalVarDef(GlobalVarDef g) {
if (g.attrNearestClassOrModule() != null) {
check(VisibilityPrivate.class, VisibilityProtected.class,
ModStatic.class, ModConstant.class, Annotation.class);
check(VisibilityPublic.class, VisibilityPrivate.class, VisibilityProtected.class,
ModStatic.class, ModConstant.class, ModReadonly.class, Annotation.class);
} else {
check(VisibilityPublic.class, ModConstant.class, Annotation.class);
check(VisibilityPublic.class, ModConstant.class, ModReadonly.class, Annotation.class);
}
if (g.hasAnnotation("@compiletime")) {
g.getAnnotation("@compiletime")
Expand Down
Loading
Loading