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
// Copyright (c) 2016-2020 Fabian Schuiki

#![allow(unused_imports)]
#![allow(unused_variables)]

use crate::common::errors::DiagBuilder2;
use crate::common::errors::*;
use crate::common::name::{get_name_table, Name};
use crate::common::source::Spanned;
use crate::common::{Session, SessionContext, Verbosity};

use crate::arenas::{Alloc, AllocOwned};
use crate::hir::visit::Visitor;
use crate::hir::{self, Decl2, FromAst, LatentNode, Library, Node};
use crate::konst2::{self, Const2, OwnedConst};
use crate::scope2::ScopeData;
use crate::syntax::ast;
use crate::ty2;

pub fn emit_pkgs(sess: &Session, nodes: Vec<&ast::DesignUnit>) {
    let (arenas, type_arena, const_arena) = (
        hir::Arenas2::new(),
        ty2::TypeArena::new(),
        konst2::ConstArena::new(),
    );
    let scope = arenas.alloc(ScopeData::root());
    let ctx = hir::AllocContext {
        sess: sess,
        arenas: &arenas,
        scope: scope,
    };

    // Allocate the library.
    let name = get_name_table().intern("magic", true);
    let lib = Library::new(name, &nodes, ctx).unwrap();
    if sess.failed() {
        return;
    }

    // Force name resolution and HIR creation.
    debugln!("forcing HIR creation");
    lib.accept(&mut IdentityVisitor);

    // Visit the type declarations.
    debugln!("listing type declarations");
    let mut v = TypeVisitor {
        sess: sess,
        type_arena: &type_arena,
        const_arena: &const_arena,
    };
    lib.accept(&mut v);

    // // Visit the names.
    // debugln!("names:");
    // let mut v = NameVisitor;
    // lib.accept(&mut v);

    // // Collect references to all packages.
    // let mut v = PackageGatherer(Vec::new());
    // lib.accept(&mut v);
    // debugln!("gathered packages:");
    // for pkg in v.0 {
    //     debugln!("- {}", pkg.name().value);
    // }
}

struct IdentityVisitor;

impl<'t> Visitor<'t> for IdentityVisitor {
    fn as_visitor(&mut self) -> &mut Visitor<'t> {
        self
    }
}

// struct NameVisitor;

// impl<'t> Visitor<'t> for NameVisitor {
//     fn as_visitor(&mut self) -> &mut Visitor<'t> {
//         self
//     }

//     fn visit_name(&mut self, name: Spanned<Name>) {
//         debugln!("- {}", name.value);
//     }
// }

// struct PackageGatherer<'t>(Vec<&'t hir::Package2<'t>>);

// impl<'t> Visitor<'t> for PackageGatherer<'t> {
//     fn as_visitor(&mut self) -> &mut Visitor<'t> {
//         self
//     }

//     fn visit_pkg(&mut self, pkg: &'t hir::Package2<'t>) {
//         self.0.push(pkg);
//         pkg.walk(self);
//     }
// }

#[derive(Copy, Clone)]
struct TypeVisitor<'t> {
    sess: &'t Session,
    type_arena: &'t ty2::TypeArena<'t>,
    const_arena: &'t konst2::ConstArena<'t>,
}

impl<'a, 't: 'a> SessionContext for &'a TypeVisitor<'t> {
    fn has_verbosity(&self, verb: Verbosity) -> bool {
        self.sess.has_verbosity(verb)
    }
}

impl<'a, 't: 'a> DiagEmitter for &'a TypeVisitor<'t> {
    fn emit(&self, diag: DiagBuilder2) {
        self.sess.emit(diag);
    }
}

impl<'b, 'a, 't: 'a, T: 't> Alloc<'b, 't, T> for &'a TypeVisitor<'t>
where
    ty2::TypeArena<'t>: Alloc<'t, 't, T>,
{
    fn alloc(&'b self, value: T) -> &'t mut T {
        self.type_arena.alloc(value)
    }
}

impl<'b, 'a, 't: 'a> Alloc<'b, 't, konst2::IntegerConst<'t>> for &'a TypeVisitor<'t> {
    fn alloc(&'b self, value: konst2::IntegerConst<'t>) -> &'t mut konst2::IntegerConst<'t> {
        self.const_arena.alloc(value)
    }
}

impl<'a, 'b, 't: 'a> AllocOwned<'b, 't, konst2::Const2<'t>> for &'a TypeVisitor<'t> {
    fn alloc_owned(&'b self, value: OwnedConst<'t>) -> &'t Const2<'t> {
        self.const_arena.alloc_owned(value)
    }
}

impl<'a, 'b, 't: 'a> AllocOwned<'b, 't, ty2::Type> for &'a TypeVisitor<'t> {
    fn alloc_owned(&'b self, value: ty2::OwnedType<'t>) -> &'t ty2::Type {
        self.type_arena.alloc_owned(value)
    }
}

impl<'t> Visitor<'t> for TypeVisitor<'t> {
    fn as_visitor(&mut self) -> &mut Visitor<'t> {
        self
    }

    fn visit_type_decl(&mut self, decl: &'t hir::TypeDecl2<'t>) {
        if let Ok(t) = decl.declared_type(self as &_) {
            debugln!("type {} = {}", decl.name(), t);
        }
        decl.walk(self);
    }
}