Metaobject code generation method and device, electronic equipment and storage medium
By pre-parsering the source code and generating index mapping information before compilation, the problem of low efficiency and large code size in the function binding of meta-object code in the Qt framework is solved, achieving efficient binding and compact code generation.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-12-30
- Publication Date
- 2026-04-07
AI Technical Summary
In the Qt framework, function binding of meta-object code is inefficient and results in excessive code size, making it difficult to maintain a small code size while ensuring efficient binding.
By pre-parsing the source code file, identifying the linking statements and generating index mapping information, the meta-object code is generated using the index mapping information during compilation, avoiding function identifier lookup and index matching at runtime.
It improves function binding efficiency, controls code size, and meets the dual requirements of program efficiency and size in resource-constrained environments.
Smart Images

Figure CN121807307A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, and in particular to a method, apparatus, electronic device, and storage medium for generating meta-object code. Background Technology
[0002] Currently, the Qt framework is frequently used to construct meta-object code for communication between meta-objects. Qt is a cross-platform C++ application development framework widely used in graphical user interface (GUI) development. In Qt-based software development, the signal and slot mechanism is the core method for achieving inter-object communication, enabling reactive event handling. In the signal and slot mechanism, a "signal" is a notification function emitted by an object when a specific event occurs, and a "slot" is a function used to receive and process these signals. By associating specific signals with slot functions using the QObject::connect() function of the Qt framework, a reactive event handling chain can be established. Currently, in the signal and slot function binding scheme, Qt4 primarily uses the Meta-Object Compiler (MOC) to generate class meta-information at compile time. This meta-information records the mapping relationship between the function identifiers and corresponding index numbers of all signal and slot functions of that class. Subsequently, at runtime, the function identifiers of signals and slots in the meta-information are parsed, and the corresponding signal and slot functions are found based on their indices, thus completing the signal and slot function binding. Although this approach generates less code, the need for runtime function identifier lookup and index matching to determine the corresponding signal and slot functions leads to low function binding efficiency, thus affecting function call efficiency. To address this, Qt5 introduced a template-based binding scheme, where function addresses are determined at compile time through template instantiation. Subsequently, at runtime, the corresponding functions can be directly determined based on the function addresses for signal and slot function binding, thus avoiding the cumbersome and inefficient function identifier parsing process of Qt4 and improving the runtime efficiency of the meta-object code.
[0003] However, during compilation, the use of template instantiation to determine function addresses generates a large amount of template code, significantly increasing the size of the meta-object code. This generated meta-object code struggles to balance program efficiency and code size, failing to maintain a small code size while ensuring efficient binding. Summary of the Invention
[0004] This application provides a method, apparatus, electronic device, and storage medium for generating meta-object code, which can improve the function binding efficiency of meta-object code and effectively control the code size, thus solving the technical problem that function binding of meta-object code cannot maintain a small code size while ensuring efficient binding.
[0005] In a first aspect, embodiments of this application provide a method for generating meta-object code, including: Before compiling the first source code file, the first source code file is pre-parsed to identify the first linking statement in the source code. The first linking statement is used to connect the first function and the second function. Based on the first join statement, obtain the corresponding first function index and second function index, and generate index mapping information based on the first function index and second function index; During the compilation of the first source code file, the meta-object code corresponding to the first join statement is generated based on the index mapping information.
[0006] Furthermore, the first connection statement is the first signal-slot connection statement, the first function index is the signal function index, and the second function index is the slot function index; Based on the index mapping information, the corresponding meta-object code for the first join statement is generated, including: Based on the signal function index and slot function index in the index mapping information, generate the meta-object code corresponding to the first signal and slot connection statement.
[0007] Furthermore, the first connection statement is a first signal and slot connection statement, the first function index is a signal function index, the second function index is a slot function index, and the index mapping information is a second signal and slot connection statement rewritten based on the first signal and slot connection statement; Based on the index mapping information, the corresponding meta-object code for the first join statement is generated, including: The compiler identifies the second signal-slot connection statement based on the meta-object, obtains the signal function index and slot function index of the second signal-slot connection statement, and generates the corresponding meta-object code for the first signal-slot connection statement based on the obtained signal function index and slot function index.
[0008] Furthermore, index mapping information is generated based on the first function index and the second function index, including: A separate second source code file is generated based on the signal function index and slot function index. The second source code file contains second signal and slot connection statements and is used to compile together with the first source code file.
[0009] Furthermore, the first connection statement is the first signal and slot connection statement, the first function index is the signal function index, the second function index is the slot function index, and the index mapping information is the third signal and slot connection statement obtained by modifying the first signal and slot connection statement. Based on the index mapping information, the corresponding meta-object code for the first join statement is generated, including: The compiler identifies the third signal-slot connection statement based on the meta-object, obtains the signal function index and slot function index of the third signal-slot connection statement, and generates the corresponding meta-object code for the first signal-slot connection statement based on the obtained signal function index and slot function index.
[0010] Furthermore, index mapping information is generated based on the first function index and the second function index, including: Replace the function identifiers in the first signal and slot connection statement with the signal function index and slot function index to obtain the third signal and slot connection statement.
[0011] Furthermore, the first join statement is the first signal-slot join statement, the first function index is the signal function index, the second function index is the slot function index, and the index mapping information is the abstract syntax tree rule information; Based on the index mapping information, the corresponding meta-object code for the first join statement is generated, including: Determine the first syntax rule for the function identifier in the first signal-slot connection statement within the abstract syntax tree; Based on the abstract syntax tree rule information, the first syntax rule is replaced with the second syntax rule corresponding to the signal function index and slot function index; The meta-object code corresponding to the first signal and slot connection statement is generated based on the second syntax rule.
[0012] Furthermore, based on the first join statement, the corresponding first function index and second function index are obtained, including: Query the first meta-object information corresponding to the signal sending object in the first signal and slot connection statement, and determine the signal function index based on the position of the signal sending object in the signal list of the first meta-object information; Query the second-level object information corresponding to the slot receiving object in the first signal and slot connection statement, and determine the slot function index based on the position of the slot receiving object in the slot list of the second-level object information.
[0013] Furthermore, the first join statement is an event callback registration statement, the first function index is an event type index, and the second function index is callback function reference information. Based on the index mapping information, the corresponding meta-object code for the first join statement is generated, including: Identify index mapping information to obtain the event type index and corresponding callback function reference information; Event callback dispatch code is generated based on the event type index and the corresponding callback function reference information, and the event callback dispatch code is used as the meta-object code of the corresponding first join statement.
[0014] Furthermore, the first source code file is pre-parsed to identify the first linking statement in the source code, including: The abstract syntax tree of the first source code file is parsed using static code analysis tools; Traverse the function call expression nodes in the abstract syntax tree; The function call expression node whose function name is the specified join function name is identified and used as the first join statement.
[0015] In a second aspect, embodiments of this application provide a meta-object code generation apparatus, comprising: The pre-parsing module is used to pre-parse the first source code file before compiling it, and to identify the first linking statement in the source code. The first linking statement is used to link the first function and the second function. The index generation module is used to obtain the corresponding first function index and second function index based on the first join statement, and to generate index mapping information based on the first function index and second function index. The code generation module is used to generate the meta-object code corresponding to the first join statement based on the index mapping information during the compilation of the first source code file.
[0016] In a third aspect, embodiments of this application provide an electronic device, including: Memory and one or more processors; The memory is used to store one or more programs; When the one or more programs are executed by the one or more processors, the one or more processors implement the meta-object code generation method as described in the first aspect.
[0017] In a fourth aspect, embodiments of this application provide a storage medium containing computer-executable instructions, which, when executed by a computer processor, are used to perform the meta-object code generation method as described in the first aspect.
[0018] This embodiment of the application pre-parses the first source code file before compilation to identify the first linking statement in the source code. The first linking statement connects the first function and the second function. Based on the first linking statement, the corresponding first function index and second function index are obtained, and index mapping information is generated based on the first function index and the second function index. During the compilation of the first source code file, meta-object code corresponding to the first linking statement is generated based on the index mapping information. By adopting the above technical means, by pre-parseing the source code and generating index mapping information before compilation, and using the index mapping information to generate meta-object code during compilation, the meta-object code carries function index information. At the same time, the parsing of function indexes is transferred to be completed before compilation. This improves the efficiency of function binding while effectively controlling the code size, meeting the dual requirements of program efficiency and size in resource-constrained environments. Attached Figure Description
[0019] Figure 1 This is a flowchart of a meta-object code generation method provided in Embodiment 1 of this application; Figure 2 This is a flowchart illustrating the identification of the first signal and slot connection statement in Embodiment 1 of this application; Figure 3 This is the first generation flowchart of the meta-object code in Embodiment 1 of this application; Figure 4 This is a flowchart illustrating the second generation of meta-object code in Embodiment 1 of this application; Figure 5 This is the third generation flowchart of the meta-object code in Embodiment 1 of this application; Figure 6 This is a schematic diagram of the structure of a meta-object code generation device provided in Embodiment 2 of this application; Figure 7 This is a schematic diagram of the structure of an electronic device provided in Embodiment 3 of this application. Detailed Implementation
[0020] To make the objectives, technical solutions, and advantages of this application clearer, specific embodiments of this application will be described in further detail below with reference to the accompanying drawings. It should be understood that the specific embodiments described herein are merely for explaining this application and not for limiting it. It should also be noted that, for ease of description, only the parts relevant to this application are shown in the drawings, not all of them. Before discussing exemplary embodiments in more detail, it should be mentioned that some exemplary embodiments are described as processes or methods depicted as flowcharts. Although the flowcharts describe operations (or steps) as sequential processes, many of these operations can be performed in parallel, concurrently, or simultaneously. Furthermore, the order of the operations can be rearranged. The process can be terminated when its operation is completed, but may also have additional steps not included in the drawings. The process can correspond to a method, function, procedure, subroutine, subprogram, etc.
[0021] This application provides a method for generating meta-object code, which aims to generate index mapping information by pre-parsing the source code before compilation and using the index mapping information to generate meta-object code during compilation. This allows the meta-object code to carry function index information, while the parsing of function indexes is transferred to be completed before compilation, thereby improving the efficiency of function binding and effectively controlling the code size.
[0022] In the function binding scheme, Qt4 primarily uses the MetaObject Compiler (MOC) to generate class metadata at compile time. This metadata records the mapping relationship between the function identifiers and corresponding index numbers of all signals and slots in that class. Subsequently, at runtime, the function identifiers of the signals and slots in the metadata are parsed, and the corresponding indexes are used to find the signal and slot functions, thus completing the function binding of the signals and slots.
[0023] For example, in Qt4, the `connect` function (the core connection function in the Qt framework used to establish associations between signal and slot functions) is represented in the meta-object code as follows: connect(sender,SIGNAL(valueChanged(int)),receiver,SLOT(setText(QString))) Here, "valueChanged" represents the function identifier of the signal function, and "setText" represents the function identifier of the slot function. Based on this `connect` function, subsequent function binding between the signal and slot will be performed during program runtime. Specifically, by parsing the above function identifiers, the corresponding function indices are generated through mapping code, ultimately binding the two function indices. The execution flow of the `connect` function at this point is as follows: 1. Resolve the function identifier valueChanged(int) and map it to the function index index1 in the signal sending object; 2. Parse the function identifier setText(QString) and map it to the function index index2 in the slot receiver object; 3. Binding sender-index1 and receiver-index2, the final binding result is represented as follows: connect(sender,index1,receiver,index2).
[0024] While this approach uses function index binding, resulting in less generated code, it suffers from low function binding efficiency due to the need for runtime function identifier lookup and index matching to determine the corresponding signal and slot functions, thus impacting function call efficiency. To address this, Qt5 introduced a template- and function pointer-based binding scheme. Function addresses are determined at compile time through template instantiation. Subsequently, at runtime, the corresponding functions can be directly determined based on their addresses for signal and slot binding, avoiding the cumbersome and inefficient function identifier resolution process of Qt4 and improving the runtime efficiency of meta-object code.
[0025] For example, in Qt5, the `connect` function is resolved at compile time. In this case, the `connect` function does not need to store the function identifier, but it does need to store some additional metadata (for reflection) and function pointers. This is represented in the `connect` function as follows: connect(sender,&A::valueChanged,receiver,&B::setText) Here, "&A::valueChanged" is the function pointer to the signal function, and "&B::setText" is the function pointer to the slot function. Then, the member function pointers of objects A and B are parsed through the template to store their addresses. The implementation process is as follows: 1. Generate a specific function func1 based on the template for "sender,&A::valueChanged"; 2. Generate a specific function func2 based on the template for "receiver,&B::setText"; 3. Binding is performed on the function addresses `address1` for `func1` and `address2` for `func2`. The final compiled `connect` function is represented as follows: connect(sender,address1,receiver,address2) Because the Qt5 framework determines function addresses at compile time and directly binds signals and slots, it eliminates the need for function lookup and binding at runtime. This avoids the cumbersome and inefficient function identifier resolution process of Qt4, improving the runtime efficiency of metaobject code. However, when compiling metaobject code, the Qt5 framework uses template instantiation to determine function addresses, generating a large amount of template code, significantly increasing the size of the metaobject code. This generated metaobject code struggles to balance program efficiency and code size, failing to maintain a small code size while ensuring efficient binding.
[0026] Based on this, an embodiment of this application provides a method for generating meta-object code to solve the technical problem that function binding of meta-object code cannot maintain a small code size while ensuring efficient binding.
[0027] Example 1: Figure 1A flowchart of a meta-object code generation method according to Embodiment 1 of this application is provided. The meta-object code generation method provided in this embodiment can be executed by a meta-object code generation device, which can be implemented by software and / or hardware. The meta-object code generation device can consist of two or more physical entities, or it can consist of a single physical entity. Generally, the meta-object code generation device can be a computer or other computer equipment.
[0028] The following description uses the meta-object code generation device as the main entity executing the meta-object code generation method as an example. (Refer to...) Figure 1 The method for generating the meta-object code specifically includes: S110. Before compiling the first source code file, the first source code file is pre-parsed to identify the first linking statement in the source code. The first linking statement is used to link the first function and the second function.
[0029] This application optimizes the function binding code generation process during the generation of meta-object code before compiling the first source code file, thereby achieving the goal of effectively controlling code size while ensuring runtime efficiency.
[0030] In this process, the source code file of an application needs to have its connection statements used for meta-object communication compiled to generate meta-object code for the connection statements. Subsequently, during application runtime, the corresponding meta-object functions can be bound based on this meta-object code, eliminating the need for runtime searching and binding, thus improving runtime efficiency. The current application's source code file is defined as the first source code file. The first source code file refers to a text file containing program logic, written in a programming language, and serves as the initial input to the compilation process, used to compile subsequent meta-object code. Pre-parsing of the source code file refers to the syntactic and semantic analysis process performed on the source code using static code analysis techniques before formal compilation begins. This aims to extract declarative association information from the code, i.e., the connection statements. The first connection statement refers to the declarative function call statement in the source code, used to statically establish a call relationship between two executable objects (the first function and the second function). The first function refers to the function that acts as the event trigger source or call initiator in the association relationship established by the first connection statement, used to trigger event signals. The second function refers to the function that acts as an event responder or caller in the above-mentioned relationship. Its execution is triggered by the first function's event and is used to respond to event signals.
[0031] The following describes the meta-object code generation method of this application in detail, taking the generation of meta-object code in the Qt framework as an example. Here, the first connection statement is the first signal-slot connection statement, the first function is the signal function, and the second function is the slot function. Before compiling the first source code file, this application first performs pre-parsing to identify the signal-slot connection statements in its source code, defining the identified signal-slot connection statements as the first signal-slot connection statements. Specifically, before formally compiling the first source code file (such as the C++ header file ah and the implementation file a.cpp), a pre-parsing process is initiated. This involves scanning the signal-slot connection statements in the source code (e.g., the connect function call in the Qt framework) and extracting their key information, including the signal sending object, the function identifier of the signal function (e.g., valueChanged(int)), the slot receiving object, and the function identifier of the slot function (e.g., setText(QString)).
[0032] Furthermore, the pre-parsing process can analyze the class structure to which these functions belong, collecting class meta-information (such as function declarations and inheritance relationships), thus providing a data foundation for subsequent index mapping. By pre-parsing the source code of the first source code file, it is possible to obtain all signal and slot function association information before the application's compilation process, thereby providing the necessary data support for the subsequent generation of efficient meta-object code.
[0033] Optionally, refer to Figure 2 The first source code file is pre-parsed to identify the first linking statement in the source code, including: S1101. Parse the abstract syntax tree of the first source code file using a static code analysis tool; S1102, Traverse the function call expression nodes in the abstract syntax tree; S1103. Identify the call node in the function call expression node whose function name is the specified connection function name, and use it as the first connection statement.
[0034] This application employs a static code analysis tool (such as Clang) to pre-parse the first source code file. During pre-parse, the static code analysis tool first performs a complete lexical and syntactic analysis on the first source code file, constructing an Abstract Syntax Tree (AST) that accurately reflects the program's syntactic structure. The AST transforms the source code into a structured network of nodes, where each function definition, variable declaration, and function call becomes a node object with clear semantics within the AST. Subsequently, the static code analysis tool traverses the AST, examining each call node identified as a function call expression. During this traversal, the static code analysis tool determines whether the function name of each call node matches a pre-defined, specified connection function name (such as the core function QObject::connect used to establish connections in the Qt framework). When a match is found, the call node is identified as the first connection statement (such as the first signal and slot connection statement).
[0035] Optionally, if the first connection statement is a first signal and slot connection statement, the static code analysis tool can also analyze the parameter structure of the call node to confirm the specific syntax form of the SIGNAL() and SLOT() macros contained in its parameters (i.e., the specific syntax form in the signal and slot syntax, used to convert the function signature (function identifier and parameter type) into a string), thereby verifying that it is a first signal and slot connection statement.
[0036] Furthermore, for each first signal-slot join statement, the static code analysis tool extracts the type of the signal sender, the function identifier of the signal function ("valueChanged(int)"), the type of the slot receiver, and the function identifier of the slot function ("setText(QString)"). Simultaneously, the static code analysis tool also analyzes the class definitions corresponding to the sender and receiver (e.g., classA and classB), collecting all functions declared in the `signals:` and `slots:` sections of these classes to build a metadata database, thus completing the identification of the first signal-slot join statement. If the first join statement is another type of join statement, the identifiers of the two functions and their corresponding object types are extracted in the same way to construct the metadata database and complete the identification of the first join statement.
[0037] It should be noted that the meta-object code generation method of this application can be used to generate function connection code in any meta-object communication scheme. For example, in scenarios such as event listening systems and message distribution in remote procedure call frameworks, string identifiers such as event type names, message IDs, or field names can be converted into integer indices at compile time, and corresponding optimized dispatch code can be generated, thereby improving binding and invocation efficiency while simplifying code generation. This application does not impose fixed restrictions on the specific application scenarios of meta-object code generation, and will not be elaborated further here.
[0038] S120. Obtain the corresponding first function index and second function index based on the first join statement, and generate index mapping information based on the first function index and second function index.
[0039] Furthermore, based on the first join statement obtained from the above analysis, the function indices of its corresponding first and second functions are obtained for constructing index mapping information. The function indices include a first function index and a second function index. The first function index is a unique identifier assigned or calculated for the first function; this identifier can be an integer or a string, used to locate the first function at runtime. Similarly, the second function index is a unique identifier assigned or calculated for the second function; this identifier can be an integer, used to locate the second function at runtime. The index mapping information is a data structure used to record the static mapping relationship between the first join statement and its corresponding first and second function indices. Specific information about the first and second functions can be obtained from the meta-object system through the first and second function indices.
[0040] Based on the first join statement, the context (e.g., class, module) to which the first and second functions belong is determined, and their unique position identifiers within that context are queried or calculated. For example, in an object-oriented context, the class's meta-information table (e.g., set in a meta-object system) can be queried, and the function names can be assigned an index based on their order in the declaration list. Then, the logical position of the first join statement in the source code, the index value of the first function, and the index value of the second function are associated to form a mapping record. This mapping record is then stored in a structured manner, generating corresponding index mapping information.
[0041] Taking signal and slot connection statements as an example, based on the first signal and slot connection statement extracted through pre-parsing, a unique function index number can be assigned to each signal function and slot function (e.g., by querying the class's meta-object table to determine the function's position within the class). For each connection statement, the function identifier is parsed to map it to the corresponding function index (e.g., mapping valueChanged(int) to index1, and setText(QString) to index2). Depending on actual needs, this index mapping information can be stored in the first source code file by replacing the first signal and slot connection statement, so that when the first source code file is compiled subsequently, the corresponding meta-object code for the first signal and slot connection statement can be generated based on this index mapping information. Alternatively, the index mapping information can be stored in an intermediate file or memory data structure, forming a mapping table that records the index pairs corresponding to each connection statement, for use in generating meta-object code during subsequent compilation of the source code file. This application does not impose fixed restrictions on the form of this index mapping information, and will not elaborate further here.
[0042] Based on this index mapping information, the specific indexes of all function bindings can be determined during the compilation of the first source code file. This avoids the inefficient operation of looking up the index through the function identifier at runtime, eliminating the performance bottleneck of function identifier resolution during program execution. Furthermore, compared to template instantiation, index mapping maintains code compactness, thereby saving code storage space and controlling code size.
[0043] Optionally, when the first join statement is a first signal-slot join statement, obtaining the corresponding first function index and second function index based on the first join statement includes: Query the first meta-object information corresponding to the signal sending object in the first signal and slot connection statement, and determine the signal function index based on the position of the signal sending object in the signal list of the first meta-object information; Query the second-level object information corresponding to the slot receiving object in the first signal and slot connection statement, and determine the slot function index based on the position of the slot receiving object in the slot list of the second-level object information.
[0044] For example, suppose the first signal is connected to the slot as follows: connect(sender,SIGNAL(valueChanged(int)),receiver,SLOT(setText(QString))) When processing this join statement, the class name of the signal sender (e.g., the sender object in the above statement) is first extracted as the lookup key. The meta-object information corresponding to this class name is retrieved from a pre-generated meta-information database (e.g., a meta-information database stored in the source object system, which can be obtained by parsing the member variables and member functions of each object), and defined as the first meta-object information. The first meta-object information explicitly lists the function signatures declared under all signals: sections of this class, strictly following their declaration order. Subsequently, by matching the signal function identifier "valueChanged(int)" in the join statement with this list, the specific index of the function signature is directly determined based on its position in the list (e.g., if it is the first signal declared in the class, the index is 0).
[0045] Accordingly, for the slot receiver, the meta-object information corresponding to its class name (e.g., the receiver object in the above statement) is retrieved from the pre-generated meta-information database and defined as the second meta-object information. The slot function index is determined based on its position by locating the list of function signatures for the class under all slots: sections and matching the slot function identifier "setText(QString)" with the list.
[0046] Then, based on the above signal function index and slot function index, index mapping information can be generated. The whole process is completed before compilation, thus transforming the function identifier parsing and lookup matching work required at program runtime into index position lookup of the static metadata table.
[0047] S130. During the compilation of the first source code file, the meta-object code corresponding to the first join statement is generated based on the index mapping information.
[0048] Furthermore, based on the generated index mapping information, optimized meta-object code can be generated during the formal compilation of the first source code file. Meta-object code refers to the low-level instruction code generated during compilation based on the index mapping information, which can be directly executed at runtime. It is used to implement a binding mechanism for function calls to meta-objects directly through indexes, replacing the method of obtaining the code at runtime and improving runtime efficiency. Moreover, the significant difference between generating the meta-object code for the first join statement using index mapping information and generating it based on a template is that using only index mapping information avoids the additional code introduced by the template, thus ensuring that the size of the meta-object code remains relatively small.
[0049] Optionally, the first connection statement is a first signal-slot connection statement, the first function index is a signal function index, and the second function index is a slot function index; Based on the index mapping information, the corresponding meta-object code for the first join statement is generated, including: Based on the signal function index and slot function index in the index mapping information, generate the meta-object code corresponding to the first signal and slot connection statement.
[0050] When generating meta-object code based on signal function indices and slot function indices using index mapping information, the meta-object compiler can directly read the index values of the signal function indices and slot function indices, and generate the underlying binding and dispatch code, i.e., the meta-object code, accordingly. This code can directly locate and call the corresponding function at runtime through index lookup, without needing to determine the index at runtime, thus improving runtime efficiency.
[0051] For example, the original statement for connecting the first signal to the slot is as follows: Based on the index mapping information mentioned above, the `connect(sender, SIGNAL(valueChanged(int)), receiver, SLOT(setText(QString)))` method can replace the index mapping information with an efficient index binding form during the compilation of the first source code file, such as: connect(sender,index1,receiver,index2).
[0052] The generated meta-object code directly completes the binding based on the function index, thus eliminating the need for function identifier resolution of the connect function at application runtime. It also avoids a large amount of redundant code generated by template instantiation, thereby balancing the running efficiency and resource consumption in the process of binding signal and slot functions and effectively controlling the code size of the code file.
[0053] Optionally, the index mapping information is a second signal and slot connection statement rewritten based on the first signal and slot connection statement; the index mapping information is generated based on the first function index and the second function index, including: A separate second source code file is generated based on the signal function index and slot function index. The second source code file contains second signal and slot connection statements and is used to compile together with the first source code file.
[0054] Because the first source code files (ah and a.cpp) delete classa member variables, class-related information (i.e., function indexes), and connect function information by default during the compilation of binary code, these information cannot be obtained in the compiled code. Therefore, this application uses pre-parsing to extract the information that needs to be discarded during compilation from the first source code files to generate signal and slot connection statements containing function index information, defined as second signal and slot connection statements. A new source code file, moc_a.cpp, is generated based on the extracted information and defined as the second source code file. In a specific embodiment, signal and slot connection statements containing function index information are written in the second source code file in a way that will not be discarded during compilation, such as storing member variables or class-related information in specific objects (e.g., strings, overridden functions). This ensures that the second source code file participates in the compilation process together with the first source code files (ah, a.cpp, and moc_a.cpp) for the generation of meta-object code and will not be discarded during compilation.
[0055] Furthermore, referring to Figure 3 The first connection statement is the first signal and slot connection statement, the first function index is the signal function index, the second function index is the slot function index, and the index mapping information is the second signal and slot connection statement rewritten based on the first signal and slot connection statement. Based on the index mapping information, the corresponding meta-object code for the first join statement is generated, including: S1301. The meta-object compiler identifies the second signal-slot connection statement and obtains the signal function index and slot function index of the second signal-slot connection statement. S1302. Generate the meta-object code corresponding to the first signal and slot connection statement based on the obtained signal function index and slot function index.
[0056] Based on this second source code file, when the compilation process starts, the Meta-Object Compiler (MOC) processes this independent second source code file (such as moc_a.cpp), identifying the linking statements centered around indices, namely the second signal and slot linking statements. It then reads the signal function indices and slot function indices from these statements, and based on these two precise index values, generates the final runtime meta-object code. This allows the runtime system to directly locate and call the corresponding index2 slot function on the receiver object by index value when signal index1 is emitted on the sender object, without any function identifier comparison or lookup. This transforms the linking process from a lookup to a direct jump, improving the efficiency of signal and slot function execution.
[0057] Optionally, the index mapping information is a third signal-slot connection statement modified based on the first signal-slot connection statement; the index mapping information is generated based on the first function index and the second function index, including: Replace the function identifiers in the first signal and slot connection statement with the signal function index and slot function index to obtain the third signal and slot connection statement.
[0058] Unlike the aforementioned method of creating a separate second source code file for joint compilation, this application can also perform code replacement on the original first source code file to construct a signal and slot connection statement containing function index information, defined as a third signal and slot connection statement. Specifically, based on the pre-parsed signal function index (e.g., index1) and slot function index (e.g., index2), the position of the first signal and slot connection statement in the original first source code file (e.g., a.cpp) is locked. Then, a code replacement operation is performed, replacing the original first signal and slot connection statement containing string macros with strings. For example, the first signal and slot connection statement is represented as: In the statement `connect(sender, SIGNAL(valueChanged(int)), receiver, SLOT(setText(QString)))`, the strings "SIGNAL(valueChanged(int))" and "SLOT(setText(QString))" are completely removed and replaced with the previously determined signal function indices `index1` and `index2`, generating a completely new third signal and slot connection statement, ultimately manifested as `connect(sender, index1, receiver, index2)`. This third signal and slot connection statement fundamentally changes the nature of the code, eliminating string parameters and allowing the compiler to process it as a function call with integer indices as parameters. Based on this third signal and slot connection statement, an updated first source code file is generated. This first source code file participates in the compilation process, ensuring both program efficiency and a compact code size.
[0059] Furthermore, referring to Figure 4 The first connection statement is the first signal and slot connection statement, the first function index is the signal function index, the second function index is the slot function index, and the index mapping information is the third signal and slot connection statement obtained by modifying the first signal and slot connection statement. Based on the index mapping information, the corresponding meta-object code for the first join statement is generated, including: S1303. The meta-object compiler identifies the third signal-slot connection statement and obtains the signal function index and slot function index of the third signal-slot connection statement. S1304. Generate the meta-object code corresponding to the first signal and slot connection statement based on the obtained signal function index and slot function index.
[0060] Based on the first source code file with the updated third signal and slot connection statements, during source code compilation, the meta-object compiler identifies these optimized third signal and slot connection statements that use function indices as parameters by scanning the source code (or its intermediate representation). It then directly obtains the explicitly defined signal and slot function indices from the function call parameters of these statements. Based on these two precise index values, it generates the optimized meta-object code required for final runtime.
[0061] Subsequently, during program execution, when signal index1 is triggered on the sender object, the system can directly locate and call the function corresponding to slot index2 on the receiver object through array index addressing or similar constant-time operations, without any function identifier comparison, hash lookup, or template expansion. This optimizes the signal and slot connection and invocation process from runtime lookup to a direct mapping jump, improving the efficiency of signal and slot functions.
[0062] Optionally, the first join statement is a first signal and slot join statement, the first function index is a signal function index, the second function index is a slot function index, and the index mapping information is abstract syntax tree rule information; Based on the index mapping information, the corresponding meta-object code for the first join statement is generated, including: S1305. Determine the first syntax rule for the function identifier in the first signal-slot connection statement in the abstract syntax tree; S1306. Based on the abstract syntax tree rule information, replace the first syntax rule with the second syntax rule corresponding to the signal function index and slot function index; S1307. Generate the meta-object code corresponding to the first signal and slot connection statement based on the second syntax rule.
[0063] This application can also directly perform the essential transformation of signal and slot connection statements on the intermediate representation of the program's Abstract Syntax Tree (AST). By defining index mapping information as an abstract syntax tree rule, this predefined transformation rule guides the compiler on how to modify its internal abstract syntax tree. When the compilation process of the first source code file begins, after parsing the first source code file (such as ah and a.cpp) and constructing the initial abstract syntax tree, the compiler (such as Clang) uses a specific processing plugin (such as a Clang plugin or an AST manipulation tool) to accurately locate the node corresponding to the first signal and slot connection statement in the abstract syntax tree. Then, by analyzing its structure, it determines the syntax rules followed by the string literal nodes representing the SIGNAL(...) and SLOT(...) macro parameters, defining them as the first syntax rule. Subsequently, the processing plugin applies the previously pre-parsed and computed AST rule information, defining it as the second syntax rule. The second syntax rule contains knowledge of which specific integer index constant to map a particular function identifier to. According to this set of rules, the processing plugin removes the subtree nodes that originally represented string literals and replaces them with new nodes that represent integer index constants (index1 and index2). This transforms the original first signal-slot join statement's AST representation in memory into a new form, such as connect(sender, index1, receiver, index2).
[0064] Subsequent compilation steps, including code generation, optimization, and final meta-object code generation, will all be based on this optimized AST. This avoids managing additional source files (such as moc_a.cpp), making the signal and slot connection statement construction process more concise. Furthermore, since optimization is completed in the earliest stage of compilation, it ultimately ensures that the simplicity of the original compilation process is maintained to the greatest extent possible without increasing code size and achieving efficient binding.
[0065] Optionally, the first join statement is an event callback registration statement, the first function index is an event type index, the second function index is callback function reference information, and the meta-object code corresponding to the first join statement is generated based on the index mapping information, including: Identify index mapping information to obtain the event type index and corresponding callback function reference information; Event callback dispatch code is generated based on the event type index and the corresponding callback function reference information, and the event callback dispatch code is used as the meta-object code of the corresponding first join statement.
[0066] Unlike the aforementioned meta-object code generation scheme based on signals and slot functions in the QT framework, the meta-object code generation method in this application is also applied to the event-driven architecture of an event listener system. In the event listener scenario, the first connection statement represents an event callback registration statement, which declares the association between a specific type of event and a specific callback function through strings or other forms. During the pre-parsing stage, the parser identifies this event callback registration statement, maps the string parameter used to identify the event category to an integer event type index, and extracts or calculates the direct reference information of the callback function associated with the event (such as its address in memory, function object, or internal identifier of a callable object), thereby generating structured index mapping information.
[0067] During subsequent compilation, the code generator reads this mapping information, extracts the event type index and callback function reference pairs, and generates event callback dispatch code as the final meta-object code. This dispatch code can be implemented based on a static dispatch table or a set of conditional jump instructions. Based on this dispatch code, when an event is triggered at runtime, the pre-bound callback function reference is directly located in the dispatch table according to the event type index corresponding to the triggered event, and the call is executed accordingly. This avoids the overhead of searching for the callback function at runtime through string comparison or dynamic lookup. Since the dispatch logic is based on constant integer indices and direct reference calls, the generated machine code can be simplified, thus ensuring that the size of the generated code is effectively reduced while implementing event dispatch, achieving a balance between execution efficiency and space usage.
[0068] The above describes a method that, before compiling the first source code file, pre-parses the first source code file to identify the first linking statement, which connects the first function and the second function. Based on the first linking statement, it obtains the corresponding first function index and second function index, and generates index mapping information. During the compilation of the first source code file, it generates meta-object code corresponding to the first linking statement based on the index mapping information. By pre-parseting the source code and generating index mapping information before compilation, and then using this information to generate meta-object code during compilation, the meta-object code carries function index information. Furthermore, the parsing of function indexes is completed before compilation, thereby improving function binding efficiency while effectively controlling code size, thus meeting the dual requirements of efficiency and size in resource-constrained environments.
[0069] Example 2: Based on the above embodiments, Figure 6 This is a schematic diagram of a meta-object code generation device provided in Embodiment 2 of this application. (Reference) Figure 6 The meta-object code generation device provided in this embodiment specifically includes: The pre-parsing module 21 is used to pre-parse the first source code file before compiling it, and to identify the first linking statement in the source code. The first linking statement is used to link the first function and the second function. Index generation module 22 is used to obtain the corresponding first function index and second function index based on the first join statement, and to generate index mapping information based on the first function index and second function index; The code generation module 23 is used to generate the meta-object code corresponding to the first linking statement based on the index mapping information during the compilation of the first source code file.
[0070] Specifically, the first connection statement is the first signal-slot connection statement, the first function index is the signal function index, and the second function index is the slot function index; Based on the index mapping information, the corresponding meta-object code for the first join statement is generated, including: Based on the signal function index and slot function index in the index mapping information, generate the meta-object code corresponding to the first signal and slot connection statement.
[0071] Specifically, the first connection statement is the first signal and slot connection statement, the first function index is the signal function index, the second function index is the slot function index, and the index mapping information is the second signal and slot connection statement rewritten based on the first signal and slot connection statement; Based on the index mapping information, the corresponding meta-object code for the first join statement is generated, including: The compiler identifies the second signal-slot connection statement based on the meta-object, obtains the signal function index and slot function index of the second signal-slot connection statement, and generates the corresponding meta-object code for the first signal-slot connection statement based on the obtained signal function index and slot function index.
[0072] Specifically, index mapping information is generated based on the first function index and the second function index, including: A separate second source code file is generated based on the signal function index and slot function index. The second source code file contains second signal and slot connection statements and is used to compile together with the first source code file. Specifically, the first connection statement is the first signal and slot connection statement, the first function index is the signal function index, the second function index is the slot function index, and the index mapping information is the third signal and slot connection statement obtained by modifying the first signal and slot connection statement. Based on the index mapping information, the corresponding meta-object code for the first join statement is generated, including: The compiler identifies the third signal-slot connection statement based on the meta-object, obtains the signal function index and slot function index of the third signal-slot connection statement, and generates the corresponding meta-object code for the first signal-slot connection statement based on the obtained signal function index and slot function index.
[0073] Specifically, index mapping information is generated based on the first function index and the second function index, including: Replace the function identifiers in the first signal and slot connection statement with the signal function index and slot function index to obtain the third signal and slot connection statement.
[0074] Specifically, the first join statement is the first signal and slot join statement, the first function index is the signal function index, the second function index is the slot function index, and the index mapping information is the abstract syntax tree rule information; Based on the index mapping information, the corresponding meta-object code for the first join statement is generated, including: Determine the first syntax rule for the function identifier in the first signal-slot connection statement within the abstract syntax tree; Based on the abstract syntax tree rule information, the first syntax rule is replaced with the second syntax rule corresponding to the signal function index and slot function index; The meta-object code corresponding to the first signal and slot connection statement is generated based on the second syntax rule.
[0075] Specifically, obtaining the corresponding first function index and second function index based on the first join statement includes: Query the first meta-object information corresponding to the signal sending object in the first signal and slot connection statement, and determine the signal function index based on the position of the signal sending object in the signal list of the first meta-object information; Query the second-level object information corresponding to the slot receiving object in the first signal and slot connection statement, and determine the slot function index based on the position of the slot receiving object in the slot list of the second-level object information.
[0076] Specifically, the first join statement is an event callback registration statement, the first function index is an event type index, the second function index is callback function reference information, and the meta-object code corresponding to the first join statement is generated based on the index mapping information, including: Identify index mapping information to obtain the event type index and corresponding callback function reference information; Event callback dispatch code is generated based on the event type index and the corresponding callback function reference information, and the event callback dispatch code is used as the meta-object code of the corresponding first join statement.
[0077] Specifically, the first source code file is pre-parsed to identify the first linking statement in the source code, including: The abstract syntax tree of the first source code file is parsed using static code analysis tools; Traverse the function call expression nodes in the abstract syntax tree; The function call expression node whose function name is the specified join function name is identified and used as the first join statement.
[0078] The above describes a method that, before compiling the first source code file, pre-parses the first source code file to identify the first linking statement, which connects the first function and the second function. Based on the first linking statement, it obtains the corresponding first function index and second function index, and generates index mapping information. During the compilation of the first source code file, it generates meta-object code corresponding to the first linking statement based on the index mapping information. By pre-parseting the source code and generating index mapping information before compilation, and then using this information to generate meta-object code during compilation, the meta-object code carries function index information. Furthermore, the parsing of function indexes is completed before compilation, thereby improving function binding efficiency while effectively controlling code size, thus meeting the dual requirements of efficiency and size in resource-constrained environments.
[0079] The meta-object code generation apparatus provided in Embodiment 2 of this application can be used to execute the meta-object code generation method provided in Embodiment 1 above, and has corresponding functions and beneficial effects.
[0080] Example 3: This application provides an electronic device in embodiment three, referring to... Figure 7 The electronic device includes a processor 31, a memory 32, a communication module 33, an input device 34, and an output device 35. The electronic device may have one or more processors and one or more memories. The processor, memory, communication module, input device, and output device of the electronic device can be connected via a bus or other means.
[0081] Memory, as a computer-readable storage medium, can be used to store software programs, computer-executable programs, and modules, such as program instructions / modules corresponding to the meta-object code generation method described in any embodiment of this application (e.g., the pre-parsing module, index generation module, and code generation module in the meta-object code generation apparatus). Memory may primarily include a program storage area and a data storage area, wherein the program storage area may store the operating system and at least one application program required for a function; the data storage area may store data created based on the use of the device, etc. Furthermore, memory may include high-speed random access memory and non-volatile memory, such as at least one disk storage device, flash memory device, or other non-volatile solid-state storage device. In some instances, memory may further include memory remotely located relative to the processor, and these remote memories can be connected to the device via a network. Examples of such networks include, but are not limited to, the Internet, corporate intranets, local area networks, mobile communication networks, and combinations thereof.
[0082] The communication module is used for data transmission.
[0083] The processor executes various functional applications and data processing of the device by running software programs, instructions, and modules stored in memory, thus implementing the aforementioned meta-object code generation method.
[0084] Input devices can be used to receive input numerical or character information, and to generate key signal inputs related to user settings and function control of the device. Output devices may include display devices such as displays.
[0085] The electronic device provided above can be used to execute the meta-object code generation method provided in Embodiment 1 above, and has corresponding functions and beneficial effects.
[0086] Example 4: This application embodiment also provides a storage medium containing computer-executable instructions, which, when executed by a computer processor, are used to execute a meta-object code generation method. The meta-object code generation method includes: before compiling a first source code file, pre-parsing the first source code file to identify a first linking statement in the source code, the first linking statement being used to link a first function and a second function; obtaining corresponding first function indexes and second function indices based on the first linking statement; generating index mapping information based on the first function indexes and second function indices; and generating meta-object code corresponding to the first linking statement based on the index mapping information during the compilation of the first source code file.
[0087] Storage medium – any type of memory device or storage device. The term “storage medium” is intended to include: mounting media, such as CD-ROM, floppy disk, or magnetic tape devices; computer system memory or random access memory, such as DRAM, DDR RAM, SRAM, EDO RAM, Rambus RAM, etc.; non-volatile memory, such as flash memory, magnetic media (e.g., hard disk or optical storage); registers or other similar types of memory elements, etc. Storage medium may also include other types of memory or combinations thereof. Furthermore, storage medium may reside in a first computer system in which the program is executed, or it may reside in a different second computer system connected to the first computer system via a network (such as the Internet). The second computer system can provide program instructions to the first computer for execution. The term “storage medium” can include two or more storage media residing in different locations (e.g., in different computer systems connected via a network). Storage medium may store program instructions (e.g., specifically implemented as a computer program) executable by one or more processors.
[0088] Of course, the computer-executable instructions provided in the embodiments of this application are not limited to the meta-object code generation method described above, but can also execute related operations in the meta-object code generation method provided in any embodiment of this application.
[0089] The meta-object code generation apparatus, storage medium, and electronic device provided in the above embodiments can execute the meta-object code generation method provided in any embodiment of this application. For technical details not described in detail in the above embodiments, please refer to the meta-object code generation method provided in any embodiment of this application.
[0090] The above description is merely a preferred embodiment and the technical principles employed in this application. This application is not limited to the specific embodiments described herein, and various obvious changes, readjustments, and substitutions that can be made by those skilled in the art will not depart from the scope of protection of this application. Therefore, although this application has been described in detail through the above embodiments, this application is not limited to the above embodiments, and may include more other equivalent embodiments without departing from the concept of this application, the scope of which is determined by the scope of the claims.
Claims
1. A method for generating meta-object code, characterized in that, include: Before compiling the first source code file, the first source code file is pre-parsed to identify the first connection statement in the source code, which is used to connect the first function and the second function; Based on the first join statement, obtain the corresponding first function index and second function index, and generate index mapping information based on the first function index and second function index; During the compilation of the first source code file, meta-object code corresponding to the first join statement is generated based on the index mapping information.
2. The meta-object code generation method according to claim 1, characterized in that, The first connection statement is a first signal-slot connection statement, the first function index is a signal function index, and the second function index is a slot function index; The step of generating the meta-object code corresponding to the first join statement based on the index mapping information includes: Based on the signal function index and the slot function index in the index mapping information, the meta-object code corresponding to the first signal and slot connection statement is generated.
3. The meta-object code generation method according to claim 1, characterized in that, The first connection statement is a first signal and slot connection statement, the first function index is a signal function index, the second function index is a slot function index, and the index mapping information is a second signal and slot connection statement rewritten based on the first signal and slot connection statement; The step of generating the meta-object code corresponding to the first join statement based on the index mapping information includes: The compiler identifies the second signal and slot connection statement based on the meta-object, obtains the signal function index and the slot function index of the second signal and slot connection statement, and generates meta-object code corresponding to the first signal and slot connection statement based on the obtained signal function index and slot function index.
4. The meta-object code generation method according to claim 3, characterized in that, The generation of index mapping information based on the first function index and the second function index includes: A separate second source code file is generated based on the signal function index and the slot function index. The second source code file contains the second signal and slot connection statements and is used to be compiled together with the first source code file.
5. The meta-object code generation method according to claim 1, characterized in that, The first connection statement is a first signal and slot connection statement, the first function index is a signal function index, the second function index is a slot function index, and the index mapping information is a third signal and slot connection statement modified based on the first signal and slot connection statement; The step of generating the meta-object code corresponding to the first join statement based on the index mapping information includes: The compiler identifies the third signal and slot connection statement based on the meta-object, obtains the signal function index and the slot function index of the third signal and slot connection statement, and generates the meta-object code corresponding to the first signal and slot connection statement based on the obtained signal function index and slot function index.
6. The meta-object code generation method according to claim 5, characterized in that, The generation of index mapping information based on the first function index and the second function index includes: The third signal and slot connection statement is obtained by replacing the function identifiers in the first signal and slot connection statement with the signal function index and the slot function index.
7. The meta-object code generation method according to claim 1, characterized in that, The first join statement is a first signal and slot join statement, the first function index is a signal function index, the second function index is a slot function index, and the index mapping information is abstract syntax tree rule information; The step of generating the meta-object code corresponding to the first join statement based on the index mapping information includes: Determine the first syntax rule for the function identifier in the first signal and slot connection statement in the abstract syntax tree; Based on the abstract syntax tree rule information, the first syntax rule is replaced with a second syntax rule corresponding to the signal function index and the slot function index; Based on the second syntax rule, generate the meta-object code corresponding to the first signal and slot connection statement.
8. The meta-object code generation method according to any one of claims 2-7, characterized in that, The step of obtaining the corresponding first function index and second function index based on the first join statement includes: Query the first meta-object information corresponding to the signal sending object in the first signal and slot connection statement, and determine the signal function index according to the position of the signal sending object in the signal list of the first meta-object information; Query the second meta-object information corresponding to the slot receiving object in the first signal and slot connection statement, and determine the slot function index based on the position of the slot receiving object in the slot list of the second meta-object information.
9. The meta-object code generation method according to claim 1, characterized in that, The first connection statement is an event callback registration statement, the first function index is an event type index, the second function index is callback function reference information, and the step of generating the meta-object code corresponding to the first connection statement based on the index mapping information includes: Identify the index mapping information to obtain the event type index and the corresponding callback function reference information; Based on the event type index and the corresponding callback function reference information, an event callback dispatch code is generated, and the event callback dispatch code is used as the meta-object code corresponding to the first connection statement.
10. The meta-object code generation method according to claim 1, characterized in that, The step of pre-parsing the first source code file and identifying the first linking statement in the source code includes: The abstract syntax tree of the first source code file is parsed using static code analysis tools; Traverse the function call expression nodes in the abstract syntax tree; Identify the call node in the function call expression node whose function name is the specified connection function name, and use it as the first connection statement.
11. A meta-object code generation device, characterized in that, include: The pre-parsing module is used to pre-parse the first source code file before compiling it, and to identify the first connection statement in the source code, which is used to connect the first function and the second function. The index generation module is used to obtain the corresponding first function index and second function index based on the first join statement, and to generate index mapping information based on the first function index and second function index. The code generation module is used to generate meta-object code corresponding to the first connection statement based on the index mapping information during the compilation of the first source code file.
12. An electronic device, characterized in that, include: Memory and one or more processors; The memory is used to store one or more programs; When the one or more programs are executed by the one or more processors, the one or more processors implement the meta-object code generation method as described in any one of claims 1-9.
13. A storage medium containing computer-executable instructions, characterized in that, The computer-executable instructions, when executed by a computer processor, are used to perform the meta-object code generation method as described in any one of claims 1-9.