GDevelop Core
Core library for developing platforms and tools compatible with GDevelop.
|
Internal class used to generate code from events. More...
#include <EventsCodeGenerator.h>
Classes | |
struct | CallbackDescriptor |
Public Types | |
enum | VariableScope { LAYOUT_VARIABLE = 0 , PROJECT_VARIABLE , OBJECT_VARIABLE } |
Public Member Functions | |
EventsCodeGenerator (const gd::Project &project_, const gd::Layout &layout, const gd::Platform &platform_) | |
Construct a code generator for the specified platform/project/layout. | |
EventsCodeGenerator (const gd::Platform &platform, const gd::ProjectScopedContainers &projectScopedContainers_) | |
Construct a code generator for the specified objects/groups and platform. | |
void | PreprocessEventList (gd::EventsList &listEvent) |
Preprocess an events list (replacing for example links with the linked events). More... | |
virtual gd::String | GenerateEventsListCode (gd::EventsList &events, EventsCodeGenerationContext &context) |
Generate code for executing an event list. More... | |
virtual gd::String | GenerateConditionsListCode (gd::InstructionsList &conditions, EventsCodeGenerationContext &context) |
Generate code for executing a condition list. More... | |
virtual gd::String | GenerateActionsListCode (gd::InstructionsList &actions, EventsCodeGenerationContext &context) |
Generate code for executing an action list. More... | |
std::vector< gd::String > | GenerateParametersCodes (const std::vector< gd::Expression > ¶meters, const std::vector< gd::ParameterMetadata > ¶metersInfo, EventsCodeGenerationContext &context, std::vector< std::pair< gd::String, gd::String > > *supplementaryParametersTypes=0) |
Generate the code for a parameter of an action/condition/expression. More... | |
gd::String | GenerateConditionCode (gd::Instruction &condition, gd::String returnBoolean, EventsCodeGenerationContext &context) |
Generate code for a single condition. More... | |
gd::String | GenerateActionCode (gd::Instruction &action, EventsCodeGenerationContext &context, const gd::String &optionalAsyncCallbackName="") |
Generate code for a single action. More... | |
virtual const CallbackDescriptor | GenerateCallback (const gd::String &callbackFunctionName, gd::EventsCodeGenerationContext &parentContext, gd::InstructionsList &actions, gd::EventsList *subEvents=nullptr) |
Generates actions and events as a callback. More... | |
const gd::String | GenerateEventsParameters (const gd::EventsCodeGenerationContext &context) |
Generates the parameters list of an event's generated function. | |
virtual gd::String | GenerateObjectsDeclarationCode (EventsCodeGenerationContext &context) |
Generate code for declaring objects lists. More... | |
void | AddIncludeFile (gd::String file) |
Declare an include file to be added. More... | |
void | AddIncludeFiles (std::vector< gd::String > files) |
Declare a list of include files to be added. More... | |
void | AddGlobalDeclaration (gd::String declaration) |
Add a declaration which will be inserted after includes. | |
void | AddCustomCodeOutsideMain (gd::String code) |
Add some code before events outside the main function. | |
const std::set< gd::String > & | GetIncludeFiles () const |
Get the set containing the include files. | |
const gd::String & | GetCustomCodeOutsideMain () const |
Get the custom code to be inserted outside main. | |
const std::set< gd::String > & | GetCustomGlobalDeclaration () const |
Get the custom declaration to be inserted after includes. | |
bool | GenerateCodeForRuntime () |
Return true if code generation is made for runtime only. | |
void | SetGenerateCodeForRuntime (bool compilationForRuntime_) |
Set if the code generated is meant to be used for runtime only and not in the IDE. | |
void | ReportError () |
Report that an error occurred during code generation ( Event code won't be generated ) | |
bool | ErrorOccurred () const |
Return true if an error has occurred during code generation (in this case, generated code is not usable). More... | |
const gd::ObjectsContainersList & | GetObjectsContainersList () const |
const gd::ProjectScopedContainers & | GetProjectScopedContainers () const |
bool | HasProjectAndLayout () const |
Return true if the code generation is done for a given project and layout. If not, this means that the code is generated for a function. | |
const gd::Project & | GetProject () const |
Get the project the code is being generated for. More... | |
const gd::Layout & | GetLayout () const |
Get the layout the code is being generated for. More... | |
const gd::Platform & | GetPlatform () const |
Get the platform the code is being generated for. | |
size_t | GetMaxCustomConditionsDepth () const |
Get the maximum depth of custom conditions reached during code generation. | |
size_t | GetMaxConditionsListsSize () const |
Get the maximum size of a list of conditions. | |
virtual gd::String | GenerateBooleanFullName (const gd::String &boolName, const gd::EventsCodeGenerationContext &context) |
Generate the full name for accessing to a boolean variable used for conditions. More... | |
virtual gd::String | GenerateUpperScopeBooleanFullName (const gd::String &boolName, const gd::EventsCodeGenerationContext &context) |
Generate the full name for accessing to a boolean variable used for conditions. More... | |
virtual gd::String | GenerateBooleanInitializationToFalse (const gd::String &boolName, const gd::EventsCodeGenerationContext &context) |
Must create a boolean. Its value must be false. More... | |
virtual gd::String | GetObjectListName (const gd::String &name, const gd::EventsCodeGenerationContext &context) |
Get the full name for accessing to a list of objects. More... | |
virtual gd::String | GenerateProfilerSectionBegin (const gd::String §ion) |
Generate the code to notify the profiler of the beginning of a section. | |
virtual gd::String | GenerateProfilerSectionEnd (const gd::String §ion) |
Generate the code to notify the profiler of the end of a section. | |
virtual gd::String | GetCodeNamespaceAccessor () |
Get the namespace to be used to store code generated objects/values/functions, with the extra "dot" at the end to be used to access to a property/member. More... | |
virtual gd::String | GetCodeNamespace () |
Get the namespace to be used to store code generated objects/values/functions. More... | |
size_t | GenerateSingleUsageUniqueIdFor (const gd::Instruction *instruction) |
size_t | GenerateSingleUsageUniqueIdForEventsList () |
virtual gd::String | GenerateRelationalOperation (const gd::String &relationalOperator, const gd::String &lhs, const gd::String &rhs) |
Generate a relational operation. More... | |
Static Public Member Functions | |
static void | DeleteUselessEvents (gd::EventsList &events) |
Remove non executable events from the event list. | |
static gd::String | ConvertToString (gd::String plainString) |
Must convert a plain string ( with line feed, quotes ) to a string that can be inserted into code. More... | |
static gd::String | ConvertToStringExplicit (gd::String plainString) |
Convert a plain string (with line feed, quotes) to a string that can be inserted into code. The string construction must be explicit: for example, quotes must be added if the target language need quotes. More... | |
Protected Member Functions | |
virtual const gd::String | GenerateRelationalOperatorCodes (const gd::String &operatorString) |
virtual gd::String | GenerateParameterCodes (const gd::Expression ¶meter, const gd::ParameterMetadata &metadata, gd::EventsCodeGenerationContext &context, const gd::String &lastObjectName, std::vector< std::pair< gd::String, gd::String > > *supplementaryParametersTypes) |
Generate the code for a single parameter. More... | |
virtual gd::String | GenerateGetVariable (const gd::String &variableName, const VariableScope &scope, gd::EventsCodeGenerationContext &context, const gd::String &objectName) |
Generate the code to get a variable. | |
virtual gd::String | GenerateVariableAccessor (gd::String childName) |
Generate the code to get the child of a variable. | |
virtual gd::String | GenerateVariableValueAs (const gd::String &type) |
virtual gd::String | GenerateVariableBracketAccessor (gd::String expressionCode) |
Generate the code to get the child of a variable, using generated the expression. | |
virtual gd::String | GenerateBadVariable () |
Generate the code to reference a variable which is in an empty/null state. | |
virtual gd::String | GenerateObject (const gd::String &objectName, const gd::String &type, gd::EventsCodeGenerationContext &context) |
Generate the code to reference an object. More... | |
virtual gd::String | GeneratePropertyGetter (const gd::PropertiesContainer &propertiesContainer, const gd::NamedPropertyDescriptor &property, const gd::String &type, gd::EventsCodeGenerationContext &context) |
virtual gd::String | GenerateParameterGetter (const gd::ParameterMetadata ¶meter, const gd::String &type, gd::EventsCodeGenerationContext &context) |
virtual gd::String | GenerateBadObject () |
Generate the code to reference an object which is in an empty/null state. | |
virtual gd::String | GenerateObjectFunctionCall (gd::String objectListName, const ObjectMetadata &objMetadata, const gd::ExpressionCodeGenerationInformation &codeInfo, gd::String parametersStr, gd::String defaultOutput, gd::EventsCodeGenerationContext &context) |
Call a function of the current object. More... | |
virtual gd::String | GenerateObjectBehaviorFunctionCall (gd::String objectListName, gd::String behaviorName, const gd::BehaviorMetadata &autoInfo, const gd::ExpressionCodeGenerationInformation &codeInfo, gd::String parametersStr, gd::String defaultOutput, gd::EventsCodeGenerationContext &context) |
Call a function of a behavior of the current object. More... | |
virtual gd::String | GenerateScopeBegin (gd::EventsCodeGenerationContext &context, const gd::String &extraVariable="") |
Called when a new scope must be entered. More... | |
virtual gd::String | GenerateScopeEnd (gd::EventsCodeGenerationContext &context, const gd::String &extraVariable="") |
Called when a new must be ended. More... | |
virtual gd::String | GenerateNegatedPredicate (const gd::String &predicate) const |
Must negate a predicate. More... | |
virtual gd::String | GenerateFreeCondition (const std::vector< gd::String > &arguments, const gd::InstructionMetadata &instrInfos, const gd::String &returnBoolean, bool conditionInverted, gd::EventsCodeGenerationContext &context) |
virtual gd::String | GenerateObjectCondition (const gd::String &objectName, const gd::ObjectMetadata &objInfo, const std::vector< gd::String > &arguments, const gd::InstructionMetadata &instrInfos, const gd::String &returnBoolean, bool conditionInverted, gd::EventsCodeGenerationContext &context) |
virtual gd::String | GenerateBehaviorCondition (const gd::String &objectName, const gd::String &behaviorName, const gd::BehaviorMetadata &autoInfo, const std::vector< gd::String > &arguments, const gd::InstructionMetadata &instrInfos, const gd::String &returnBoolean, bool conditionInverted, gd::EventsCodeGenerationContext &context) |
virtual gd::String | GenerateFreeAction (const gd::String &functionCallName, const std::vector< gd::String > &arguments, const gd::InstructionMetadata &instrInfos, gd::EventsCodeGenerationContext &context, const gd::String &optionalAsyncCallbackName="") |
virtual gd::String | GenerateObjectAction (const gd::String &objectName, const gd::ObjectMetadata &objInfo, const gd::String &functionCallName, const std::vector< gd::String > &arguments, const gd::InstructionMetadata &instrInfos, gd::EventsCodeGenerationContext &context, const gd::String &optionalAsyncCallbackName="") |
virtual gd::String | GenerateBehaviorAction (const gd::String &objectName, const gd::String &behaviorName, const gd::BehaviorMetadata &autoInfo, const gd::String &functionCallName, const std::vector< gd::String > &arguments, const gd::InstructionMetadata &instrInfos, gd::EventsCodeGenerationContext &context, const gd::String &optionalAsyncCallbackName="") |
gd::String | GenerateRelationalOperatorCall (const gd::InstructionMetadata &instrInfos, const std::vector< gd::String > &arguments, const gd::String &callStartString, std::size_t startFromArgument=0) |
gd::String | GenerateOperatorCall (const gd::InstructionMetadata &instrInfos, const std::vector< gd::String > &arguments, const gd::String &callStartString, const gd::String &getterStartString, std::size_t startFromArgument=0) |
gd::String | GenerateCompoundOperatorCall (const gd::InstructionMetadata &instrInfos, const std::vector< gd::String > &arguments, const gd::String &callStartString, std::size_t startFromArgument=0) |
gd::String | GenerateMutatorCall (const gd::InstructionMetadata &instrInfos, const std::vector< gd::String > &arguments, const gd::String &callStartString, std::size_t startFromArgument=0) |
gd::String | GenerateTrue () const |
Return the "true" keyword in the target language. | |
gd::String | GenerateFalse () const |
Return the "false" keyword in the target language. | |
virtual gd::String | GenerateArgumentsList (const std::vector< gd::String > &arguments, size_t startFrom=0) |
Generate the list of comma-separated arguments to be used to call a function. More... | |
virtual gd::String | GenerateGetBehaviorNameCode (const gd::String &behaviorName) |
Protected Attributes | |
const gd::Platform & | platform |
The platform being used. | |
gd::ProjectScopedContainers | projectScopedContainers |
bool | hasProjectAndLayout |
const gd::Project * | project |
The project being used. | |
const gd::Layout * | scene |
The scene being generated. | |
bool | errorOccurred |
Must be set to true if an error occurred. | |
bool | compilationForRuntime |
std::set< gd::String > | includeFiles |
gd::String | customCodeOutsideMain |
std::set< gd::String > | customGlobalDeclarations |
size_t | maxCustomConditionsDepth |
size_t | maxConditionsListsSize |
The maximum size of a list of conditions. | |
std::set< size_t > | instructionUniqueIds |
The unique ids generated for instructions. | |
size_t | eventsListNextUniqueId |
Friends | |
class | ExpressionCodeGenerator |
Internal class used to generate code from events.
|
inline |
|
inline |
Declare a list of include files to be added.
|
static |
Must convert a plain string ( with line feed, quotes ) to a string that can be inserted into code.
Usage example :
plainString | The string to convert |
|
static |
Convert a plain string (with line feed, quotes) to a string that can be inserted into code. The string construction must be explicit: for example, quotes must be added if the target language need quotes.
Usage example :
plainString | The string to convert |
|
inline |
Return true if an error has occurred during code generation (in this case, generated code is not usable).
gd::String gd::EventsCodeGenerator::GenerateActionCode | ( | gd::Instruction & | action, |
EventsCodeGenerationContext & | context, | ||
const gd::String & | optionalAsyncCallbackName = "" |
||
) |
Generate code for a single action.
The generation is really done in GenerateFreeAction/GenerateObjectAction or GenerateBehaviorAction.
condition | instruction to be done. |
context | Context used for generation |
Generate code for an action.
|
virtual |
Generate code for executing an action list.
The default implementation just calls repeatedly GenerateActionCode.
game | Game used |
scene | Scene used |
actions | std::vector of actions |
context | Context used for generation |
Generate actions code.
|
protectedvirtual |
Generate the list of comma-separated arguments to be used to call a function.
arguments | The code already generated for the arguments |
startFrom | Index of the first argument, the previous will be ignored. |
|
inlinevirtual |
Generate the full name for accessing to a boolean variable used for conditions.
Default implementation just returns the boolean name passed as argument.
|
inlinevirtual |
Must create a boolean. Its value must be false.
The default implementation generates C-style code.
|
virtual |
Generates actions and events as a callback.
This is used by asynchronous functions to run the code out of the normal events flow.
|
protected |
Generate call using a compound assignment operators ( =,+=,-=,*=,/= ). Operator position is deduced from parameters type. Expression is assumed to be placed just before the operator.
Information | about the instruction |
Arguments,in | their C++ form. |
String | to be placed at the start of the call ( the function to be called typically ). Example : MyObject->Set |
Arguments | will be generated starting from this number. For example, set this to 1 to skip the first argument. |
gd::String gd::EventsCodeGenerator::GenerateConditionCode | ( | gd::Instruction & | condition, |
gd::String | returnBoolean, | ||
EventsCodeGenerationContext & | context | ||
) |
Generate code for a single condition.
The generation is really done in GenerateFreeCondition/GenerateObjectCondition or GenerateBehaviorCondition.
condition | instruction to be done. |
returnBoolean | The name of the boolean that must contains the condition result. |
context | Context used for generation |
|
virtual |
Generate code for executing a condition list.
The default implementation create the condition calls using C-style ifs and booleans.
game | Game used |
scene | Scene used |
conditions | std::vector of conditions |
context | Context used for generation |
Generate code for a list of conditions. Bools containing conditions results are named conditionXIsTrue.
|
virtual |
Generate code for executing an event list.
events | std::vector of events |
context | Context used for generation |
Generate events list code.
|
protectedvirtual |
Generate the getter to get the name of the specified behavior.
|
inlineprotectedvirtual |
Must negate a predicate.
The default implementation generates C-style code : It wraps the predicate inside parenthesis and add a !.
|
inlineprotectedvirtual |
Generate the code to reference an object.
objectName | the name of the object. |
type | what is the expected type (object, objectPtr...) in which the object must be generated. |
context | The context for code generation |
|
protectedvirtual |
Call a function of a behavior of the current object.
objectListName | The full name of the object list being used |
behaviorName | The full name of the behavior to be used |
objMetadata | Metadata about the behavior being used. |
functionCallName | The function to be called on this object. |
parametersStr | The parameters of the function |
context | The context : May be used to get information about the current scope. |
|
protectedvirtual |
Call a function of the current object.
objectListName | The full name of the object list being used |
objMetadata | Metadata about the object being used. |
functionCallName | The function to be called on this object. |
parametersStr | The parameters of the function |
context | The context : May be used to get information about the current scope. |
|
virtual |
Generate code for declaring objects lists.
This method is used for each event.
context | The context to be used. |
|
protected |
Generate call using an operator ( =,+,-,*,/ ). Operator position is deduced from parameters type. Expression is assumed to be placed just before the operator.
Information | about the instruction |
Arguments,in | their C++ form. |
String | to be placed at the start of the call ( the function to be called typically ). Example : MyObject->Set |
String | to be placed at the start of the call of the getter ( the "getter" function to be called typically ). Example : MyObject->Get |
Arguments | will be generated starting from this number. For example, set this to 1 to skip the first argument. |
|
protectedvirtual |
Generate the code for a single parameter.
Standard supported parameters type, and how they are used in code:
"Code only" parameters types:
Other standard parameters type that should be implemented by platforms:
objectList
but do not pick object if they are not already picked.vector< gd::String > gd::EventsCodeGenerator::GenerateParametersCodes | ( | const std::vector< gd::Expression > & | parameters, |
const std::vector< gd::ParameterMetadata > & | parametersInfo, | ||
EventsCodeGenerationContext & | context, | ||
std::vector< std::pair< gd::String, gd::String > > * | supplementaryParametersTypes = 0 |
||
) |
Generate the code for a parameter of an action/condition/expression.
This method uses GenerateParameterCodes to generate the parameters code.
scene | Scene used |
parameters | std::vector of actual parameters. |
parametersInfo | std::vector of information about parameters |
context | Context used for generation |
supplementaryParametersTypes | Optional std::vector of new parameters types ( std::vector of pair<gd::String,gd::String>("type", "valueToBeInserted") ) |
|
virtual |
Generate a relational operation.
relationalOperator | the operator |
lhs | the left hand operand |
rhs | the right hand operand |
|
protected |
Generate call using a relational operator. Relational operator position is deduced from parameters type. Rhs hand side expression is assumed to be placed just before the relational operator.
Information | about the instruction |
Arguments,in | their C++ form. |
String | to be placed at the start of the call ( the function to be called typically ). Example : MyObject->Get |
Arguments | will be generated starting from this number. For example, set this to 1 to skip the first argument. |
|
inlineprotectedvirtual |
Called when a new scope must be entered.
context | The context : Internal events of the scope have been generated, but GenerateObjectsDeclarationCode was not called. |
extraVariable | An optional supplementary variable that should be inherited from the parent scope. |
|
inlineprotectedvirtual |
Called when a new must be ended.
context | The context : Internal events of the scope have been generated, but GenerateObjectsDeclarationCode was not called. |
extraVariable | An optional supplementary variable that should be inherited from the parent scope. |
size_t gd::EventsCodeGenerator::GenerateSingleUsageUniqueIdFor | ( | const gd::Instruction * | instruction | ) |
Generate a single unique number for the specified instruction.
This is useful for instructions that need to identify themselves in the generated code like the "Trigger Once" conditions. The id is stable across code generations if the instructions are the same objects in memory.
Note that if this function is called multiple times with the same instruction, the unique number returned will be different. This is because a single instruction might appear at multiple places in events due to the usage of links.
size_t gd::EventsCodeGenerator::GenerateSingleUsageUniqueIdForEventsList | ( | ) |
Generate a single unique number for an events list.
This is useful to create unique function names for events list, that are stable across code generation given the exact same list of events. They are not stable if events are moved/reorganized.
|
inlinevirtual |
Generate the full name for accessing to a boolean variable used for conditions.
Default implementation just returns the boolean name passed as argument.
|
inlinevirtual |
Get the namespace to be used to store code generated objects/values/functions.
Example: "gdjs.something"
|
inlinevirtual |
Get the namespace to be used to store code generated objects/values/functions, with the extra "dot" at the end to be used to access to a property/member.
Example: "gdjs.something."
|
inline |
Get the layout the code is being generated for.
|
virtual |
Get the full name for accessing to a list of objects.
Default implementation simply returns the name mangled using gd::EventsCodeNameMangler.
|
inline |
Get the project the code is being generated for.
void gd::EventsCodeGenerator::PreprocessEventList | ( | gd::EventsList & | listEvent | ) |
Preprocess an events list (replacing for example links with the linked events).
This should be called before any code generation.
Call preprocessing method of each event
|
protected |
Is set to true if the code generation is made for runtime only.
|
protected |
Custom code inserted before events ( and not in events function )
|
protected |
Custom global C++ declarations inserted after includes
|
protected |
The next identifier to use for an events list function name.
|
protected |
true only if project and layout are valid references. If false, they should not be used.
|
protected |
List of headers files used by instructions. A (shared) pointer is used so as context created from another one can share the same list.
|
protected |
The maximum depth value for all the custom conditions created.