Compiler.hx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. /*
  2. * Copyright (C)2005-2019 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package haxe.macro;
  23. import haxe.display.Display;
  24. import haxe.macro.Expr;
  25. import haxe.hxb.WriterConfig;
  26. /**
  27. All these methods can be called for compiler configuration macros.
  28. **/
  29. class Compiler {
  30. /**
  31. A conditional compilation flag can be set on the command line using
  32. `-D key=value`.
  33. Returns the value of a compiler flag.
  34. If the compiler flag is defined but no value is set,
  35. `Compiler.getDefine` returns `"1"` (e.g. `-D key`).
  36. If the compiler flag is not defined, `Compiler.getDefine` returns
  37. `null`.
  38. Note: This is a macro and cannot be called from within other macros. Refer
  39. to `haxe.macro.Context.definedValue` to obtain defined values in macro context.
  40. @see https://haxe.org/manual/lf-condition-compilation.html
  41. **/
  42. macro /* <-- ! */ static public function getDefine(key:String) {
  43. return macro $v{haxe.macro.Context.definedValue(key)};
  44. }
  45. #if macro
  46. static var ident = ~/^[A-Za-z_][A-Za-z0-9_]*$/;
  47. static var path = ~/^[A-Za-z_][A-Za-z0-9_.]*$/;
  48. public static function allowPackage(v:String) {
  49. load("allow_package", 1)(v);
  50. }
  51. /**
  52. Set a conditional compiler flag.
  53. Usage of this function outside of initialization macros is deprecated and may cause compilation server issues.
  54. **/
  55. public static function define(flag:String, ?value:String) {
  56. Context.assertInitMacro();
  57. load("define", 2)(flag, value);
  58. }
  59. /**
  60. Add a class path where ".hx" source files or packages (sub-directories) can be found.
  61. Usage of this function outside of initialization macros is deprecated and may cause compilation server issues.
  62. **/
  63. public static function addClassPath(path:String) {
  64. Context.assertInitMacro();
  65. load("add_class_path", 1)(path);
  66. }
  67. public static function getOutput():String {
  68. return load("get_output", 0)();
  69. }
  70. public static function setOutput(fileOrDir:String) {
  71. load("set_output", 1)(fileOrDir);
  72. }
  73. public static function getDisplayPos():Null<{file:String, pos:Int}> {
  74. return load("get_display_pos", 0)();
  75. }
  76. /**
  77. Returns all the configuration settings applied to the compiler.
  78. Usage of this function outside a macro context returns `null`.
  79. **/
  80. public static function getConfiguration():Null<CompilerConfiguration> {
  81. return load("get_configuration", 0)();
  82. }
  83. /**
  84. Sets the target configuration.
  85. Usage of this function outside a macro context does nothing.
  86. **/
  87. public static function setPlatformConfiguration(config:PlatformConfig):Void {
  88. load("set_platform_configuration", 1)(config);
  89. }
  90. /**
  91. Adds a native library depending on the platform (e.g. `-swf-lib` for Flash).
  92. Usage of this function outside of initialization macros is deprecated and may cause compilation server issues.
  93. **/
  94. public static function addNativeLib(name:String) {
  95. Context.assertInitMacro();
  96. load("add_native_lib", 1)(name);
  97. }
  98. /**
  99. Includes all modules in package `pack` in the compilation.
  100. In order to include single modules, their paths can be listed directly
  101. on command line: `haxe ... ModuleName pack.ModuleName`.
  102. By default `Compiler.include` will search for modules in the directories defined with `-cp`.
  103. If you want to specify a different set of paths to search for modules, you can use the optional
  104. argument `classPath`.
  105. Usage of this function outside of initialization macros is deprecated and may cause compilation server issues.
  106. @param pack The package dot-path as String. Use `''` to include the root package.
  107. @param rec If true, recursively adds all sub-packages.
  108. @param ignore Array of module names to ignore for inclusion.
  109. You can use `module*` with a * at the end for Wildcard matching
  110. @param classPaths An alternative array of paths (directory names) to use to search for modules to include.
  111. Note that if you pass this argument, only the specified paths will be used for inclusion.
  112. @param strict If true and given package wasn't found in any of class paths, fail with an error.
  113. **/
  114. public static function include(pack:String, ?rec = true, ?ignore:Array<String>, ?classPaths:Array<String>, strict = false) {
  115. function include(pack:String, ?rec = true, ?ignore:Array<String>, ?classPaths:Array<String>, strict = false) {
  116. var ignoreWildcard:Array<String> = [];
  117. var ignoreString:Array<String> = [];
  118. if (ignore != null) {
  119. for (ignoreRule in ignore) {
  120. if (StringTools.endsWith(ignoreRule, "*")) {
  121. ignoreWildcard.push(ignoreRule.substr(0, ignoreRule.length - 1));
  122. } else {
  123. ignoreString.push(ignoreRule);
  124. }
  125. }
  126. }
  127. var skip = if (ignore == null) {
  128. function(c) return false;
  129. } else {
  130. function(c:String) {
  131. if (Lambda.has(ignoreString, c))
  132. return true;
  133. for (ignoreRule in ignoreWildcard)
  134. if (StringTools.startsWith(c, ignoreRule))
  135. return true;
  136. return false;
  137. }
  138. }
  139. var displayValue = Context.definedValue("display");
  140. if (classPaths == null) {
  141. classPaths = Context.getClassPath();
  142. // do not force inclusion when using completion
  143. switch (displayValue) {
  144. case null:
  145. case "usage":
  146. case _:
  147. return;
  148. }
  149. // normalize class path
  150. for (i in 0...classPaths.length) {
  151. var cp = StringTools.replace(classPaths[i], "\\", "/");
  152. if (StringTools.endsWith(cp, "/"))
  153. cp = cp.substr(0, -1);
  154. if (cp == "")
  155. cp = ".";
  156. classPaths[i] = cp;
  157. }
  158. }
  159. var prefix = pack == '' ? '' : pack + '.';
  160. var found = false;
  161. for (cp in classPaths) {
  162. var path = pack == '' ? cp : cp + "/" + pack.split(".").join("/");
  163. if (!sys.FileSystem.exists(path) || !sys.FileSystem.isDirectory(path))
  164. continue;
  165. found = true;
  166. for (file in sys.FileSystem.readDirectory(path)) {
  167. if (StringTools.endsWith(file, ".hx") && file.substr(0, file.length - 3).indexOf(".") < 0) {
  168. if (file == "import.hx")
  169. continue;
  170. var cl = prefix + file.substr(0, file.length - 3);
  171. if (skip(cl))
  172. continue;
  173. load("include_module", 1)(cl);
  174. } else if (rec && sys.FileSystem.isDirectory(path + "/" + file) && !skip(prefix + file))
  175. include(prefix + file, true, ignore, classPaths);
  176. }
  177. }
  178. if (strict && !found)
  179. Context.error('Package "$pack" was not found in any of class paths', Context.currentPos());
  180. }
  181. Context.assertInitMacro();
  182. Context.onAfterInitMacros(() -> include(pack, rec, ignore, classPaths, strict));
  183. }
  184. /**
  185. Exclude a class or an enum without changing it to `@:nativeGen`.
  186. **/
  187. static function excludeBaseType(baseType:Type.BaseType):Void {
  188. if (!baseType.isExtern) {
  189. var meta = baseType.meta;
  190. if (!meta.has(":nativeGen")) {
  191. meta.add(":hxGen", [], baseType.pos);
  192. }
  193. baseType.exclude();
  194. }
  195. }
  196. /**
  197. Exclude a specific class, enum, or all classes and enums in a
  198. package from being generated. Excluded types become `extern`.
  199. @param pack The package dot-path as String. Use `''` to exclude the root package.
  200. @param rec If true, recursively excludes all sub-packages.
  201. **/
  202. public static function exclude(pack:String, ?rec = true) {
  203. Context.onGenerate(function(types) {
  204. for (t in types) {
  205. var b:Type.BaseType, name;
  206. switch (t) {
  207. case TInst(c, _):
  208. name = c.toString();
  209. var c = c.get();
  210. switch (c.kind) {
  211. case KModuleFields(module):
  212. name = module;
  213. case _:
  214. }
  215. b = c;
  216. case TEnum(e, _):
  217. name = e.toString();
  218. b = e.get();
  219. default:
  220. continue;
  221. }
  222. var p = b.pack.join(".");
  223. if ((p == pack || name == pack) || (rec && StringTools.startsWith(p, pack + ".")))
  224. excludeBaseType(b);
  225. }
  226. }, false);
  227. }
  228. /**
  229. Exclude classes and enums listed in an extern file (one per line) from being generated.
  230. **/
  231. public static function excludeFile(fileName:String) {
  232. fileName = Context.resolvePath(fileName);
  233. var f = sys.io.File.read(fileName, true);
  234. var classes = new haxe.ds.StringMap();
  235. try {
  236. while (true) {
  237. var l = StringTools.trim(f.readLine());
  238. if (l == "" || !~/[A-Za-z0-9._]/.match(l))
  239. continue;
  240. classes.set(l, true);
  241. }
  242. } catch (e:haxe.io.Eof) {}
  243. Context.onGenerate(function(types) {
  244. for (t in types) {
  245. switch (t) {
  246. case TInst(c, _):
  247. if (classes.exists(c.toString()))
  248. excludeBaseType(c.get());
  249. case TEnum(e, _):
  250. if (classes.exists(e.toString()))
  251. excludeBaseType(e.get());
  252. default:
  253. }
  254. }
  255. });
  256. }
  257. /**
  258. Marks types or packages to be kept by DCE.
  259. This also extends to the sub-types of resolved modules.
  260. In order to include module sub-types directly, their full dot path
  261. including the containing module has to be used
  262. (e.g. `msignal.Signal.Signal0`).
  263. This operation has no effect if the type has already been loaded, e.g.
  264. through `Context.getType`.
  265. @param path A package, module or sub-type dot path to keep.
  266. @param paths An Array of package, module or sub-type dot paths to keep.
  267. @param recursive If true, recurses into sub-packages for package paths.
  268. **/
  269. public static function keep(?path:String, ?paths:Array<String>, ?recursive:Bool = true) {
  270. if (null == paths)
  271. paths = [];
  272. if (null != path)
  273. paths.push(path);
  274. for (path in paths) {
  275. addGlobalMetadata(path, "@:keep", recursive, true, true);
  276. }
  277. }
  278. /**
  279. Enables null safety for a type or a package.
  280. @param path A package, module or sub-type dot path to enable null safety for.
  281. @param recursive If true, recurses into sub-packages for package paths.
  282. **/
  283. public static function nullSafety(path:String, mode:NullSafetyMode = Loose, recursive:Bool = true) {
  284. addGlobalMetadata(path, '@:nullSafety($mode)', recursive);
  285. }
  286. /**
  287. Adds metadata `meta` to all types (if `toTypes = true`) or fields (if
  288. `toFields = true`) whose dot-path matches `pathFilter`.
  289. If `recursive` is true a dot-path is considered matched if it starts
  290. with `pathFilter`. This automatically applies to path filters of
  291. packages. Otherwise an exact match is required.
  292. If `pathFilter` is the empty String `""` it matches everything (if
  293. `recursive = true`) or only top-level types (if `recursive = false`).
  294. This operation has no effect if the type has already been loaded, e.g.
  295. through `Context.getType`.
  296. **/
  297. public static function addGlobalMetadata(pathFilter:String, meta:String, ?recursive:Bool = true, ?toTypes:Bool = true, ?toFields:Bool = false) {
  298. load("add_global_metadata_impl", 5)(pathFilter, meta, recursive, toTypes, toFields);
  299. }
  300. @:deprecated
  301. public static function addMetadata(meta:String, className:String, ?field:String, ?isStatic:Bool) {
  302. var pathFilter = field == null ? className : '$className.$field';
  303. addGlobalMetadata(pathFilter, meta, false, field == null, field != null);
  304. }
  305. /**
  306. Reference a json file describing user-defined metadata
  307. See https://github.com/HaxeFoundation/haxe/blob/development/src-json/meta.json
  308. **/
  309. public static function registerMetadataDescriptionFile(path:String, ?source:String):Void {
  310. var f = sys.io.File.getContent(path);
  311. var content:Array<MetadataDescription> = haxe.Json.parse(f);
  312. for (m in content)
  313. registerCustomMetadata(m, source);
  314. }
  315. /**
  316. Reference a json file describing user-defined defines
  317. See https://github.com/HaxeFoundation/haxe/blob/development/src-json/define.json
  318. **/
  319. public static function registerDefinesDescriptionFile(path:String, ?source:String):Void {
  320. var f = sys.io.File.getContent(path);
  321. var content:Array<DefineDescription> = haxe.Json.parse(f);
  322. for (d in content)
  323. registerCustomDefine(d, source);
  324. }
  325. /**
  326. Register a custom metadata for documentation and completion purposes
  327. **/
  328. public static function registerCustomMetadata(meta:MetadataDescription, ?source:String):Void {
  329. load("register_metadata_impl", 2)(meta, source);
  330. }
  331. /**
  332. Register a custom define for documentation purposes
  333. **/
  334. public static function registerCustomDefine(define:DefineDescription, ?source:String):Void {
  335. load("register_define_impl", 2)(define, source);
  336. }
  337. /**
  338. Change the default JS output by using a custom generator callback
  339. **/
  340. public static function setCustomJSGenerator(callb:JSGenApi->Void) {
  341. load("set_custom_js_generator", 1)(callb);
  342. }
  343. static inline function load(f, nargs):Dynamic {
  344. return @:privateAccess Context.load(f, nargs);
  345. }
  346. /**
  347. Clears cached results of file lookups
  348. **/
  349. public static function flushDiskCache() {
  350. load("flush_disk_cache", 0)();
  351. }
  352. /**
  353. Gets the current hxb writer configuration, if any.
  354. **/
  355. static public function getHxbWriterConfiguration():Null<WriterConfig> {
  356. return load("get_hxb_writer_config", 0)();
  357. }
  358. /**
  359. Sets the hxb writer configuration to `config`. If no hxb writer configuration
  360. exists, it is created.
  361. The intended usage is
  362. ```
  363. var config = Compiler.getHxbWriterConfiguration();
  364. config.archivePath = "newPath.zip";
  365. // Other changes
  366. Compiler.setHxbWriterConfiguration(config);
  367. ```
  368. If `config` is `null`, hxb writing is disabled.
  369. @see haxe.hxb.WriterConfig
  370. **/
  371. static public function setHxbWriterConfiguration(config:Null<WriterConfig>) {
  372. load("set_hxb_writer_config", 1)(config);
  373. }
  374. #end
  375. #if (js || lua || macro)
  376. /**
  377. Embed a JavaScript or Lua file at compile time (can be called by `--macro` or within an `__init__` method).
  378. **/
  379. public static #if !macro macro #end function includeFile(file:String, position:IncludePosition = Top) {
  380. return switch ((position : String).toLowerCase()) {
  381. case Inline:
  382. if (Context.getLocalModule() == "")
  383. Context.error("Cannot use inline mode when includeFile is called by `--macro`", Context.currentPos());
  384. var f = try sys.io.File.getContent(Context.resolvePath(file)) catch (e:Dynamic) Context.error(Std.string(e), Context.currentPos());
  385. var p = Context.currentPos();
  386. if (Context.defined("js")) {
  387. macro @:pos(p) js.Syntax.plainCode($v{f});
  388. } else {
  389. macro @:pos(p) untyped __lua__($v{f});
  390. }
  391. case Top | Closure:
  392. @:privateAccess Context.includeFile(file, position);
  393. macro {};
  394. case _:
  395. Context.error("unknown includeFile position: " + position, Context.currentPos());
  396. }
  397. }
  398. #end
  399. }
  400. enum abstract IncludePosition(String) from String to String {
  401. /**
  402. Prepend the file content to the output file.
  403. **/
  404. var Top = "top";
  405. /**
  406. Prepend the file content to the body of the top-level closure.
  407. Since the closure is in strict-mode, there may be run-time error if the input is not strict-mode-compatible.
  408. **/
  409. var Closure = "closure";
  410. /**
  411. Directly inject the file content at the call site.
  412. **/
  413. var Inline = "inline";
  414. }
  415. enum abstract NullSafetyMode(String) to String {
  416. /**
  417. Disable null safety.
  418. **/
  419. var Off;
  420. /**
  421. Loose safety.
  422. If an expression is checked `!= null`, then it's considered safe even if it could be modified after the check.
  423. E.g.
  424. ```haxe
  425. function example(o:{field:Null<String>}) {
  426. if(o.field != null) {
  427. mutate(o);
  428. var notNullable:String = o.field; //no error
  429. }
  430. }
  431. function mutate(o:{field:Null<String>}) {
  432. o.field = null;
  433. }
  434. ```
  435. **/
  436. var Loose;
  437. /**
  438. Full scale null safety.
  439. If a field is checked `!= null` it stays safe until a call is made or any field of any object is reassigned,
  440. because that could potentially alter an object of the checked field.
  441. E.g.
  442. ```haxe
  443. function example(o:{field:Null<String>}, b:{o:{field:Null<String>}}) {
  444. if(o.field != null) {
  445. var notNullable:String = o.field; //no error
  446. someCall();
  447. var notNullable:String = o.field; // Error!
  448. }
  449. if(o.field != null) {
  450. var notNullable:String = o.field; //no error
  451. b.o = {field:null};
  452. var notNullable:String = o.field; // Error!
  453. }
  454. }
  455. ```
  456. **/
  457. var Strict;
  458. /**
  459. Full scale null safety for a multi-threaded environment.
  460. With this mode checking a field `!= null` does not make it safe, because it could be changed from another thread
  461. at the same time or immediately after the check.
  462. The only nullable thing could be safe are local variables.
  463. **/
  464. var StrictThreaded;
  465. }
  466. typedef MetadataDescription = {
  467. final metadata:String;
  468. final doc:String;
  469. /**
  470. External resources for more information about this metadata.
  471. **/
  472. @:optional final links:Array<String>;
  473. /**
  474. List (small description) of parameters that this metadata accepts.
  475. **/
  476. @:optional final params:Array<String>;
  477. /**
  478. Haxe target(s) for which this metadata is used.
  479. **/
  480. @:optional final platforms:Array<Platform>;
  481. /**
  482. Places where this metadata can be applied.
  483. **/
  484. @:optional final targets:Array<MetadataTarget>;
  485. }
  486. typedef DefineDescription = {
  487. final define:String;
  488. final doc:String;
  489. /**
  490. External resources for more information about this define.
  491. **/
  492. @:optional final links:Array<String>;
  493. /**
  494. List (small description) of parameters that this define accepts.
  495. **/
  496. @:optional final params:Array<String>;
  497. /**
  498. Haxe target(s) for which this define is used.
  499. **/
  500. @:optional final platforms:Array<Platform>;
  501. }
  502. typedef CompilerConfiguration = {
  503. /**
  504. The version integer of the current Haxe compiler build.
  505. **/
  506. final version:Int;
  507. /**
  508. Returns an array of the arguments passed to the compiler from either the `.hxml` file or the command line.
  509. **/
  510. final args:Array<String>;
  511. /**
  512. If `--debug` mode is enabled, this is `true`.
  513. **/
  514. final debug:Bool;
  515. /**
  516. If `--verbose` mode is enabled, this is `true`.
  517. **/
  518. final verbose:Bool;
  519. /**
  520. If `--no-opt` is enabled, this is `false`.
  521. **/
  522. final foptimize:Bool;
  523. /**
  524. The target platform.
  525. **/
  526. final platform:Platform;
  527. /**
  528. The compilation configuration for the target platform.
  529. **/
  530. final platformConfig:PlatformConfig;
  531. /**
  532. A list of paths being used for the standard library.
  533. **/
  534. final stdPath:Array<String>;
  535. /**
  536. The path of the class passed using the `-main` argument.
  537. **/
  538. final mainClass:TypePath;
  539. /**
  540. Special access rules for packages depending on the compiler configuration.
  541. For example, the "java" package is "Forbidden" when the target platform is Python.
  542. **/
  543. final packageRules:Map<String, PackageRule>;
  544. }
  545. enum Platform {
  546. Cross;
  547. Js;
  548. Lua;
  549. Neko;
  550. Flash;
  551. Php;
  552. Cpp;
  553. Jvm;
  554. Python;
  555. Hl;
  556. Eval;
  557. CustomTarget(name:String);
  558. }
  559. enum PackageRule {
  560. Forbidden;
  561. Directory(path:String);
  562. Remap(path:String);
  563. }