-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
43695e3
commit 09ee5dd
Showing
12 changed files
with
399 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use crate::lexer::{Span, Token}; | ||
|
||
/// A path of identifier (e.g. `foo.bar.Foo`). | ||
pub struct Path { | ||
components: Vec<Token>, | ||
} | ||
|
||
impl Path { | ||
pub fn new(components: Vec<Token>) -> Self { | ||
assert!(!components.is_empty()); | ||
assert!(components.last().unwrap().is_identifier()); | ||
|
||
for i in 0..components.len() { | ||
if i % 2 == 0 { | ||
assert!(components[i].is_identifier()); | ||
} else { | ||
assert!(components[i].is_full_stop()); | ||
} | ||
} | ||
|
||
Self { components } | ||
} | ||
|
||
pub fn span(&self) -> Span { | ||
let mut iter = self.components.iter(); | ||
let mut span = iter.next().unwrap().span().clone(); | ||
|
||
for s in iter { | ||
span = &span + s.span(); | ||
} | ||
|
||
span | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use super::{Codegen, LlvmType}; | ||
use llvm_sys::core::{LLVMAddFunction, LLVMFunctionType, LLVMGetNamedFunction}; | ||
use llvm_sys::prelude::{LLVMTypeRef, LLVMValueRef}; | ||
use std::ffi::CStr; | ||
use std::marker::PhantomData; | ||
|
||
/// A function. | ||
pub struct LlvmFunc<'a, 'b: 'a> { | ||
value: LLVMValueRef, | ||
phantom: PhantomData<&'a Codegen<'b>>, | ||
} | ||
|
||
impl<'a, 'b: 'a> LlvmFunc<'a, 'b> { | ||
pub fn get<N: AsRef<CStr>>(cx: &'a Codegen<'b>, name: N) -> Option<Self> { | ||
let name = name.as_ref(); | ||
let value = unsafe { LLVMGetNamedFunction(cx.module, name.as_ptr()) }; | ||
|
||
if value.is_null() { | ||
None | ||
} else { | ||
Some(Self { | ||
value, | ||
phantom: PhantomData, | ||
}) | ||
} | ||
} | ||
|
||
pub fn new<N: AsRef<CStr>>( | ||
cx: &'a Codegen<'b>, | ||
name: N, | ||
params: &[LlvmType<'a, 'b>], | ||
ret: LlvmType<'a, 'b>, | ||
) -> Self { | ||
let name = name.as_ref(); | ||
let mut params: Vec<LLVMTypeRef> = params.iter().map(|p| p.as_raw()).collect(); | ||
let ty = unsafe { | ||
LLVMFunctionType( | ||
ret.as_raw(), | ||
params.as_mut_ptr(), | ||
params.len().try_into().unwrap(), | ||
0, | ||
) | ||
}; | ||
|
||
Self { | ||
value: unsafe { LLVMAddFunction(cx.module, name.as_ptr(), ty) }, | ||
phantom: PhantomData, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.