Compilation period code generation method and system and electronic equipment

By extracting and classifying annotation information during compilation and converting source code using preset template files, the problems of high external dependencies, low security, low efficiency, and poor consistency of annotation code in existing technologies are solved, achieving efficient and secure code generation.

CN121657982APending Publication Date: 2026-03-13SHANGHAI WENDIE NETWORK TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-12-10
Publication Date
2026-03-13

AI Technical Summary

Technical Problem

In existing technologies, the annotation code corresponding to the generated data structure suffers from problems such as high external dependencies, low security, low efficiency, and poor code consistency.

Method used

By obtaining the abstract syntax tree of the source code, extracting annotation information and classifying and storing it, and using a preset template file to convert the code format, a code file to be compiled is generated, thus realizing compile-time code generation.

Benefits of technology

It has no additional runtime overhead, improves the efficiency and security of code generation, reduces maintenance costs, and ensures code consistency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121657982A_ABST
    Figure CN121657982A_ABST
Patent Text Reader

Abstract

The invention provides a compiling period code generation method and system and electronic equipment, and the method comprises the steps: obtaining a source code, generating an abstract syntax tree of the source code, extracting annotation information based on the abstract syntax tree, carrying out the classified storage of the annotation information, and obtaining a first mapping relation corresponding to class annotations and a second mapping relation corresponding to field annotations, the structural bodies in the abstract syntax tree are subjected to description conversion in a classified mode according to the first mapping relation and the second mapping relation, metadata used for describing the structural bodies are obtained, the metadata is associated with corresponding preset template files according to the second mapping relation, and the preset template files comprise template codes of plain texts. Performing code format conversion on the metadata according to an associated preset template file to obtain a to-be-compiled code file, and compiling the to-be-compiled code file to obtain an executable file corresponding to the source code; according to the method, code generation is realized in a non-intrusive manner in the compiling period, and the consistency, efficiency and safety of code generation are improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of computer software compilation technology, and in particular to a method, system and electronic device for compiling code. Background Technology

[0002] Game development involves defining a large number of data structures and writing their serialization code. Related technologies utilize Interface Definition Language (IDL)-dependent solutions, runtime reflection solutions, and code generation tools to generate the corresponding serialization code.

[0003] However, IDL-dependent solutions, such as Protobuf (an efficient data serialization format for efficiently transferring structured data between different systems), suffer from issues such as the need to maintain independent interface definition files, synchronization problems between source code and IDL files, and inconsistencies between generated code style and project specifications. Runtime reflection solutions lack compile-time type safety checks and introduce additional performance overhead, such as a 30% reduction in throughput in Java reflection. Code generation tools require independent preprocessing steps, such as pre-compilation in Thrift, and cannot utilize native language features, such as template metaprogramming in C++. Summary of the Invention

[0004] This invention provides a compile-time code generation method, system, and electronic device to solve the technical problems of high external dependencies, low security, low efficiency, and poor code consistency when generating code corresponding to annotations of data structures.

[0005] This invention provides a compile-time code generation method, the method comprising: acquiring source code; generating an abstract syntax tree (AST) of the source code, and extracting annotation information based on the AST, the annotation information including class annotations for marking structures requiring code transformation, and field annotations for marking processing rules of fields in the structures; classifying and storing the annotation information to obtain a first mapping relationship corresponding to the class annotations and a second mapping relationship corresponding to the field annotations; classifying and transforming the structures in the AST according to the first mapping relationship and the second mapping relationship to obtain metadata for describing the structures, and associating the metadata with a corresponding preset template file according to the second mapping relationship, the preset template file including plain text template code; performing code format conversion on the metadata according to the associated preset template file to obtain a code file to be compiled, and compiling the code file to be compiled to obtain an executable file corresponding to the source code.

[0006] In one embodiment of the present invention, the annotation information is classified and stored to obtain a first mapping relationship corresponding to the class annotation and a second mapping relationship corresponding to the field annotation, including: generating a first mapping relationship corresponding to the class annotation based on the name of the structure in the annotation information and the corresponding preset first annotation identifier, wherein the preset first annotation identifier includes a component identifier and / or a general identifier; and generating a second mapping relationship corresponding to the field annotation based on the name of the field in the annotation information and the corresponding processing rule, wherein the processing rule includes a language type and / or a rule attribute.

[0007] In one embodiment of the present invention, extracting annotation information based on the abstract syntax tree includes: extracting class annotations of structures in the abstract syntax tree according to the preset first annotation identifier, and ignoring structures that are not marked with the preset first annotation identifier; if a structure has a class annotation, then extracting field annotations of fields in the structure according to the preset second annotation identifier and the abstract syntax tree, and ignoring fields that are not marked with the preset second annotation identifier, wherein the preset second annotation identifier includes a field identifier.

[0008] In one embodiment of the present invention, the structures in the abstract syntax tree are classified and described according to the first mapping relationship and the second mapping relationship to obtain metadata describing the structures, and the metadata is associated with the corresponding preset template file according to the second mapping relationship. This includes: reading the structures in the abstract syntax tree according to the first mapping relationship and the second mapping relationship, and performing description transformation to obtain metadata describing the structures, wherein the metadata includes field-level metadata and structure-level metadata; obtaining the corresponding preset template file from memory through a template runtime instance and performing syntax detection; and associating the fields in the metadata with the template runtime instance of the corresponding processing rule based on the second mapping relationship.

[0009] In one embodiment of the present invention, before obtaining the metadata used to describe the structure, the method further includes: establishing an annotation template mapping channel; and reading a preset template file corresponding to the processing rule into the memory through the annotation template mapping channel.

[0010] In one embodiment of the present invention, the metadata is converted into a code format according to the associated preset template file to obtain a code file to be compiled, including: converting the fields in the metadata into the corresponding code format according to the associated preset template file to obtain a target code file; generating an auxiliary function file corresponding to the target code file based on the metadata and the preset template file; wherein the code file to be compiled includes the target code file and the auxiliary function file.

[0011] In one embodiment of the present invention, the metadata is converted into a code format according to the associated preset template file to obtain a code file to be compiled, including: converting the fields in the metadata into corresponding code formats according to the rule attributes in the metadata and the associated preset template file to obtain a target code file, wherein the code format includes at least one of serialized code, deserialized code, auxiliary function logic, and multi-language adaptation code, and the target code file provides a calling interface for the source code; generating an auxiliary function file based on the preset template file and the metadata, wherein the auxiliary function file includes macro definitions and object processing functions corresponding to the auxiliary function logic.

[0012] In one embodiment of the present invention, after generating the abstract syntax tree of the source code, the method further includes: performing anomaly detection on the annotation information corresponding to the abstract syntax tree; if the result of the anomaly detection is an anomaly, then terminating code generation and generating a correction prompt; wherein, the anomaly detection includes annotation format detection, preset template file reading status detection, class annotation and field annotation conflict detection, and field unique identifier duplicate detection.

[0013] This invention provides a compile-time code generation system, comprising: an acquisition module for acquiring source code; an annotation extraction module for generating an abstract syntax tree (AST) of the source code and extracting annotation information based on the AST, the annotation information including class annotations for marking structures requiring code transformation and field annotations for marking processing rules of fields in the structures; an annotation classification module for classifying and storing the annotation information to obtain a first mapping relationship corresponding to the class annotations and a second mapping relationship corresponding to the field annotations; a template association module for classifying and transforming the structures in the AST according to the first and second mapping relationships to obtain metadata describing the structures, and associating the metadata with corresponding preset template files according to the second mapping relationship, the preset template files including plain text template code; and a code generation module for performing code format conversion on the metadata according to the associated preset template files to obtain a code file to be compiled, and compiling the code file to be compiled to obtain an executable file corresponding to the source code.

[0014] The present invention provides an electronic device, the electronic device comprising: one or more processors; and a storage device for storing one or more programs, wherein when the one or more programs are executed by the one or more processors, the electronic device enables the compile-time code generation method as described in any of the above embodiments.

[0015] The beneficial effects of this invention are as follows: The compile-time code generation method, system, and electronic device proposed in this invention, by classifying and storing annotation information and plain text template code, converts the source code into a code format to obtain the corresponding code file to be compiled, thereby compiling and generating the corresponding executable file. Code generation is completed during the compile-time period without additional runtime overhead. Furthermore, the annotation information is non-intrusive, which can reduce maintenance costs. The compile-time processing does not affect runtime behavior. The plain text template code improves code consistency, code generation efficiency, and security. Attached Figure Description

[0016] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with this application and, together with the description, serve to explain the principles of this application. It is obvious that the drawings described below are merely some embodiments of this application, and those skilled in the art can obtain other drawings based on these drawings without any inventive effort.

[0017] In the attached diagram: Figure 1 A schematic diagram of an exemplary system architecture provided in an embodiment of the present invention; Figure 2 This is a flowchart illustrating a compile-time code generation method provided in one embodiment of the present invention; Figure 3 This is a schematic diagram illustrating the code generation of a nested structure provided in one embodiment of the present invention; Figure 4 This is a schematic diagram illustrating the implementation of the compile-time code generation method provided in one embodiment of the present invention; Figure 5(a) is a schematic diagram of the target code file provided in one embodiment of the present invention; Figure 5(b) is a schematic diagram of the target code file provided in another embodiment of the present invention; Figure 6 This is a block diagram of a compile-time code generation system provided in one embodiment of the present invention; Figure 7 This is a schematic diagram of the structure of a computer system for an electronic device provided in one embodiment of the present invention. Detailed Implementation

[0018] The following specific examples illustrate the implementation of the present invention. Those skilled in the art can easily understand other advantages and effects of the present invention from the content disclosed in this specification. The present invention can also be implemented or applied through other different specific embodiments. Various details in this specification can also be modified or changed based on different viewpoints and applications without departing from the spirit of the present invention. In the absence of conflict, the following embodiments and features in the embodiments can be combined with each other.

[0019] It should be noted that the illustrations provided in the following embodiments are only schematic representations of the basic concept of the present invention. The drawings only show the components related to the present invention and are not drawn according to the actual number, shape and size of the components in the actual implementation. In the actual implementation, the form, quantity and proportion of each component can be arbitrarily changed, and the layout of the components may also be more complex.

[0020] In the following description, numerous details are explored to provide a more thorough explanation of embodiments of the invention. However, it will be apparent to those skilled in the art that embodiments of the invention may be practiced without these specific details. In other embodiments, well-known structures and devices are shown in block diagram form rather than in detail to avoid obscuring embodiments of the invention.

[0021] In related technologies, when generating code corresponding to annotations for data structures, there are technical problems such as high external dependencies, low security, low efficiency, and poor code consistency.

[0022] Please see Figure 1 , Figure 1 This is a schematic diagram of an exemplary system architecture provided in an embodiment of the present invention. Figure 1 As shown, the system architecture may include a user space 110, a code generation tool 120, and a build system 130. The user space 110 provides annotated source code to the code generation tool 120, which generates object code files and corresponding helper function files. The build system 130 calls the compiler and linker to obtain the corresponding executable file.

[0023] For example, the code generation tool 120 acquires the source code; generates an abstract syntax tree (AST) of the source code, and extracts annotation information based on the AST. The annotation information includes class annotations used to mark structures that require code transformation, and field annotations used to mark the processing rules of fields in the structures. The annotation information is classified and stored to obtain a first mapping relationship corresponding to class annotations and a second mapping relationship corresponding to field annotations. The structures in the AST are classified and transformed according to the first and second mapping relationships to obtain metadata used to describe the structures. The metadata is associated with the corresponding preset template file according to the second mapping relationship. The preset template file includes plain text template code. The metadata is converted into a code format according to the associated preset template file to obtain a code file to be compiled. The code file to be compiled is then compiled to obtain an executable file corresponding to the source code.

[0024] To address the aforementioned technical problems, this invention provides a compile-time code generation method, system, and electronic device. The implementation details of the technical solutions in the embodiments of this invention are described in detail below.

[0025] Please see Figure 2 , Figure 2 This is a flowchart illustrating a compile-time code generation method provided in one embodiment of the present invention. Figure 2 As shown, in an exemplary embodiment, the compile-time code generation method includes at least steps S210 to S250, which are described in detail below: Step S210: Obtain the source code.

[0026] In one embodiment of the present invention, source code marked with annotation information is read by the compiler front end.

[0027] In one embodiment of the invention, the compilers include the open-source Clang and GCC (GNU Compiler Collection).

[0028] Step S220: Generate an abstract syntax tree of the source code and extract annotation information based on the abstract syntax tree.

[0029] The annotation information includes class annotations used to mark the structure that requires code transformation, and field annotations used to mark the processing rules of the fields in the structure.

[0030] In one embodiment of the present invention, an Abstract Syntax Tree (AST) is used to represent source code in a tree-like manner on a syntactic structure.

[0031] In one embodiment of the present invention, the structure is a nested structure or a single structure.

[0032] In one embodiment of the present invention, the method of extracting annotation information based on an abstract syntax tree includes: extracting class annotations of structures in the abstract syntax tree according to a preset first annotation identifier, and ignoring structures that are not marked with the preset first annotation identifier; if a structure has a class annotation, then extracting field annotations of fields in the structure according to a preset second annotation identifier and the abstract syntax tree, and ignoring fields that are not marked with the preset second annotation identifier, wherein the preset second annotation identifier includes a field identifier.

[0033] In one embodiment of the present invention, the code generation (Gen) tool includes an annotation extraction module to extract annotation information. The annotation extraction module can capture annotation information using tools for analyzing, refactoring, and transforming source code (such as Clang LibTooling), thereby maintaining deep integration with native language syntax parsing.

[0034] In one embodiment of the present invention, the component class structure of the component identifier is used to represent a data container that can be attached to an entity in a data-driven software architecture pattern. The general identifier is used to annotate ordinary class structures, that is, non-component class structures, and the component class structure may include ordinary class structures, that is, nested structures.

[0035] In one embodiment of the present invention, an embedded annotation system is used to provide compiler-recognizable macro tags, namely, a preset first annotation identifier and a preset second annotation identifier. For example, component identifiers are represented by GenCmp, general identifiers by GenType, and field identifiers by GenProp. For example: `#define GenType(X, ...) X __attribute__((annotate("class:"#__VA_ARGS__)))`; where `#define` is a preprocessor directive, `GenType` is a macro name, also known as a general identifier, used to mark the existence of annotation information in ordinary class structures, `X` in `(X, ...)` is the type name, `...` in `(X, ...)` are variable arguments, i.e., the annotation information corresponding to class annotations, `__attribute__` is the attribute syntax keyword corresponding to GCC / Clang, `annotate` is the annotation attribute, `"class:"` is a fixed prefix used to identify class annotations, and `#__VA_ARGS__` is used to convert the variable arguments in `(X, ...)` into strings.

[0036] For example, `#define GenProp(...) __attribute__((annotate("prop:" #__VA_ARGS__)))`; `#define` is a preprocessor directive, `GenProp` is the macro name, i.e., the field identifier, used to mark that the field has annotation information, `(...)` are variable arguments, i.e., the annotation information corresponding to the field annotation, `__attribute__` is the attribute syntax keyword corresponding to GCC / Clang, `annotate` is the annotation attribute, `"prop:"` is the annotation prefix, used to identify the field annotation, and `#__VA_ARGS__` converts the variable arguments `(...)` into a string.

[0037] In one embodiment of the present invention, in addition to the attribute((annotate)) macro, the [[GenType]] attribute syntax of C++20 can also be used, or the annotation function can be implemented through preprocessor macro expansion.

[0038] In one embodiment of the present invention, the field is annotated using a structure of "field identifier (unique identity identifier, processing rule)".

[0039] In one embodiment of the present invention, the processing rules include a language type and rule attributes. The language type includes at least one of serialization, scripting languages ​​(such as Lua), data exchange format languages ​​(such as JSON), and programming languages ​​(such as C++, Golang). The rule attributes include at least one of database storage (db), broadcast message (replication, rep) generation, and key tagging rules (such as tagging as CSV).

[0040] In one embodiment of the present invention, the following is an example of extracting annotation information corresponding to a single structure: GenCmp(struct)Version / / Annotation for the class corresponding to the component identifier: tells the compiler to parse this class. { GenProp(json = csv, id = 1) / / Field annotation for field1 field1... GenProp(serialize = rep, id = 2) / / Field annotation for field2 field2... GenProp(serialize = db, id = 3) / / Field annotation for field 3 field3.... GenProp(serialize=db|rep, id = 4) / / Field annotation for field4 field4... bool is_success=false; / / Do nothing. Meta(luafun) void CreateUser(); / / Reads the Lua template and generates Lua helper code } Specifically, `GenProp(json = csv, id = 1)` identifies field 1, which is used for serializing and deserializing JSON objects, and marks the field key as `csv`. `GenProp(serialize = rep, id = 2)` identifies field 2, which is used for broadcast message generation. `GenProp(serialize = db, id = 3)` identifies field 3, which is used for database storage serialization. `GenProp(serialize = db | rep)` identifies field 4, which supports both database storage and broadcast message generation. Auxiliary fields without the `GenProp` annotation are not included in the parsing scope. Parsing represents the code transformation process after understanding the structure and meaning of the source code.

[0041] In one embodiment of the present invention, the following is an example of extracting annotation information corresponding to a nested structure: GenType(struct) Date / / Class annotation corresponding to the generic identifier: tells the compiler to parse this class. {...} GenType(struct) Information / / Class annotation corresponding to the generic identifier: tells the compiler to parse this class. { GenProp(serialize = db, id = 1) / / Field annotation: Date field used for database storage Date date1; GenProp(serialize = rep, id = 1) / / Field annotation: The date field is used for broadcast message generation Date date2; } GenCmp(struct) Version / / Class annotation corresponding to the component identifier: tells the compiler to parse this class. { GenProp(serialize = db, id = 1) / / Field annotation: used for database storage Information info; bool is_success = false; / / Auxiliary field, no parsing is performed. } In particular, structs with unmarked class annotations and fields with unmarked field annotations will be ignored by the AST.

[0042] In one embodiment of the present invention, the outer annotation in a nested structure can be an inner structure.

[0043] This invention implements the following annotation functions through an embedded annotation system: declaring data structures in the source code using specific annotation syntax (such as GenType, GenProp, etc.); annotating attribute-level metadata, such as the unique identifier (id) and default value of a field; supporting nested structure definitions; and controlling field-level serialization strategies, such as generating code for persistence purposes like serialize = db.

[0044] Step S230: Classify and store the annotation information to obtain the first mapping relationship corresponding to class annotations and the second mapping relationship corresponding to field annotations.

[0045] In one embodiment of the present invention, the annotation information is classified and stored to obtain a first mapping relationship corresponding to class annotations and a second mapping relationship corresponding to field annotations. This includes: generating a first mapping relationship corresponding to class annotations based on the name of the structure in the annotation information and the corresponding preset first annotation identifier, wherein the preset first annotation identifier includes component identifier and / or general identifier; and generating a second mapping relationship corresponding to field annotations based on the name of the field in the annotation information and the corresponding processing rule, wherein the processing rule includes language type and / or rule attribute.

[0046] In one embodiment of the present invention, the structure type is distinguished by the GenType and GenCmp identifiers, and a class-annotation mapping table is generated, that is, the first mapping relationship, such as Date→GenType, Version→GenCmp.

[0047] In one embodiment of the present invention, a field-processing rule list, i.e. a second mapping relationship, is generated by grouping GenProp's language types such as serialize, json and lua and / or rule attributes (such as db, rep, db|rep). The mapping is: field1→json,csv, field2→serialize,rep, field3→serialize,db.

[0048] In one embodiment of the present invention, type conversion between different languages ​​can be achieved through a field-processing rule list.

[0049] Step S240: Based on the first mapping relationship and the second mapping relationship, the structures in the abstract syntax tree are classified and transformed to obtain metadata for describing the structures, and the metadata is associated with the corresponding preset template file according to the second mapping relationship.

[0050] The preset template file includes plain text template code.

[0051] In one embodiment of the present invention, before obtaining the metadata used to describe the structure, the method further includes: establishing an annotation template mapping channel; and reading a preset template file corresponding to the processing rule into memory through the annotation template mapping channel.

[0052] In one embodiment of the present invention, the Gen tool is automatically loaded upon compiler front-end startup to establish an annotation template mapping channel. The Gen tool categorizes and processes the extracted annotation information and reads the corresponding preset template file. Loading involves reading the corresponding data or program from the storage medium into memory, making it usable.

[0053] In one embodiment of the present invention, the Gen tool reads a separately stored preset template file, such as a JSON template, a serialize template, or a Lua template. The preset template file is decoupled from the Gen tool and read by the AST parsing runtime instance, supporting dynamic updates and extensions. For example, an XML (Extensible Markup Language) serialization template can be added through the template manager in the Gen tool without modifying the core logic of the Gen tool.

[0054] In one embodiment of the present invention, the structures in the abstract syntax tree are classified and described according to a first mapping relationship and a second mapping relationship to obtain metadata for describing the structures, and the metadata is associated with the corresponding preset template files according to the second mapping relationship. This includes: reading the structures in the abstract syntax tree according to the first mapping relationship and the second mapping relationship, and performing description transformation to obtain metadata for describing the structures, the metadata including field-level metadata and structure-level metadata; obtaining the corresponding preset template files from memory through a template runtime instance and performing syntax detection; and associating the fields in the metadata with the template runtime instances of the corresponding processing rules based on the second mapping relationship.

[0055] In one embodiment of the present invention, class annotations and field annotations are organized according to the structure hierarchy obtained from the abstract syntax tree to generate corresponding metadata.

[0056] In one embodiment of the present invention, the structures in the abstract syntax tree are read according to the first mapping relationship and the second mapping relationship, and description transformation is performed to obtain metadata for describing the structures. This includes: determining multiple structures to be transformed according to the first mapping relationship; recursively performing description transformation operations on each structure to be transformed according to the abstract syntax tree to generate corresponding metadata; the description transformation operation includes: constructing corresponding structure-level metadata based on the structure hierarchy relationship obtained from the abstract syntax tree, the structure-level metadata including annotation information corresponding to class annotations and an overall description of the structure; constructing corresponding field-level metadata for fields with field annotations in the structure based on the second mapping relationship, the field-level metadata including field description information and annotation information corresponding to field annotations; if the field type is a structure and the structure has a class annotation in the first mapping relationship, then the description transformation operation is recursively performed to generate the metadata of the structure, and the nesting relationship is maintained in the current field-level metadata to maintain the hierarchical structure of the source code.

[0057] In one embodiment of the present invention, the Gen tool calls the AST parsing runtime instance, loads the structure of the tag class annotation (such as Date, Information, Version), and generates metadata containing information such as the type, field name, and field annotation of the corresponding field in the structure.

[0058] In one embodiment of the present invention, metadata is managed through a metadata manager in the Gen tool.

[0059] In one embodiment of the present invention, when generating metadata, the type (CV) qualifiers and template instantiation information in the source code must be retained. The CV qualifiers include constant qualifiers and variable qualifiers; the template instantiation information includes the instantiated data corresponding to the data structure.

[0060] In one embodiment of the present invention, the structure-level metadata is as follows, corresponding to GenType: struct Record { / / Describes the metadata of a structure using the Record structure. struct EnumType { / / Structure of enumeration type std::string name; / / Name of the enumeration item int index = 0; / / Enumeration value of the enumeration item }; std::string qualified_name; / / Fully qualified name (e.g., ::ns::Class) to identify the complete namespace of this structure and avoid naming conflicts. AnnotationClass annotation; / / Information about class annotations std::vector <field>fields; / / Collection of fields is_enum_type = false; / / Structure type distinguishing flag }; In one embodiment of the present invention, the field-level metadata is as follows, corresponding to GenProp: struct Field { / / Describes the metadata corresponding to a field through the Field structure `Type type;` / / Field type, such as fully preserving the C++ type system. std::string name; / / Field name std::string location; / / Source code location, such as line number, file AnnotationProp annotation; / / Annotation information for field annotations, such as processing rules for id, json, etc. std::vector <field>fields; / / Nested fields supported }; In one embodiment of the present invention, based on the classification results of field annotations, fields with field annotations are bound to the corresponding template runtime instances of preset template files. For example: Fields marked serialize=db: Associated with the template runtime instance holding the serialize template to generate db serialization or deserialization code; The field marked serialize = rep: associates with the runtime instance of the template holding the serialize template to generate message broadcast code; Fields marked json = csv: Associated with the runtime instance of the template corresponding to the json template to generate csv fields of json; The field marked serialize = db | rep: also associates the template runtime instance corresponding to the serialize template to support the generation of two types of module target code files.

[0061] In one embodiment of the present invention, the template runtime instance reads the corresponding preset template file from memory, checks the syntax rules of the preset template file, such as the key-value pair format of the JSON template and the field storage mapping rules of the DB template, and provides syntax support for subsequent code generation.

[0062] This invention ensures the precise association between field annotations and the preset template file by parsing the field annotations of the structure and combining them with the core logic of obtaining the AST at compile time and mapping it to the data of the template runtime instance in memory.

[0063] Step S250: Convert the metadata into a code format according to the associated preset template file to obtain the code file to be compiled, and compile the code file to obtain the executable file corresponding to the source code.

[0064] In one embodiment of the present invention, the metadata is converted into a code format according to an associated preset template file to obtain a code file to be compiled, including: converting the fields in the metadata into the corresponding code format according to the associated preset template file to obtain a target code file; generating an auxiliary function file corresponding to the target code file based on the metadata and the preset template file; wherein, the code file to be compiled includes the target code file and the auxiliary function file.

[0065] In one embodiment of the present invention, the metadata is converted into a code format according to an associated preset template file to obtain a code file to be compiled. This includes: converting the fields in the metadata into corresponding code formats according to the rule attributes in the metadata and the associated preset template file to obtain a target code file. The code format includes at least one of serialized code, deserialized code, auxiliary function logic, and multi-language adaptation code. The target code file provides a calling interface for the source code. An auxiliary function file is generated based on the preset template file and the metadata. The auxiliary function file includes macro definitions and object processing functions corresponding to the auxiliary function logic.

[0066] In one embodiment of the present invention, the Gen tool generates a target code file containing the following logic in one go, based on the AST and template runtime instance: Serialization code: for example, `json = csv` generates a CSV format serialization function; `serialize = db` generates a database storage format serialization function, such as `SerializeToDB`; Deserialization code: such as loading objects from JSON, restoring objects from database strings, such as DeserializeFromDB; Auxiliary function logic: such as object creation, archive structure generation, and broadcast message generation; Multi-language adaptation code: such as generating C++ language broadcast message code for serialize = rep, or generating Lua file helper functions, such as Meta(luafun) helper tags.

[0067] In one embodiment of the present invention, please refer to Figure 3 , Figure 3 This is a schematic diagram illustrating the code generation of a nested structure provided in one embodiment of the present invention. For example... Figure 3 As shown, the AST loads the Version structure; the info field is marked as needing parsing, and is_success is not marked and is ignored; the AST loads the Information structure; the date1 field is marked as needing to parse db, and associated with the template runtime instance holding the serialize db template to generate the DB storage module; the date2 field is marked as needing to parse rep, and associated with the template runtime instance holding the serialize rep template to generate the Rep message broadcast module; finally, the AST loads the Date structure, and the corresponding structure can be parsed layer by layer through the metadata determined by the structure hierarchy relationship in the AST.

[0068] In one embodiment of the present invention, the target code file can hide the specific implementation details of functions such as serialization, deserialization, object function, and database storage in the source code, and only expose the calling interface. For example, it can hide the storage encryption logic of the db field and the transmission protocol of rep messages, thus providing high security.

[0069] In one embodiment of the present invention, the code generation tool further includes a template rendering module and a template engine. The template rendering module converts the data type of the metadata into the target language type, and the template engine then populates the converted data according to an associated preset template file to obtain the target code file.

[0070] In one embodiment of the invention, the template engine includes at least one of a mustache engine, Jinja2, and a custom string concatenation engine for populating metadata and template code.

[0071] This invention dynamically generates different language or function names by parsing annotation fields once and reading different preset template files, ensuring the integrity and reusability of the target code file.

[0072] In one embodiment of the present invention, a preset template file is parsed, and an auxiliary function file corresponding to the structure is generated according to the AST, including: macro definitions such as #define VERSION_SAVE_DB... (save to database) and #define VERSION_LOAD_DB... (load from database); and object processing functions such as save as JSON and save as broadcast message.

[0073] In one embodiment of the present invention, compiling the executable file corresponding to the source code includes: integrating the target code file and the auxiliary function file to obtain an intermediate representation; generating machine code for the target platform based on the intermediate representation; and combining the machine code and the dependent runtime libraries to obtain the executable file.

[0074] In one embodiment of the present invention, the target code file and the auxiliary function file are integrated by the compiler front end to generate the corresponding intermediate representation (IR); the compiler back end reads the IR, parses it and generates machine code for the target platform, such as an x86 / ARM architecture program; the machine code and dependent runtime libraries, such as template runtime library and serialization library, are combined by the linker to generate an executable program, that is, an executable file. In one embodiment of the present invention, after obtaining the executable file, the executable file is run to verify the core functions, such as: structure storage: data is written to / read from the database through the serialize=db field; message broadcasting: broadcast messages are generated and sent through the serialize=rep field, optional: encryption is supported; serialization / deserialization: the serialize=json field completes the conversion between objects and JSON, such as saving as JSON or loading from JSON.

[0075] In one embodiment of the present invention, please refer to Figure 4 , Figure 4 This is a schematic diagram illustrating an embodiment of the compile-time code generation method provided in this invention. For example... Figure 4 As shown, the compiler frontend reads the source code and generates an abstract syntax tree (AST). Annotation information is then categorized using a code generation tool and the AST. A template runtime instance reads and inspects preset template files, associating fields with their corresponding runtime instances to generate helper function files and object code files. The compiler backend then generates the corresponding machine code and combines it with dependent runtime libraries to produce an executable file. The preset template files include files for JSON templates, serialize templates, Lua templates, CSV serialization, broadcast message generation, serialization, and deserialization.

[0076] In one embodiment of the present invention, the helper function file includes processing logic for creating an object, saving it as JSON, loading it from the JSON, saving it as a broadcast message, loading it from the broadcast message, saving it to a database, and loading at least one helper function from the database. For example, saving it as a broadcast message can be achieved by using #define VERSION_SAVE_MSG.

[0077] In one embodiment of the present invention, there are multiple target code files. Please refer to Figures 5(a) and 5(b). Figure 5(a) is a schematic diagram of the target code files provided in one embodiment of the present invention, and Figure 5(b) is a schematic diagram of the target code files provided in another embodiment of the present invention. As shown in Figures 5(a) and 5(b), the two target code files are generated based on the annotation information corresponding to a single structure. The first target code file includes the target code corresponding to field1; the second target code file includes the target code corresponding to fields2 to 4.

[0078] In one embodiment of the present invention, after generating the abstract syntax tree of the source code, the method further includes: performing anomaly detection on the annotation information corresponding to the abstract syntax tree; if the result of the anomaly detection is an anomaly, then terminating the code generation and generating a correction prompt; wherein, the anomaly detection includes annotation format detection, preset template file reading status detection, class annotation and field annotation conflict detection, and field unique identifier duplicate detection.

[0079] After generating the abstract syntax tree of the source code, the process also includes: performing anomaly detection on the annotation information corresponding to the abstract syntax tree; if the anomaly detection result is an anomaly, then code generation is terminated and a correction prompt is generated; wherein, the anomaly detection includes annotation format detection, reading status detection of preset template files, conflict detection of class annotations and field annotations, and detection that there are at least one of multiple identical unique identifiers in the fields of the structure marked by the same type of annotation. Annotation format detection includes the unique identifier of the field annotation and annotation attributes.

[0080] In one embodiment of the present invention, the Gen tool embeds error detection logic to detect the following abnormal scenarios: Annotation format error: The field's unique identifier is not a number, such as GenProp(id=A); Preset template file reading status detection, that is, field annotations do not match the preset template file, such as serialize = xml but the xml template is not loaded; Duplicate IDs: Within the same GenCmp annotation structure, there are fields with the same ID; Conflict detection between class annotations and field annotations: For example, if a struct is not marked with GenType / GenCmp, but a field within that struct is marked with GenProp; In one embodiment of the present invention, if an error is detected, the Gen tool terminates the code generation process and outputs a correction prompt on the user interface. The correction prompt includes error information and correction direction; wherein, the error information, such as the GenProp annotation of field1 lacking processing rules or not finding the preset template file corresponding to serialize=xml, ensures the correctness of annotation parsing and reduces the risk of subsequent compilation failures and runtime errors.

[0081] This invention implements code generation functionality through the annotation extraction module, template manager, metadata manager, template rendering module, and template engine in the Gen tool.

[0082] Compared to interface definition language (IDL)-dependent solutions widely used in microservice communication, such as Protobuf, this invention differs substantially from Protobuf in terms of code maintenance, development process, runtime performance and flexibility, and compatibility with the existing ecosystem.

[0083] Regarding code maintenance: Protobuf requires developers to maintain .proto files separately from business code; when data structures change, both the .proto files and source code need to be modified simultaneously, making it easy to miss or become out of sync due to dual maintenance; it is also highly intrusive, as the generated code usually exists as a separate file, potentially affecting the project structure. In contrast, this invention directly uses annotations such as GenType and GenProp in the source code, eliminating the need for additional files; when modifying data structures, only the annotation information in the source code needs to be adjusted, without synchronizing other files; annotation identification does not affect the original code semantics, and after compilation, it does not affect runtime behavior, resulting in no code intrusion, thus reducing maintenance costs and avoiding synchronization issues.

[0084] In terms of development process: Protobuf requires additional build steps, namely, compiling the .proto file to generate code before integrating it into the project; multi-language support requires explicit generation, meaning that serialization code for different languages ​​needs to be manually generated by calling protoc. In contrast, this invention directly generates target language code through a template engine, eliminating the need to manually call code generation tools, offering greater flexibility and supporting custom target languages; it is also IDE-friendly, with annotation information coexisting with source code, and intelligent code completion and refactoring tools (such as Clang-Tidy) still functioning normally.

[0085] Regarding runtime performance and flexibility: Protobuf requires linking to its runtime library, which may increase binary size; some serialization operations depend on runtime type information, and the overhead of reflection mechanisms may affect performance. In contrast, this invention has zero runtime overhead, with all code generation completed at compile time, resulting in no additional runtime overhead and thus providing higher performance and smaller binary size; the generated serialization code is static and can be optimized by the compiler, such as inlining; new template code can be customized for different scenarios, such as high-performance binary protocols and JSON compatibility.

[0086] Regarding compatibility with the existing ecosystem: Protobuf has strong coupling in encoding formats, requiring serialized data to conform to the Protobuf binary format; cross-language compatibility is limited, such as C++ templates and Java generics not being directly mapped to IDL. This invention, however, can freely choose to generate serialization code in different formats such as JSON, binary, and XML, adapting to different protocol requirements; it can directly utilize the type system in the source code, such as C++'s `std::vector` and default values, without adaptation, and supports more complex generics, templates, and other features; code generation can be enabled only for certain data structures without affecting existing source code.

[0087] Please see Figure 6 , Figure 6 This is a block diagram of a compile-time code generation system provided in one embodiment of the present invention. This system can be applied to... Figure 1 The implementation environment shown is not limited to this system. This system can also be applied to other exemplary implementation environments and specifically configured in other devices.

[0088] like Figure 6 As shown, a compile-time code generation system 600 according to an embodiment of the present invention includes: an acquisition module 610, an annotation extraction module 620, an annotation classification module 630, a template association module 640, and a code generation module 650.

[0089] Among them, the acquisition module 610 is used to acquire source code; The annotation extraction module 620 is used to generate an abstract syntax tree of the source code and extract annotation information based on the abstract syntax tree. The annotation information includes class annotations used to mark the code transformation required for the structure, and field annotations used to mark the processing rules of the fields in the structure. The annotation classification module 630 is used to classify and store annotation information to obtain the first mapping relationship corresponding to class annotations and the second mapping relationship corresponding to field annotations. The template association module 640 is used to classify and transform the structures in the abstract syntax tree according to the first mapping relationship and the second mapping relationship to obtain metadata for describing the structures, and associate the metadata with the corresponding preset template file according to the second mapping relationship. The preset template file includes plain text template code. The code generation module 650 is used to convert the metadata into a code format based on the associated preset template file to obtain a code file to be compiled, and then compile the code file to obtain the executable file corresponding to the source code.

[0090] It should be noted that the compile-time code generation system and the compile-time code generation method provided in the above embodiments belong to the same concept. The specific methods of execution of each module and unit have been described in detail in the method embodiments and will not be repeated here. In practical applications, the compile-time code generation system provided in the above embodiments can be assigned to different functional modules as needed, that is, the internal structure of the system can be divided into different functional modules to complete all or part of the functions described above. This is not a limitation here.

[0091] Embodiments of the present invention also provide an electronic device, including: one or more processors; and a storage device for storing one or more programs, wherein when the one or more programs are executed by the one or more processors, the electronic device implements the compile-time code generation method provided in the above embodiments.

[0092] Please see Figure 7 , Figure 7 This is a schematic diagram of the structure of a computer system for an electronic device provided in one embodiment of the present invention. Figure 7 The computer system 700 of the illustrated electronic device is merely an example and should not impose any limitation on the functionality and scope of use of the embodiments of the present invention.

[0093] like Figure 7 As shown, the computer system 700 includes a central processing unit 701, which can perform various appropriate actions and processes based on a program stored in a read-only memory 702 or a program loaded from a storage section 708 into a random access memory 703, such as executing the methods described in the above embodiments. The random access memory 703 also stores various programs and data required for system operation. The central processing unit 701, the read-only memory 702, and the random access memory 703 are interconnected via a bus 704. An input / output interface 705 is also connected to the bus 704.

[0094] The following components are connected to the input / output interface 705: an input section 706 including a keyboard, mouse, etc.; an output section 707 including a cathode ray tube (CRT), liquid crystal display (LCD), etc., and speakers, etc.; a storage section 708 including a hard disk, etc.; and a communication section 709 including a network interface card such as a LAN (Local Area Network) card, modem, etc. The communication section 709 performs communication processing via a network such as the Internet. A drive 710 is also connected to the input / output interface 705 as needed. A removable medium 711, such as a disk, optical disk, magneto-optical disk, semiconductor memory, etc., is installed on the drive 710 as needed so that computer programs read from it can be installed into the storage section 708 as needed.

[0095] In particular, according to embodiments of the present invention, the processes described above with reference to the flowcharts can be implemented as computer software programs. For example, embodiments of the present invention include a computer program product comprising a computer program carried on a computer-readable medium, the computer program containing computer programs for performing the methods shown in the flowcharts. In such embodiments, the computer program can be downloaded and installed from a network via communication section 709, and / or installed from removable medium 711. When the computer program is executed by central processing unit 701, it performs various functions defined in the system of the present invention.

[0096] The computer-readable medium shown in the embodiments of the present invention can be a computer-readable signal medium, a computer-readable storage medium, or any combination thereof. A computer-readable storage medium can be, for example, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination thereof. More specific examples of a computer-readable storage medium may include, but are not limited to: an electrical connection having one or more wires, a portable computer disk, a hard disk, a random access memory, a read-only memory, an erasable programmable read-only memory (EPROM), flash memory, optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination thereof. In the present invention, a computer-readable signal medium may include a data signal propagated in baseband or as part of a carrier wave, carrying a computer-readable computer program. Such propagated data signals may take various forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination thereof. A computer-readable signal medium may also be any computer-readable medium other than a computer-readable storage medium, which can send, propagate, or transmit a program for use by or in conjunction with an instruction execution system, apparatus, or device. Computer programs contained on computer-readable media can be transmitted using any suitable medium, including but not limited to wireless, wired, etc., or any suitable combination thereof.

[0097] The flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of systems, methods, and computer program products according to various embodiments of the present invention. Each block in a flowchart or block diagram may represent a module, segment, or portion of code containing one or more executable instructions for implementing a specified logical function. It should also be noted that in some alternative implementations, the functions indicated in the blocks may occur in a different order than those indicated in the drawings. For example, two consecutively indicated blocks may actually be executed substantially in parallel, and they may sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each block in a block diagram or flowchart, and combinations of blocks in a block diagram or flowchart, may be implemented using a dedicated hardware-based system that performs the specified function or operation, or using a combination of dedicated hardware and computer instructions.

[0098] The units described in the embodiments of the present invention can be implemented in software or hardware, and the described units can also be located in a processor. The names of these units do not necessarily limit the specific unit itself. Therefore, the technical solutions according to the embodiments of the present invention can be embodied in the form of a software product, which can be stored in a non-volatile storage medium (such as a CD-ROM, USB flash drive, portable hard drive, etc.) or on a network, including several instructions to cause a computing device (such as a personal computer, server, touch terminal, or network device, etc.) to execute the method according to the embodiments of the present invention.

[0099] Another aspect of the present invention provides a computer-readable storage medium storing a computer program thereon, which, when executed by a computer's processor, causes the computer to perform the compile-time code generation method provided in the above embodiments. This computer-readable storage medium may be included in the electronic device described in the above embodiments, or it may exist independently and not assembled into the electronic device.

[0100] In the above embodiments, unless otherwise specified, the use of ordinal numbers such as "first" and "second" to describe common objects only indicates that they refer to different instances of the same object, rather than indicating that the objects being described must be in a given order, whether temporally, spatially, sequentially, or in any other way.

[0101] The above embodiments are merely illustrative of the principles and effects of the present invention and are not intended to limit the invention. Any person skilled in the art can modify or alter the above embodiments without departing from the spirit and scope of the present invention. Therefore, all equivalent modifications or alterations made by those skilled in the art without departing from the spirit and technical concept disclosed in the present invention should still be covered by the claims of the present invention.< / field> < / field>

Claims

1. A compile-time code generation method, characterized in that, The method includes: Obtain the source code; An abstract syntax tree of the source code is generated, and annotation information is extracted based on the abstract syntax tree. The annotation information includes class annotations for marking the code transformation required for the structure, and field annotations for marking the processing rules of the fields in the structure. The annotation information is categorized and stored to obtain a first mapping relationship corresponding to the class annotation and a second mapping relationship corresponding to the field annotation; The structures in the abstract syntax tree are classified and described according to the first mapping relationship and the second mapping relationship to obtain metadata for describing the structures. The metadata is then associated with the corresponding preset template file according to the second mapping relationship. The preset template file includes plain text template code. The metadata is converted into a code format based on the associated preset template file to obtain a code file to be compiled, and the code file to be compiled is then compiled to obtain an executable file corresponding to the source code.

2. The compile-time code generation method according to claim 1, characterized in that, The annotation information is categorized and stored to obtain a first mapping relationship corresponding to the class annotation and a second mapping relationship corresponding to the field annotation, including: Based on the name of the structure in the annotation information and the corresponding preset first annotation identifier, a first mapping relationship corresponding to the class annotation is generated, wherein the preset first annotation identifier includes component identifier and / or general identifier; Based on the field names and corresponding processing rules in the annotation information, a second mapping relationship corresponding to the field annotation is generated, wherein the processing rules include language type and / or rule attributes.

3. The compile-time code generation method according to claim 2, characterized in that, Based on the abstract syntax tree, annotation information is extracted, including: Extract class annotations of structs in the abstract syntax tree based on the preset first annotation identifier, and ignore structs not marked with the preset first annotation identifier; If a structure has a class annotation, then the field annotations of the fields in the structure are extracted according to the preset second annotation identifier and the abstract syntax tree, and fields that are not marked with the preset second annotation identifier are ignored. The preset second annotation identifier includes field identifiers.

4. The compile-time code generation method according to claim 1, characterized in that, The structures in the abstract syntax tree are classified and described according to the first mapping relationship and the second mapping relationship to obtain metadata describing the structures. The metadata is then associated with corresponding preset template files according to the second mapping relationship, including: The structures in the abstract syntax tree are read according to the first mapping relationship and the second mapping relationship, and the description transformation is performed to obtain metadata for describing the structures. The metadata includes field-level metadata and structure-level metadata. The corresponding preset template file is retrieved from memory through the template runtime instance, and syntax checking is performed. Based on the second mapping relationship, the fields in the metadata are associated with the template runtime instance of the corresponding processing rule.

5. The compile-time code generation method according to claim 4, characterized in that, Before obtaining the metadata describing the structure, the process also includes: Establish annotation template mapping channels; The preset template file corresponding to the processing rule is read into the memory through the annotation template mapping channel.

6. The compile-time code generation method according to claim 1, characterized in that, The metadata is converted into a code format based on the associated preset template file to obtain a code file to be compiled, including: Based on the associated preset template file, the fields in the metadata are converted into the corresponding code format to obtain the target code file; Based on the metadata and the preset template file, generate the auxiliary function file corresponding to the target code file; The code file to be compiled includes the target code file and the helper function file.

7. The compile-time code generation method according to claim 6, characterized in that, The metadata is converted into a code format based on the associated preset template file to obtain a code file to be compiled, including: Based on the rule attributes in the metadata and the associated preset template file, the fields in the metadata are converted into corresponding code formats to obtain a target code file. The code format includes at least one of serialized code, deserialized code, auxiliary function logic, and multi-language adaptation code. The target code file provides a calling interface for the source code. A helper function file is generated based on the preset template file and the metadata. The helper function file includes macro definitions and object processing functions corresponding to the helper function logic.

8. The compile-time code generation method according to any one of claims 1-7, characterized in that, After generating the abstract syntax tree of the source code, the process also includes: Anomaly detection is performed on the annotation information corresponding to the abstract syntax tree; If the anomaly detection result is an anomaly, code generation will be terminated and a correction prompt will be generated; The anomaly detection includes annotation format detection, preset template file reading status detection, class annotation and field annotation conflict detection, and field unique identifier duplicate detection.

9. A compile-time code generation system, characterized in that, The system includes: The acquisition module is used to acquire source code; The annotation extraction module is used to generate an abstract syntax tree of the source code and extract annotation information based on the abstract syntax tree. The annotation information includes class annotations used to mark the code transformation required for the structure, and field annotations used to mark the processing rules of the fields in the structure. The annotation classification module is used to classify and store the annotation information to obtain a first mapping relationship corresponding to the class annotation and a second mapping relationship corresponding to the field annotation; The template association module is used to classify and transform the structures in the abstract syntax tree according to the first mapping relationship and the second mapping relationship to obtain metadata for describing the structures, and associate the metadata with the corresponding preset template file according to the second mapping relationship. The preset template file includes plain text template code. The code generation module is used to convert the metadata into a code format based on the associated preset template file to obtain a code file to be compiled, and to compile the code file to be compiled to obtain an executable file corresponding to the source code.

10. An electronic device, characterized in that, The electronic device includes: One or more processors; A storage device for storing one or more programs, which, when executed by one or more processors, cause the electronic device to implement the compile-time code generation method as described in any one of claims 1 to 8.