cxx_build/gen/
include.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
use crate::gen::out::{Content, OutFile};
use crate::syntax::{self, IncludeKind};
use std::ops::{Deref, DerefMut};

/// The complete contents of the "rust/cxx.h" header.
pub static HEADER: &str = include_str!("include/cxx.h");

/// A header to #include.
///
/// The cxxbridge tool does not parse or even require the given paths to exist;
/// they simply go into the generated C++ code as #include lines.
#[derive(Clone, PartialEq, Debug)]
pub struct Include {
    /// The header's path, not including the enclosing quotation marks or angle
    /// brackets.
    pub path: String,
    /// Whether to emit `#include "path"` or `#include <path>`.
    pub kind: IncludeKind,
}

#[derive(Default, PartialEq)]
pub(crate) struct Includes<'a> {
    pub custom: Vec<Include>,
    pub algorithm: bool,
    pub array: bool,
    pub cassert: bool,
    pub cstddef: bool,
    pub cstdint: bool,
    pub cstring: bool,
    pub exception: bool,
    pub functional: bool,
    pub initializer_list: bool,
    pub iterator: bool,
    pub memory: bool,
    pub new: bool,
    pub stdexcept: bool,
    pub string: bool,
    pub type_traits: bool,
    pub utility: bool,
    pub vector: bool,
    pub basetsd: bool,
    pub sys_types: bool,
    pub content: Content<'a>,
}

impl<'a> Includes<'a> {
    pub(crate) fn new() -> Self {
        Includes::default()
    }

    pub(crate) fn insert(&mut self, include: impl Into<Include>) {
        self.custom.push(include.into());
    }

    pub(crate) fn has_cxx_header(&self) -> bool {
        self.custom
            .iter()
            .any(|header| header.path == "rust/cxx.h" || header.path == "rust\\cxx.h")
    }
}

pub(super) fn write(out: &mut OutFile) {
    let header = out.header;
    let include = &mut out.include;
    let cxx_header = include.has_cxx_header();
    let out = &mut include.content;

    if header {
        writeln!(out, "#pragma once");
    }

    for include in &include.custom {
        match include.kind {
            IncludeKind::Quoted => {
                writeln!(out, "#include \"{}\"", include.path.escape_default());
            }
            IncludeKind::Bracketed => {
                writeln!(out, "#include <{}>", include.path);
            }
        }
    }

    let Includes {
        custom: _,
        algorithm,
        array,
        cassert,
        cstddef,
        cstdint,
        cstring,
        exception,
        functional,
        initializer_list,
        iterator,
        memory,
        new,
        stdexcept,
        string,
        type_traits,
        utility,
        vector,
        basetsd,
        sys_types,
        content: _,
    } = *include;

    if algorithm && !cxx_header {
        writeln!(out, "#include <algorithm>");
    }
    if array && !cxx_header {
        writeln!(out, "#include <array>");
    }
    if cassert && !cxx_header {
        writeln!(out, "#include <cassert>");
    }
    if cstddef && !cxx_header {
        writeln!(out, "#include <cstddef>");
    }
    if cstdint && !cxx_header {
        writeln!(out, "#include <cstdint>");
    }
    if cstring {
        writeln!(out, "#include <cstring>");
    }
    if exception && !cxx_header {
        writeln!(out, "#include <exception>");
    }
    if functional {
        writeln!(out, "#include <functional>");
    }
    if initializer_list && !cxx_header {
        writeln!(out, "#include <initializer_list>");
    }
    if iterator && !cxx_header {
        writeln!(out, "#include <iterator>");
    }
    if memory {
        writeln!(out, "#include <memory>");
    }
    if new && !cxx_header {
        writeln!(out, "#include <new>");
    }
    if stdexcept && !cxx_header {
        writeln!(out, "#include <stdexcept>");
    }
    if string && !cxx_header {
        writeln!(out, "#include <string>");
    }
    if type_traits && !cxx_header {
        writeln!(out, "#include <type_traits>");
    }
    if utility && !cxx_header {
        writeln!(out, "#include <utility>");
    }
    if vector && !cxx_header {
        writeln!(out, "#include <vector>");
    }
    if basetsd && !cxx_header {
        writeln!(out, "#if defined(_WIN32)");
        writeln!(out, "#include <basetsd.h>");
    }
    if sys_types && !cxx_header {
        if basetsd {
            writeln!(out, "#else");
        } else {
            writeln!(out, "#if not defined(_WIN32)");
        }
    }
    if sys_types && !cxx_header {
        writeln!(out, "#include <sys/types.h>");
    }
    if (basetsd || sys_types) && !cxx_header {
        writeln!(out, "#endif");
    }
}

impl<'i, 'a> Extend<&'i Include> for Includes<'a> {
    fn extend<I: IntoIterator<Item = &'i Include>>(&mut self, iter: I) {
        self.custom.extend(iter.into_iter().cloned());
    }
}

impl From<&syntax::Include> for Include {
    fn from(include: &syntax::Include) -> Self {
        Include {
            path: include.path.clone(),
            kind: include.kind,
        }
    }
}

impl<'a> Deref for Includes<'a> {
    type Target = Content<'a>;

    fn deref(&self) -> &Self::Target {
        &self.content
    }
}

impl<'a> DerefMut for Includes<'a> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.content
    }
}