pulsar_ir/
label.rs

1// Copyright (C) 2024 Ethan Uppal. All rights reserved.
2use pulsar_frontend::ty::Type;
3use std::fmt::{Display, Formatter};
4
5/// If one exists, the start symbol for a pulsar program is guaranteed to begin
6/// with the following.
7pub const MAIN_SYMBOL_PREFIX: &str = "_pulsar_Smain";
8
9pub struct LabelName {
10    unmangled: String,
11    mangled: String,
12    is_native: bool
13}
14
15impl LabelName {
16    pub fn from_native(value: String, args: &Vec<Type>, ret: &Type) -> Self {
17        let mut mangled = String::new();
18        mangled.push_str("_pulsar");
19        mangled.push_str(&format!("_S{}", value));
20        for arg in args {
21            mangled.push_str(&format!("_{}", arg.mangle()));
22        }
23        mangled.push_str(&format!("_{}", ret.mangle()));
24        Self {
25            unmangled: value,
26            mangled,
27            is_native: true
28        }
29    }
30
31    pub fn mangle(&self) -> &String {
32        &self.mangled
33    }
34}
35
36impl Display for LabelName {
37    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38        if self.is_native {
39            write!(f, "@native({})", self.unmangled)?;
40        } else {
41            self.mangled.fmt(f)?;
42        }
43
44        Ok(())
45    }
46}
47
48pub enum LabelVisibility {
49    Public,
50    Private,
51    External
52}
53
54impl Display for LabelVisibility {
55    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
56        match &self {
57            LabelVisibility::Public => "public",
58            LabelVisibility::Private => "private",
59            LabelVisibility::External => "external"
60        }
61        .fmt(f)
62    }
63}
64
65pub struct Label {
66    pub name: LabelName,
67    pub visibility: LabelVisibility
68}
69
70impl Label {
71    pub fn from(name: LabelName, visibility: LabelVisibility) -> Label {
72        Label { name, visibility }
73    }
74}
75
76impl Display for Label {
77    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
78        write!(f, "{} {}", self.visibility, self.name)
79    }
80}