|
| 1 | +package yutautil; |
| 2 | + |
| 3 | +import haxe.macro.Context; |
| 4 | +import haxe.macro.Expr; |
| 5 | +import haxe.macro.Type; |
| 6 | + |
| 7 | +class CUMacroTools |
| 8 | +{ |
| 9 | + // Static maps: "Class.method" -> array of tags/gotos |
| 10 | + static var tags:Map<String, Array<String>> = new Map(); |
| 11 | + static var gotos:Map<String, Array<{tag:String, pos:Position}>> = new Map(); |
| 12 | + |
| 13 | + public static var validate:Bool = true; // Set manually to enable validation |
| 14 | + public static var debug:Bool = true; // Set manually to enable debug output |
| 15 | + |
| 16 | + static var validationRegistered:Bool = false; // Ensure validation is only registered once |
| 17 | + static var needsValidation:Bool = false; // Set to true if GoTo is used |
| 18 | + |
| 19 | + /** |
| 20 | + * WARNING: This macro uses untyped C++ goto, which may have unintended side-effects and is not portable. |
| 21 | + * Use with extreme caution! |
| 22 | + */ |
| 23 | + public static macro function GoTo(tag:String):Expr { |
| 24 | + // getMethodKey must be called inside the macro |
| 25 | + function getMethodKey():String { |
| 26 | + var curClass = Context.getLocalClass(); |
| 27 | + var className = curClass != null ? curClass.get().name : "UnknownClass"; |
| 28 | + var curMethod = Context.getLocalMethod(); |
| 29 | + var methodName = curMethod != null ? curMethod : "UnknownMethod"; |
| 30 | + return className + "." + methodName; |
| 31 | + } |
| 32 | + |
| 33 | + var pos = Context.currentPos(); |
| 34 | + var key = getMethodKey(); |
| 35 | + var arr = gotos.get(key); |
| 36 | + if (arr == null) { |
| 37 | + arr = []; |
| 38 | + gotos.set(key, arr); |
| 39 | + } |
| 40 | + arr.push({tag: tag, pos: pos}); |
| 41 | + |
| 42 | + needsValidation = true; |
| 43 | + |
| 44 | + // Register validation function only once if needed |
| 45 | + if (validate && !validationRegistered) { |
| 46 | + validationRegistered = true; |
| 47 | + Context.onAfterGenerate(function() { |
| 48 | + // Only run on C++ targets and if validation is needed |
| 49 | + if (!Context.defined("cpp") || !needsValidation) return; |
| 50 | + |
| 51 | + var errors:Array<String> = []; |
| 52 | + for (key in gotos.keys()) { |
| 53 | + var gotosArr = gotos.get(key); |
| 54 | + var tagsArr = tags.get(key); |
| 55 | + for (goto in gotosArr) { |
| 56 | + if (tagsArr == null || tagsArr.indexOf(goto.tag) == -1) { |
| 57 | + var err = "Expected an existing goto tag in this method: '" + goto.tag + "' (" + key + ")"; |
| 58 | + Context.error(err, goto.pos); |
| 59 | + errors.push(err); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + if (debug) { |
| 65 | + var info = "CUMacroTools Debug Info:\n"; |
| 66 | + info += "Registered tags:\n"; |
| 67 | + for (key in tags.keys()) { |
| 68 | + info += " " + key + ": " + tags.get(key) + "\n"; |
| 69 | + } |
| 70 | + info += "Registered gotos:\n"; |
| 71 | + for (key in gotos.keys()) { |
| 72 | + info += " " + key + ": "; |
| 73 | + var arr = gotos.get(key); |
| 74 | + info += [for (g in arr) g.tag].join(", ") + "\n"; |
| 75 | + } |
| 76 | + if (errors.length > 0) { |
| 77 | + info += "Errors:\n" + errors.join("\n") + "\n"; |
| 78 | + } |
| 79 | + Context.info(info, Context.currentPos()); |
| 80 | + } |
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | + var code = 'goto ' + tag + ';'; |
| 85 | + return macro untyped __cpp__($v{code}); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * WARNING: This macro emits a C++ label, which may have unintended side-effects and is not portable. |
| 90 | + * Use with extreme caution! |
| 91 | + */ |
| 92 | + public static macro function GoToTag(tag:String):Expr { |
| 93 | + // getMethodKey must be called inside the macro |
| 94 | + function getMethodKey():String { |
| 95 | + var curClass = Context.getLocalClass(); |
| 96 | + var className = curClass != null ? curClass.get().name : "UnknownClass"; |
| 97 | + var curMethod = Context.getLocalMethod(); |
| 98 | + var methodName = curMethod != null ? curMethod : "UnknownMethod"; |
| 99 | + return className + "." + methodName; |
| 100 | + } |
| 101 | + |
| 102 | + var key = getMethodKey(); |
| 103 | + var tagArr = tags.get(key); |
| 104 | + if (tagArr == null) { |
| 105 | + tagArr = []; |
| 106 | + tags.set(key, tagArr); |
| 107 | + } |
| 108 | + if (tagArr.indexOf(tag) == -1) tagArr.push(tag) else { |
| 109 | + Context.error("CUMacroTools.GoToTag: Tag '" + tag + "' already exists in method '" + key + "'." + "You cannot have the same tag twice.", Context.currentPos()); |
| 110 | + } |
| 111 | + |
| 112 | + Context.warning("CUMacroTools.GoToTag: Using C++ labels is not portable and may lead to unexpected behavior. Use with caution.", Context.currentPos()); |
| 113 | + |
| 114 | + // Check if this is C++. |
| 115 | + if (!Context.defined("cpp")) { |
| 116 | + Context.error("CUMacroTools.GoToTag can only be used in C++ targets.", Context.currentPos()); |
| 117 | + } |
| 118 | + |
| 119 | + var code = tag + ':'; |
| 120 | + return macro untyped __cpp__($v{code}); |
| 121 | + } |
| 122 | + |
| 123 | + // The same functions above, but as C++ names. |
| 124 | + |
| 125 | + public static macro function CGoTo(tag:String):Expr { |
| 126 | + return GoTo(tag); |
| 127 | + } |
| 128 | + |
| 129 | + public static macro function CLabel(tag:String):Expr { |
| 130 | + return GoToTag(tag); |
| 131 | +} |
| 132 | + |
| 133 | + |
| 134 | + |
0 commit comments