Skip to content

Commit

Permalink
deploy: 1bf729f
Browse files Browse the repository at this point in the history
  • Loading branch information
raviqqe committed Feb 18, 2024
1 parent b6ee7cc commit 8700f26
Showing 1 changed file with 97 additions and 39 deletions.
136 changes: 97 additions & 39 deletions src/melior_macro/dialect/generation/operation_builder.rs.html
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,70 @@
<a href="#166" id="166">166</a>
<a href="#167" id="167">167</a>
<a href="#168" id="168">168</a>
<a href="#169" id="169">169</a>
<a href="#170" id="170">170</a>
<a href="#171" id="171">171</a>
<a href="#172" id="172">172</a>
<a href="#173" id="173">173</a>
<a href="#174" id="174">174</a>
<a href="#175" id="175">175</a>
<a href="#176" id="176">176</a>
<a href="#177" id="177">177</a>
<a href="#178" id="178">178</a>
<a href="#179" id="179">179</a>
<a href="#180" id="180">180</a>
<a href="#181" id="181">181</a>
<a href="#182" id="182">182</a>
<a href="#183" id="183">183</a>
<a href="#184" id="184">184</a>
<a href="#185" id="185">185</a>
<a href="#186" id="186">186</a>
<a href="#187" id="187">187</a>
<a href="#188" id="188">188</a>
<a href="#189" id="189">189</a>
<a href="#190" id="190">190</a>
<a href="#191" id="191">191</a>
<a href="#192" id="192">192</a>
<a href="#193" id="193">193</a>
<a href="#194" id="194">194</a>
<a href="#195" id="195">195</a>
<a href="#196" id="196">196</a>
<a href="#197" id="197">197</a>
</pre></div><pre class="rust"><code><span class="kw">use </span><span class="kw">crate</span>::dialect::{
error::Error, operation::OperationBuilder, utility::sanitize_snake_case_identifier,
error::Error,
operation::{OperationBuilder, OperationField},
utility::sanitize_snake_case_identifier,
};
<span class="kw">use </span>proc_macro2::TokenStream;
<span class="kw">use </span>quote::{format_ident, quote};

<span class="kw">pub fn </span>generate_operation_builder(builder: <span class="kw-2">&amp;</span>OperationBuilder) -&gt; <span class="prelude-ty">Result</span>&lt;TokenStream, Error&gt; {
<span class="kw">let </span>state_types = builder.type_state().parameters();
<span class="kw">let </span>field_fns = generate_field_fns(builder);
<span class="kw">let </span>result_fns = builder
.operation()
.results()
.map(|result| generate_field_fn(builder, result))
.collect::&lt;Vec&lt;<span class="kw">_</span>&gt;&gt;();
<span class="kw">let </span>operand_fns = builder
.operation()
.operands()
.map(|operand| generate_field_fn(builder, operand))
.collect::&lt;Vec&lt;<span class="kw">_</span>&gt;&gt;();
<span class="kw">let </span>region_fns = builder
.operation()
.regions()
.map(|region| generate_field_fn(builder, region))
.collect::&lt;Vec&lt;<span class="kw">_</span>&gt;&gt;();
<span class="kw">let </span>successor_fns = builder
.operation()
.successors()
.map(|successor| generate_field_fn(builder, successor))
.collect::&lt;Vec&lt;<span class="kw">_</span>&gt;&gt;();
<span class="kw">let </span>attribute_fns = builder
.operation()
.attributes()
.map(|attribute| generate_field_fn(builder, attribute))
.collect::&lt;Vec&lt;<span class="kw">_</span>&gt;&gt;();

<span class="kw">let </span>new_fn = generate_new_fn(builder);
<span class="kw">let </span>build_fn = generate_build_fn(builder)<span class="question-mark">?</span>;

Expand All @@ -185,6 +240,7 @@
builder.operation().documentation_name()
);
<span class="kw">let </span>type_arguments = builder.type_state().parameters();
<span class="kw">let </span>state_types = builder.type_state().parameters();

<span class="prelude-val">Ok</span>(<span class="macro">quote! </span>{
<span class="attr">#[doc = #doc]
Expand All @@ -196,57 +252,59 @@

#new_fn

#(#field_fns)*
#(#result_fns)*
#(#operand_fns)*
#(#region_fns)*
#(#successor_fns)*
#(#attribute_fns)*

#build_fn
})
}

<span class="comment">// TODO Split this function for different kinds of fields.
</span><span class="kw">fn </span>generate_field_fns(builder: <span class="kw-2">&amp;</span>OperationBuilder) -&gt; Vec&lt;TokenStream&gt; {
builder.operation().fields().map(<span class="kw">move </span>|field| {
<span class="kw">let </span>builder_identifier = builder.identifier();
<span class="kw">let </span>identifier = field.singular_identifier();
<span class="kw">let </span>parameter_type = field.parameter_type();
<span class="kw">let </span>argument = <span class="macro">quote! </span>{ #identifier: #parameter_type };
<span class="kw">let </span>add = <span class="macro">format_ident!</span>(<span class="string">"add_{}"</span>, field.plural_kind_identifier());
</span><span class="kw">fn </span>generate_field_fn(builder: <span class="kw-2">&amp;</span>OperationBuilder, field: <span class="kw-2">&amp;</span><span class="kw">impl </span>OperationField) -&gt; TokenStream {
<span class="kw">let </span>builder_identifier = builder.identifier();
<span class="kw">let </span>identifier = field.singular_identifier();
<span class="kw">let </span>parameter_type = field.parameter_type();
<span class="kw">let </span>argument = <span class="macro">quote! </span>{ #identifier: #parameter_type };
<span class="kw">let </span>add = <span class="macro">format_ident!</span>(<span class="string">"add_{}"</span>, field.plural_kind_identifier());

<span class="comment">// Argument types can be singular and variadic. But `add` functions in Melior
// are always variadic, so we need to create a slice or `Vec` for singular
// arguments.
</span><span class="kw">let </span>add_arguments = field.add_arguments(identifier);
<span class="comment">// Argument types can be singular and variadic. But `add` functions in Melior
// are always variadic, so we need to create a slice or `Vec` for singular
// arguments.
</span><span class="kw">let </span>add_arguments = field.add_arguments(identifier);

<span class="kw">if </span>field.is_optional() {
<span class="kw">let </span>parameters = builder.type_state().parameters().collect::&lt;Vec&lt;<span class="kw">_</span>&gt;&gt;();
<span class="kw">if </span>field.is_optional() {
<span class="kw">let </span>parameters = builder.type_state().parameters().collect::&lt;Vec&lt;<span class="kw">_</span>&gt;&gt;();

<span class="macro">quote! </span>{
<span class="kw">impl</span>&lt;<span class="lifetime">'c</span>, #(#parameters),<span class="kw-2">*</span>&gt; #builder_identifier&lt;<span class="lifetime">'c</span>, #(#parameters),<span class="kw-2">*</span>&gt; {
<span class="kw">pub fn </span>#identifier(<span class="kw-2">mut </span><span class="self">self</span>, #argument) -&gt; #builder_identifier&lt;<span class="lifetime">'c</span>, #(#parameters),<span class="kw-2">*</span>&gt; {
<span class="self">self</span>.builder = <span class="self">self</span>.builder.#add(#add_arguments);
<span class="self">self
</span>}
}
<span class="macro">quote! </span>{
<span class="kw">impl</span>&lt;<span class="lifetime">'c</span>, #(#parameters),<span class="kw-2">*</span>&gt; #builder_identifier&lt;<span class="lifetime">'c</span>, #(#parameters),<span class="kw-2">*</span>&gt; {
<span class="kw">pub fn </span>#identifier(<span class="kw-2">mut </span><span class="self">self</span>, #argument) -&gt; #builder_identifier&lt;<span class="lifetime">'c</span>, #(#parameters),<span class="kw-2">*</span>&gt; {
<span class="self">self</span>.builder = <span class="self">self</span>.builder.#add(#add_arguments);
<span class="self">self
</span>}
}
} <span class="kw">else if </span>field.is_result() &amp;&amp; builder.operation().can_infer_type() {
<span class="macro">quote!</span>()
} <span class="kw">else </span>{
<span class="kw">let </span>parameters = builder.type_state().parameters_without(field.name());
<span class="kw">let </span>arguments_set = builder.type_state().arguments_set(field.name(), <span class="bool-val">true</span>);
<span class="kw">let </span>arguments_unset = builder.type_state().arguments_set(field.name(), <span class="bool-val">false</span>);
}
} <span class="kw">else if </span>field.is_result() &amp;&amp; builder.operation().can_infer_type() {
<span class="macro">quote!</span>()
} <span class="kw">else </span>{
<span class="kw">let </span>parameters = builder.type_state().parameters_without(field.name());
<span class="kw">let </span>arguments_set = builder.type_state().arguments_set(field.name(), <span class="bool-val">true</span>);
<span class="kw">let </span>arguments_unset = builder.type_state().arguments_set(field.name(), <span class="bool-val">false</span>);

<span class="macro">quote! </span>{
<span class="kw">impl</span>&lt;<span class="lifetime">'c</span>, #(#parameters),<span class="kw-2">*</span>&gt; #builder_identifier&lt;<span class="lifetime">'c</span>, #(#arguments_unset),<span class="kw-2">*</span>&gt; {
<span class="kw">pub fn </span>#identifier(<span class="self">self</span>, #argument) -&gt; #builder_identifier&lt;<span class="lifetime">'c</span>, #(#arguments_set),<span class="kw-2">*</span>&gt; {
#builder_identifier {
context: <span class="self">self</span>.context,
builder: <span class="self">self</span>.builder.#add(#add_arguments),
_state: Default::default(),
}
<span class="macro">quote! </span>{
<span class="kw">impl</span>&lt;<span class="lifetime">'c</span>, #(#parameters),<span class="kw-2">*</span>&gt; #builder_identifier&lt;<span class="lifetime">'c</span>, #(#arguments_unset),<span class="kw-2">*</span>&gt; {
<span class="kw">pub fn </span>#identifier(<span class="self">self</span>, #argument) -&gt; #builder_identifier&lt;<span class="lifetime">'c</span>, #(#arguments_set),<span class="kw-2">*</span>&gt; {
#builder_identifier {
context: <span class="self">self</span>.context,
builder: <span class="self">self</span>.builder.#add(#add_arguments),
_state: Default::default(),
}
}
}
}
}).collect()
}
}

<span class="kw">fn </span>generate_build_fn(builder: <span class="kw-2">&amp;</span>OperationBuilder) -&gt; <span class="prelude-ty">Result</span>&lt;TokenStream, Error&gt; {
Expand Down

0 comments on commit 8700f26

Please sign in to comment.