Skip to content

Commit

Permalink
Merge b2ac92e into 5f81ac4
Browse files Browse the repository at this point in the history
  • Loading branch information
deavmi committed May 6, 2024
2 parents 5f81ac4 + b2ac92e commit 9652c29
Show file tree
Hide file tree
Showing 8 changed files with 887 additions and 105 deletions.
872 changes: 775 additions & 97 deletions source/tlang/compiler/parsing/core.d

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions source/tlang/compiler/symbols/data.d
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,12 @@ public class Statement

public enum AccessorType
{
PUBLIC, PRIVATE, PROTECTED, UNKNOWN
UNKNOWN, PUBLIC, PRIVATE, PROTECTED
}

public enum InitScope
{
VIRTUAL, STATIC, UNKNOWN
UNKNOWN, VIRTUAL, STATIC
}

public class Assignment : Statement
Expand All @@ -268,7 +268,7 @@ public class Assignment : Statement
public class Entity : Statement
{
/* Accessor type */
private AccessorType accessorType = AccessorType.PUBLIC;
private AccessorType accessorType;

/* Function/Modifier type */
private InitScope initScope;
Expand Down
2 changes: 1 addition & 1 deletion source/tlang/compiler/typecheck/core.d
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module tlang.compiler.typecheck.core;
import tlang.compiler.symbols.check;
import tlang.compiler.symbols.data;
import std.conv : to, ConvException;
import std.string;
import std.string : split, cmp, format;
import std.stdio;
import tlang.misc.logging;
import tlang.compiler.parsing.core;
Expand Down
75 changes: 74 additions & 1 deletion source/tlang/compiler/typecheck/dependency/core.d
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module tlang.compiler.typecheck.dependency.core;
import tlang.compiler.symbols.check;
import tlang.compiler.symbols.data;
import std.conv : to;
import std.string;
import std.string : cmp;
import std.stdio;
import tlang.misc.logging;
import tlang.compiler.parsing.core;
Expand Down Expand Up @@ -451,6 +451,73 @@ public class DNodeGenerator
throw new DependencyException(DependencyError.GENERAL_ERROR, message);
}

/**
* Performs an access check to the given
* entity but from the accessing-environment
* of the provided statement
*
* Params:
* stmtCtx = the `Statement` to derive the
* access environment from
* referent = the `Entity` being referred
* to
* ignoreAccessModifiers = is we should
* ignore thie check entirely (default: `false`)
* Returns: `true` if allowed, `false`
* otherwise
*/
private bool accessCheck
(
Statement stmtCtx, Entity referent,
bool ignoreAccessModifiers = false
)
{
// If ignoring mode then always allow accesses
if(ignoreAccessModifiers)
{
return true;
}

// Find the top-most container (the module)
// of the incoming statement, this determines
// the accessing-environment
Container accEnv = resolver.findContainerOfType(Module.classinfo, stmtCtx);
assert(accEnv);

// If the referent is in the same module
// (TODO: See if we should do respecting WITHIN the same module)
if(resolver.isDescendant(accEnv, referent))
{
return true;
}
// Else, check if public
else
{
return referent.getAccessorType() == AccessorType.PUBLIC;
}
}

private void accessCheckAuto
(
Statement stmtCtx, Entity referent,
bool ignoreAccessModifiers = false
)
{
if(accessCheck(stmtCtx, referent, ignoreAccessModifiers))
{
DEBUG("Access check passed for accEnv: ", stmtCtx, " with referentEnt: ", referent);
}
else
{
DEBUG("Access check FAILED for accEnv: ", stmtCtx, " with referentEnt: ", referent);
// TODO: Make the error below way nicer, also maybe include module idk
// expect("Cannot access '"~to!(string)(referent)~"' from statement '"~to!(string)(stmtCtx));

import tlang.compiler.typecheck.dependency.exceptions : AccessViolation;
throw new AccessViolation(stmtCtx, referent);
}
}

public DNode root;


Expand Down Expand Up @@ -619,6 +686,9 @@ public class DNodeGenerator
/* TODO: We need to fetch the cached function definition here and call it */
Entity funcEntity = resolver.resolveBest(context.container, funcCall.getName());
assert(funcEntity);

// Access check
accessCheckAuto(exp, funcEntity);

// FIXME: The below is failing (we probably need a forward look ahead?)
// OR use the addFuncDef list?
Expand Down Expand Up @@ -724,6 +794,9 @@ public class DNodeGenerator
{
/* FIXME: Below assumes basic variable declarations at module level, fix later */

// Access check
accessCheckAuto(exp, namedEntity);

/**
* If `namedEntity` is a `Variable`
*
Expand Down
31 changes: 31 additions & 0 deletions source/tlang/compiler/typecheck/dependency/exceptions.d
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module tlang.compiler.typecheck.dependency.exceptions;

import tlang.misc.exceptions : TError;
import std.conv : to;
import std.string : format;

// FIXME: Extend TError rather than Exception
public enum DependencyError
Expand All @@ -25,4 +26,34 @@ public final class DependencyException : TError
{
return errTye;
}
}

/**
* An access violation occurs when you
* try to access one entity from an
* environment that doesn't allow
* for that due to the access rights
* of the referent entity
*/
public final class AccessViolation : TError
{
import tlang.compiler.symbols.data : Entity, Statement;

this
(
Statement env,
Entity referent
)
{
super
(
format
(
"Cannot access entity '%s' with access modifier %s from statement '%s'",
referent.getName(),
referent.getAccessorType(),
env
)
);
}
}
2 changes: 1 addition & 1 deletion source/tlang/testing/modules/a.t
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import niks.c, b;

public ubyte j = 0;

int ident(int i)
private int ident(int i)
{
c.k();
return i;
Expand Down
2 changes: 1 addition & 1 deletion source/tlang/testing/modules/b.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module b;

import a;

int doThing()
public int doThing()
{
int local = 0;

Expand Down
2 changes: 1 addition & 1 deletion source/tlang/testing/modules/niks/c.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module c;

import a;

void k()
public void k()
{
a.j = a.j + 2;
}

0 comments on commit 9652c29

Please sign in to comment.