cxx/syntax/mod.rs

202 lines
4.0 KiB
Rust
Raw Normal View History

2019-10-20 14:51:12 -04:00
// Functionality that is shared between the cxxbridge macro and the cmd.
pub mod atom;
mod attrs;
pub mod check;
mod derive;
mod discriminant;
2019-10-20 14:51:12 -04:00
mod doc;
pub mod error;
pub mod file;
2019-10-20 14:51:12 -04:00
pub mod ident;
mod impls;
pub mod mangle;
mod names;
pub mod namespace;
2019-10-20 14:51:12 -04:00
mod parse;
2020-09-06 23:28:18 -07:00
pub mod qualified;
pub mod report;
2019-10-20 14:51:12 -04:00
pub mod set;
pub mod symbol;
2019-10-20 14:51:12 -04:00
mod tokens;
pub mod types;
use self::discriminant::Discriminant;
use self::namespace::Namespace;
use self::parse::kw;
use self::symbol::Symbol;
use proc_macro2::{Ident, Span};
use syn::punctuated::Punctuated;
use syn::token::{Brace, Bracket, Paren};
use syn::{Expr, Lifetime, Token, Type as RustType};
2019-10-20 14:51:12 -04:00
pub use self::atom::Atom;
pub use self::derive::Derive;
2019-10-20 14:51:12 -04:00
pub use self::doc::Doc;
pub use self::parse::parse_items;
pub use self::types::Types;
pub enum Api {
Include(Include),
2019-10-20 14:51:12 -04:00
Struct(Struct),
Enum(Enum),
2019-10-20 14:51:12 -04:00
CxxType(ExternType),
CxxFunction(ExternFn),
RustType(ExternType),
RustFunction(ExternFn),
2020-05-04 02:34:33 -07:00
TypeAlias(TypeAlias),
2020-10-03 22:20:22 -07:00
Impl(Impl),
2019-10-20 14:51:12 -04:00
}
pub struct Include {
pub path: String,
pub kind: IncludeKind,
pub begin_span: Span,
pub end_span: Span,
}
2020-10-28 12:40:27 -07:00
/// Whether to emit `#include "path"` or `#include <path>`.
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum IncludeKind {
2020-10-28 12:40:27 -07:00
/// `#include "quoted/path/to"`
Quoted,
/// `#include <bracketed/path/to>`
Bracketed,
}
2019-10-20 14:51:12 -04:00
pub struct ExternType {
pub doc: Doc,
pub type_token: Token![type],
pub name: Pair,
pub semi_token: Token![;],
pub trusted: bool,
2019-10-20 14:51:12 -04:00
}
pub struct Struct {
pub doc: Doc,
pub derives: Vec<Derive>,
2019-10-20 14:51:12 -04:00
pub struct_token: Token![struct],
pub name: Pair,
pub brace_token: Brace,
2019-10-20 14:51:12 -04:00
pub fields: Vec<Var>,
}
pub struct Enum {
pub doc: Doc,
pub enum_token: Token![enum],
pub name: Pair,
pub brace_token: Brace,
pub variants: Vec<Variant>,
pub repr: Atom,
}
2019-10-20 14:51:12 -04:00
pub struct ExternFn {
pub lang: Lang,
2019-10-20 14:51:12 -04:00
pub doc: Doc,
pub name: Pair,
pub sig: Signature,
pub semi_token: Token![;],
}
2020-05-04 02:34:33 -07:00
pub struct TypeAlias {
2020-09-13 10:34:31 -07:00
pub doc: Doc,
2020-05-04 02:34:33 -07:00
pub type_token: Token![type],
pub name: Pair,
2020-05-04 02:34:33 -07:00
pub eq_token: Token![=],
pub ty: RustType,
pub semi_token: Token![;],
}
2020-10-03 22:20:22 -07:00
pub struct Impl {
pub impl_token: Token![impl],
pub ty: Type,
pub brace_token: Brace,
}
pub struct Signature {
2020-09-06 23:45:55 -07:00
pub unsafety: Option<Token![unsafe]>,
pub fn_token: Token![fn],
2019-10-20 14:51:12 -04:00
pub receiver: Option<Receiver>,
pub args: Punctuated<Var, Token![,]>,
2019-10-20 14:51:12 -04:00
pub ret: Option<Type>,
2020-03-16 00:30:23 -07:00
pub throws: bool,
pub paren_token: Paren,
pub throws_tokens: Option<(kw::Result, Token![<], Token![>])>,
2019-10-20 14:51:12 -04:00
}
#[derive(Eq, PartialEq, Hash)]
2019-10-20 14:51:12 -04:00
pub struct Var {
pub ident: Ident,
2019-10-20 14:51:12 -04:00
pub ty: Type,
}
pub struct Receiver {
pub ampersand: Token![&],
pub lifetime: Option<Lifetime>,
2019-10-20 14:51:12 -04:00
pub mutability: Option<Token![mut]>,
2020-04-20 02:13:56 -07:00
pub var: Token![self],
pub ty: ResolvableName,
pub shorthand: bool,
2019-10-20 14:51:12 -04:00
}
pub struct Variant {
pub ident: Ident,
pub discriminant: Discriminant,
pub expr: Option<Expr>,
}
2019-10-20 14:51:12 -04:00
pub enum Type {
Ident(ResolvableName),
2019-10-20 14:51:12 -04:00
RustBox(Box<Ty1>),
RustVec(Box<Ty1>),
2019-10-20 14:51:12 -04:00
UniquePtr(Box<Ty1>),
Ref(Box<Ref>),
Str(Box<Ref>),
2020-04-24 15:20:26 -07:00
CxxVector(Box<Ty1>),
Fn(Box<Signature>),
Void(Span),
Slice(Box<Slice>),
SliceRefU8(Box<Ref>),
2019-10-20 14:51:12 -04:00
}
pub struct Ty1 {
pub name: Ident,
2019-10-20 14:51:12 -04:00
pub langle: Token![<],
pub inner: Type,
pub rangle: Token![>],
}
pub struct Ref {
pub ampersand: Token![&],
pub lifetime: Option<Lifetime>,
2019-10-20 14:51:12 -04:00
pub mutability: Option<Token![mut]>,
pub inner: Type,
}
pub struct Slice {
pub bracket: Bracket,
pub inner: Type,
}
#[derive(Copy, Clone, PartialEq)]
pub enum Lang {
Cxx,
Rust,
}
2020-10-30 21:21:23 -07:00
// An association of a defined Rust name with a fully resolved, namespace
// qualified C++ name.
#[derive(Clone)]
pub struct Pair {
2020-11-01 20:45:14 -08:00
pub namespace: Namespace,
2020-11-02 00:18:19 -08:00
pub cxx: Ident,
pub rust: Ident,
}
// Wrapper for a type which needs to be resolved before it can be printed in
// C++.
#[derive(Clone, PartialEq, Hash)]
pub struct ResolvableName {
pub rust: Ident,
}