Skip to main content

rucc_sema/
decl.rs

1//! Declared objects and functions, with their linkage and their storage duration resolved.
2//!
3//! Design: `spec/07-types-and-semantics.md` sections 7.4 and 7.12.
4//!
5//! Only the things that exist at run time are here. A `typedef` is a name for a type and lives
6//! in the type table as sugar, an enumerator is a constant and has been folded into the
7//! expressions that used it, and a tag is a type. What is left is objects and functions, which
8//! are what the walk to the IR needs a list of.
9//!
10//! An initializer is flattened. Brace elision, designators and the order the program wrote
11//! things in are all resolved here into a list of values and the byte offsets they go at, so
12//! that nothing downstream walks a nest of braces against a nest of types a second time. The
13//! contract is that the object starts as zero and the entries are applied in order, which is
14//! also what makes partial initialization and an overwriting designator fall out rather than
15//! need rules of their own.
16
17use rucc_base::{Idx, IdxRange, Symbol};
18use rucc_types::TypeId;
19
20use crate::expr::ExprId;
21use crate::stmt::StmtId;
22
23/// One declared object or function in the arena.
24pub type DeclId = Idx<Decl>;
25
26/// The table of references to declarations, which is what a declaration statement is a run of.
27#[derive(Debug)]
28pub struct DeclRef;
29
30/// A run of declarations.
31pub type DeclList = IdxRange<DeclRef>;
32
33/// A run of the values one initializer stores.
34pub type InitList = IdxRange<InitEntry>;
35
36/// An object or a function, as it was declared.
37#[derive(Debug, Clone, PartialEq, Eq)]
38pub struct Decl {
39    /// The name, absent for a compound literal and for a parameter that was not given one.
40    pub name: Option<Symbol>,
41    /// The type, after the adjustments a declaration performs: an array parameter has already
42    /// become a pointer, and a function parameter a function pointer.
43    pub ty: TypeId,
44    /// Whether it is an object or a function.
45    pub kind: DeclKind,
46    /// Whether the name is shared with other translation units, and how.
47    pub linkage: Linkage,
48    /// How long the object lives.
49    pub duration: StorageDuration,
50    /// How much of a definition this declaration is.
51    pub state: Definition,
52    /// The alignment `alignas` asked for, absent when the type's own alignment stands.
53    pub alignment: Option<u32>,
54    /// The initializer, flattened, absent when there was none. An empty list is `= {}`, which
55    /// C23 added and which zero-initializes, and is not the same as no initializer at all.
56    pub init: Option<InitList>,
57    /// The parameters of a function definition, in order, and empty for everything else.
58    ///
59    /// A parameter is an object with automatic storage like any other, and the body refers to
60    /// one the same way it refers to a local. What is different is that nothing in the body
61    /// declares it, so without this there is no way to ask which objects a definition takes and
62    /// in what order, which is the first question the walk to the IR has: the entry block's
63    /// parameters are these, in this order.
64    ///
65    /// A declaration that is not a definition has none of these even when it was written with a
66    /// prototype, because `int f(int a);` declares no object called `a`. The types are in the
67    /// function type, which is where a call reads them.
68    pub params: DeclList,
69    /// The body of a function definition.
70    pub body: Option<StmtId>,
71}
72
73/// Whether a declaration declares an object or a function.
74///
75/// A `typedef` and an enumerator are neither: one is a name for a type and the other is a
76/// constant, and both have been resolved by the time anything reads this.
77#[derive(Debug, Clone, Copy, PartialEq, Eq)]
78pub enum DeclKind {
79    /// An object, which includes parameters, block-scope variables and compound literals.
80    Object,
81    /// A function.
82    Function,
83}
84
85/// Whether a name is shared with other translation units, and how.
86#[derive(Debug, Clone, Copy, PartialEq, Eq)]
87pub enum Linkage {
88    /// The name is not shared. Block-scope objects without `extern`, parameters, and anything
89    /// declared in a function's body except a function or an `extern` object.
90    None,
91    /// The name is shared within the translation unit and not outside it, which is what
92    /// `static` at file scope means.
93    Internal,
94    /// The name is shared with every translation unit that declares it.
95    External,
96}
97
98/// How long an object lives.
99#[derive(Debug, Clone, Copy, PartialEq, Eq)]
100pub enum StorageDuration {
101    /// From the start of the program to the end of it.
102    Static,
103    /// From the start of the thread to the end of it, which is `_Thread_local`.
104    Thread,
105    /// From the point the declaration is reached to the end of the block, which is where a
106    /// variable length array's deallocation and a compound literal's lifetime both come from.
107    Automatic,
108}
109
110/// How much of a definition a declaration is.
111///
112/// The three states are what the one-definition rules are written in terms of, and keeping
113/// them apart is what makes a tentative definition become a definition at the end of the
114/// translation unit rather than at the point it was read.
115#[derive(Debug, Clone, Copy, PartialEq, Eq)]
116pub enum Definition {
117    /// A declaration and nothing more, which is what `extern int x;` is and what every
118    /// function declaration without a body is.
119    Declared,
120    /// A file-scope object with no initializer and no `extern`, which is a definition only if
121    /// nothing else in the translation unit defines it. C calls this a tentative definition and
122    /// it is the reason `int x; int x;` is one object and not an error.
123    Tentative,
124    /// A definition: an object with an initializer, a block-scope object with automatic
125    /// storage, or a function with a body.
126    Defined,
127}
128
129/// One value an initializer stores, and where it goes.
130///
131/// The offsets are from the start of the object being initialized, so a nested aggregate has
132/// already been walked and there is nothing left to elide or designate.
133#[derive(Debug, Clone, Copy, PartialEq, Eq)]
134pub struct InitEntry {
135    /// The byte offset from the start of the object.
136    pub offset: u64,
137    /// The value, already converted to the type of what is at that offset.
138    pub value: ExprId,
139    /// The bit offset within the byte at `offset`, for a bit-field.
140    pub bit_offset: u32,
141    /// The width in bits, for a bit-field, and zero for everything else. A bit-field of width
142    /// zero has no name and cannot be initialized, so zero is free to mean this instead.
143    pub bit_width: u32,
144}
145
146impl InitEntry {
147    /// A value at a byte offset, which is what everything that is not a bit-field is.
148    #[must_use]
149    pub const fn at(offset: u64, value: ExprId) -> InitEntry {
150        InitEntry { offset, value, bit_offset: 0, bit_width: 0 }
151    }
152
153    /// Whether this entry writes part of a byte rather than whole bytes.
154    #[must_use]
155    pub const fn is_bit_field(&self) -> bool {
156        self.bit_width != 0
157    }
158}