-
Notifications
You must be signed in to change notification settings - Fork 40
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
Refactor dialect macro input #655
Changes from 13 commits
61b7909
206cf19
e6323a0
b7af8f1
b2383b1
86e1744
53668df
7e52a5b
b8a425f
3e49500
a0a89a5
edb2474
c85d202
a8bbe59
9c96cf9
4628c35
9d6ed36
aaf204f
2d847de
b8ceece
62d907f
d31dba5
0e905d9
de69341
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,39 +16,53 @@ use operation::Operation; | |
use proc_macro::TokenStream; | ||
use proc_macro2::Span; | ||
use quote::quote; | ||
use std::{env, fmt::Display, path::Path, str}; | ||
use std::{ | ||
env, | ||
fmt::Display, | ||
path::{Component, Path}, | ||
str, | ||
}; | ||
use tblgen::{record::Record, record_keeper::RecordKeeper, TableGenParser}; | ||
|
||
const LLVM_INCLUDE_DIRECTORY: &str = env!("LLVM_INCLUDE_DIRECTORY"); | ||
|
||
pub fn generate_dialect(input: DialectInput) -> Result<TokenStream, Box<dyn std::error::Error>> { | ||
let mut parser = TableGenParser::new(); | ||
|
||
if let Some(source) = input.table_gen() { | ||
let base = Path::new(env!("LLVM_INCLUDE_DIRECTORY")); | ||
|
||
// Here source looks like `include "foo.td" include "bar.td"`. | ||
for (index, path) in source.split_ascii_whitespace().enumerate() { | ||
if index % 2 == 0 { | ||
continue; // skip "include" | ||
} | ||
|
||
let path = &path[1..(path.len() - 1)]; // remove "" | ||
let path = Path::new(path).parent().unwrap(); | ||
let path = base.join(path); | ||
parser = parser.add_include_path(&path.to_string_lossy()); | ||
} | ||
|
||
parser = parser.add_source(source).map_err(create_syn_error)?; | ||
} | ||
|
||
if let Some(file) = input.td_file() { | ||
parser = parser.add_source_file(file).map_err(create_syn_error)?; | ||
parser = parser.add_source_file(file); | ||
} | ||
|
||
for path in input.include_directories().chain([LLVM_INCLUDE_DIRECTORY]) { | ||
parser = parser.add_include_directory(path); | ||
} | ||
|
||
let llvm_include_directory = Path::new(LLVM_INCLUDE_DIRECTORY); | ||
|
||
for path in input.directories() { | ||
let path = if matches!( | ||
Path::new(path).components().next(), | ||
Some(Component::CurDir | Component::ParentDir) | ||
) { | ||
path.into() | ||
} else { | ||
llvm_include_directory.join(path).display().to_string() | ||
}; | ||
|
||
parser = parser.add_include_directory(&path); | ||
} | ||
|
||
for path in input | ||
.include_directories() | ||
.chain([env!("LLVM_INCLUDE_DIRECTORY")]) | ||
{ | ||
parser = parser.add_include_path(path); | ||
if input.files().count() > 0 { | ||
parser = parser.add_source( | ||
&input | ||
.files() | ||
.map(|path| format!(r#"include "{path}""#)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I noticed that the For now, we fall back to the original approach of defining the main file on the fly with a bunch of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, now I wonder if we should expose the |
||
.collect::<String>(), | ||
)?; | ||
} | ||
|
||
let keeper = parser.parse().map_err(Error::Parse)?; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We got permission to publish to the original tblgen crate, i published 0.5.2 there, if you can remove the "package =..." to use it. Thanks!