Bash++
Bash++ compiler internal documentation
bpp_codegen.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <string>
9#include <memory>
10#include <optional>
11#include <deque>
12#include <type_traits>
13
14#include "bpp.h"
15#include <error/InternalError.h>
16#include <AST/ASTNode.h>
17
18namespace bpp {
19// Minimal forward declarations
20class bpp_program;
21class bash_while_or_until_condition;
22class bpp_class;
23class bpp_object;
24
39 std::string pre_code;
40 std::string code;
41 std::string post_code;
42
48 std::string full_code() const {
49 return pre_code + (pre_code.empty() ? "" : "\n") + code + (post_code.empty() ? "" : "\n") + post_code;
50 }
51};
52
53code_segment generate_supershell_code(
54 const std::string& code_to_run,
55 std::shared_ptr<bpp::bpp_program> program
56 );
57
59 std::shared_ptr<bpp_object> object,
60 const std::string& object_ref,
61 std::shared_ptr<bpp::bpp_program> program
62 );
63
65 const std::string& reference_code,
66 std::shared_ptr<bpp_class> assumed_class
67 );
68
70 const std::string& reference_code,
71 std::shared_ptr<bpp_class> assumed_class,
72 bool force_static_reference,
73 std::shared_ptr<bpp::bpp_program> program
74 );
75
77 const std::string& reference_code,
78 const std::string& method_name,
79 std::shared_ptr<bpp_class> assumed_class,
80 bool force_static_reference,
81 std::shared_ptr<bpp::bpp_program> program
82 );
83
85 const std::string& reference_code,
86 const std::string& class_name,
87 std::shared_ptr<bpp::bpp_program> program
88 );
89
91 const std::string& reference_code,
92 std::shared_ptr<bpp::bpp_program> program
93 );
94
96 const std::string& new_address,
97 std::shared_ptr<bpp::bpp_class> new_class,
98 bool inline_new
99 );
100
101// Generators for standardized system methods
102std::shared_ptr<bpp::bpp_method> generate_copy_method(
103 std::shared_ptr<bpp::bpp_class> containing_class,
104 std::shared_ptr<bpp::bpp_program> program
105 );
106
107std::shared_ptr<bpp::bpp_method> generate_new_method(
108 std::shared_ptr<bpp::bpp_class> containing_class
109 );
110
111std::shared_ptr<bpp::bpp_method> generate_delete_method(
112 std::shared_ptr<bpp::bpp_class> containing_class
113 );
114
115std::string get_encased_ref(const std::string& ref, uint8_t indirection_level);
116
117
118// Entity reference resolution
119
130 std::shared_ptr<bpp::bpp_entity> entity;
134 std::shared_ptr<bpp::bpp_class> class_containing_the_method;
135
140
141 std::optional<reference_error> error;
142};
143
145 const std::string& file,
146 std::shared_ptr<bpp::bpp_entity> context,
147 std::deque<AST::Token<std::string>>* identifiers,
148 std::deque<std::string>* identifier_texts,
149 bool declare_local,
150 std::shared_ptr<bpp::bpp_program> program
151);
152
154 const std::string& file,
155 std::shared_ptr<bpp::bpp_entity> context,
156 auto identifiers,
157 bool declare_local,
158 std::shared_ptr<bpp::bpp_program> program
159) {
160 // identifiers should be either:
161 // 1. A pointer to a container of strings (eg, std::deque<std::string>*)
162 // 2. A pointer to a container of AST::Token<std::string> (eg, std::deque<AST::Token<std::string>>*)
163
164 using ident_t = std::remove_cvref_t<decltype(identifiers)>;
165 using container_t = std::remove_pointer_t<ident_t>;
166 using value_t = typename container_t::value_type;
167
168 std::deque<AST::Token<std::string>> node_deque;
169 std::deque<std::string> text_deque;
170
171
172 if constexpr (std::is_convertible_v<value_t, AST::Token<std::string>>) {
173 // identifiers: deque<TerminalNode*>*
174 for (const auto& node : *identifiers) {
175 node_deque.push_back(node);
176 }
177 for (const auto& node : node_deque) {
178 text_deque.push_back(node.getValue());
179 }
180 return resolve_reference_impl(file, context, &node_deque, &text_deque, declare_local, program);
181 } else if constexpr (std::is_convertible_v<value_t, std::string>) {
182 // identifiers: deque<string>*
183 for (const auto& id : *identifiers) {
184 text_deque.push_back(id);
185 }
186 return resolve_reference_impl(file, context, &node_deque, &text_deque, declare_local, program);
187 } else {
188 static_assert(sizeof(value_t) == 0,
189 "resolve_reference: Identifiers must be either strings or TerminalNode pointers.");
190 }
191
192}
193
194} // namespace bpp
A class representing a token in the Bash++ AST. Tokens store their value along with line and column i...
Definition Token.h:21
Definition bash_case.cpp:8
code_segment generate_new_code(const std::string &new_address, std::shared_ptr< bpp::bpp_class > new_class, bool inline_new)
Generate the assignments necessary to create a new object of a given class.
Definition bpp_codegen.cpp:279
entity_reference resolve_reference(const std::string &file, std::shared_ptr< bpp::bpp_entity > context, auto identifiers, bool declare_local, std::shared_ptr< bpp::bpp_program > program)
Definition bpp_codegen.h:153
code_segment generate_typeof_code(const std::string &reference_code, std::shared_ptr< bpp::bpp_program > program)
Definition bpp_codegen.cpp:251
code_segment generate_destructor_call_code(const std::string &reference_code, std::shared_ptr< bpp_class > assumed_class, bool force_static_reference, std::shared_ptr< bpp::bpp_program > program)
Definition bpp_codegen.cpp:195
code_segment generate_dynamic_cast_code(const std::string &reference_code, const std::string &class_name, std::shared_ptr< bpp::bpp_program > program)
Generates a code segment for performing a dynamic cast.
Definition bpp_codegen.cpp:235
code_segment generate_delete_code(std::shared_ptr< bpp::bpp_object > object, const std::string &object_ref, std::shared_ptr< bpp::bpp_program > program)
Generates a code segment for deleting an object.
Definition bpp_codegen.cpp:83
std::string get_encased_ref(const std::string &ref, uint8_t indirection_level)
Encases a temporary variable reference with the appropriate level of indirection.
Definition bpp_codegen.cpp:496
std::shared_ptr< bpp::bpp_method > generate_delete_method(std::shared_ptr< bpp::bpp_class > containing_class)
Definition bpp_codegen.cpp:446
code_segment generate_method_call_code(const std::string &reference_code, const std::string &method_name, std::shared_ptr< bpp::bpp_class > assumed_class, bool force_static_reference, std::shared_ptr< bpp::bpp_program > program)
Generates a code segment for calling a method.
Definition bpp_codegen.cpp:133
code_segment generate_constructor_call_code(const std::string &reference_code, std::shared_ptr< bpp_class > assumed_class)
Definition bpp_codegen.cpp:176
std::shared_ptr< bpp::bpp_method > generate_new_method(std::shared_ptr< bpp::bpp_class > containing_class)
Definition bpp_codegen.cpp:418
entity_reference resolve_reference_impl(const std::string &file, std::shared_ptr< bpp::bpp_entity > context, std::deque< AST::Token< std::string > > *nodes, std::deque< std::string > *identifiers, bool declare_local, std::shared_ptr< bpp::bpp_program > program)
Resolves a reference to an entity in a particular context.
Definition bpp_codegen.cpp:545
std::shared_ptr< bpp::bpp_method > generate_copy_method(std::shared_ptr< bpp::bpp_class > containing_class, std::shared_ptr< bpp::bpp_program > program)
Generates a copy method for a class.
Definition bpp_codegen.cpp:350
A struct to hold (compiled) code segments.
Definition bpp_codegen.h:38
std::string full_code() const
Return the full code segment as a single string.
Definition bpp_codegen.h:48
std::string post_code
Definition bpp_codegen.h:41
std::string code
Definition bpp_codegen.h:40
std::string pre_code
Definition bpp_codegen.h:39
Definition bpp_codegen.h:136
AST::Token< std::string > token
Definition bpp_codegen.h:138
std::string message
Definition bpp_codegen.h:137
Represents the result of resolving a reference to an entity.
Definition bpp_codegen.h:129
bool created_first_temporary_variable
Definition bpp_codegen.h:132
std::shared_ptr< bpp::bpp_class > class_containing_the_method
Definition bpp_codegen.h:134
std::shared_ptr< bpp::bpp_entity > entity
Definition bpp_codegen.h:130
bool created_second_temporary_variable
Definition bpp_codegen.h:133
code_segment reference_code
Definition bpp_codegen.h:131
std::optional< reference_error > error
Definition bpp_codegen.h:141