cxx/syntax/mod.rs

271 lines
5.8 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;
pub mod attrs;
2019-10-20 14:51:12 -04:00
pub mod check;
2020-11-27 14:30:12 -08:00
pub 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;
mod improper;
pub mod instantiate;
pub mod mangle;
pub mod map;
mod names;
pub mod namespace;
2019-10-20 14:51:12 -04:00
mod parse;
mod pod;
2020-09-06 23:28:18 -07:00
pub mod qualified;
pub mod report;
mod resolve;
2019-10-20 14:51:12 -04:00
pub mod set;
pub mod symbol;
2019-10-20 14:51:12 -04:00
mod tokens;
mod toposort;
pub mod trivial;
2019-10-20 14:51:12 -04:00
pub mod types;
use self::attrs::OtherAttrs;
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, Generics, Lifetime, LitInt, Token, Type as RustType};
2019-10-20 14:51:12 -04:00
pub use self::atom::Atom;
2020-11-27 12:06:12 -08:00
pub use self::derive::{Derive, Trait};
2019-10-20 14:51:12 -04:00
pub use self::doc::Doc;
pub use self::names::ForeignName;
2019-10-20 14:51:12 -04:00
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 lang: Lang,
2019-10-20 14:51:12 -04:00
pub doc: Doc,
pub derives: Vec<Derive>,
pub attrs: OtherAttrs,
2020-12-30 17:25:38 -08:00
pub visibility: Token![pub],
2019-10-20 14:51:12 -04:00
pub type_token: Token![type],
pub name: Pair,
pub generics: Lifetimes,
2020-11-27 20:00:53 -08:00
pub colon_token: Option<Token![:]>,
pub bounds: Vec<Derive>,
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>,
pub attrs: OtherAttrs,
2020-12-30 15:26:06 -08:00
pub visibility: Token![pub],
2019-10-20 14:51:12 -04:00
pub struct_token: Token![struct],
pub name: Pair,
pub generics: Lifetimes,
pub brace_token: Brace,
2019-10-20 14:51:12 -04:00
pub fields: Vec<Var>,
}
pub struct Enum {
pub doc: Doc,
2020-11-27 11:33:29 -08:00
pub derives: Vec<Derive>,
pub attrs: OtherAttrs,
2020-12-30 17:15:46 -08:00
pub visibility: Token![pub],
pub enum_token: Token![enum],
pub name: Pair,
pub generics: Lifetimes,
pub brace_token: Brace,
pub variants: Vec<Variant>,
pub repr: Atom,
pub repr_type: Type,
pub explicit_repr: bool,
}
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 attrs: OtherAttrs,
2020-12-30 18:02:38 -08:00
pub visibility: Token![pub],
pub name: Pair,
pub sig: Signature,
pub semi_token: Token![;],
pub trusted: bool,
}
2020-05-04 02:34:33 -07:00
pub struct TypeAlias {
2020-09-13 10:34:31 -07:00
pub doc: Doc,
2020-11-27 20:00:53 -08:00
pub derives: Vec<Derive>,
pub attrs: OtherAttrs,
2020-12-30 17:31:19 -08:00
pub visibility: Token![pub],
2020-05-04 02:34:33 -07:00
pub type_token: Token![type],
pub name: Pair,
pub generics: Lifetimes,
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 impl_generics: Lifetimes,
2020-11-29 18:09:52 -08:00
pub negative: bool,
2020-10-03 22:20:22 -07:00
pub ty: Type,
pub ty_generics: Lifetimes,
2020-10-03 22:20:22 -07:00
pub brace_token: Brace,
2020-11-29 18:09:52 -08:00
pub negative_token: Option<Token![!]>,
2020-10-03 22:20:22 -07:00
}
#[derive(Clone, Default)]
pub struct Lifetimes {
pub lt_token: Option<Token![<]>,
pub lifetimes: Punctuated<Lifetime, Token![,]>,
pub gt_token: Option<Token![>]>,
}
pub struct Signature {
2020-09-06 23:45:55 -07:00
pub unsafety: Option<Token![unsafe]>,
pub fn_token: Token![fn],
2020-11-15 18:11:40 -08:00
pub generics: Generics,
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
}
pub struct Var {
pub doc: Doc,
pub attrs: OtherAttrs,
pub visibility: Token![pub],
pub name: Pair,
2019-10-20 14:51:12 -04:00
pub ty: Type,
}
pub struct Receiver {
2020-11-15 16:07:08 -08:00
pub pinned: bool,
pub ampersand: Token![&],
pub lifetime: Option<Lifetime>,
pub mutable: bool,
2020-04-20 02:13:56 -07:00
pub var: Token![self],
2021-01-01 14:15:18 -08:00
pub ty: NamedType,
pub shorthand: bool,
2020-11-15 16:07:08 -08:00
pub pin_tokens: Option<(kw::Pin, Token![<], Token![>])>,
pub mutability: Option<Token![mut]>,
2019-10-20 14:51:12 -04:00
}
pub struct Variant {
2020-12-30 00:01:26 -08:00
pub doc: Doc,
pub attrs: OtherAttrs,
2020-12-21 16:00:41 -08:00
pub name: Pair,
pub discriminant: Discriminant,
pub expr: Option<Expr>,
}
2019-10-20 14:51:12 -04:00
pub enum Type {
2021-01-01 14:15:18 -08:00
Ident(NamedType),
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>),
2020-12-01 15:27:43 -08:00
SharedPtr(Box<Ty1>),
2020-12-28 17:09:48 -08:00
WeakPtr(Box<Ty1>),
2019-10-20 14:51:12 -04:00
Ref(Box<Ref>),
Str(Box<Ref>),
2020-04-24 15:20:26 -07:00
CxxVector(Box<Ty1>),
Fn(Box<Signature>),
Void(Span),
SliceRef(Box<SliceRef>),
2020-11-12 10:24:18 +08:00
Array(Box<Array>),
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 {
2020-11-15 15:07:04 -08:00
pub pinned: bool,
2019-10-20 14:51:12 -04:00
pub ampersand: Token![&],
pub lifetime: Option<Lifetime>,
pub mutable: bool,
2019-10-20 14:51:12 -04:00
pub inner: Type,
2020-11-15 15:07:04 -08:00
pub pin_tokens: Option<(kw::Pin, Token![<], Token![>])>,
pub mutability: Option<Token![mut]>,
2019-10-20 14:51:12 -04:00
}
pub struct SliceRef {
pub ampersand: Token![&],
pub lifetime: Option<Lifetime>,
pub mutable: bool,
pub bracket: Bracket,
pub inner: Type,
pub mutability: Option<Token![mut]>,
}
2020-11-12 10:24:18 +08:00
pub struct Array {
pub bracket: Bracket,
pub inner: Type,
pub semi_token: Token![;],
pub len: usize,
2020-11-24 20:45:13 -08:00
pub len_token: LitInt,
2020-11-12 10:24:18 +08:00
}
#[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,
pub cxx: ForeignName,
2020-11-02 00:18:19 -08:00
pub rust: Ident,
}
// Wrapper for a type which needs to be resolved before it can be printed in
// C++.
2020-12-30 19:48:42 -08:00
#[derive(PartialEq, Eq, Hash)]
2021-01-01 14:15:18 -08:00
pub struct NamedType {
pub rust: Ident,
2020-12-30 19:48:42 -08:00
pub generics: Lifetimes,
}