1 
2 module llvm.c.versions;
3 
4 private
5 {
6 	import std.conv : to;
7 	import std.algorithm;
8 	import std.range;
9 }
10 
11 /// Makes an ordered identifier from a major, minor, and patch number
12 pure nothrow @nogc
13 ulong LLVMDVersion(ushort major, ushort minor, ushort patch)
14 {
15 	return cast(ulong)(major) << (ushort.sizeof*2*8) | cast(ulong)(minor) << (ushort.sizeof*8) | cast(ulong)(patch);
16 }
17 
18 private enum KnownVersions = [
19 	[3,8,0],
20 	[3,7,1],
21 	[3,7,0],
22 	[3,6,2],
23 	[3,6,1],
24 	[3,6,0],
25 	[3,5,2],
26 	[3,5,1],
27 	[3,5,0],
28 	[3,4,2],
29 	[3,4,1],
30 	[3,4,0],
31 	[3,3,0],
32 	[3,2,0],
33 	[3,1,0],
34 ];
35 
36 mixin(KnownVersions.map!(ver =>
37 	q{version(LLVM_%MAJOR_%MINOR_%PATCH) {
38 			enum LLVM_VERSION_MAJOR = %MAJOR;
39 			enum LLVM_VERSION_MINOR = %MINOR;
40 			enum LLVM_VERSION_PATCH = %PATCH;
41 		}}.replace("%MAJOR", ver[0].to!string).replace("%MINOR", ver[1].to!string).replace("%PATCH", ver[2].to!string)
42 	).join("else\n") ~
43 	q{else {
44 		enum LLVM_VERSION_MAJOR = KnownVersions[0][0];
45 		enum LLVM_VERSION_MINOR = KnownVersions[0][1];
46 		enum LLVM_VERSION_PATCH = KnownVersions[0][2];
47 	}}
48 );
49 
50 /// LLVM Version that LLVM-D was compiled with
51 enum LLVM_Version = LLVMDVersion(LLVM_VERSION_MAJOR, LLVM_VERSION_MINOR, LLVM_VERSION_PATCH);
52 /// ditto
53 enum LLVM_VersionString = LLVM_VERSION_MAJOR.to!string ~ "." ~ LLVM_VERSION_MINOR.to!string ~ "." ~ LLVM_VERSION_PATCH.to!string;
54 
55 // Qualifiers are expressed in the form ["+", "3", "2, "0", "-", "3", "5", "0"].
56 // The example translates to "added in LLVM 3.2.0 and removed in LLVM 3.5.0"
57 bool matchVersionQualifiers(string[] qualifiers)
58 {
59 	while(qualifiers.length > 0)
60 	{
61 		string op = qualifiers[0];
62 		ulong ver = LLVMDVersion(qualifiers[1].to!ushort, qualifiers[2].to!ushort, qualifiers[3].to!ushort);
63 		if((op == "+" && LLVM_Version < ver) ||
64 		   (op == "-" && LLVM_Version >= ver))
65 		{
66 			return false;
67 		}
68 		qualifiers = qualifiers[4 .. $];
69 	}
70 	return true;
71 }