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 #ifndef GDCORE_EVENTSCODEGENERATOR_H
7 #define GDCORE_EVENTSCODEGENERATOR_H
8 
9 #include <set>
10 #include <utility>
11 #include <vector>
12 
13 #include "GDCore/Events/Event.h"
14 #include "GDCore/Events/Instruction.h"
15 #include "GDCore/Project/ProjectScopedContainers.h"
16 #include "GDCore/String.h"
17 namespace gd {
18 class EventsList;
19 class Expression;
20 class Project;
21 class Layout;
22 class ObjectsContainer;
23 class ObjectsContainersList;
24 class ExternalEvents;
25 class ParameterMetadata;
26 class ObjectMetadata;
27 class BehaviorMetadata;
28 class InstructionMetadata;
29 class EventsCodeGenerationContext;
30 class ExpressionCodeGenerationInformation;
31 class InstructionMetadata;
32 class Platform;
33 } // namespace gd
34 
35 namespace gd {
36 
40 class GD_CORE_API EventsCodeGenerator {
41  friend class ExpressionCodeGenerator;
42 
43  public:
47  static void DeleteUselessEvents(gd::EventsList& events);
48 
53  EventsCodeGenerator(const gd::Project& project_,
54  const gd::Layout& layout,
55  const gd::Platform& platform_);
56 
62  const gd::Platform& platform,
63  const gd::ProjectScopedContainers& projectScopedContainers_);
64  virtual ~EventsCodeGenerator(){};
65 
72  void PreprocessEventList(gd::EventsList& listEvent);
73 
81  virtual gd::String GenerateEventsListCode(
83 
97  virtual gd::String GenerateConditionsListCode(
99 
111  virtual gd::String GenerateActionsListCode(
113 
128  std::vector<gd::String> GenerateParametersCodes(
129  const std::vector<gd::Expression>& parameters,
130  const std::vector<gd::ParameterMetadata>& parametersInfo,
132  std::vector<std::pair<gd::String, gd::String> >*
133  supplementaryParametersTypes = 0);
134 
145  gd::String GenerateConditionCode(gd::Instruction& condition,
146  gd::String returnBoolean,
147  EventsCodeGenerationContext& context);
148 
159  gd::String GenerateActionCode(
160  gd::Instruction& action,
162  const gd::String& optionalAsyncCallbackName = "");
163 
165  CallbackDescriptor(const gd::String functionName_,
166  const gd::String argumentsList_,
167  const std::set<gd::String> requiredObjects_)
168  : functionName(functionName_),
169  argumentsList(argumentsList_),
170  requiredObjects(requiredObjects_){};
183  const std::set<gd::String> requiredObjects;
184  };
185 
196  virtual const CallbackDescriptor GenerateCallback(
197  const gd::String& callbackFunctionName,
198  gd::EventsCodeGenerationContext& parentContext,
199  gd::InstructionsList& actions,
200  gd::EventsList* subEvents = nullptr);
201 
205  const gd::String GenerateEventsParameters(
206  const gd::EventsCodeGenerationContext& context);
207 
215  virtual gd::String GenerateObjectsDeclarationCode(
216  EventsCodeGenerationContext& context);
217 
233  static gd::String ConvertToString(gd::String plainString);
234 
251  static gd::String ConvertToStringExplicit(gd::String plainString);
252 
262  if (!file.empty()) includeFiles.insert(file);
263  };
264 
269  void AddIncludeFiles(std::vector<gd::String> files) {
270  for (std::size_t i = 0; i < files.size(); ++i) AddIncludeFile(files[i]);
271  };
272 
276  void AddGlobalDeclaration(gd::String declaration) {
277  customGlobalDeclarations.insert(declaration);
278  };
279 
284  customCodeOutsideMain += code;
285  };
286 
289  const std::set<gd::String>& GetIncludeFiles() const { return includeFiles; }
290 
294  return customCodeOutsideMain;
295  }
296 
299  const std::set<gd::String>& GetCustomGlobalDeclaration() const {
300  return customGlobalDeclarations;
301  }
302 
306  bool GenerateCodeForRuntime() { return compilationForRuntime; }
307 
312  void SetGenerateCodeForRuntime(bool compilationForRuntime_) {
313  compilationForRuntime = compilationForRuntime_;
314  }
315 
320  void ReportError();
321 
329  bool ErrorOccurred() const { return errorOccurred; };
330 
331  const gd::ObjectsContainersList& GetObjectsContainersList() const {
332  return projectScopedContainers.GetObjectsContainersList();
333  };
334 
335  const gd::ProjectScopedContainers& GetProjectScopedContainers() const {
336  return projectScopedContainers;
337  }
338 
343  bool HasProjectAndLayout() const { return hasProjectAndLayout; }
344 
349  const gd::Project& GetProject() const { return *project; }
350 
355  const gd::Layout& GetLayout() const { return *scene; }
356 
360  const gd::Platform& GetPlatform() const { return platform; }
361 
367  return maxCustomConditionsDepth;
368  }
369 
373  size_t GetMaxConditionsListsSize() const { return maxConditionsListsSize; }
374 
382  const gd::String& boolName,
383  const gd::EventsCodeGenerationContext& context) {
384  return boolName;
385  }
386 
394  const gd::String& boolName,
395  const gd::EventsCodeGenerationContext& context) {
396  return boolName;
397  }
398 
405  const gd::String& boolName,
406  const gd::EventsCodeGenerationContext& context) {
407  return "bool " + boolName + " = false;\n";
408  }
409 
416  virtual gd::String GetObjectListName(
417  const gd::String& name, const gd::EventsCodeGenerationContext& context);
418 
424  return "";
425  };
426 
431  return "";
432  };
433 
441  virtual gd::String GetCodeNamespaceAccessor() { return ""; };
442 
449  virtual gd::String GetCodeNamespace() { return ""; };
450 
451  enum VariableScope { LAYOUT_VARIABLE = 0, PROJECT_VARIABLE, OBJECT_VARIABLE };
452 
465  size_t GenerateSingleUsageUniqueIdFor(const gd::Instruction* instruction);
466 
474  size_t GenerateSingleUsageUniqueIdForEventsList();
475 
476  virtual gd::String GenerateRelationalOperation(
477  const gd::String& relationalOperator,
478  const gd::String& lhs,
479  const gd::String& rhs);
480 
481  protected:
482  virtual const gd::String GenerateRelationalOperatorCodes(
483  const gd::String& operatorString);
484 
521  virtual gd::String GenerateParameterCodes(
522  const gd::Expression& parameter,
523  const gd::ParameterMetadata& metadata,
525  const gd::String& lastObjectName,
526  std::vector<std::pair<gd::String, gd::String> >*
527  supplementaryParametersTypes);
528 
533  const gd::String& variableName,
534  const VariableScope& scope,
536  const gd::String& objectName) {
537  if (scope == LAYOUT_VARIABLE) {
538  return "getLayoutVariable(" + variableName + ")";
539 
540  } else if (scope == PROJECT_VARIABLE) {
541  return "getProjectVariable(" + variableName + ")";
542  }
543 
544  return "getVariableForObject(" + objectName + ", " + variableName + ")";
545  }
546 
551  return ".getChild(" + ConvertToStringExplicit(childName) + ")";
552  };
553 
554  virtual gd::String GenerateVariableValueAs(const gd::String& type) {
555  return type == "number|string" ? ".getAsNumberOrString()"
556  : type == "string" ? ".getAsString()"
557  : ".getAsNumber()";
558  }
559 
565  gd::String expressionCode) {
566  return ".getChild(" + expressionCode + ")";
567  };
568 
573  virtual gd::String GenerateBadVariable() { return "fakeBadVariable"; }
574 
582  virtual gd::String GenerateObject(const gd::String& objectName,
583  const gd::String& type,
585  return "fakeObjectListOf_" + objectName;
586  }
587 
588  virtual gd::String GeneratePropertyGetter(
589  const gd::PropertiesContainer& propertiesContainer,
590  const gd::NamedPropertyDescriptor& property,
591  const gd::String& type,
593 
594  virtual gd::String GenerateParameterGetter(
595  const gd::ParameterMetadata& parameter,
596  const gd::String& type,
598 
603  virtual gd::String GenerateBadObject() { return "fakeNullObject"; }
604 
617  virtual gd::String GenerateObjectFunctionCall(
618  gd::String objectListName,
619  const ObjectMetadata& objMetadata,
621  gd::String parametersStr,
622  gd::String defaultOutput,
624 
638  virtual gd::String GenerateObjectBehaviorFunctionCall(
639  gd::String objectListName,
640  gd::String behaviorName,
641  const gd::BehaviorMetadata& autoInfo,
643  gd::String parametersStr,
644  gd::String defaultOutput,
646 
656  const gd::String& extraVariable = "") {
657  return "{\n";
658  };
659 
668  const gd::String& extraVariable = "") {
669  return "}\n";
670  };
671 
679  const gd::String& predicate) const {
680  return "!(" + predicate + ")";
681  };
682 
683  virtual gd::String GenerateFreeCondition(
684  const std::vector<gd::String>& arguments,
685  const gd::InstructionMetadata& instrInfos,
686  const gd::String& returnBoolean,
687  bool conditionInverted,
689 
690  virtual gd::String GenerateObjectCondition(
691  const gd::String& objectName,
692  const gd::ObjectMetadata& objInfo,
693  const std::vector<gd::String>& arguments,
694  const gd::InstructionMetadata& instrInfos,
695  const gd::String& returnBoolean,
696  bool conditionInverted,
698 
699  virtual gd::String GenerateBehaviorCondition(
700  const gd::String& objectName,
701  const gd::String& behaviorName,
702  const gd::BehaviorMetadata& autoInfo,
703  const std::vector<gd::String>& arguments,
704  const gd::InstructionMetadata& instrInfos,
705  const gd::String& returnBoolean,
706  bool conditionInverted,
708 
709  virtual gd::String GenerateFreeAction(
710  const gd::String& functionCallName,
711  const std::vector<gd::String>& arguments,
712  const gd::InstructionMetadata& instrInfos,
714  const gd::String& optionalAsyncCallbackName = "");
715 
716  virtual gd::String GenerateObjectAction(
717  const gd::String& objectName,
718  const gd::ObjectMetadata& objInfo,
719  const gd::String& functionCallName,
720  const std::vector<gd::String>& arguments,
721  const gd::InstructionMetadata& instrInfos,
723  const gd::String& optionalAsyncCallbackName = "");
724 
725  virtual gd::String GenerateBehaviorAction(
726  const gd::String& objectName,
727  const gd::String& behaviorName,
728  const gd::BehaviorMetadata& autoInfo,
729  const gd::String& functionCallName,
730  const std::vector<gd::String>& arguments,
731  const gd::InstructionMetadata& instrInfos,
733  const gd::String& optionalAsyncCallbackName = "");
734 
735  gd::String GenerateRelationalOperatorCall(
736  const gd::InstructionMetadata& instrInfos,
737  const std::vector<gd::String>& arguments,
738  const gd::String& callStartString,
739  std::size_t startFromArgument = 0);
740 
741  gd::String GenerateOperatorCall(const gd::InstructionMetadata& instrInfos,
742  const std::vector<gd::String>& arguments,
743  const gd::String& callStartString,
744  const gd::String& getterStartString,
745  std::size_t startFromArgument = 0);
746  gd::String GenerateCompoundOperatorCall(
747  const gd::InstructionMetadata& instrInfos,
748  const std::vector<gd::String>& arguments,
749  const gd::String& callStartString,
750  std::size_t startFromArgument = 0);
751  gd::String GenerateMutatorCall(const gd::InstructionMetadata& instrInfos,
752  const std::vector<gd::String>& arguments,
753  const gd::String& callStartString,
754  std::size_t startFromArgument = 0);
755 
759  gd::String GenerateTrue() const { return "true"; };
760 
764  gd::String GenerateFalse() const { return "false"; };
765 
773  virtual gd::String GenerateArgumentsList(
774  const std::vector<gd::String>& arguments, size_t startFrom = 0);
775 
779  virtual gd::String GenerateGetBehaviorNameCode(
780  const gd::String& behaviorName);
781 
783 
784  gd::ProjectScopedContainers projectScopedContainers;
785 
789  const gd::Layout* scene;
790 
794 
795  std::set<gd::String>
801  std::set<gd::String>
807 
808  std::set<size_t>
812 };
813 
814 } // namespace gd
815 
816 #endif // GDCORE_EVENTSCODEGENERATOR_H
Contains user-friendly information about a behavior type.
Definition: BehaviorMetadata.h:31
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:40
virtual gd::String GenerateNegatedPredicate(const gd::String &predicate) const
Must negate a predicate.
Definition: EventsCodeGenerator.h:678
size_t GetMaxConditionsListsSize() const
Get the maximum size of a list of conditions.
Definition: EventsCodeGenerator.h:373
const gd::Project * project
The project being used.
Definition: EventsCodeGenerator.h:788
void AddIncludeFiles(std::vector< gd::String > files)
Declare a list of include files to be added.
Definition: EventsCodeGenerator.h:269
virtual gd::String GetCodeNamespace()
Get the namespace to be used to store code generated objects/values/functions.
Definition: EventsCodeGenerator.h:449
const gd::Platform & GetPlatform() const
Get the platform the code is being generated for.
Definition: EventsCodeGenerator.h:360
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:393
void AddGlobalDeclaration(gd::String declaration)
Add a declaration which will be inserted after includes.
Definition: EventsCodeGenerator.h:276
virtual gd::String GenerateVariableBracketAccessor(gd::String expressionCode)
Generate the code to get the child of a variable, using generated the expression.
Definition: EventsCodeGenerator.h:564
const std::set< gd::String > & GetIncludeFiles() const
Get the set containing the include files.
Definition: EventsCodeGenerator.h:289
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:312
size_t maxCustomConditionsDepth
Definition: EventsCodeGenerator.h:804
virtual gd::String GenerateScopeBegin(gd::EventsCodeGenerationContext &context, const gd::String &extraVariable="")
Called when a new scope must be entered.
Definition: EventsCodeGenerator.h:654
bool compilationForRuntime
Definition: EventsCodeGenerator.h:792
const gd::Platform & platform
The platform being used.
Definition: EventsCodeGenerator.h:782
virtual gd::String GenerateProfilerSectionBegin(const gd::String &section)
Generate the code to notify the profiler of the beginning of a section.
Definition: EventsCodeGenerator.h:423
const gd::Layout * scene
The scene being generated.
Definition: EventsCodeGenerator.h:789
virtual gd::String GenerateBadVariable()
Generate the code to reference a variable which is in an empty/null state.
Definition: EventsCodeGenerator.h:573
virtual gd::String GenerateBooleanInitializationToFalse(const gd::String &boolName, const gd::EventsCodeGenerationContext &context)
Must create a boolean. Its value must be false.
Definition: EventsCodeGenerator.h:404
bool GenerateCodeForRuntime()
Return true if code generation is made for runtime only.
Definition: EventsCodeGenerator.h:306
virtual gd::String GenerateBadObject()
Generate the code to reference an object which is in an empty/null state.
Definition: EventsCodeGenerator.h:603
bool hasProjectAndLayout
Definition: EventsCodeGenerator.h:786
gd::String GenerateTrue() const
Return the "true" keyword in the target language.
Definition: EventsCodeGenerator.h:759
size_t GetMaxCustomConditionsDepth() const
Get the maximum depth of custom conditions reached during code generation.
Definition: EventsCodeGenerator.h:366
gd::String GenerateFalse() const
Return the "false" keyword in the target language.
Definition: EventsCodeGenerator.h:764
gd::String customCodeOutsideMain
Definition: EventsCodeGenerator.h:799
size_t maxConditionsListsSize
The maximum size of a list of conditions.
Definition: EventsCodeGenerator.h:806
size_t eventsListNextUniqueId
Definition: EventsCodeGenerator.h:810
std::set< gd::String > customGlobalDeclarations
Definition: EventsCodeGenerator.h:802
const std::set< gd::String > & GetCustomGlobalDeclaration() const
Get the custom declaration to be inserted after includes.
Definition: EventsCodeGenerator.h:299
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:582
bool HasProjectAndLayout() const
Return true if the code generation is done for a given project and layout. If not,...
Definition: EventsCodeGenerator.h:343
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:381
virtual gd::String GenerateProfilerSectionEnd(const gd::String &section)
Generate the code to notify the profiler of the end of a section.
Definition: EventsCodeGenerator.h:430
const gd::String & GetCustomCodeOutsideMain() const
Get the custom code to be inserted outside main.
Definition: EventsCodeGenerator.h:293
const gd::Project & GetProject() const
Get the project the code is being generated for.
Definition: EventsCodeGenerator.h:349
void AddCustomCodeOutsideMain(gd::String code)
Add some code before events outside the main function.
Definition: EventsCodeGenerator.h:283
std::set< gd::String > includeFiles
Definition: EventsCodeGenerator.h:796
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.
Definition: EventsCodeGenerator.h:532
void AddIncludeFile(gd::String file)
Declare an include file to be added.
Definition: EventsCodeGenerator.h:261
virtual gd::String GenerateVariableAccessor(gd::String childName)
Generate the code to get the child of a variable.
Definition: EventsCodeGenerator.h:550
bool errorOccurred
Must be set to true if an error occurred.
Definition: EventsCodeGenerator.h:791
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:441
bool ErrorOccurred() const
Return true if an error has occurred during code generation (in this case, generated code is not usab...
Definition: EventsCodeGenerator.h:329
std::set< size_t > instructionUniqueIds
The unique ids generated for instructions.
Definition: EventsCodeGenerator.h:809
const gd::Layout & GetLayout() const
Get the layout the code is being generated for.
Definition: EventsCodeGenerator.h:355
virtual gd::String GenerateScopeEnd(gd::EventsCodeGenerationContext &context, const gd::String &extraVariable="")
Called when a new must be ended.
Definition: EventsCodeGenerator.h:667
A list of events.
Definition: EventsList.h:33
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:38
Definition: InstructionsList.h:25
Represent a layout ( also called a scene ) of a project.
Definition: Layout.h:39
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
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:30
A container of properties, used for custom behaviors, custom objects, extensions.....
Definition: PropertiesContainer.h:17
String represents an UTF8 encoded string.
Definition: String.h:31
bool empty() const
Returns true if the string is empty.
Definition: String.h:155
Definition: CommonTools.h:24
Definition: EventsCodeGenerator.h:164
const std::set< gd::String > requiredObjects
Definition: EventsCodeGenerator.h:183
const gd::String argumentsList
Definition: EventsCodeGenerator.h:178
const gd::String functionName
Definition: EventsCodeGenerator.h:170