MetaProcessor

- Removed `break` which caused only one `sizeof()` function call to be replaced, we should have been looping over each `FunctionCall` found with `search` and then replacing ech that had a name of `sizeof` with a `NumberLiteral` expression
- Moved all type alias replacement code into a new method; `doTypeAlias(Container, Statement)`
- Added some rudiementary support for replacing any `IdentExpression` containing `size_t` with `uint`

IdentExpression

- Made it `MStatementSearchable` and `MStatementReplaceable`

Test cases

- Updated test case `meta/sizeof.t` to test the `sizeof(<expr>)` AST node in the `MetaProcessor`

Documentation

- Added diagram showing the `MStatementSearchable` and `MStatementReplaceable` in action
This commit is contained in:
Tristan B. Velloza Kildaire 2023-05-21 13:19:31 +02:00
parent 2ed89d7244
commit 04fc87d849
5 changed files with 86 additions and 29 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 194 KiB

Binary file not shown.

View File

@ -767,12 +767,8 @@ public class PointerDereferenceAssignment : Statement
}
}
public class IdentExpression : Expression
public class IdentExpression : Expression, MStatementSearchable, MStatementReplaceable
{
/* name */
private string name;
@ -790,6 +786,27 @@ public class IdentExpression : Expression
{
name = newName;
}
public override Statement[] search(TypeInfo_Class clazzType)
{
/* List of returned matches */
Statement[] matches;
/* Are we (ourselves) of this type? */
if(clazzType.isBaseOf(this.classinfo))
{
matches ~= [this];
}
return matches;
}
public override bool replace(Statement thiz, Statement that)
{
// Nothing to replace within us
return false;
}
}
public class VariableExpression : IdentExpression

View File

@ -46,28 +46,13 @@ public class MetaProcessor
{
gprintln("MetaProcessor: Examining AST node '"~curStmt.toString()~"'...");
/**
* Apply type-rewriting to any `MTypeRewritable` AST node
* (a.k.a. a node which contains a type and can have it set)
*
* NOTE: This is just for the "type" fields in AST nodes,
* we should have some full recursive re-writer.
*
* An example of why is for supporting something like:
*
* `sizeof(size_t)` <- currently is not supported by this
*/
if(cast(MTypeRewritable)curStmt)
{
typeRewrite(cast(MTypeRewritable)curStmt);
}
// Perform replacement of `size_t` with concrete type
doTypeAlias(container, curStmt);
/**
* Here we will also search for any `IdentExpression`
* which contains `size_t`, `ssize_t` etc. and replace
* them
*/
// TODO: Implement me
// TODO: Put the two above into one function that does both `size_t` changes (`MTypeRewritable` and `identExpression`-based)
/**
* Search for any `sizeof(<ident_type>)` expressions
@ -99,8 +84,6 @@ public class MetaProcessor
/* Traverse down from the `Container` we are process()'ing and apply the replacement */
MStatementReplaceable containerRepl = cast(MStatementReplaceable)container;
containerRepl.replace(curFoundStmt, replacementStmt);
break;
}
else
{
@ -156,6 +139,59 @@ public class MetaProcessor
}
}
/**
* Performs the replacement of type alieses such as `size_t`, `ssize_t`
* and so forth with their concrete type
*
* Params:
* container = the current `Container` being processsed
* curStmt = the current `Statement` to consider
*/
private void doTypeAlias(Container container, Statement curStmt)
{
/**
* Apply type-rewriting to any `MTypeRewritable` AST node
* (a.k.a. a node which contains a type and can have it set)
*
* NOTE: This is just for the "type" fields in AST nodes,
* we should have some full recursive re-writer.
*
* An example of why is for supporting something like:
*
* `sizeof(size_t)` <- currently is not supported by this
*/
if(cast(MTypeRewritable)curStmt)
{
typeRewrite(cast(MTypeRewritable)curStmt);
}
/**
* Here we will also search for any `IdentExpression`
* which contains `size_t`, `ssize_t` etc. and replace
* them
*/
if(cast(MStatementSearchable)curStmt && cast(MStatementReplaceable)curStmt)
{
MStatementSearchable searchableStmt = cast(MStatementSearchable)curStmt;
IdentExpression[] foundStmts = cast(IdentExpression[])searchableStmt.search(IdentExpression.classinfo);
// TODO: Implement me
// gprintln("IdentExpressions found: "~to!(string)(foundStmts));
foreach(IdentExpression identExp; foundStmts)
{
gprintln(identExp);
if(identExp.getName() == "size_t")
{
gprintln("Found type alias");
// TODO: Testing code below
// TODO: Replace with correct compiler configured type
container.replace(identExp, new IdentExpression("uint"));
}
}
}
}
private IntegerLiteral sizeOf_Literalize(string typeName)
{
IntegerLiteral literal = new IntegerLiteral("TODO_LITERAL_GOES_HERESIZEOF_REPLACEMENT", IntegerLiteralEncoding.UNSIGNED_INTEGER);

View File

@ -1,3 +1,7 @@
module meta_sizeof;
size_t myVar = sizeof(uint);
size_t myVar1 = sizeof(uint);
size_t myVar2 = sizeof(ubyte);
size_t myVar3 = sizeof(ushort)+1;
myVar3 = sizeof(ulong)+sizeof(size_t);