Skip to content

Fix build and split macros and plugin. #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

name = "postgres_extension"
version = "0.0.1"
authors = ["Daniel Fagnan <[email protected]>"]
authors = ["Daniel Fagnan <[email protected]>",
"Jeff Davis <[email protected]>"]

[lib]
name = "postgres_extension"
crate-type = ["dylib"]
crate-type = ["rlib"]

[dependencies]
libc = "*"
5 changes: 3 additions & 2 deletions macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

name = "postgres_extension_macros"
version = "0.0.1"
authors = ["Daniel Fagnan <[email protected]>"]
authors = ["Daniel Fagnan <[email protected]>",
"Jeff Davis <[email protected]>"]

[lib]
name = "postgres_extension_macros"
crate-type = ["dylib", "rlib"]
crate-type = ["rlib"]
55 changes: 0 additions & 55 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,58 +1,3 @@
#![feature(plugin_registrar, quote, box_syntax, rustc_private)]
#![allow(unused_imports)]

extern crate syntax;
extern crate syntax_ext;
extern crate rustc;
extern crate rustc_front;
extern crate rustc_plugin;

use rustc_plugin::Registry;
use syntax::ext::base::{MultiDecorator, MultiModifier};
use syntax::parse::token::intern;
use rustc::front::map::blocks::MaybeFnLike;

use syntax::ext::base::{ExtCtxt, Annotatable};
use syntax::codemap::Span;
use syntax::ptr::P;

use syntax::ast::{Item, ItemFn, MetaItem};
use syntax::attr;
use syntax_ext::deriving::generic::{combine_substructure, EnumMatching, FieldInfo, MethodDef, Struct, Substructure, TraitDef, ty};
use syntax::parse::token::InternedString;

#[plugin_registrar]
pub fn plugin_registrar(reg: &mut Registry) {
reg.register_syntax_extension(intern("pg_export"), MultiModifier(box expand_pg_export));
}

pub fn expand_pg_export(cx: &mut ExtCtxt, span: Span, _: &MetaItem, item: Annotatable) -> Annotatable {

//TODO: enforce func type check
// if !func.is_fn_like() {
// cx.span_err(span, "you can only export a function to PostgreSQL.");
// }

match item {
Annotatable::Item(it) => {
let mut new_it = (*it).clone();
new_it.attrs.push(attr::mk_attr_outer(attr::mk_attr_id(), attr::mk_word_item(InternedString::new("no_mangle"))));
Annotatable::Item(P(new_it))
}
Annotatable::ImplItem(it) => {
let mut new_it = (*it).clone();
new_it.attrs.push(attr::mk_attr_outer(attr::mk_attr_id(), attr::mk_word_item(InternedString::new("no_mangle"))));
Annotatable::ImplItem(P(new_it))
}
Annotatable::TraitItem(tt) => {
let mut new_it = (*tt).clone();
new_it.attrs.push(attr::mk_attr_outer(attr::mk_attr_id(), attr::mk_word_item(InternedString::new("no_mangle"))));
Annotatable::TraitItem(P(new_it))
}
}

}

/// Postgres has a macro called `PG_MODULE_MAGIC` that is supposed
/// to be called within extensions. This generates a bunch
/// of metadata structures that Postgres reads to determine
Expand Down
11 changes: 11 additions & 0 deletions plugin/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "postgres_extension_plugin"
version = "0.0.1"
authors = ["Daniel Fagnan <[email protected]>",
"Jeff Davis <[email protected]>"]

[dependencies]

[lib]
crate-type = ["dylib"]

54 changes: 54 additions & 0 deletions plugin/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#![feature(plugin_registrar, quote, box_syntax, rustc_private)]
#![allow(unused_imports)]

extern crate syntax;
extern crate syntax_ext;
extern crate rustc;
extern crate rustc_plugin;

use rustc_plugin::Registry;
use syntax::ext::base::{MultiDecorator, MultiModifier};
use syntax::parse::token::intern;
use rustc::hir::map::blocks::MaybeFnLike;

use syntax::ext::base::{ExtCtxt, Annotatable};
use syntax::codemap::Span;
use syntax::ptr::P;

use syntax::ast::{Item, MetaItem};
use syntax::attr;
use syntax_ext::deriving::generic::{combine_substructure, EnumMatching, FieldInfo, MethodDef, Struct, Substructure, TraitDef, ty};
use syntax::parse::token::InternedString;

#[plugin_registrar]
pub fn plugin_registrar(reg: &mut Registry) {
reg.register_syntax_extension(intern("pg_export"), MultiModifier(box expand_pg_export));
}

pub fn expand_pg_export(_cx: &mut ExtCtxt, _span: Span, _: &MetaItem, item: Annotatable) -> Annotatable {

//TODO: enforce func type check
// if !func.is_fn_like() {
// cx.span_err(span, "you can only export a function to PostgreSQL.");
// }

match item {
Annotatable::Item(it) => {
let mut new_it = (*it).clone();
new_it.attrs.push(attr::mk_attr_outer(attr::mk_attr_id(), attr::mk_word_item(InternedString::new("no_mangle"))));
Annotatable::Item(P(new_it))
}
Annotatable::ImplItem(it) => {
let mut new_it = (*it).clone();
new_it.attrs.push(attr::mk_attr_outer(attr::mk_attr_id(), attr::mk_word_item(InternedString::new("no_mangle"))));
Annotatable::ImplItem(P(new_it))
}
Annotatable::TraitItem(tt) => {
let mut new_it = (*tt).clone();
new_it.attrs.push(attr::mk_attr_outer(attr::mk_attr_id(), attr::mk_word_item(InternedString::new("no_mangle"))));
Annotatable::TraitItem(P(new_it))
}
}

}

8 changes: 5 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(core)]
#![allow(non_camel_case_types)]

extern crate libc;
extern crate core;
Expand All @@ -14,7 +14,7 @@ type fmNodePtr = *mut c_void;
type fmAggrefPtr = *mut c_void;

/// A trait that is implemented for all Postgres-compatible data types.
trait PgType {}
pub trait PgType {}

#[allow(dead_code)]
extern {
Expand Down Expand Up @@ -460,6 +460,7 @@ pub struct FunctionCallInfoData {
}

pub struct FunctionCallInfo<'a> {
#[allow(dead_code)]
ptr: *mut FunctionCallInfoData,
marker: PhantomData<&'a FunctionCallInfoData>
}
Expand All @@ -468,11 +469,12 @@ pub struct FunctionCallInfo<'a> {
/// a pointer-sized unsigned integer that acts like
/// a pointer.
pub struct Datum {
#[allow(dead_code)]
val: usize
}

impl Datum {
pub fn new_str(value: &str) -> Datum {
pub fn new_str(_value: &str) -> Datum {
// We need to allocate our string onto the heap
// and with the custom `palloc` allocator. `palloc`
// allocates memory into contexts such that they
Expand Down