[vala/staging] vala: Move setting of default defines for VALA_0_XX and GLIB_2_XX to Context
- From: Rico Tzschichholz <ricotz src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [vala/staging] vala: Move setting of default defines for VALA_0_XX and GLIB_2_XX to Context
- Date: Sat, 24 Nov 2018 19:12:15 +0000 (UTC)
commit 3d926c1288b3ec4fd692dd1de6b91fb6c2090183
Author: Rico Tzschichholz <ricotz ubuntu com>
Date: Sat Nov 24 13:43:21 2018 +0100
vala: Move setting of default defines for VALA_0_XX and GLIB_2_XX to Context
This avoids duplicated code and reduces the setup burden for library users.
Also start to warn about redefinitions, especially for our default defines.
compiler/valacompiler.vala | 20 ++----------
vala/valacodecontext.vala | 79 ++++++++++++++++++++++++++++++++++++++++------
valadoc/treebuilder.vala | 27 ++--------------
3 files changed, 73 insertions(+), 53 deletions(-)
---
diff --git a/compiler/valacompiler.vala b/compiler/valacompiler.vala
index 405a4965e..e0b02060e 100644
--- a/compiler/valacompiler.vala
+++ b/compiler/valacompiler.vala
@@ -300,30 +300,14 @@ class Vala.Compiler {
}
}
- for (int i = 2; i <= 44; i += 2) {
- context.add_define ("VALA_0_%d".printf (i));
- }
-
if (context.profile == Profile.POSIX) {
if (!nostdpkg) {
/* default package */
context.add_external_package ("posix");
}
} else if (context.profile == Profile.GOBJECT) {
- int glib_major = 2;
- int glib_minor = 40;
- if (target_glib != null && target_glib.scanf ("%d.%d", out glib_major, out
glib_minor) != 2) {
- Report.error (null, "Invalid format for --target-glib");
- }
-
- context.target_glib_major = glib_major;
- context.target_glib_minor = glib_minor;
- if (context.target_glib_major != 2) {
- Report.error (null, "This version of valac only supports GLib 2");
- }
-
- for (int i = 16; i <= glib_minor; i += 2) {
- context.add_define ("GLIB_2_%d".printf (i));
+ if (target_glib != null) {
+ context.set_target_glib_version (target_glib);
}
if (!nostdpkg) {
diff --git a/vala/valacodecontext.vala b/vala/valacodecontext.vala
index 65a5e2ab9..815899cee 100644
--- a/vala/valacodecontext.vala
+++ b/vala/valacodecontext.vala
@@ -165,16 +165,6 @@ public class Vala.CodeContext {
public Profile profile { get; set; }
- /**
- * Target major version number of glib for code generation.
- */
- public int target_glib_major { get; set; }
-
- /**
- * Target minor version number of glib for code generation.
- */
- public int target_glib_minor { get; set; }
-
public bool verbose_mode { get; set; }
public bool version_header { get; set; }
@@ -222,6 +212,9 @@ public class Vala.CodeContext {
static StaticPrivate context_stack_key = StaticPrivate ();
+ int target_glib_major;
+ int target_glib_minor;
+
/**
* The root namespace of the symbol tree.
*/
@@ -246,6 +239,8 @@ public class Vala.CodeContext {
public UsedAttr used_attr { get; set; }
public CodeContext () {
+ add_default_defines ();
+
resolver = new SymbolResolver ();
analyzer = new SemanticAnalyzer ();
flow_analyzer = new FlowAnalyzer ();
@@ -511,6 +506,14 @@ public class Vala.CodeContext {
}
public void add_define (string define) {
+ if (is_defined (define)) {
+ Report.warning (null, "`%s' is already defined".printf (define));
+ if (/VALA_0_\d+/.match_all (define)) {
+ Report.warning (null, "`VALA_0_XX' defines are automatically added up to
current compiler version in use");
+ } else if (/GLIB_2_\d+/.match_all (define)) {
+ Report.warning (null, "`GLIB_2_XX' defines are automatically added up to
targeted glib version");
+ }
+ }
defines.add (define);
}
@@ -518,6 +521,62 @@ public class Vala.CodeContext {
return (define in defines);
}
+ void add_default_defines () {
+ int api_major = 0;
+ int api_minor = 0;
+
+ if (API_VERSION.scanf ("%d.%d", out api_major, out api_minor) != 2
+ || api_major > 0
+ || api_minor % 2 != 0) {
+ Report.error (null, "Invalid format for Vala.API_VERSION");
+ return;
+ }
+
+ for (int i = 2; i <= api_minor; i += 2) {
+ defines.add ("VALA_0_%d".printf (i));
+ }
+
+ target_glib_major = 2;
+ target_glib_minor = 40;
+
+ for (int i = 16; i <= target_glib_minor; i += 2) {
+ defines.add ("GLIB_2_%d".printf (i));
+ }
+ }
+
+ /**
+ * Set the target version of glib for code generation.
+ *
+ * This may be called once or not at all.
+ *
+ * @param target_glib a string of the format "%d.%d"
+ */
+ public void set_target_glib_version (string target_glib) {
+ int glib_major = target_glib_major;
+ int glib_minor = target_glib_minor;
+
+ if (target_glib != null && target_glib.scanf ("%d.%d", out glib_major, out glib_minor) != 2
+ || glib_minor % 2 != 0) {
+ Report.error (null, "Invalid format or version for target GLib");
+ }
+
+ if (glib_major != 2) {
+ Report.error (null, "This version of valac only supports GLib 2");
+ }
+
+ if (glib_minor <= target_glib_minor) {
+ // no additional defines needed
+ return;
+ }
+
+ for (int i = target_glib_major + 2; i <= glib_minor; i += 2) {
+ defines.add ("GLIB_2_%d".printf (i));
+ }
+
+ target_glib_major = glib_minor;
+ target_glib_minor = glib_major;
+ }
+
public string? get_vapi_path (string pkg) {
var path = get_file_path (pkg + ".vapi", "vala" + Config.PACKAGE_SUFFIX + "/vapi",
"vala/vapi", vapi_directories);
diff --git a/valadoc/treebuilder.vala b/valadoc/treebuilder.vala
index 5c76aafcc..7c35905dc 100644
--- a/valadoc/treebuilder.vala
+++ b/valadoc/treebuilder.vala
@@ -898,32 +898,9 @@ public class Valadoc.Drivers.TreeBuilder : Vala.CodeVisitor {
}
}
- for (int i = 2; i <= 44; i += 2) {
- context.add_define ("VALA_0_%d".printf (i));
- }
-
if (context.profile == Vala.Profile.GOBJECT) {
- int glib_major = 2;
- int glib_minor = 40;
-
- context.target_glib_major = glib_major;
- context.target_glib_minor = glib_minor;
- if (context.target_glib_major != 2) {
- Vala.Report.error (null, "This version of valac only supports GLib 2");
- }
-
- if (settings.target_glib != null && settings.target_glib.scanf ("%d.%d", out
glib_major, out glib_minor) != 2) {
- Vala.Report.error (null, "Invalid format for --target-glib");
- }
-
- context.target_glib_major = glib_major;
- context.target_glib_minor = glib_minor;
- if (context.target_glib_major != 2) {
- Vala.Report.error (null, "This version of valac only supports GLib 2");
- }
-
- for (int i = 16; i <= glib_minor; i += 2) {
- context.add_define ("GLIB_2_%d".printf (i));
+ if (settings.target_glib != null) {
+ context.set_target_glib_version (settings.target_glib);
}
// default packages
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]