dylib | get c exports | c type to data type | Search

The getTypes function is an asynchronous function that retrieves function types from a given C++ code snippet, optionally specifying a function name to retrieve the type for. It uses an XPath expression to parse the code and extract the function types, and is exported as a module for use in other parts of the application.

Run example

npm run import -- "get c types"

get c types


let selectCode

async function getTypes(code, functionName) {
    if(!selectCode)
        selectCode = await importer.import("select antlr tree")

    if(functionName) {
        let result = await selectCode(['(//functionDefinition[contains(.//declaratorid/@strdata, "' + functionName + '")]/declSpecifierSeq/declSpecifier/typeSpecifier|//functionDefinition[contains(.//declaratorid/@strdata, "' + functionName + '")]/declarator//typeSpecifier)', './@strdata'], code, 'cpp')
        return result
    }

    var result = await selectCode(['(//functionDefinition/declSpecifierSeq/declSpecifier/typeSpecifier|//functionDefinition/declarator//typeSpecifier)', './@strdata'], code, 'cpp')
    return result
}

module.exports = getTypes

What the code could have been:

const importer = require('importer');

/**
 * Retrieves the types of functions in a given code.
 * 
 * @param {string} code The code to parse.
 * @param {string} functionName The name of the function to retrieve the type for.
 * @returns {Promise} A list of function types.
 */
async function getTypes(code, functionName) {
    // Ensure we have the selectCode function
    if (!selectCode) {
        // Import the necessary module on first use
        selectCode = await _import('select-antlr-tree');
    }

    // Function to retrieve the types of functions
    const getFunctions = async (query) => {
        // Execute the query on the code and return the result
        return await selectCode(query, code, 'cpp');
    };

    // If a specific function name is provided, retrieve its type
    if (functionName) {
        // Construct the query to retrieve the function type
        const query = [
            '(//functionDefinition[contains(.//declarator/id/@strdata, "' + functionName + '")]/declSpecifierSeq/declSpecifier/typeSpecifier|' +
            '//functionDefinition[contains(.//declarator/id/@strdata, "' + functionName + '")]/declarator//typeSpecifier)',
            './@strdata'
        ];

        // Execute the query and return the result
        return getFunctions(query);
    }

    // Otherwise, retrieve all function types
    else {
        // Construct the query to retrieve all function types
        const query = [
            '(//functionDefinition/declSpecifierSeq/declSpecifier/typeSpecifier|//functionDefinition/declarator//typeSpecifier)',
            './@strdata'
        ];

        // Execute the query and return the result
        return getFunctions(query);
    }
}

// Import the necessary function to retrieve the selectCode
const _import = async (name) => {
    try {
        return await importer.import(name);
    }
    catch (error) {
        // TODO: Handle the error when the module is not found
        console.error(`Error importing module ${name}: ${error}`);
        throw error;
    }
};

module.exports = getTypes;

Code Breakdown

Function Overview

The getTypes function is an asynchronous function that retrieves function types from a given code snippet in C++.

Parameters

Implementation

  1. The function checks if selectCode has been initialized. If not, it imports the select antlr tree module and assigns it to selectCode.
  2. If functionName is provided, it constructs a XPath expression to select the function definition with the specified name and retrieve its type specifier.
  3. The function calls selectCode with the constructed XPath expression, the code, and the language ('cpp') and returns the result.
  4. If functionName is not provided, it constructs a XPath expression to select all function definitions and retrieve their type specifiers.
  5. The function calls selectCode with the constructed XPath expression, the code, and the language ('cpp') and returns the result.

Export

The getTypes function is exported as a module, making it available for use in other parts of the application.