Hello and welcome, everybody.
And in this lecture, I'm going to explain to you what I mean by fix ups.
So when you look at structures and see what are the rules, firstly, they can be defined anywhere in
a source file and structures can be forward declared.
So essentially we can define a structure somewhere in our source file.
We can forward declare it so it can be accessed in a different part of the program.
Right.
So they're the structure rules now.
They bring some problems.
So how do we know that a structure where referencing actually exists?
If we haven't yet parsed it, that's a serious problem because it could lead to a crash such as accessing
pointers in our compiler that I know.
So how do we ensure that structures declared below code can still be accessed if they are declared as
a pointer?
So I explain that to you now.
So the solution is what I call a fix up.
So when we encounter a structure variable declaration and the structure doesn't exist, we register
a fix up, which is basically a reminder to our compiler.
So for example, we have a structure variable called ABC Oak and I don't know the structure name is
Dog, let's say.
And now it hasn't been declared yet, but we've defined that variable above it.
So what we do is we, we create a reminder.
So we say to the compiler, please remember that we need to come back and resolve this when we know
what the structure dog is.
And that's basically what we're saying.
So the fix up are a little reminders, reminders to fix the problem.
So here's an example that I've made.
So you can see here we have a structure called BCA and above that we have a structure point of variable
called ABC that is of type BCA A, but the problem is structure.
BCA hasn't yet been declared.
So when we do parse variable ABC, it's going to set the structure data type.
So when we do parse variable ABC, it's going to set the data type to a data type struct, but the size
will be zero and so will the structure.
No, because we don't yet know what struct BCA is.
So what we then do is we once we've set those default values, we create a fix up and then the fix up
will be resolved later after parsing is completed.
So now let me show you what happens after parsing is complete.
So after parsing is complete, we can see that we now have the size of four bytes because that's the
size of the structure.
And we now have a structure node that's been set to a pointer address to point to the structure instance
in our compilers memory which is struck BCA.
So that's what happens when it's resolved.
So we register the fix up and then after parsing we resolve all of the fix ups so that they are resolved
correctly with a size and a struct node.
So that is how the fix up works and that allows us to be able to access ABC even though BCA hasn't been
defined yet.
So as soon as BCA is parsed into our abstract syntax tree and the parsing is complete, we call fix
up resolve, which will go through all of the fix ups, all of the fix up reminders, and it will essentially
change their size and their struc node to point to the ones that have been resolved now.
So there's also a bit of error checking.
So let's say that we call fix ups resolve and it goes through the fix up, but there's no structure
associated.
Then that means that a structure variable was created, a structure point of variable was created that
pointed to an invalid type.
And we know it's an invalid type because we run this function only after parsing is complete.
So of course if the structure was defined, it would have been put into memory because it would have
been parsed.
So that's how we check for errors there.
If when we call fix ups resolve, we can't find the structure that the fix up is associated to.
Then we know that it hasn't been declared.
So our conclusion is that the fix subsystem ensures that we can understand the structure, even if it
has not been encountered yet.
So this is essential because structures can be forward declared.
They don't have to be declared above the variables that are using them.
Thanks for watching.
We'll be implementing this shortly.