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