Skip to content

Commit

Permalink
Add @:copyValue
Browse files Browse the repository at this point in the history
  • Loading branch information
SomeRanDev committed Nov 29, 2024
1 parent 8696657 commit 7dce4a1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
15 changes: 13 additions & 2 deletions src/reflaxe/config/Meta.hx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,18 @@ enum abstract Meta(String) from String to String {
If used on a type, temporary variables that might have been generated
by the Haxe compiler (only used once) will be removed and inlined.
See `reflaxe/compiler/TemporaryVarRemover.hx`.
See `reflaxe/preprocessors/implementations/RemoveTemporaryVariablesImpl.hx`.
**/
var AvoidTemporaries = ":avoid_temporaries";
var AvoidTemporaries = ":avoidTemporaries";

/**
Indicates this type is "copied" instead of referenced when assigned.
This is used by `RemoveLocalVariableAliases` to ensure only aliases
that are references are removed.
At the current moment this is only used by `RemoveLocalVariableAliases`.
See `reflaxe/preprocessors/implementations/RemoveLocalVariableAliasesImpl.hx`.
**/
var CopyValue = ":copyValue";
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import reflaxe.helpers.Context;
import haxe.macro.Type;

using reflaxe.helpers.NameMetaHelper;
using reflaxe.helpers.NullableMetaAccessHelper;
using reflaxe.helpers.NullHelper;
using reflaxe.helpers.TypedExprHelper;
using reflaxe.helpers.TypeHelper;
Expand Down Expand Up @@ -42,6 +43,7 @@ class RemoveLocalVariableAliasesImpl {
final innerType = Context.followWithAbstracts(t);
return switch(innerType) {
case TAbstract(_.get() => abs, []): abs.hasMeta(":runtimeValue");
case _ if(innerType.getMeta().maybeHas(":copyValue")): true;
case _ if(innerType.isString()): true;
case _: false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ using reflaxe.helpers.TypedExprHelper;
enum RemoveTemporaryVariablesMode {
/**
Only variables assigned from class fields with the
`@:avoid_temporaries` metadata are removed. This meta
`@:avoidTemporaries` metadata are removed. This meta
can be placed on the field itself, or the class declaration
of the type the field is using.
Expand All @@ -33,7 +33,7 @@ enum RemoveTemporaryVariablesMode {
```haxe
// Given a Rect class like this...
@:avoid_temporaries
@:avoidTemporaries
class Rect { ... }
// This would be converted...
Expand Down Expand Up @@ -205,15 +205,15 @@ class RemoveTemporaryVariablesImpl {
/**
Given a `TVar` and its expression, check if it should be removed
on the basis of its type or field declaration having the
`@:avoid_temporaries` metadata.
`@:avoidTemporaries` metadata.
**/
static function shouldRemoveVariableBeauseAvoidTemporaries(tvar: TVar, maybeExpr: Null<TypedExpr>) {
final fieldAccess = isField(maybeExpr);
if(fieldAccess == null) {
return false;
}

// Check if type has `@:avoid_temporaries`.
// Check if type has `@:avoidTemporaries`.
final isAvoidTemporariesType = switch(tvar.t) {
case TInst(clsRef, _): clsRef.get().hasMeta(Meta.AvoidTemporaries);
case TAbstract(absRef, _): absRef.get().hasMeta(Meta.AvoidTemporaries);
Expand All @@ -222,7 +222,7 @@ class RemoveTemporaryVariablesImpl {
}
if(isAvoidTemporariesType) { return true; }

// Check if field has `@:avoid_temporaries`.
// Check if field has `@:avoidTemporaries`.
return switch(fieldAccess) {
case FInstance(_, _, cf) | FStatic(_, cf): cf.get().hasMeta(Meta.AvoidTemporaries);
case _: false;
Expand Down

0 comments on commit 7dce4a1

Please sign in to comment.