Gyoji Compiler
Loading...
Searching...
No Matches
function-scope.hpp
1#pragma once
2#include <gyoji-frontend.hpp>
3#include <gyoji-mir.hpp>
4
6
7 class Scope;
8 class ScopeOperation;
9 class ScopeTracker;
10 class LocalVariable;
11
24 public:
25 FunctionPoint(size_t _basic_block_id, size_t _location);
27 size_t get_basic_block_id() const;
28 size_t get_location() const;
29 private:
30 size_t basic_block_id;
31 size_t location;
32 };
33
47 public:
48
75
76 static Gyoji::owned<ScopeOperation> create_variable(
77 std::string _variable_name,
78 const Gyoji::mir::Type *_variable_type,
79 const Gyoji::context::SourceReference & _source_ref
80 );
81 static Gyoji::owned<ScopeOperation> create_label(
82 std::string _label_name,
83 const Gyoji::context::SourceReference & _source_ref
84 );
85 static Gyoji::owned<ScopeOperation> create_goto(
86 std::string _goto_label,
88 const Gyoji::context::SourceReference & _source_ref
89 );
90 static Gyoji::owned<ScopeOperation> create_child(
92 const Gyoji::context::SourceReference & _source_ref
93 );
94
95 void dump(int indent) const;
96 const ScopeOperationType & get_type() const;
97 const std::string & get_label_name() const;
98
99 const std::string & get_goto_label() const;
100 const FunctionPoint & get_goto_point() const;
101
102 const std::string & get_variable_name() const;
103 const Scope *get_child() const;
104
105 const Gyoji::context::SourceReference & get_source_ref() const;
106
107
108 private:
110 ScopeOperationType _type,
111 const Gyoji::context::SourceReference & _source_ref
112 );
114
115 const Gyoji::context::SourceReference & source_ref;
116
117 LocalVariable *local_variable;
118 const Gyoji::mir::Type *variable_type;
119 std::string variable_name;
120
121 std::string label_name;
122
123 std::string goto_label;
125
127 };
128
129 class LocalVariable {
130 public:
131 LocalVariable(
132 const Gyoji::mir::Type *_type,
133 const Gyoji::context::SourceReference & _source_ref
134 );
141 ~LocalVariable();
142 const Gyoji::mir::Type *get_type() const;
143 const Gyoji::context::SourceReference & get_source_ref() const;
144 private:
145 const Gyoji::mir::Type *type;
146 const Gyoji::context::SourceReference & source_ref;
147 };
148
164 class Scope {
165 public:
170 Scope();
177 Scope(bool _is_loop,
178 size_t _loop_break_blockid,
179 size_t _loop_continue_blockid
180 );
187 ~Scope();
188 void add_operation(Gyoji::owned<ScopeOperation> op);
189
190 void dump(int indent) const;
191
197 const LocalVariable *get_variable(std::string name) const;
198
199 bool is_loop() const;
200
201 size_t get_loop_break_blockid() const;
202
203 size_t get_loop_continue_blockid() const;
204
205 const std::map<std::string, Gyoji::owned<LocalVariable>> & get_variables() const;
206 private:
207 friend ScopeTracker;
208 Scope *parent;
209
210 // If this scope is a loop,
211 // what is the block id to jump
212 // to for a 'break' or 'continue' operation?
213 bool scope_is_loop;
214 size_t loop_break_blockid;
215 size_t loop_continue_blockid;
216
219 };
220
243 public:
245 size_t _block_id
246 );
254 size_t get_block() const;
255 bool is_resolved() const;
256 void resolve(const Gyoji::context::SourceReference & _src_ref);
257 const Gyoji::context::SourceReference & get_source_ref() const;
258 private:
259 bool resolved;
260 size_t block_id;
261 const Gyoji::context::SourceReference * src_ref;
262 };
263
291 public:
292 ScopeTracker(const Gyoji::context::CompilerContext & _compiler_context);
300
304 void scope_push(
305 const Gyoji::context::SourceReference & _source_ref
306 );
307
312 void scope_push_loop(
313 const Gyoji::context::SourceReference & _source_ref,
314 size_t _loop_break_blockid,
315 size_t _loop_continue_blockid
316 );
317
321 void scope_pop();
322
329 // Use this for 'label' to say we have a label and it's in the current
330 // scope, but it's on the 'notfound' list, so move it over to
331 // the real list because we've found it now.
332 void label_define(
333 std::string label_name,
334 const Gyoji::context::SourceReference & _source_ref
335 );
336
337 // Use this for 'label' to say we have a label, it's in the
338 // current scope, but it wasn't forwar-declared on the
339 // notfound list.
340 void label_define(
341 std::string label_name,
342 size_t label_block_id,
343 const Gyoji::context::SourceReference & _source_ref
344 );
345
346 // Use this for 'goto' to say we want a label, but we
347 // don't yet know where it will live, so put it on the
348 // notfound labels list.
349 void label_declare(std::string label_name, size_t label_blockid);
350
351 void add_goto(
352 std::string goto_label,
353 Gyoji::owned<FunctionPoint> function_point,
354 const Gyoji::context::SourceReference & _source_ref
355 );
356
357 const FunctionLabel * get_label(std::string name) const;
358
362 bool add_variable(std::string variable_name, const Gyoji::mir::Type *mir_type, const Gyoji::context::SourceReference & source_ref);
363
364 void dump() const;
365
394 bool check(
396 ) const;
397
402 bool is_in_loop() const;
403
404 size_t get_loop_break_blockid() const;
405 size_t get_loop_continue_blockid() const;
406
414 const LocalVariable* get_variable(std::string variable_name) const;
415
422
429
430 std::vector<std::string> get_variables_to_unwind_for_break() const;
431
438
439 const Scope *get_current() const;
440
441 private:
442 void add_flat_op(const ScopeOperation *op);
443
444 void add_operation(Gyoji::owned<ScopeOperation> op);
445
446
447
449 Scope *current;
450 const Gyoji::context::CompilerContext & compiler_context;
451
452 // Labels that actually have a definition.
454
455 // Labels that have been referenced in a 'goto'
456 // but not yet defined in a scope.
458
459 std::vector<size_t> tracker_prior_point;
460 std::map<size_t, size_t> tracker_backward_edges;
462
463 std::map<std::string, size_t> tracker_label_locations;
464 std::map<size_t, std::string> tracker_goto_labels_at;
465 };
466};
Compiler Context.
Definition gyoji-context.hpp:30
References a location in the source-file.
Definition source-reference.hpp:16
A named label inside a scope.
Definition function-scope.hpp:242
~FunctionLabel()
Move along, nothing to see here.
Definition function-scope.cpp:657
Location inside a specific basic block.
Definition function-scope.hpp:23
Primitive operation in a scope.
Definition function-scope.hpp:46
ScopeOperationType
Definition function-scope.hpp:49
@ GOTO_DEFINITION
Definition function-scope.hpp:61
@ CHILD_SCOPE
Definition function-scope.hpp:66
@ VAR_DECL
Definition function-scope.hpp:53
@ LABEL_DEFINITION
Definition function-scope.hpp:57
~ScopeOperation()
Move along, nothing to see here.
Definition function-scope.cpp:70
Tracks variables declared in each scope along with abels and goto statements in a highly simplified i...
Definition function-scope.hpp:290
void label_define(std::string label_name, const Gyoji::context::SourceReference &_source_ref)
Definition function-scope.cpp:274
void scope_push(const Gyoji::context::SourceReference &_source_ref)
Definition function-scope.cpp:228
std::vector< std::string > get_variables_to_unwind_for_label(std::string &label) const
bool add_variable(std::string variable_name, const Gyoji::mir::Type *mir_type, const Gyoji::context::SourceReference &source_ref)
Definition function-scope.cpp:412
std::vector< std::string > get_variables_to_unwind_for_root() const
Definition function-scope.cpp:368
std::vector< std::string > get_variables_to_unwind_for_scope() const
Definition function-scope.cpp:382
void scope_pop()
Definition function-scope.cpp:252
bool check(std::vector< std::pair< const ScopeOperation *, std::vector< const ScopeOperation * > > > &goto_fixups) const
Definition function-scope.cpp:523
bool is_in_loop() const
Definition function-scope.cpp:484
void scope_push_loop(const Gyoji::context::SourceReference &_source_ref, size_t _loop_break_blockid, size_t _loop_continue_blockid)
Definition function-scope.cpp:239
~ScopeTracker()
Move along, nothing to see here.
Definition function-scope.cpp:222
const LocalVariable * get_variable(std::string variable_name) const
Definition function-scope.cpp:352
Represents variable declarations, labels, and goto inside a lexical scope.
Definition function-scope.hpp:164
Scope()
Definition function-scope.cpp:147
~Scope()
Move along, nothing to see here.
Definition function-scope.cpp:165
const LocalVariable * get_variable(std::string name) const
Definition function-scope.cpp:182
This represents a type as declared in a translation unit.
Definition types.hpp:299
Converts the strongly-typed syntax tree into the MIR representation.
Definition function-resolver.hpp:23