GDevelop Core
Core library for developing platforms and tools compatible with GDevelop.
EventsCodeGenerator.h
1 /*
2  * GDevelop Core
3  * Copyright 2008-present Florian Rival ([email protected]). All rights
4  * reserved. This project is released under the MIT License.
5  */
6 #pragma once
7 
8 #include <set>
9 #include <utility>
10 #include <vector>
11 
12 #include "GDCore/Events/Event.h"
13 #include "GDCore/Events/Instruction.h"
14 #include "GDCore/Events/CodeGeneration/DiagnosticReport.h"
15 #include "GDCore/Project/ProjectScopedContainers.h"
16 #include "GDCore/String.h"
17 
18 namespace gd {
19 class EventsList;
20 class Expression;
21 class Project;
22 class Layout;
23 class ObjectsContainer;
24 class ObjectsContainersList;
25 class ExternalEvents;
26 class ParameterMetadata;
27 class ObjectMetadata;
28 class BehaviorMetadata;
29 class InstructionMetadata;
30 class EventsCodeGenerationContext;
31 class ExpressionCodeGenerationInformation;
32 class InstructionMetadata;
33 class Platform;
34 } // namespace gd
35 
36 namespace gd {
37 
41 class GD_CORE_API EventsCodeGenerator {
42  friend class ExpressionCodeGenerator;
43 
44  public:
48  static void DeleteUselessEvents(gd::EventsList& events);
49 
54  EventsCodeGenerator(const gd::Project& project_,
55  const gd::Layout& layout,
56  const gd::Platform& platform_);
57 
63  const gd::Platform& platform,
64  const gd::ProjectScopedContainers& projectScopedContainers_);
65  virtual ~EventsCodeGenerator(){};
66 
73  void PreprocessEventList(gd::EventsList& listEvent);
74 
82  virtual gd::String GenerateEventsListCode(
84 
98  virtual gd::String GenerateConditionsListCode(
100 
112  virtual gd::String GenerateActionsListCode(
114 
129  std::vector<gd::String> GenerateParametersCodes(
130  const std::vector<gd::Expression>& parameters,
131  const ParameterMetadataContainer& parametersInfo,
133  std::vector<std::pair<gd::String, gd::String> >*
134  supplementaryParametersTypes = 0);
135 
146  gd::String GenerateConditionCode(gd::Instruction& condition,
147  gd::String returnBoolean,
148  EventsCodeGenerationContext& context);
149 
160  gd::String GenerateActionCode(
161  gd::Instruction& action,
163  const gd::String& optionalAsyncCallbackName = "");
164 
166  CallbackDescriptor(const gd::String functionName_,
167  const gd::String argumentsList_,
168  const std::set<gd::String> requiredObjects_)
169  : functionName(functionName_),
170  argumentsList(argumentsList_),
171  requiredObjects(requiredObjects_){};
184  const std::set<gd::String> requiredObjects;
185  };
186 
197  virtual const CallbackDescriptor GenerateCallback(
198  const gd::String& callbackFunctionName,
199  gd::EventsCodeGenerationContext& parentContext,
200  gd::InstructionsList& actions,
201  gd::EventsList* subEvents = nullptr);
202 
206  const gd::String GenerateEventsParameters(
207  const gd::EventsCodeGenerationContext& context);
208 
216  virtual gd::String GenerateObjectsDeclarationCode(
217  EventsCodeGenerationContext& context);
218 
234  static gd::String ConvertToString(gd::String plainString);
235 
252  static gd::String ConvertToStringExplicit(gd::String plainString);
253 
263  if (!file.empty()) includeFiles.insert(file);
264  };
265 
270  void AddIncludeFiles(std::vector<gd::String> files) {
271  for (std::size_t i = 0; i < files.size(); ++i) AddIncludeFile(files[i]);
272  };
273 
277  void AddGlobalDeclaration(gd::String declaration) {
278  customGlobalDeclarations.insert(declaration);
279  };
280 
285  customCodeOutsideMain += code;
286  };
287 
290  const std::set<gd::String>& GetIncludeFiles() const { return includeFiles; }
291 
295  return customCodeOutsideMain;
296  }
297 
300  const std::set<gd::String>& GetCustomGlobalDeclaration() const {
301  return customGlobalDeclarations;
302  }
303 
307  bool GenerateCodeForRuntime() { return compilationForRuntime; }
308 
313  void SetGenerateCodeForRuntime(bool compilationForRuntime_) {
314  compilationForRuntime = compilationForRuntime_;
315  }
316 
321  void ReportError();
322 
330  bool ErrorOccurred() const { return errorOccurred; };
331 
332  const gd::ObjectsContainersList& GetObjectsContainersList() const {
333  return projectScopedContainers.GetObjectsContainersList();
334  };
335 
336  const gd::ProjectScopedContainers& GetProjectScopedContainers() const {
337  return projectScopedContainers;
338  }
339 
347  return projectScopedContainers;
348  }
349 
354  bool HasProjectAndLayout() const { return hasProjectAndLayout; }
355 
360  const gd::Project& GetProject() const { return *project; }
361 
366  const gd::Layout& GetLayout() const { return *scene; }
367 
371  const gd::Platform& GetPlatform() const { return platform; }
372 
378  return maxCustomConditionsDepth;
379  }
380 
384  size_t GetMaxConditionsListsSize() const { return maxConditionsListsSize; }
385 
386  void SetDiagnosticReport(gd::DiagnosticReport* diagnosticReport_) {
387  diagnosticReport = diagnosticReport_;
388  }
389 
390  gd::DiagnosticReport* GetDiagnosticReport() {
391  return diagnosticReport;
392  }
393 
401  const gd::String& boolName,
402  const gd::EventsCodeGenerationContext& context) {
403  return boolName;
404  }
405 
413  const gd::String& boolName,
414  const gd::EventsCodeGenerationContext& context) {
415  return boolName;
416  }
417 
424  const gd::String& boolName,
425  const gd::EventsCodeGenerationContext& context) {
426  return "bool " + boolName + " = false;\n";
427  }
428 
435  virtual gd::String GetObjectListName(
436  const gd::String& name, const gd::EventsCodeGenerationContext& context);
437 
443  return "";
444  };
445 
450  return "";
451  };
452 
460  virtual gd::String GetCodeNamespaceAccessor() { return ""; };
461 
468  virtual gd::String GetCodeNamespace() { return ""; };
469 
470  enum VariableScope {
471  LAYOUT_VARIABLE = 0,
472  PROJECT_VARIABLE,
473  OBJECT_VARIABLE,
474  ANY_VARIABLE,
475  VARIABLE_OR_PROPERTY,
476  VARIABLE_OR_PROPERTY_OR_PARAMETER
477  };
478 
491  size_t GenerateSingleUsageUniqueIdFor(const gd::Instruction* instruction);
492 
500  size_t GenerateSingleUsageUniqueIdForEventsList();
501 
502  virtual gd::String GenerateRelationalOperation(
503  const gd::String& relationalOperator,
504  const gd::String& lhs,
505  const gd::String& rhs);
506 
510  virtual gd::String GenerateLocalVariablesStackAccessor();
511 
516  gd::String
517  GenerateAnyOrSceneVariableGetter(const gd::Expression &variableExpression,
518  EventsCodeGenerationContext &context);
519 
520  virtual gd::String GeneratePropertySetterWithoutCasting(
521  const gd::PropertiesContainer &propertiesContainer,
522  const gd::NamedPropertyDescriptor &property,
523  const gd::String &operandCode);
524 
525 protected:
526  virtual const gd::String GenerateRelationalOperatorCodes(
527  const gd::String& operatorString);
528 
565  virtual gd::String GenerateParameterCodes(
566  const gd::Expression& parameter,
567  const gd::ParameterMetadata& metadata,
569  const gd::String& lastObjectName,
570  std::vector<std::pair<gd::String, gd::String> >*
571  supplementaryParametersTypes);
572 
577  const gd::String& variableName,
578  const VariableScope& scope,
580  const gd::String& objectName,
581  bool hasChild) {
582  // This code is only used as a mock.
583  // See the real implementation in GDJS.
584  if (scope == LAYOUT_VARIABLE) {
585  return "getLayoutVariable(" + variableName + ")";
586 
587  } else if (scope == PROJECT_VARIABLE) {
588  return "getProjectVariable(" + variableName + ")";
589  } else if (scope == ANY_VARIABLE || scope == VARIABLE_OR_PROPERTY ||
590  scope == VARIABLE_OR_PROPERTY_OR_PARAMETER) {
591  // TODO Split the 3 cases to make tests stronger.
592  return "getAnyVariable(" + variableName + ")";
593  }
594 
595  return "getVariableForObject(" + objectName + ", " + variableName + ")";
596  }
597 
602  return ".getChild(" + ConvertToStringExplicit(childName) + ")";
603  };
604 
605  virtual gd::String GenerateVariableValueAs(const gd::String& type) {
606  return type == "number|string" ? ".getAsNumberOrString()"
607  : type == "string" ? ".getAsString()"
608  : ".getAsNumber()";
609  }
610 
616  gd::String expressionCode) {
617  return ".getChild(" + expressionCode + ")";
618  };
619 
624  virtual gd::String GenerateBadVariable() { return "fakeBadVariable"; }
625 
633  virtual gd::String GenerateObject(const gd::String& objectName,
634  const gd::String& type,
636  return "fakeObjectListOf_" + objectName;
637  }
638 
639  virtual gd::String GeneratePropertyGetter(
640  const gd::PropertiesContainer& propertiesContainer,
641  const gd::NamedPropertyDescriptor& property,
642  const gd::String& type,
644 
645  virtual gd::String GeneratePropertyGetterWithoutCasting(
646  const gd::PropertiesContainer &propertiesContainer,
647  const gd::NamedPropertyDescriptor &property);
648 
649  virtual gd::String GenerateParameterGetter(
650  const gd::ParameterMetadata& parameter,
651  const gd::String& type,
653 
654  virtual gd::String
655  GenerateParameterGetterWithoutCasting(const gd::ParameterMetadata &parameter);
656 
661  virtual gd::String GenerateBadObject() { return "fakeNullObject"; }
662 
675  virtual gd::String GenerateObjectFunctionCall(
676  gd::String objectListName,
677  const ObjectMetadata& objMetadata,
679  gd::String parametersStr,
680  gd::String defaultOutput,
682 
696  virtual gd::String GenerateObjectBehaviorFunctionCall(
697  gd::String objectListName,
698  gd::String behaviorName,
699  const gd::BehaviorMetadata& autoInfo,
701  gd::String parametersStr,
702  gd::String defaultOutput,
704 
714  const gd::String& extraVariable = "") {
715  return "{\n";
716  };
717 
726  const gd::String& extraVariable = "") {
727  return "}\n";
728  };
729 
737  const gd::String& predicate) const {
738  return "!(" + predicate + ")";
739  };
740 
741  virtual gd::String GenerateFreeCondition(
742  const std::vector<gd::String>& arguments,
743  const gd::InstructionMetadata& instrInfos,
744  const gd::String& returnBoolean,
745  bool conditionInverted,
747 
748  virtual gd::String GenerateObjectCondition(
749  const gd::String& objectName,
750  const gd::ObjectMetadata& objInfo,
751  const std::vector<gd::String>& arguments,
752  const gd::InstructionMetadata& instrInfos,
753  const gd::String& returnBoolean,
754  bool conditionInverted,
756 
757  virtual gd::String GenerateBehaviorCondition(
758  const gd::String& objectName,
759  const gd::String& behaviorName,
760  const gd::BehaviorMetadata& autoInfo,
761  const std::vector<gd::String>& arguments,
762  const gd::InstructionMetadata& instrInfos,
763  const gd::String& returnBoolean,
764  bool conditionInverted,
766 
767  virtual gd::String GenerateFreeAction(
768  const gd::String& functionCallName,
769  const std::vector<gd::String>& arguments,
770  const gd::InstructionMetadata& instrInfos,
772  const gd::String& optionalAsyncCallbackName = "");
773 
774  virtual gd::String GenerateObjectAction(
775  const gd::String& objectName,
776  const gd::ObjectMetadata& objInfo,
777  const gd::String& functionCallName,
778  const std::vector<gd::String>& arguments,
779  const gd::InstructionMetadata& instrInfos,
781  const gd::String& optionalAsyncCallbackName = "");
782 
783  virtual gd::String GenerateBehaviorAction(
784  const gd::String& objectName,
785  const gd::String& behaviorName,
786  const gd::BehaviorMetadata& autoInfo,
787  const gd::String& functionCallName,
788  const std::vector<gd::String>& arguments,
789  const gd::InstructionMetadata& instrInfos,
791  const gd::String& optionalAsyncCallbackName = "");
792 
793  gd::String GenerateRelationalOperatorCall(
794  const gd::InstructionMetadata& instrInfos,
795  const std::vector<gd::String>& arguments,
796  const gd::String& callStartString,
797  std::size_t startFromArgument = 0);
798 
799  gd::String GenerateOperatorCall(const gd::InstructionMetadata& instrInfos,
800  const std::vector<gd::String>& arguments,
801  const gd::String& callStartString,
802  const gd::String& getterStartString,
803  std::size_t startFromArgument = 0);
804  gd::String GenerateCompoundOperatorCall(
805  const gd::InstructionMetadata& instrInfos,
806  const std::vector<gd::String>& arguments,
807  const gd::String& callStartString,
808  std::size_t startFromArgument = 0);
809  gd::String GenerateMutatorCall(const gd::InstructionMetadata& instrInfos,
810  const std::vector<gd::String>& arguments,
811  const gd::String& callStartString,
812  std::size_t startFromArgument = 0);
813 
817  gd::String GenerateTrue() const { return "true"; };
818 
822  gd::String GenerateFalse() const { return "false"; };
823 
831  virtual gd::String GenerateArgumentsList(
832  const std::vector<gd::String>& arguments, size_t startFrom = 0);
833 
837  virtual gd::String GenerateGetBehaviorNameCode(
838  const gd::String& behaviorName);
839 
840  void CheckBehaviorParameters(
841  const gd::Instruction &instruction,
842  const gd::InstructionMetadata &instrInfos);
843 
845 
846  gd::ProjectScopedContainers projectScopedContainers;
847 
851  const gd::Layout* scene;
852 
856 
857  std::set<gd::String>
863  std::set<gd::String>
869 
870  std::set<size_t>
874 
875  gd::DiagnosticReport* diagnosticReport;
876 };
877 
878 } // namespace gd
879 
Contains user-friendly information about a behavior type.
Definition: BehaviorMetadata.h:32
Definition: DiagnosticReport.h:57
Used to manage the context when generating code for events.
Definition: EventsCodeGenerationContext.h:27
Internal class used to generate code from events.
Definition: EventsCodeGenerator.h:41
virtual gd::String GenerateNegatedPredicate(const gd::String &predicate) const
Must negate a predicate.
Definition: EventsCodeGenerator.h:736
size_t GetMaxConditionsListsSize() const
Get the maximum size of a list of conditions.
Definition: EventsCodeGenerator.h:384
const gd::Project * project
The project being used.
Definition: EventsCodeGenerator.h:850
void AddIncludeFiles(std::vector< gd::String > files)
Declare a list of include files to be added.
Definition: EventsCodeGenerator.h:270
virtual gd::String GetCodeNamespace()
Get the namespace to be used to store code generated objects/values/functions.
Definition: EventsCodeGenerator.h:468
const gd::Platform & GetPlatform() const
Get the platform the code is being generated for.
Definition: EventsCodeGenerator.h:371
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.
Definition: EventsCodeGenerator.h:412
void AddGlobalDeclaration(gd::String declaration)
Add a declaration which will be inserted after includes.
Definition: EventsCodeGenerator.h:277
virtual gd::String GenerateVariableBracketAccessor(gd::String expressionCode)
Generate the code to get the child of a variable, using generated the expression.
Definition: EventsCodeGenerator.h:615
const std::set< gd::String > & GetIncludeFiles() const
Get the set containing the include files.
Definition: EventsCodeGenerator.h:290
void SetGenerateCodeForRuntime(bool compilationForRuntime_)
Set if the code generated is meant to be used for runtime only and not in the IDE.
Definition: EventsCodeGenerator.h:313
size_t maxCustomConditionsDepth
Definition: EventsCodeGenerator.h:866
virtual gd::String GenerateScopeBegin(gd::EventsCodeGenerationContext &context, const gd::String &extraVariable="")
Called when a new scope must be entered.
Definition: EventsCodeGenerator.h:712
bool compilationForRuntime
Definition: EventsCodeGenerator.h:854
const gd::Platform & platform
The platform being used.
Definition: EventsCodeGenerator.h:844
virtual gd::String GenerateProfilerSectionBegin(const gd::String &section)
Generate the code to notify the profiler of the beginning of a section.
Definition: EventsCodeGenerator.h:442
const gd::Layout * scene
The scene being generated.
Definition: EventsCodeGenerator.h:851
virtual gd::String GenerateBadVariable()
Generate the code to reference a variable which is in an empty/null state.
Definition: EventsCodeGenerator.h:624
virtual gd::String GenerateBooleanInitializationToFalse(const gd::String &boolName, const gd::EventsCodeGenerationContext &context)
Must create a boolean. Its value must be false.
Definition: EventsCodeGenerator.h:423
bool GenerateCodeForRuntime()
Return true if code generation is made for runtime only.
Definition: EventsCodeGenerator.h:307
virtual gd::String GenerateBadObject()
Generate the code to reference an object which is in an empty/null state.
Definition: EventsCodeGenerator.h:661
virtual gd::String GenerateGetVariable(const gd::String &variableName, const VariableScope &scope, gd::EventsCodeGenerationContext &context, const gd::String &objectName, bool hasChild)
Generate the code to get a variable.
Definition: EventsCodeGenerator.h:576
bool hasProjectAndLayout
Definition: EventsCodeGenerator.h:848
gd::String GenerateTrue() const
Return the "true" keyword in the target language.
Definition: EventsCodeGenerator.h:817
size_t GetMaxCustomConditionsDepth() const
Get the maximum depth of custom conditions reached during code generation.
Definition: EventsCodeGenerator.h:377
gd::String GenerateFalse() const
Return the "false" keyword in the target language.
Definition: EventsCodeGenerator.h:822
gd::String customCodeOutsideMain
Definition: EventsCodeGenerator.h:861
size_t maxConditionsListsSize
The maximum size of a list of conditions.
Definition: EventsCodeGenerator.h:868
gd::ProjectScopedContainers & GetProjectScopedContainers()
Give access to the project scoped containers as code generation might push and pop variable container...
Definition: EventsCodeGenerator.h:346
size_t eventsListNextUniqueId
Definition: EventsCodeGenerator.h:872
std::set< gd::String > customGlobalDeclarations
Definition: EventsCodeGenerator.h:864
const std::set< gd::String > & GetCustomGlobalDeclaration() const
Get the custom declaration to be inserted after includes.
Definition: EventsCodeGenerator.h:300
virtual gd::String GenerateObject(const gd::String &objectName, const gd::String &type, gd::EventsCodeGenerationContext &context)
Generate the code to reference an object.
Definition: EventsCodeGenerator.h:633
bool HasProjectAndLayout() const
Return true if the code generation is done for a given project and layout. If not,...
Definition: EventsCodeGenerator.h:354
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.
Definition: EventsCodeGenerator.h:400
virtual gd::String GenerateProfilerSectionEnd(const gd::String &section)
Generate the code to notify the profiler of the end of a section.
Definition: EventsCodeGenerator.h:449
const gd::String & GetCustomCodeOutsideMain() const
Get the custom code to be inserted outside main.
Definition: EventsCodeGenerator.h:294
const gd::Project & GetProject() const
Get the project the code is being generated for.
Definition: EventsCodeGenerator.h:360
void AddCustomCodeOutsideMain(gd::String code)
Add some code before events outside the main function.
Definition: EventsCodeGenerator.h:284
std::set< gd::String > includeFiles
Definition: EventsCodeGenerator.h:858
void AddIncludeFile(gd::String file)
Declare an include file to be added.
Definition: EventsCodeGenerator.h:262
virtual gd::String GenerateVariableAccessor(gd::String childName)
Generate the code to get the child of a variable.
Definition: EventsCodeGenerator.h:601
bool errorOccurred
Must be set to true if an error occurred.
Definition: EventsCodeGenerator.h:853
virtual gd::String GetCodeNamespaceAccessor()
Get the namespace to be used to store code generated objects/values/functions, with the extra "dot" a...
Definition: EventsCodeGenerator.h:460
bool ErrorOccurred() const
Return true if an error has occurred during code generation (in this case, generated code is not usab...
Definition: EventsCodeGenerator.h:330
std::set< size_t > instructionUniqueIds
The unique ids generated for instructions.
Definition: EventsCodeGenerator.h:871
const gd::Layout & GetLayout() const
Get the layout the code is being generated for.
Definition: EventsCodeGenerator.h:366
virtual gd::String GenerateScopeEnd(gd::EventsCodeGenerationContext &context, const gd::String &extraVariable="")
Called when a new must be ended.
Definition: EventsCodeGenerator.h:725
A list of events.
Definition: EventsList.h:32
Information about how generate code for an expression.
Definition: ExpressionMetadata.h:24
Generate code for a parsed expression.
Definition: ExpressionCodeGenerator.h:35
Class representing an expression used as a parameter of a gd::Instruction. This class is nothing more...
Definition: Expression.h:30
An instruction is a member of an event: It can be a condition or an action.
Definition: Instruction.h:30
Describe user-friendly information about an instruction (action or condition), its parameters and the...
Definition: InstructionMetadata.h:39
Definition: InstructionsList.h:25
Represent a layout ( also called a scene ) of a project.
Definition: Layout.h:40
Used to describe a property shown in a property grid.
Definition: NamedPropertyDescriptor.h:21
Contains user-friendly information about an object type, and a function to create a new gd::Object of...
Definition: ObjectMetadata.h:37
A list of objects containers, useful for accessing objects in a scoped way, along with methods to acc...
Definition: ObjectsContainersList.h:29
Used as a base class for classes that will own events-backed functions.
Definition: ParameterMetadataContainer.h:26
Describe a parameter of an instruction (action, condition) or of an expression: type,...
Definition: ParameterMetadata.h:27
Base class for implementing a platform.
Definition: Platform.h:42
Base class representing a project (game), including all resources, scenes, objects,...
Definition: Project.h:50
Holds references to variables, objects, properties and other containers.
Definition: ProjectScopedContainers.h:34
A container of properties, used for custom behaviors, custom objects, extensions.....
Definition: PropertiesContainer.h:17
String represents an UTF8 encoded string.
Definition: String.h:33
bool empty() const
Returns true if the string is empty.
Definition: String.h:157
Definition: CommonTools.h:24
Definition: EventsCodeGenerator.h:165
const std::set< gd::String > requiredObjects
Definition: EventsCodeGenerator.h:184
const gd::String argumentsList
Definition: EventsCodeGenerator.h:179
const gd::String functionName
Definition: EventsCodeGenerator.h:171