substrate/layout/
element.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
//! Basic layout elements.
//!
//! Substrate layouts consist of cells, instances, geometric shapes, and text annotations.

use std::{collections::HashMap, sync::Arc};

use arcstr::ArcStr;
use geometry::{
    prelude::{Bbox, Point},
    rect::Rect,
    transform::{
        Transform, TransformMut, TransformRef, Transformation, Translate, TranslateMut,
        TranslateRef,
    },
};
use indexmap::IndexMap;
use layir::{LayerBbox, Shape, Text};
use serde::{Deserialize, Serialize};

use crate::types::layout::PortGeometry;
use crate::{
    error::{Error, Result},
    types::NameBuf,
};

use super::{schema::Schema, Draw, DrawReceiver, Instance, Layout};

/// A context-wide unique identifier for a cell.
#[derive(
    Default, Debug, Copy, Clone, Serialize, Deserialize, Hash, PartialEq, Eq, PartialOrd, Ord,
)]
pub struct CellId(u64);

impl CellId {
    pub(crate) fn increment(&mut self) {
        *self = CellId(self.0 + 1)
    }
}

/// A mapping from names to ports.
pub type NamedPorts<L> = IndexMap<NameBuf, PortGeometry<L>>;

/// A raw layout cell.
#[derive(Default, Debug, Clone, PartialEq)]
pub struct RawCell<L> {
    pub(crate) id: CellId,
    pub(crate) name: ArcStr,
    pub(crate) elements: Vec<Element<L>>,
    ports: NamedPorts<L>,
    port_names: HashMap<String, NameBuf>,
}

impl<L> RawCell<L> {
    pub(crate) fn new(id: CellId, name: impl Into<ArcStr>) -> Self {
        Self {
            id,
            name: name.into(),
            elements: Vec::new(),
            ports: IndexMap::new(),
            port_names: HashMap::new(),
        }
    }

    pub(crate) fn with_ports(self, ports: NamedPorts<L>) -> Self {
        let port_names = ports.keys().map(|k| (k.to_string(), k.clone())).collect();
        Self {
            ports,
            port_names,
            ..self
        }
    }

    #[doc(hidden)]
    pub fn port_map(&self) -> &NamedPorts<L> {
        &self.ports
    }

    #[allow(dead_code)]
    pub(crate) fn add_element(&mut self, elem: impl Into<Element<L>>) {
        self.elements.push(elem.into());
    }

    #[allow(dead_code)]
    pub(crate) fn add_elements(&mut self, elems: impl IntoIterator<Item = impl Into<Element<L>>>) {
        self.elements.extend(elems.into_iter().map(|x| x.into()));
    }

    /// The ID of this cell.
    pub fn id(&self) -> CellId {
        self.id
    }

    /// Returns an iterator over the elements of this cell.
    pub fn elements(&self) -> impl Iterator<Item = &Element<L>> {
        self.elements.iter()
    }

    /// Returns an iterator over the ports of this cell, as `(name, geometry)` pairs.
    pub fn ports(&self) -> impl Iterator<Item = (&NameBuf, &PortGeometry<L>)> {
        self.ports.iter()
    }

    /// Returns a reference to the port with the given name, if it exists.
    pub fn port_named(&self, name: &str) -> Option<&PortGeometry<L>> {
        let name_buf = self.port_names.get(name)?;
        self.ports.get(name_buf)
    }
}

impl<L> Bbox for RawCell<L> {
    fn bbox(&self) -> Option<geometry::rect::Rect> {
        self.elements.bbox()
    }
}

impl<L: PartialEq> LayerBbox<L> for RawCell<L> {
    fn layer_bbox(&self, layer: &L) -> Option<Rect> {
        self.elements.layer_bbox(layer)
    }
}

impl<L: Clone> TranslateRef for RawCell<L> {
    fn translate_ref(&self, p: Point) -> Self {
        Self {
            id: self.id,
            name: self.name.clone(),
            elements: self.elements.translate_ref(p),
            ports: self
                .ports
                .iter()
                .map(|(k, v)| (k.clone(), v.translate_ref(p)))
                .collect(),
            port_names: self.port_names.clone(),
        }
    }
}

impl<L: Clone> TransformRef for RawCell<L> {
    fn transform_ref(&self, trans: Transformation) -> Self {
        Self {
            id: self.id,
            name: self.name.clone(),
            elements: self.elements.transform_ref(trans),
            ports: self
                .ports
                .iter()
                .map(|(k, v)| (k.clone(), v.transform_ref(trans)))
                .collect(),
            port_names: self.port_names.clone(),
        }
    }
}

/// A raw layout instance.
///
/// Consists of a pointer to an underlying cell and its instantiated transformation.
#[derive(Default, Debug, Clone, PartialEq)]
#[allow(dead_code)]
pub struct RawInstance<L> {
    pub(crate) cell: Arc<RawCell<L>>,
    pub(crate) trans: Transformation,
}

impl<L> RawInstance<L> {
    /// Create a new raw instance of the given cell.
    pub fn new(cell: impl Into<Arc<RawCell<L>>>, trans: Transformation) -> Self {
        Self {
            cell: cell.into(),
            trans,
        }
    }

    /// Returns a raw reference to the child cell.
    ///
    /// The returned cell does not store any information related
    /// to this instance's transformation.
    /// Consider using [`RawInstance::cell`] instead.
    #[inline]
    pub fn raw_cell(&self) -> &RawCell<L> {
        &self.cell
    }
}

impl<L: Clone> RawInstance<L> {
    /// Returns a reference to the child cell.
    ///
    /// The returned object provides coordinates in the parent cell's coordinate system.
    /// If you want coordinates in the child cell's coordinate system,
    /// consider using [`RawInstance::raw_cell`] instead.
    #[inline]
    pub fn cell(&self) -> RawCell<L> {
        self.cell.transform_ref(self.trans)
    }
}

impl<L> Bbox for RawInstance<L> {
    fn bbox(&self) -> Option<Rect> {
        self.cell.bbox().map(|rect| rect.transform(self.trans))
    }
}

impl<L: PartialEq> LayerBbox<L> for RawInstance<L> {
    fn layer_bbox(&self, layer: &L) -> Option<Rect> {
        self.cell
            .layer_bbox(layer)
            .map(|rect| rect.transform(self.trans))
    }
}

impl<T: Layout> TryFrom<Instance<T>> for RawInstance<<T::Schema as Schema>::Layer> {
    type Error = Error;

    fn try_from(value: Instance<T>) -> Result<Self> {
        Ok(Self {
            cell: value.try_cell()?.raw,
            trans: value.trans,
        })
    }
}

impl<S: Schema> Draw<S> for RawInstance<S::Layer> {
    fn draw(self, recv: &mut DrawReceiver<S>) -> Result<()> {
        recv.draw_element(self);
        Ok(())
    }
}

impl<L> TranslateMut for RawInstance<L> {
    fn translate_mut(&mut self, p: Point) {
        self.transform_mut(Transformation::from_offset(p));
    }
}

impl<L> TransformMut for RawInstance<L> {
    fn transform_mut(&mut self, trans: Transformation) {
        self.trans = Transformation::cascade(trans, self.trans);
    }
}

impl<L: Clone> TranslateRef for RawInstance<L> {
    fn translate_ref(&self, p: Point) -> Self {
        self.clone().translate(p)
    }
}

impl<L: Clone> TransformRef for RawInstance<L> {
    fn transform_ref(&self, trans: Transformation) -> Self {
        self.clone().transform(trans)
    }
}

impl<S: Schema> Draw<S> for Shape<S::Layer> {
    fn draw(self, recv: &mut DrawReceiver<S>) -> Result<()> {
        recv.draw_element(self);
        Ok(())
    }
}

impl<S: Schema> Draw<S> for Text<S::Layer> {
    fn draw(self, recv: &mut DrawReceiver<S>) -> Result<()> {
        recv.draw_element(self);
        Ok(())
    }
}

/// A primitive layout element.
#[derive(Debug, Clone, PartialEq)]
pub enum Element<L> {
    /// A raw layout instance.
    Instance(RawInstance<L>),
    /// A primitive layout shape.
    Shape(Shape<L>),
    /// A primitive text annotation.
    Text(Text<L>),
}

/// A pointer to a primitive layout element.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ElementRef<'a, L> {
    /// A raw layout instance.
    Instance(&'a RawInstance<L>),
    /// A primitive layout shape.
    Shape(&'a Shape<L>),
    /// A primitive text annotation.
    Text(&'a Text<L>),
}

impl<L> Element<L> {
    /// Converts from `&Element` to `ElementRef`.
    ///
    /// Produces a new `ElementRef` containing a reference into
    /// the original element, but leaves the original in place.
    pub fn as_ref(&self) -> ElementRef<'_, L> {
        match self {
            Self::Instance(x) => ElementRef::Instance(x),
            Self::Shape(x) => ElementRef::Shape(x),
            Self::Text(x) => ElementRef::Text(x),
        }
    }

    /// If this is an `Instance` variant, returns the contained instance.
    /// Otherwise, returns [`None`].
    pub fn instance(self) -> Option<RawInstance<L>> {
        match self {
            Self::Instance(x) => Some(x),
            _ => None,
        }
    }

    /// If this is a `Shape` variant, returns the contained shape.
    /// Otherwise, returns [`None`].
    pub fn shape(self) -> Option<Shape<L>> {
        match self {
            Self::Shape(x) => Some(x),
            _ => None,
        }
    }

    /// If this is a `Text` variant, returns the contained text.
    /// Otherwise, returns [`None`].
    pub fn text(self) -> Option<Text<L>> {
        match self {
            Self::Text(x) => Some(x),
            _ => None,
        }
    }
}

impl<L> From<layir::Element<L>> for Element<L> {
    fn from(value: layir::Element<L>) -> Self {
        match value {
            layir::Element::Text(t) => Self::Text(t),
            layir::Element::Shape(s) => Self::Shape(s),
        }
    }
}

impl<'a, L> ElementRef<'a, L> {
    /// If this is an `Instance` variant, returns the contained instance.
    /// Otherwise, returns [`None`].
    pub fn instance(self) -> Option<&'a RawInstance<L>> {
        match self {
            Self::Instance(x) => Some(x),
            _ => None,
        }
    }

    /// If this is a `Shape` variant, returns the contained shape.
    /// Otherwise, returns [`None`].
    pub fn shape(self) -> Option<&'a Shape<L>> {
        match self {
            Self::Shape(x) => Some(x),
            _ => None,
        }
    }

    /// If this is a `Text` variant, returns the contained text.
    /// Otherwise, returns [`None`].
    pub fn text(self) -> Option<&'a Text<L>> {
        match self {
            Self::Text(x) => Some(x),
            _ => None,
        }
    }
}

impl<L> Bbox for Element<L> {
    fn bbox(&self) -> Option<geometry::rect::Rect> {
        match self {
            Element::Instance(inst) => inst.bbox(),
            Element::Shape(shape) => shape.bbox(),
            Element::Text(_) => None,
        }
    }
}

impl<L: PartialEq> LayerBbox<L> for Element<L> {
    fn layer_bbox(&self, layer: &L) -> Option<geometry::rect::Rect> {
        match self {
            Element::Instance(inst) => inst.layer_bbox(layer),
            Element::Shape(shape) => shape.layer_bbox(layer),
            Element::Text(_) => None,
        }
    }
}

impl<L> From<RawInstance<L>> for Element<L> {
    fn from(value: RawInstance<L>) -> Self {
        Self::Instance(value)
    }
}

impl<L> From<Shape<L>> for Element<L> {
    fn from(value: Shape<L>) -> Self {
        Self::Shape(value)
    }
}

impl<L> From<Text<L>> for Element<L> {
    fn from(value: Text<L>) -> Self {
        Self::Text(value)
    }
}

impl<L: Clone> TranslateRef for Element<L> {
    fn translate_ref(&self, p: Point) -> Self {
        self.clone().translate(p)
    }
}

impl<L: Clone> TransformRef for Element<L> {
    fn transform_ref(&self, trans: Transformation) -> Self {
        self.clone().transform(trans)
    }
}

impl<L> TranslateMut for Element<L> {
    fn translate_mut(&mut self, p: Point) {
        match self {
            Element::Instance(inst) => inst.translate_mut(p),
            Element::Shape(shape) => shape.translate_mut(p),
            Element::Text(text) => text.translate_mut(p),
        }
    }
}

impl<L> TransformMut for Element<L> {
    fn transform_mut(&mut self, trans: Transformation) {
        match self {
            Element::Instance(inst) => inst.transform_mut(trans),
            Element::Shape(shape) => shape.transform_mut(trans),
            Element::Text(text) => text.transform_mut(trans),
        }
    }
}

impl<S: Schema> Draw<S> for Element<S::Layer> {
    fn draw(self, cell: &mut DrawReceiver<S>) -> Result<()> {
        cell.draw_element(self);
        Ok(())
    }
}

impl<L> Bbox for ElementRef<'_, L> {
    fn bbox(&self) -> Option<geometry::rect::Rect> {
        match self {
            ElementRef::Instance(inst) => inst.bbox(),
            ElementRef::Shape(shape) => shape.bbox(),
            ElementRef::Text(_) => None,
        }
    }
}

impl<L: PartialEq> LayerBbox<L> for ElementRef<'_, L> {
    fn layer_bbox(&self, layer: &L) -> Option<Rect> {
        match self {
            ElementRef::Instance(inst) => inst.layer_bbox(layer),
            ElementRef::Shape(shape) => shape.layer_bbox(layer),
            ElementRef::Text(_) => None,
        }
    }
}

impl<'a, L> From<&'a RawInstance<L>> for ElementRef<'a, L> {
    fn from(value: &'a RawInstance<L>) -> Self {
        Self::Instance(value)
    }
}

impl<'a, L> From<&'a Shape<L>> for ElementRef<'a, L> {
    fn from(value: &'a Shape<L>) -> Self {
        Self::Shape(value)
    }
}

impl<'a, L> From<&'a Text<L>> for ElementRef<'a, L> {
    fn from(value: &'a Text<L>) -> Self {
        Self::Text(value)
    }
}