/c2d/idl2d.d
D | 3041 lines | 2653 code | 237 blank | 151 comment | 678 complexity | f180be7a2979a478fd412b67f0d89ae4 MD5 | raw file
Possible License(s): AGPL-3.0
Large files files are truncated, but you can click here to view the full file
- // This file is part of Visual D
- //
- // Visual D integrates the D programming language into Visual Studio
- // Copyright (c) 2010 by Rainer Schuetze, All Rights Reserved
- //
- // Distributed under the Boost Software License, Version 1.0.
- // See accompanying file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt
- //
- ///////////////////////////////////////////////////////////////////////
- //
- // idl2d - convert IDL or header files to D
- //
- //
- //
- module c2d.idl2d;
-
- import c2d.tokenizer;
- import c2d.tokutil;
- import c2d.dgutil;
-
- import std.string;
- import std.file;
- import std.path;
- import std.stdio;
- import std.ascii;
- import std.algorithm;
- import std.getopt;
- import std.utf;
- import std.array;
- import std.windows.charset;
- import core.memory;
-
- version = remove_pp;
- version = static_if_to_version;
- version = vsi;
- version = macro2template;
- version = targetD2;
- //version = Win8;
-
- class Source
- {
- string filename;
- string text;
- TokenList tokens;
- }
-
- // endsWith does not work reliable and crashes on page end
- bool _endsWith(string s, string e)
- {
- return (s.length >= e.length && s[$-e.length .. $] == e);
- }
-
- alias std.string.indexOf indexOf;
-
- class idl2d
- {
- ///////////////////////////////////////////////////////
- // configuration
- version(Win8)
- {
- string vsi_base_path = r"c:\l\vs9SDK"; // r"c:\l\vs9SDK";
- string dte_path = r"m:\s\d\visuald\trunk\sdk\vsi\idl\";
- string win_path = r"c:\l\vs11\Windows Kits\8.0\Include\";
- string sdk_d_path = r"m:\s\d\visuald\trunk\sdk\";
- }
- else version(all)
- {
- string vsi_base_path;
- string dte_path;
- string win_path;
- string sdk_d_path;
- }
- else version(all)
- {
- string vsi_base_path = r"c:\l\vs9SDK";
- string dte_path = r"m:\s\d\visuald\trunk\sdk\vsi\idl\";
- string win_path = r"c:\Programme\Microsoft SDKs\Windows\v6.0A\Include\";
- string sdk_d_path = r"m:\s\d\visuald\trunk\sdk\";
- }
- else
- {
- string vsi_base_path = r"c:\Program Files\Microsoft Visual Studio 2010 SDK"; // r"c:\l\vs9SDK";
- string dte_path = r"c:\s\d\visuald\trunk\sdk\vsi\idl\";
- string win_path = r"c:\Program Files\Microsoft SDKs\Windows\v7.1\Include\";
- string sdk_d_path = r"c:\s\d\visuald\trunk\sdk\";
- }
-
- static const string dirVSI = "vsi";
- static const string dirWin = "win32";
-
- string packageVSI = "sdk." ~ dirVSI ~ ".";
- string packageWin = "sdk." ~ dirWin ~ ".";
- string packageNF = "sdk.port.";
- string keywordPrefix = "sdk_";
-
- string vsi_path; // = vsi_base_path ~ r"\VisualStudioIntegration\Common\IDL\";
- string vsi_hpath; // = vsi_base_path ~ r"\VisualStudioIntegration\Common\Inc\";
-
- string vsi_d_path; // = sdk_d_path ~ r"vsi\";
- string win_d_path; // = sdk_d_path ~ r"win32\";
-
- string[] win_idl_files;
- string[] vsi_idl_files;
- string[] vsi_h_files;
- string[] dte_idl_files;
-
- version(vsi) bool vsi = true;
- else bool vsi = false;
-
- void initFiles()
- {
- win_idl_files = [ "windef.h", "sdkddkver.h", "basetsd.h", "ntstatus.h",
- "winnt.h", "winbase.h", "winuser.h", "ktmtypes.h",
- "winerror.h", "winreg.h", "reason.h", "commctrl.h",
- "wingdi.h", "prsht.h",
- "iphlpapi.h", "iprtrmib.h", "ipexport.h", "iptypes.h", "tcpestats.h",
- /*"inaddr.h", "in6addr.h",*/
- "ipifcons.h", "ipmib.h", "tcpmib.h", "udpmib.h",
- "ifmib.h", "ifdef.h", "nldef.h", "winnls.h",
- "shellapi.h", "rpcdce.h" /*, "rpcdcep.h"*/ ];
-
- win_idl_files ~= [ "unknwn.idl", "oaidl.idl", "wtypes.idl", "oleidl.idl",
- "ocidl.idl", "objidl.idl", "docobj.idl", "oleauto.h", "objbase.h",
- "mshtmcid.h", "xmldom.idl", "xmldso.idl", "xmldomdid.h", "xmldsodid.h", "idispids.h",
- "activdbg.id*", "activscp.id*", "dbgprop.id*", // only available in Windows SDK v7.x
- ];
-
- // only available (and are required for successfull compilation) in Windows SDK v8
- foreach(f; [ "wtypesbase.idl",
- //"winapifamily.h", "apisetcconv.h", "apiset.h", // commented because it is difficult to convert this file
- "minwinbase.h", "processenv.h",
- "minwindef.h", "fileapi.h", "debugapi.h", "handleapi.h", "errhandlingapi.h",
- "fibersapi.h", "namedpipeapi.h", "profileapi.h", "heapapi.h", "synchapi.h",
- "interlockedapi.h", "processthreadsapi.h", "sysinfoapi.h", "memoryapi.h",
- "threadpoollegacyapiset.h", "utilapiset.h", "ioapiset.h",
- "threadpoolprivateapiset.h", "threadpoolapiset.h", "bemapiset.h", "wow64apiset.h",
- "jobapi.h", "timezoneapi.h", "datetimeapi.h", "stringapiset.h",
- "libloaderapi.h", "securitybaseapi.h", "namespaceapi.h", "systemtopologyapi.h", "processtopologyapi.h",
- "securityappcontainer.h", "realtimeapiset.h", "unknwnbase.idl", "objidlbase.idl", "combaseapi.h",
- // Win SDK 8.1
- "mprapidef.h", "lmerr.h", "lmcons.h",
- // Win SDK 10.0
- "coml2api.h", "jobapi2.h", "propidlbase.idl",
- // Win SDK 10.0.10586.0
- "enclaveapi.h",
- // Win SDK 10.0.14393.0
- "dpa_dsa.h",
- // Win SDK 10.0.18362.0
- "fileapifromapp.h",
- ])
- win_idl_files ~= f ~ "*"; // make it optional
-
- if(vsi)
- {
- vsi_idl_files = [ "shared.idh", "vsshell.idl", "*.idl", "*.idh" ];
- vsi_h_files = [ "completionuuids.h", "contextuuids.h", "textmgruuids.h", "vsshelluuids.h", "vsdbgcmd.h",
- "venusids.h", "stdidcmd.h", "vsshlids.h", "mnuhelpids.h", "WCFReferencesIds.h",
- "vsdebugguids.h", "VSRegKeyNames.h", "SCGuids.h", "wbids.h", "sharedids.h",
- "vseeguids.h", "version.h", "scc.h",
- "vsplatformuiuuids.*", // only in VS2010 SDK
- "vscookie.*", // only in later VS2017 SDK
- // no longer in SDK2010: "DSLToolsCmdID.h",
- ];
-
- dte_idl_files = [ "*.idl" ];
- }
- }
-
- // see also preDefined, isExpressionToken, convertDefine, convertText, translateToken
- ///////////////////////////////////////////////////////
-
- string[string] tokImports;
- int[string] disabled_defines;
- int[string] disabled_ifdef;
- string[string] converted_defines;
- bool[] pp_enable_stack;
- string[] elif_braces_stack;
- bool convert_next_cpp_quote = true;
- bool cpp_quote_in_comment = false;
- bool[string] classes;
- string[string] aliases;
- bool[string] enums;
-
- string[] currentImports;
- string[] addedImports;
-
- void reinsert_cpp_quote(ref TokenIterator tokIt)
- {
- TokenIterator it = tokIt;
- string text;
- while(!it.atEnd() && it.text == "cpp_quote")
- {
- assert(it[1].text == "(");
- assert(it[2].type == Token.String);
- assert(it[3].text == ")");
- text ~= it.pretext;
- text ~= strip(it[2].text[1..$-1]);
- it += 4;
- }
- bool endsWithBS = text.endsWith("\\") != 0;
- bool quote = text.indexOf("\\\n") >= 0 || endsWithBS || !convert_next_cpp_quote;
- if(quote)
- text = tokIt.pretext ~ "/+" ~ text[tokIt.pretext.length .. $] ~ "+/";
- convert_next_cpp_quote = !endsWithBS;
-
- TokenList tokens = scanText(text, tokIt.lineno, true);
- tokIt.eraseUntil(it);
- tokIt = insertTokenList(tokIt, tokens);
- }
-
- bool handle_cpp_quote(ref TokenIterator tokIt, bool inEnum)
- {
- // tokIt on "cpp_quote"
- TokenIterator it = tokIt;
- assert(it[1].text == "(");
- assert(it[2].type == Token.String);
- assert(it[3].text == ")");
- string text = strip(it[2].text[1..$-1]);
-
- string txt = text;
- bool convert = convert_next_cpp_quote;
- convert_next_cpp_quote = true;
-
- if(cpp_quote_in_comment || text.startsWith("/*"))
- {
- txt = replace(text, "\\\"", "\"");
- cpp_quote_in_comment = (text.indexOf("*/") < 0);
- }
- else if(text.startsWith("//"))
- txt = replace(text, "\\\"", "\"");
- else if(text.endsWith("\\")) // do not convert multi-line #define
- {
- convert_next_cpp_quote = false;
- convert = false;
- }
- else if(text.startsWith("#"))
- {
- txt = replace(txt, "\\\"", "\"");
- txt = convertPP(txt, tokIt.lineno, inEnum);
- }
-
- if(convert)
- {
- string pretext = tokIt.pretext;
- tokIt.erase();
- tokIt.erase();
- tokIt.erase();
- tokIt.erase();
-
- txt = cpp_string(txt);
- TokenList tokens = scanText(txt, tokIt.lineno, false);
- tokIt = insertTokenList(tokIt, tokens);
- tokIt.pretext = pretext ~ tokIt.pretext;
- }
- else
- tokIt.pretext ~= "// ";
-
- return convert;
- }
-
- void reinsertTextTokens(ref TokenIterator tokIt, string text)
- {
- string pretext;
- if(!tokIt.atEnd())
- {
- pretext = tokIt.pretext;
- tokIt.erase();
- }
- TokenList tokens = scanText(text, tokIt.lineno, false);
- tokIt = insertTokenList(tokIt, tokens);
- tokIt.pretext = pretext ~ tokIt.pretext;
- }
-
- bool isExpressionToken(TokenIterator tokIt, bool first)
- {
- int type = tokIt.type;
- switch(type)
- {
- case Token.Identifier:
- switch(tokIt.text)
- {
- case "_far":
- case "_pascal":
- case "_cdecl":
- case "void":
- return false;
- default:
- return !(tokIt.text in disabled_defines);
- }
- case Token.String:
- case Token.Number:
- case Token.ParenL:
- case Token.BracketL:
- case Token.BraceL:
- return true;
- case Token.ParenR:
- case Token.BracketR:
- case Token.BraceR:
- if(!first)
- return tokIt.type != Token.Identifier && tokIt.type != Token.Number && tokIt.type != Token.ParenL;
- return !first;
- case Token.Equal:
- case Token.Unequal:
- case Token.LessThan:
- case Token.LessEq:
- case Token.GreaterThan:
- case Token.GreaterEq:
- case Token.Shl:
- case Token.Shr:
- case Token.Ampersand:
- case Token.Assign:
- case Token.Dot:
- case Token.Div:
- case Token.Mod:
- case Token.Xor:
- case Token.Or:
- case Token.OrOr:
- case Token.AmpAmpersand:
- return !first;
- case Token.Plus:
- case Token.Minus:
- case Token.Asterisk:
- case Token.Tilde:
- return true; // can be unary or binary operator
- case Token.Colon:
- case Token.Question:
- if(vsi)
- goto default;
- return !first;
-
- case Token.Comma:
- return !first && !(tokIt + 1).atEnd() && tokIt[1].type != Token.EOF;
-
- case Token.Struct:
- // struct at beginning of a cast?
- if(!tokIt.atBegin() && tokIt[-1].type == Token.ParenL)
- return true;
- return false;
-
- default:
- return false;
- }
- }
-
- bool hasBalancedBrackets(TokenIterator start, TokenIterator end)
- {
- int[] brackets;
- while (start != end)
- {
- int type = start.type;
- switch(type)
- {
- case Token.ParenL:
- case Token.BracketL:
- case Token.BraceL:
- brackets ~= type;
- break;
- case Token.ParenR:
- if (brackets.empty || brackets[$-1] != Token.ParenL)
- return false;
- brackets = brackets[0..$-1];
- break;
- case Token.BracketR:
- if (brackets.empty || brackets[$-1] != Token.BracketL)
- return false;
- brackets = brackets[0..$-1];
- break;
- case Token.BraceR:
- if (brackets.empty || brackets[$-1] != Token.BraceL)
- return false;
- brackets = brackets[0..$-1];
- break;
- default:
- break;
- }
- start++;
- }
- return brackets.empty;
- }
-
- bool isExpression(TokenIterator start, TokenIterator end)
- {
- if(start == end || start.type == Token.EOF)
- return false;
- if(!isExpressionToken(start, true))
- return false;
- for(TokenIterator it = start + 1; it != end && !it.atEnd() && it.type != Token.EOF; ++it)
- if(!isExpressionToken(it, false))
- return false;
- return hasBalancedBrackets(start, end);
- }
-
- bool isPrimaryExpr(TokenIterator it)
- {
- if(it.atEnd())
- return false;
- if(it.text == "(" || it.type == Token.Number || it.type == Token.Identifier)
- return true;
- return false;
- }
-
- string getExpressionType(string ident, TokenIterator start, TokenIterator end)
- {
- while(start != end && start.text == "(" && start[1].text == "(")
- ++start;
- if(start.text == "(" && start[1].type == Token.Identifier && start[2].text == ")" && isPrimaryExpr(start + 3))
- return start[1].text;
- if(start.text == "(" && start[1].type == Token.Identifier && start[2].text == "*" && start[3].text == ")"
- && isPrimaryExpr(start + 4))
- return start[1].text ~ start[2].text;
- if(start.text == "(" && start[1].text == "struct" && start[2].type == Token.Identifier && start[3].text == "*" && start[4].text == ")"
- && isPrimaryExpr(start + 5))
- return start[2].text ~ start[3].text;
- return "int";
- }
-
- string getArgumentType(string ident, TokenIterator start, TokenIterator end, string rettype)
- {
- switch(ident)
- {
- case "IS_INTRESOURCE":
- case "MAKEINTRESOURCEA":
- case "MAKEINTRESOURCEW":
- case "MAKEINTATOM":
- return "int";
- default:
- return rettype;
- }
- }
-
- void collectClasses(TokenList tokens)
- {
- for(TokenIterator tokIt = tokens.begin(); tokIt != tokens.end; ++tokIt)
- if(tokIt.text == "class" || tokIt.text == "interface" || tokIt.text == "coclass")
- classes[tokIt[1].text] = true;
- }
-
- bool isClassIdentifier(string ident)
- {
- if(ident in classes)
- return true;
- return false;
- }
-
- // 1: yes, 0: undecided, -1: no
- int _preDefined(string cond)
- {
- switch(cond)
- {
- case "FALSE":
- return -2; // not defined for expression, but for #define
-
- case "_WIN64":
- return 4; // special cased
- case "0":
- case "MAC":
- case "_MAC":
- case "_WIN32_WCE":
- case "_IA64_":
- case "_M_AMD64":
- case "RC_INVOKED":
- case "MIDL_PASS":
- case "DO_NO_IMPORTS":
- case "_IMM_":
- case "NONAMELESSUNION":
- case "WIN16":
- case "INTEROPLIB":
- case "__INDENTSTYLE__":
- case "__CTC__":
- case "_CTC_GUIDS_":
- case "CTC_INVOKED":
- case "VS_PACKAGE_INCLUDE":
- case "URTBUILD":
- case "NOGUIDS":
- case "SHOW_INCLUDES":
- case "RGS_INVOKED":
- case "__RE_E_DEFINED__":
- case "OLE2ANSI":
-
- // for winbase
- case "STRICT":
- case "_M_CEE":
- case "_M_CEE_PURE":
- case "_DCOM_OA_REMOTING_":
- case "_DCOM_OC_REMOTING_":
- case "_SLIST_HEADER_":
- case "_RTL_RUN_ONCE_DEF":
- case "__midl":
-
- // Windows SDK 8.0
- case "NOAPISET":
- return -1;
-
- case "WINAPI":
- case "WINAPI_INLINE":
- case "APIENTRY":
- case "NTAPI":
- case "NTAPI_INLINE":
- case "interface":
- case "PtrToPtr64":
- case "Ptr64ToPtr":
- case "HandleToHandle64":
- case "Handle64ToHandle":
- return 3; // predefined for #define, but not in normal text
- case "TRUE":
- return 2; // predefined for expression, but not for #define
- case "1":
- case "__cplusplus":
- case "UNICODE":
- case "DEFINE_GUID":
- case "UNIX":
- case "_X86_":
- case "_M_IX86":
- case "MULTIPLE_WATCH_WINDOWS":
- case "PROXYSTUB_BUILD":
- case "(defined(_WIN32)||defined(_WIN64))&&!defined(OLE2ANSI)":
- case "defined(_INTEGRAL_MAX_BITS)&&_INTEGRAL_MAX_BITS>=64": // needed to define LONGLONG
- case "!defined SENTINEL_Reason": // reason.h
-
- //case "!defined(CTC_INVOKED)&&!defined(RGS_INVOKED)":
- //case "!defined(_DCOM_OA_REMOTING_)&&!defined(_DCOM_OC_REMOTING_)":
- //case "!defined(_DCOM_OA_REMOTING_)":
- //case "!defined(_DCOM_OC_REMOTING_)":
- case "_HRESULT_DEFINED":
- // case "_PALETTEENTRY_DEFINED":
- // case "_LOGPALETTE_DEFINED":
- case "_REFPOINTS_DEFINED":
- case "COMBOX_SANDBOX":
-
- // defined to avoid #define translation
- case "MAKE_HRESULT":
- case "CBPCLIPDATA":
- //case "FACILITY_ITF":
- case "PFN_TSHELL_TMP":
- case "V_INT_PTR":
- case "VT_INT_PTR":
- case "V_UINT_PTR":
- case "VT_UINT_PTR":
- case "PKGRESETFLAGS":
- case "VSLOCALREGISTRYROOTHANDLE_TO_HKEY":
- case "DTE":
- case "Project":
- case "ProjectItem":
- case "CodeModel":
- case "FileCodeModel":
- case "IDebugMachine2_V7":
- case "EnumMachines_V7":
- case "IEnumDebugMachines2_V7":
- case "IID_IEnumDebugMachines2_V7":
-
- // defined with both enum and #define in ipimb.h
- case "MIB_IPROUTE_TYPE_OTHER":
- case "MIB_IPROUTE_TYPE_INVALID":
- case "MIB_IPROUTE_TYPE_DIRECT":
- case "MIB_IPROUTE_TYPE_INDIRECT":
-
- case "NULL":
- case "VOID":
- case "CONST":
- case "CALLBACK":
- case "NOP_FUNCTION":
- case "DECLARE_HANDLE":
- case "STDMETHODCALLTYPE":
- case "STDMETHODVCALLTYPE":
- case "STDAPICALLTYPE":
- case "STDAPIVCALLTYPE":
- case "STDMETHODIMP":
- case "STDMETHODIMP_":
- case "STDOVERRIDEMETHODIMP":
- case "STDOVERRIDEMETHODIMP_":
- case "IFACEMETHODIMP":
- case "IFACEMETHODIMP_":
- case "STDMETHODIMPV":
- case "STDMETHODIMPV_":
- case "STDOVERRIDEMETHODIMPV":
- case "STDOVERRIDEMETHODIMPV_":
- case "IFACEMETHODIMPV":
- case "IFACEMETHODIMPV_":
- case "_WIN32_WINNT":
- case "GetLastError":
- case "MF_END": // defined twice in winuser.h, but said to be obsolete
- case "__int3264":
- return 1;
-
- case "_NO_SCRIPT_GUIDS": // used in activdbg.h, disable to avoid duplicate GUID definitions
- case "EnumStackFramesEx": // used in activdbg.h, but in wrong scope
- case "SynchronousCallIntoThread": // used in activdbg.h, but in wrong scope
- return 1;
-
- // winnt.h
- case "_WINDEF_":
- case "_WINBASE_":
- //if(vsi)
- // return 1;
- break;
- case "_WIN32":
- //if(!vsi)
- return 1;
- //break;
-
- // Windows SDK 8.0
- case "_CONTRACT_GEN":
- return -1;
-
- // Windows SDK 10.0.15063.0
- case "RPC_UNICODE_SUPPORTED":
- return 1;
- case "_M_HYBRID_X86_ARM64":
- return -1;
-
- // Windows SDK 10.0.17134.0
- case "IMAGE_POLICY_METADATA_NAME":
- return 1;
-
- // SDK 10.0.17763.0
- case "EN_SEARCHWEB":
- case "RtlGetNonVolatileToken":
- case "RtlFreeNonVolatileToken":
- case "RtlFlushNonVolatileMemory":
- case "RtlDrainNonVolatileFlush":
- case "RtlWriteNonVolatileMemory":
- case "RtlFlushNonVolatileMemoryRanges":
- return 1;
-
- default:
- break;
- }
-
- // header double include protection
- if(_endsWith(cond, "_DEFINED") ||
- _endsWith(cond, "_INCLUDED") ||
- _endsWith(cond, "_h__") ||
- _endsWith(cond, "_H__") ||
- _endsWith(cond, "_H_") ||
- startsWith(cond, "_INC_") ||
- _endsWith(cond, "_IDH"))
- return -1;
-
- if(cond == "_" ~ toUpper(currentModule) ~ "_")
- return -1;
-
- if(startsWith(cond, "WINAPI_FAMILY_PARTITION"))
- return 1;
-
- if(indexOf(cond, "(") < 0 && indexOf(cond, "|") < 0 && indexOf(cond, "&") < 0)
- {
- if (startsWith(cond, "CMD_ZOOM_"))
- return 1;
-
- cond = cond.replace(" ", "");
- if(startsWith(cond, "WINVER>") || startsWith(cond, "_WIN32_WINNT>") || startsWith(cond, "NTDDI_VERSION>"))
- return 1;
- if(startsWith(cond, "WINVER<") || startsWith(cond, "_WIN32_WINNT<") || startsWith(cond, "NTDDI_VERSION<"))
- return -1;
- if(startsWith(cond, "_MSC_VER>") || startsWith(cond, "_MSC_FULL_VER>"))
- return -1; // disable all msc specials
- if(startsWith(cond, "_MSC_VER<") || startsWith(cond, "_MSC_FULL_VER<"))
- return 1; // disable all msc specials
- if(startsWith(cond, "_WIN32_IE>"))
- return 1; // assue newest IE
- if(startsWith(cond, "NO"))
- return -1; // used to disable parts, we want it all
- }
- return 0;
- }
-
- int findLogicalOp(string cond, string op)
- {
- int paren = 0;
- for(int i = 0; i <= cond.length - op.length; i++)
- {
- if(paren == 0 && cond[i .. i+op.length] == op)
- return i;
- if(cond[i] == '(')
- paren++;
- else if(cond[i] == ')')
- paren--;
- }
- return -1;
- }
-
- int preDefined(string cond)
- {
- int sign = 1;
-
- for( ; ; )
- {
- int rc = _preDefined(cond);
- if(rc != 0)
- return sign * rc;
-
- if(startsWith(cond, "(") && _endsWith(cond, ")") && findLogicalOp(cond[1..$], ")") == cond.length - 2)
- cond = cond[1..$-1];
- else if(startsWith(cond, "defined(") && findLogicalOp(cond[8..$], ")") == cond.length - 9)
- cond = cond[8..$-1];
- else if(startsWith(cond, "!") && indexOf(cond[1..$-1], "&") < 0 && indexOf(cond[1..$-1], "|") < 0)
- {
- cond = cond[1..$];
- sign = -sign;
- }
- else
- {
- int idx = findLogicalOp(cond, "||");
- if(idx < 0)
- idx = findLogicalOp(cond, "&&");
- if(idx >= 0)
- {
- int rc1 = preDefined(cond[0..idx]);
- int rc2 = preDefined(cond[idx+2..$]);
- if(cond[idx] == '|')
- return rc1 > 0 || rc2 > 0 ? 1 : rc1 < 0 && rc2 < 0 ? -1 : 0;
- else // '&'
- return rc1 > 0 && rc2 > 0 ? 1 : rc1 < 0 || rc2 < 0 ? -1 : 0;
- }
- break;
- }
- }
- return 0;
- }
-
- int preDefined(TokenIterator start, TokenIterator end)
- {
- string txt = tokenListToString(start, end, false, true);
- int rc = preDefined(txt);
-
- if(rc == 0 && verbose)
- writefln("\"" ~ txt ~ "\" not defined/undefined");
- return rc;
- }
-
- void handleCondition(TokenIterator tokIt, TokenIterator lastIt, string pp)
- {
- string elif_braces;
- int predef = preDefined(tokIt + 1, lastIt + 1);
- if(pp == "pp_ifndef")
- predef = -predef;
- if(predef < 0)
- {
- string ver = (predef == -4 ? "Win32" : "none");
- tokIt.text = "version(" ~ ver ~ ") /* " ~ tokIt.text;
- lastIt.text ~= " */ {/+";
- elif_braces = "+/} ";
- }
- else if(predef > 0)
- {
- string ver = (predef == 4 ? "Win64" : "all");
- tokIt.text = "version(" ~ ver ~ ") /* " ~ tokIt.text;
- lastIt.text ~= " */ {";
- elif_braces = "} ";
- }
- else
- {
- version(static_if_to_version)
- {
- string cond = pp;
- version(remove_pp)
- if(pp == "pp_ifndef")
- cond = "all";
-
- tokIt.text = "version(" ~ cond ~ ") /* " ~ tokIt.text;
- lastIt.text ~= " */ {";
- }
- else
- {
- tokIt.text = "static if(" ~ pp ~ "(r\"";
- tokIt[1].pretext = "";
- lastIt.text ~= "\")) {";
- }
- elif_braces = "} ";
- }
-
- if(pp == "pp_elif")
- {
- tokIt.text = elif_braces_stack[$-1] ~ "else " ~ tokIt.text;
- elif_braces_stack[$-1] = elif_braces;
- }
- else
- {
- elif_braces_stack ~= elif_braces;
- pp_enable_stack ~= true;
- }
-
- }
-
- bool inDisabledPPBranch()
- {
- foreach (string s; elif_braces_stack)
- if(startsWith(s, "+/"))
- return true;
- return false;
- }
-
- string convertPP(string text, int lineno, bool inEnum)
- {
- version(remove_pp) {} else
- if(inEnum)
- return "// " ~ text;
-
- TokenList tokens = scanText(text, lineno, false);
- TokenIterator tokIt = tokens.begin();
- TokenIterator lastIt = tokens.end() - 1;
- if(lastIt.type == Token.EOF)
- --lastIt;
-
- switch(tokIt.text)
- {
- case "#include":
- tokIt.text = "public import";
- if(tokIt[1].type == Token.String)
- tokIt[1].text = fixImport(tokIt[1].text) ~ ";";
- else if(tokIt[1].text == "<")
- {
- string inc;
- TokenIterator it = tokIt + 2;
- for( ; !it.atEnd() && it.text != ">"; it.erase())
- inc ~= it.pretext ~ it.text;
-
- tokIt[1].text = fixImport(inc);
- if(!it.atEnd())
- it.text = ";";
- }
- break;
- case "#if":
- handleCondition(tokIt, lastIt, "pp_if");
- break;
- case "#ifndef":
- handleCondition(tokIt, lastIt, "pp_ifndef");
- break;
- case "#ifdef":
- handleCondition(tokIt, lastIt, "pp_ifdef");
- break;
- case "#endif":
- if(pp_enable_stack.length == 0)
- throwException(tokIt.lineno, "unbalanced #endif");
- bool enabled = pp_enable_stack[$-1];
- pp_enable_stack = pp_enable_stack[0 .. $-1];
- if(!enabled)
- tokIt.pretext = "+/" ~ tokIt.pretext;
- version(remove_pp)
- tokIt.text = elif_braces_stack[$-1] ~ "\n";
- else
- tokIt.text = elif_braces_stack[$-1] ~ "// " ~ tokIt.text;
- elif_braces_stack = elif_braces_stack[0 .. $-1];
- break;
- case "#else":
- if(pp_enable_stack.length == 0)
- throwException(tokIt.lineno, "unbalanced #else");
- if(!pp_enable_stack[$-1])
- {
- tokIt.pretext = "+/" ~ tokIt.pretext;
- pp_enable_stack[$-1] = true;
- }
- if(elif_braces_stack[$-1].startsWith("+/"))
- {
- version(remove_pp)
- tokIt.text = "+/} else {\n";
- else
- tokIt.text = "+/} else { // " ~ tokIt.text;
- elif_braces_stack[$-1] = elif_braces_stack[$-1][2..$];
- }
- else
- {
- version(remove_pp)
- tokIt.text = "} else {\n";
- else
- tokIt.text = "} else { // " ~ tokIt.text;
- }
- break;
- case "#elif":
- if(pp_enable_stack.length == 0)
- throwException(tokIt.lineno, "unbalanced #elif");
- if(!pp_enable_stack[$-1])
- {
- tokIt.pretext = "+/" ~ tokIt.pretext;
- pp_enable_stack[$-1] = true;
- }
- handleCondition(tokIt, lastIt, "pp_elif");
- //tokIt[1].pretext = "";
- //lastIt.text ~= "\")) {";
- break;
- case "#define":
- convertDefine(tokIt);
- break;
- default:
- return "// " ~ text;
- }
- string txt = tokenListToString(tokens);
- return txt;
- }
-
- bool convertDefine(ref TokenIterator tokIt)
- {
- // tokIt on "#define"
- bool convert = true;
- bool predef = false;
- bool convertMacro = false;
- string argtype;
- string rettype;
- TokenIterator it = tokIt + 1;
-
- string ident = it.text;
- for( ; !it.atEnd(); ++it) {}
- version(none){
- if(indexOf(it.pretext, "\\\n") >= 0)
- {
- convert = false;
- it.pretext = replace(it.pretext, "\\\n", "\\\n//");
- }
- // if(indexOf(it.pretext, '\n') >= 0)
- // break;
- }
-
- TokenIterator endIt = it;
- if(it[-1].type == Token.EOF)
- --endIt;
-
- int preDefType = preDefined(ident);
- if(ident in disabled_defines)
- predef = true;
- else if (preDefType == 3)
- {
- predef = true;
- convert = false;
- }
- else if (preDefType == 1 || preDefined("_" ~ ident ~ "_DEFINED") == 1)
- predef = true;
- else if(tokIt[2].text == "(" && tokIt[2].pretext.length == 0)
- {
- convert = false;
- if(tokIt[3].text == ")")
- {
- convertMacro = hasBalancedBrackets(tokIt + 4, endIt);
- argtype = "";
- if(isExpression(tokIt + 4, endIt))
- rettype = getExpressionType(ident, tokIt + 4, endIt);
- else
- rettype = "void";
- }
- if(tokIt[3].type == Token.Identifier && tokIt[4].text == ")" && isExpression(tokIt + 5, endIt))
- {
- convertMacro = true;
- rettype = getExpressionType(ident, tokIt + 5, endIt);
- argtype = getArgumentType(ident, tokIt + 5, endIt, rettype);
- }
- }
- else if(ident.startsWith("CF_VS"))
- {
- convertMacro = true;
- rettype = "UINT";
- }
- else if(!isExpression(tokIt + 2, endIt))
- convert = false;
- else if(string* m = ident in converted_defines)
- if(*m != currentModule)
- predef = true;
-
- if((!convert && !convertMacro) || it[-1].text == "\\" || predef)
- {
- tokIt.pretext ~= "// ";
- convert = false;
- }
-
- if(convertMacro)
- {
- TokenIterator lastit = endIt;
- version(macro2template) tokIt.text = "auto";
- else tokIt.text = rettype;
- string ret = (rettype != "void" ? "return " : "");
- if(argtype.length)
- {
- version(macro2template) tokIt[3].pretext ~= "ARG)(ARG ";
- else tokIt[3].pretext ~= argtype ~ " ";
- tokIt[5].pretext ~= "{ " ~ ret;
- lastit = tokIt + 5;
- }
- else if(tokIt[2].text != "(" || tokIt[2].pretext != "")
- {
- tokIt[2].pretext = "() { " ~ ret ~ tokIt[2].pretext;
- lastit = tokIt + 2;
- }
- else
- {
- tokIt[4].pretext = " { " ~ ret ~ tokIt[4].pretext;
- lastit = tokIt + 4;
- }
-
- if(lastit == endIt) // empty?
- endIt.text ~= " }";
- else
- endIt[-1].text ~= "; }";
-
- if(!inDisabledPPBranch())
- converted_defines[ident] = currentModule;
- }
- else if(convert)
- {
- if(it != tokIt + 1 && it != tokIt + 2 && it != tokIt + 3)
- {
- if(endIt == tokIt + 3 && tokIt[2].type == Token.Identifier &&
- !(tokIt[2].text in enums) && tokIt[2].text != "NULL")
- {
- if(tokIt[2].text in disabled_defines)
- tokIt.pretext ~= "// ";
- tokIt.text = "alias";
- tokIt[1].text = tokIt[2].text;
- tokIt[2].text = ident;
- }
- else
- {
- tokIt.text = "denum";
- (tokIt+2).insertBefore(createToken(" ", "=", Token.Assign, tokIt.lineno));
- if(ident.startsWith("uuid_"))
- {
- tokIt.insertAfter(createToken(" ", "GUID", Token.Identifier, tokIt.lineno));
- tokIt[4].pretext ~= "uuid(\"";
- endIt[-1].text ~= "\")";
- }
- else if(ident.startsWith("SID_S") || ident.startsWith("guid"))
- {
- tokIt.insertAfter(createToken(" ", "GUID", Token.Identifier, tokIt.lineno));
- }
- // winnt.h
- else if(ident.startsWith("SECURITY_") && tokIt[3].text == "{" && tokIt[15].text == "}")
- {
- tokIt.insertAfter(createToken(" ", "SID_IDENTIFIER_AUTHORITY", Token.Identifier, tokIt.lineno));
- tokIt[4].text = "{[";
- tokIt[16].text = "]}";
- }
- else if(_endsWith(ident, "_LUID") && tokIt[3].text == "{")
- {
- tokIt.insertAfter(createToken(" ", "LUID", Token.Identifier, tokIt.lineno));
- }
- }
- }
- else
- tokIt.pretext ~= "// ";
-
- Token tok = createToken("", ";", Token.Comma, tokIt.lineno);
- endIt.insertBefore(tok);
- if(!inDisabledPPBranch())
- converted_defines[ident] = currentModule;
- }
- else if (!predef)
- disabled_defines[ident] = 1;
-
- string repl = (convert || convertMacro ? "\n" : "\\\n//");
- for(it = tokIt; !it.atEnd(); ++it)
- if(indexOf(it.pretext, "\\\n") >= 0)
- it.pretext = replace(it.pretext, "\\\n", repl);
-
- tokIt = it - 1;
- return convert || convertMacro;
- }
-
- void disable_macro(ref TokenIterator tokIt)
- {
- TokenIterator it = tokIt + 1;
- if(it.text == "(")
- {
- if(!advanceToClosingBracket(it))
- return;
- }
- version(all)
- {
- tokIt.insertBefore(createToken("", "/", Token.Div, tokIt.lineno));
- tokIt.insertBefore(createToken("", "+", Token.Plus, tokIt.lineno));
- tokIt[-2].pretext = tokIt.pretext;
- tokIt.pretext = " ";
- it.insertBefore(createToken(" ", "+", Token.Plus, tokIt.lineno));
- it.insertBefore(createToken("", "/", Token.Div, tokIt.lineno));
- tokIt = it - 1;
- } else {
- tokIt.pretext ~= "/+";
- it[-1].text ~= "+/"; // it.pretext = "+/" ~ it.pretext;
- tokIt = it - 1;
- }
- }
-
- void replaceExpressionTokens(TokenList tokens)
- {
- //replaceTokenSequence(tokens, "= (", "= cast(", true);
- replaceTokenSequence(tokens, "$_not $_ident($_ident1)$_ident2", "$_not cast($_ident1)$_ident2", true);
- replaceTokenSequence(tokens, "$_not $_ident($_ident1)$_num2", "$_not cast($_ident1)$_num2", true);
- replaceTokenSequence(tokens, "$_not $_ident($_ident1)-$_num2", "$_not cast($_ident1)-$_num2", true);
- replaceTokenSequence(tokens, "$_not $_ident($_ident1)~", "$_not cast($_ident1)~", true);
- while(replaceTokenSequence(tokens, "$_not $_ident($_ident1)($expr)", "$_not cast($_ident1)($expr)", true) > 0) {}
- replaceTokenSequence(tokens, "$_not $_ident($_ident1)cast", "$_not cast($_ident1)cast", true);
- replaceTokenSequence(tokens, "$_not $_ident($_ident1*)$_not_semi;", "$_not cast($_ident1*)$_not_semi", true);
- replaceTokenSequence(tokens, "$_not $_ident(struct $_ident1*)$_not_semi;", "$_not cast(struct $_ident1*)$_not_semi", true);
- replaceTokenSequence(tokens, "$_not $_ident($_ident1 $_ident2*)", "$_not cast($_ident1 $_ident2*)", true);
- replaceTokenSequence(tokens, "HRESULT cast", "HRESULT", true);
- replaceTokenSequence(tokens, "extern cast", "extern", true);
- replaceTokenSequence(tokens, "!cast", "!", true);
- replaceTokenSequence(tokens, "reinterpret_cast<$_ident>", "cast($_ident)", true);
- replaceTokenSequence(tokens, "reinterpret_cast<$_ident*>", "cast($_ident*)", true);
- replaceTokenSequence(tokens, "const_cast<$_ident*>", "cast($_ident*)", true);
- }
-
- string translateModuleName(string name)
- {
- name = toLower(name);
- if(name == "version" || name == "shared" || name == "align")
- return keywordPrefix ~ name;
- return name;
- }
-
- string translatePackageName(string fname)
- {
- // "shared", "um" added in SDK 8.0, "um\minwin" in 10.0.10586.0
- return fname.replace("\\shared\\", "\\").replace("\\um\\", "\\").replace("\\minwin\\", "\\");
- }
-
- string translateFilename(string fname)
- {
- string name = getNameWithoutExt(fname);
- string nname = translateModuleName(name);
- if(name == nname)
- return translatePackageName(fname);
-
- string dir = dirName(fname);
- if(dir == ".")
- dir = "";
- else
- dir ~= "\\";
- string ext = extension(fname);
- return translatePackageName(dir ~ nname ~ ext);
- }
-
- string _fixImport(string text)
- {
- text = replace(text, "/", "\\");
- text = replace(text, "\"", "");
- text = toLower(getNameWithoutExt(text));
- string ntext = translateFilename(text);
- string name = translateModuleName(text);
- foreach(string file; srcfiles)
- {
- if(translateModuleName(getNameWithoutExt(file)) == name)
- {
- if(file.startsWith(win_path))
- return packageWin ~ ntext;
- else
- return packageVSI ~ ntext;
- }
- }
- return packageNF ~ ntext;
- }
-
- string fixImport(string text)
- {
- string imp = _fixImport(text);
- currentImports.addunique(imp);
- return imp;
- }
-
- void convertGUID(TokenIterator tokIt)
- {
- // tokIt after "{"
- static bool numberOrIdent(Token tok)
- {
- return tok.type == Token.Identifier || tok.type == Token.Number;
- }
- static string toByteArray(string txt)
- {
- string ntxt;
- for(int i = 0; i + 1 < txt.length; i += 2)
- {
- if(i > 0)
- ntxt ~= ",";
- ntxt ~= "0x" ~ txt[i .. i + 2];
- }
- return ntxt;
- }
-
- if (numberOrIdent(tokIt[0]) && tokIt[1].text == "-" &&
- numberOrIdent(tokIt[2]) && tokIt[3].text == "-" &&
- numberOrIdent(tokIt[4]) && tokIt[5].text == "-" &&
- numberOrIdent(tokIt[6]) && tokIt[7].text == "-" &&
- numberOrIdent(tokIt[8]) && tokIt[9].text == "}" &&
- tokIt[8].text.length == 12)
- {
- // 00020405-0000-0000-C000-000000000046
- tokIt[0].text = "0x" ~ tokIt[0].text; tokIt[1].text = ",";
- tokIt[2].text = "0x" ~ tokIt[2].text; tokIt[3].text = ",";
- tokIt[4].text = "0x" ~ tokIt[4].text; tokIt[5].text = ",";
-
- tokIt[6].text = "[ " ~ toByteArray(tokIt[6].text); tokIt[7].text = ",";
- tokIt[8].text = toByteArray(tokIt[8].text) ~ " ]";
- }
- else if (tokIt[0].type == Token.Identifier && tokIt[1].text == "}")
- {
- // simple identifer defined elsewhere
- tokIt[-1].text = "";
- tokIt[1].text = "";
- }
- else if (tokIt[0].type == Token.Number && tokIt[1].text == "," &&
- tokIt[2].type == Token.Number && tokIt[3].text == "," &&
- tokIt[4].type == Token.Number && tokIt[5].text == ",")
- {
- // 0x0c539790, 0x12e4, 0x11cf, 0xb6, 0x61, 0x00, 0xaa, 0x00, 0x4c, 0xd6, 0xd8
- if(tokIt[6].text == "{")
- {
- tokIt[6].pretext ~= "["; // use pretext to avoid later substitution
- tokIt[6].text = "";
- tokIt[22].pretext ~= "]";
- tokIt[22].text = "";
- }
- else if(tokIt[6].text != "[")
- {
- int i;
- for(i = 0; i < 8; i++)
- if(tokIt[5 + 2*i].text != "," || tokIt[6 + 2*i].type != Token.Number)
- break;
- if (i >= 8)
- {
- tokIt[6].pretext = " [" ~ tokIt[6].pretext;
- tokIt[21].pretext = " ]" ~ tokIt[21].pretext;
- }
- }
- }
- else if(tokIt.type == Token.String)
- {
- string txt = tokIt.text;
- // "af855397-c4dc-478b-abd4-c3dbb3759e72"
- if(txt.length == 38 && txt[9] == '-' && txt[14] == '-' && txt[19] == '-' && txt[24] == '-')
- {
- tokIt.text = "0x" ~ txt[1..9] ~ ", 0x" ~ txt[10..14] ~ ", 0x" ~ txt[15..19] ~ ", [ "
- ~ "0x" ~ txt[20..22] ~ ", 0x" ~ txt[22..24];
- for(int i = 0; i < 6; i++)
- tokIt.text ~= ", 0x" ~ txt[25 + 2*i .. 27 + 2*i];
- tokIt.text ~= " ]";
- }
- }
- else
- {
- tokIt.pretext ~= "\"";
- while(tokIt.text != "}")
- {
- ++tokIt;
- if(tokIt.atEnd())
- return;
- }
- tokIt.pretext = "\"" ~ tokIt.pretext;
- }
- }
-
- string convertText(TokenList tokens)
- {
- string prevtext;
-
- int braceCount;
- int parenCount;
- int brackCount;
- int enumLevel = -1;
-
- //replaceTokenSequence(tokens, "enum Kind { $enums ;", "class Kind { /+ $enums; +/", false);
-
- // do some preprocessor replacements to make the text bracket-balanced
- if(currentModule == "oaidl")
- {
- replaceTokenSequence(tokens, "__VARIANT_NAME_1", "", true);
- replaceTokenSequence(tokens, "__VARIANT_NAME_2", "", true);
- replaceTokenSequence(tokens, "__VARIANT_NAME_3", "", true);
- replaceTokenSequence(tokens, "__VARIANT_NAME_4", "", true);
- }
-
- if(currentModule == "windef")
- {
- // avoid removal of #define TRUE 1
- replaceTokenSequence(tokens, "#ifndef TRUE\n#define TRUE$def\n#endif\n", "#define TRUE 1\n", false);
- }
-
- if(currentModule == "winnt")
- {
- replaceTokenSequence(tokens, "#if defined(MIDL_PASS)\ntypedef struct $_ident {\n"
- ~ "#else$comment_else\n$else\n#endif$comment_endif", "$else", false);
- // remove int64 operations
- replaceTokenSequence(tokens, "#if defined(MIDL_PASS)$if_more\n#define Int32x32To64$def_more\n$defines\n"
- ~ "#error Must define a target architecture.\n#endif\n", "/+\n$*\n+/", false);
- // remove rotate operations
- replaceTokenSequence(tokens, "#define RotateLeft8$def_more\n$defines\n"
- ~ "#pragma intrinsic(_rotr16)\n", "/+\n$*\n+/", false);
-
- replaceTokenSequence(tokens, "typedef struct DECLSPEC_ALIGN($_num)", "align($_num) typedef struct", true);
- replaceTokenSequence(tokens, "typedef union DECLSPEC_ALIGN($_num)", "align($_num) typedef union", true);
- replaceTokenSequence(tokens, "struct DECLSPEC_ALIGN($_num)", "align($_num) struct", true);
-
- // win 8.1: remove template _ENUM_FLAG_INTEGER_FOR_SIZE
- replaceTokenSequence(tokens, "template $args _ENUM_FLAG_INTEGER_FOR_SIZE;", "/*$0*/", true);
- replaceTokenSequence(tokens, "template <> struct _ENUM_FLAG_INTEGER_FOR_SIZE <$arg> { $def };", "/*$0*/", true);
- replaceTokenSequence(tokens, "template <$arg> struct _ENUM_FLAG_SIZED_INTEGER { $def };", "/*$0*/", true);
-
- // win 10.0.17134.0: typedef enum MEM_EXTENDED_PARAMETER_TYPE {} MEM_EXTENDED_PARAMETER_TYPE, ...
- replaceTokenSequence(tokens, "MEM_EXTENDED_PARAMETER_TYPE, *PMEM_EXTENDED_PARAMETER_TYPE", "*PMEM_EXTENDED_PARAMETER_TYPE", true);
- // win 10.0.17763.0: typedef enum MEM_SECTION_EXTENDED_PARAMETER_TYPE {} MEM_SECTION_EXTENDED_PARAMETER_TYPE, ...
- replaceTokenSequence(tokens, "MEM_SECTION_EXTENDED_PARAMETER_TYPE, *PMEM_SECTION_EXTENDED_PARAMETER_TYPE", "*PMEM_SECTION_EXTENDED_PARAMETER_TYPE", true);
- // win 10.0.18362.0: typedef struct DECLSPEC_ALIGN(16) DECLSPEC_NOINITALL _CONTEXT ...
- replaceTokenSequence(tokens, "DECLSPEC_NOINITALL", "", true);
-
- replaceTokenSequence(tokens, "RtlZeroMemory($dest,$length)",
- "import core.stdc.string: memset; memset($dest,0,$length)", true);
- }
-
- if(currentModule == "commctrl")
- {
- // typos
- replaceTokenSequence(tokens, "PCCOMBOEXITEMW", "PCCOMBOBOXEXITEMW", true);
- replaceTokenSequence(tokens, "LPTBSAVEPARAMW", "LPTBSAVEPARAMSW", true);
- }
- if(currentModule == "oleauto")
- {
- replaceTokenSequence(tokens, "WINOLEAUTAPI_($_rettype)", "extern(Windows) $_rettype", true);
- replaceTokenSequence(tokens, "WINOLEAUTAPI", "extern(Windows) HRESULT", true);
- // rename the three argument inlined overload, LDC stumbles over it
- replaceTokenSequence(tokens, "VarCmp($arg1, $arg2, $arg3) { $code$ }",
- "VarCmp_inl($arg1, $arg2, $arg3) { $code$ }", true);
- }
- if(currentModule == "shellapi")
- {
- replaceTokenSequence(tokens, "SHSTDAPI_($_rettype)", "extern(Windows) $_rettype", true);
- replaceTokenSequence(tokens, "SHSTDAPI", "extern(Windows) HRESULT", true);
- replaceTokenSequence(tokens, "LWSTDAPIV_($_rettype)", "extern(Windows) $_rettype", true);
- }
- replaceTokenSequence(tokens, "STDAPI_($_rettype)", "extern(Windows) $_rettype", true);
- replaceTokenSequence(tokens, "STDAPI", "extern(Windows) HRESULT", true);
- replaceTokenSequence(tokens, "STDMETHODCALLTYPE", "extern(Windows)", true);
- replaceTokenSequence(tokens, "STDAPICALLTYPE", "extern(Windows)", true);
- replaceTokenSequence(tokens, "WINOLEAPI_($_rettype)", "extern(Windows) $_rettype", true);
- replaceTokenSequence(tokens, "WINOLEAPI", "extern(Windows) HRESULT", true);
- replaceTokenSequence(tokens, "$_ident WINAPIV", "extern(C) $_ident", true);
-
- replaceTokenSequence(tokens, "RPCRTAPI", "export", true);
- replaceTokenSequence(tokens, "RPC_STATUS", "int", true);
- replaceTokenSequence(tokens, "RPC_ENTRY", "extern(Windows)", true);
- replaceTokenSequence(tokens, "__RPC_USER", "extern(Windows)", true);
- replaceTokenSequence(tokens, "__RPC_STUB", "extern(Windows)", true);
- replaceTokenSequence(tokens, "__RPC_API", "extern(Windows)", true);
- replaceTokenSequence(tokens, "RPC_MGR_EPV", "void", true);
- replaceTokenSequence(tokens, "__RPC_FAR", "", true);
- replaceTokenSequence(tokens, "POINTER_32", "", true);
- replaceTokenSequence(tokens, "POINTER_64", "", true);
- replaceTokenSequence(tokens, "UNREFERENCED_PARAMETER($arg);", "/*UNREFERENCED_PARAMETER($arg);*/", true);
- if(currentModule == "rpcdce")
- {
- replaceTokenSequence(tokens, "RPC_INTERFACE_GROUP_IDLE_CALLBACK_FN($args);",
- "function($args) RPC_INTERFACE_GROUP_IDLE_CALLBACK_FN;", true);
- }
- // windef.h and ktmtypes.h
- replaceTokenSequence(tokens, "UOW UOW;", "UOW uow;", true);
-
- // enc.idl (FIELD_OFFSET already defined in winnt.h)
- replaceTokenSequence(tokens, "typedef struct _FIELD_OFFSET { $data } FIELD_OFFSET;",
- "struct _FIELD_OFFSET { $data };", true);
-
- // IP_DEST_PORT_UNREACHABLE defined twice
- if(currentModule == "ipexport")
- {
- replaceTokenSequence(tokens, "#define IP_DEST_PORT_UNREACHABLE (IP_STATUS_BASE + 5)\n" ~
- "#define IP_HOP_LIMIT_EXCEEDED (IP_STATUS_BASE + 13)\n",
- "#define IP_HOP_LIMIT_EXCEEDED (IP_STATUS_BASE + 13)\n", false);
- }
- if(currentModule == "nldef")
- {
- // expand MAKE_ROUTE_PROTOCOL
- replaceTokenSequence(tokens, "MAKE_ROUTE_PROTOCOL($_ident,$_num),",
- "MIB_IPPROTO_ __ $_ident = $_num, PROTO_IP_ __ $_ident = $_num,", true);
- }
- if(currentModule == "iphlpapi")
- {
- // imports inside extern(C) {}
- replaceTokenSequence(tokens, "extern \"C\" { $_data }", "$_data", true);
- }
- if(currentModule == "fileapifromapp")
- {
- replaceTokenSequence(tokens, "WINSTORAGEAPI", "", true);
- }
- if(currentModule == "propidlbase")
- {
- replaceTokenSequence(tokens, "_VARIANT_BOOL bool;", "/*_VARIANT_BOOL bool;*/", true);
- replaceTokenSequence(tokens, "TYPEDEF_CA($_identType,$_identName);",
- "struct $_identName { ULONG cElems; $_identType* pElems; };", true);
- }
- if(currentModule == "imageparameters140")
- {
- // type name and field name identical
- replaceTokenSequence(tokens, "ImageMoniker ImageMoniker;", "ImageMoniker mImageMoniker;", true);
- }
-
- // select unicode version of the API when defining without postfix A/W
- replaceTokenSequence(tokens, "#ifdef UNICODE\nreturn $_identW(\n#else\nreturn $_identA(\n#endif\n",
- " return $_identW(", false);
-
- replaceTokenSequence(tokens, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n", "extern \"C\" {\n", false);
- replaceTokenSequence(tokens, "#ifdef defined(__cplusplus)\nextern \"C\" {\n#endif\n", "extern \"C\" {\n", false);
- replaceTokenSequence(tokens, "#ifdef defined __cplusplus\nextern \"C\" {\n#endif\n", "extern \"C\" {\n", false);
- replaceTokenSequence(tokens, "#ifdef __cplusplus\n}\n#endif\n", "}\n", false);
-
- if(currentModule == "vsshell160")
- {
- replaceTokenSequence(tokens, "#ifndef INTEROPLIB\n[in] $ifcode\n#else\n$elsecode\n#endif", "[in] $ifcode", false);
- }
-
- for(TokenIterator tokIt = tokens.begin(); tokIt != tokens.end; )
- {
- Token tok = *tokIt;
-
- switch(tok.text)
- {
- case "(":
- parenCount++;
- break;
- case ")":
- parenCount--;
- break;
- case "[":
- brackCount++;
- break;
- case "]":
- brackCount--;
- break;
- case "{":
- braceCount++;
- break;
- case "}":
- braceCount--;
- if(braceCount <= enumLevel)
- enumLevel = -1;
- break;
-
- case "enum":
- enumLevel = braceCount;
- break;
- case ";":
- enumLevel = -1;
- break;
-
- case "importlib":
- if(tokIt[1].text == "(" && tokIt[2].type == Token.String && tokIt[3].text == ")")
- {
- tokIt.text = "import";
- tokIt[1].text = "";
- tokIt[2].pretext = " ";
- tokIt[2].text = fixImport(tokIt[2].text);
- tokIt[3].text = "";
- }
- break;
- case "import":
- if(tokIt[1].type == Token.String)
- {
- tokIt.pretext ~= "public ";
- tokIt[1].text = fixImport(tokIt[1].text);
- }
- break;
-
- case "midl_pragma":
- comment_line(tokIt);
- continue;
-
- case "cpp_quote":
- //reinsert_cpp_quote(tokIt);
- if(handle_cpp_quote(tokIt, enumLevel >= 0))
- continue;
- break;
-
- case "version":
- case "align":
- case "package":
- case "function":
- if(tokIt[1].text != "(")
- tok.text = keywordPrefix ~ tok.text;
- break;
-
- case "unsigned":
- {
- string t;
- bool skipNext = true;
- switch(tokIt[1].text)
- {
- case "__int64": t = "ulong"; break;
- case "long": t = "uint"; break;
- case "int": t = "uint"; break;
- case "__int32": t = "uint"; break;
- case "__int3264": t = "uint"; break;
- case "short": t = "ushort"; break;
- case "char": t = "ubyte"; break;
- default:
- t = "uint";
- skipNext = false;
- break;
- }
- tok.text = t;
- if(skipNext)
- (tokIt + 1).erase();
- break;
- }
- case "signed":
- {
- string t;
- bool skipNext = true;
- switch(tokIt[1].text)
- {
- case "__int64": t = "long"; break;
- case "long": t = "int"; break;
- case "int": t = "int"; break;
- case "__int32": t = "int"; break;
- case "__int3264": t = "int"; break;
- case "short": t = "short"; break;
- case "char": t = "byte"; break;
- default:
- t = "int";
- skipNext = false;
- break;
- }
- tok.text = t;
- if(skipNext)
- (tokIt + 1).erase();
- break;
- }
- // Windows SDK 8.0 => 7.1
- case "_Null_terminated_": tok.text = "__nullterminated"; break;
- case "_NullNull_terminated_": tok.text = "__nullnullterminated"; break;
- case "_Success_": tok.text = "__success"; break;
- case "_In_": tok.text = "__in"; break;
- case "_Inout_": tok.text = "__inout"; break;
- case "_In_opt_": tok.text = "__in_opt"; break;
- case "_Inout_opt_": tok.text = "__inout_opt"; break;
- case "_Inout_z_": tok.text = "__inout_z"; break;
- case "_Deref_out_": tok.text = "__deref_out"; break;
- case "_Out_": tok.text = "__out"; break;
- case "_Out_opt_": tok.text = "__out_opt"; break;
- case "_Field_range_": tok.text = "__range"; break;
- case "_Field_size_": tok.text = "__field_ecount"; break;
- case "_Field_size_opt_": tok.text = "__field_ecount_opt"; break;
- case "_Field_size_bytes_": tok.text = "__field_bcount"; break;
- case "_Field_size_bytes_opt_":tok.text = "__field_bcount_opt"; break;
-
- default:
- if(tok.type == Token.Macro && tok.text.startsWith("$"))
- tok.text = "_d_" ~ tok.text[1..$];
- else if(tok.type == Token.Number && (tok.text._endsWith("l") || tok.text._endsWith("L")))
- tok.text = tok.text[0..$-1];
- else if(tok.type == Token.Number && tok.text._endsWith("i64"))
- tok.text = tok.text[0..$-3] ~ "L";
- else if(tok.type == Token.Number && tok.text._endsWith("UI64"))
- tok.text = tok.text[0..$-4] ~ "UL";
- else if(tok.type == Token.String && tok.text.startsWith("L\""))
- tok.text = tok.text[1..$] ~ "w.ptr";
- else if(tok.type == Token.String && tok.text.startsWith("L\'"))
- tok.text = tok.text[1..$];
- else if(tok.text.startsWith("#"))
- {
- string txt = convertPP(tok.text, tok.lineno, enumLevel >= 0);
- reinsertTextTokens(tokIt, txt);
- continue;
- }
- else if(tok.text in disabled_defines)
- disable_macro(tokIt);
- else if(parenCount > 0)
- {
- // in function argument
- //if(tok.text == "const" || tok.text == "CONST")
- // tok.text = "/*const*/";
- //else
- if (tok.text.startsWith("REF") &&
- tok.text != "REFSHOWFLAGS" && !tok.text.startsWith("REFERENCE"))
- {
- tokIt.insertBefore(createToken(tok.pretext, "ref", Token.Identifier, tokIt.lineno));
- tok.pretext = " ";
- tok.text = tok.text[3..$];
- }
- }
- else if(tok.type == Token.Identifier && enumLevel >= 0 && (tokIt[-1].text == "{" || tokIt[-1].text == ","))
- enums[tok.text] = true;
- break;
- }
- prevtext = tok.text;
- ++tokIt;
- }
-
- version(none) version(vsi)
- {
- // wtypes.idl:
- replaceTokenSequence(tokens, "typedef ubyte UCHAR;", "typedef ubyte idl_UCHAR;", true);
- replaceTokenSequence(tokens, "typedef short SHORT;", "typedef short idl_SHORT;", true);
- replaceTokenSequence(tokens, "typedef ushort USHORT;", "typedef ushort idl_USHORT;", true);
- replaceTokenSequence(tokens, "typedef DWORD ULONG;", "typedef DWORD idl_ULONG;", true);
- replaceTokenSequence(tokens, "typedef double DOUBLE;", "typedef double idl_DOUBLE;", true);
- replaceTokenSequence(tokens, "typedef char OLECHAR;", "typedef char idl_OLECHAR;", true);
- replaceTokenSequence(tokens, "typedef LPSTR LPOLESTR;", "typedef LPSTR idl_LPOLESTR;", true);
- replaceTokenSequence(tokens, "typedef LPCSTR LPCOLESTR;", "typedef LPCSTR idl_LPCOLESTR;", true);
-
- replaceTokenSequence(tokens, "WCHAR OLECHAR;", "WCHAR idl_OLECHAR;", true);
- replaceTokenSequence(tokens, "OLECHAR *LPOLESTR;", "OLECHAR *idl_LPOLESTR;", true);
- replaceTokenSequence(tokens, "const OLECHAR *LPCOLESTR;", "OLECHAR *idl_LPCOLESTR;", true);
-
- replaceTokenSequence(tokens, "typedef LONG SCODE;", "typedef LONG vsi_SCODE;", true);
- }
-
- //replaceTokenSequence(tokens, "interface IWinTypes { $data }",
- // "/+interface IWinTypes {+/\n$data\n/+ } /+IWinTypes+/ +/", true);
-
- // docobj.idl (v6.0a)
- if(currentModule == "docobj")
- {
- replaceTokenSequence(tokens, "OLECMDIDF_REFRESH_PROMPTIFOFFLINE = 0x2000, OLECMDIDF_REFRESH_THROUGHSCRIPT = 0x4000 $_not,",
- "OLECMDIDF_REFRESH_PROMPTIFOFFLINE = 0x2000,\nOLECMDIDF_REFRESH_THROUGHSCRIPT = 0x4000, $_not", true);
- r…
Large files files are truncated, but you can click here to view the full file