You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
// IR Function:
// - 保存参数列表、基本块列表
// - 记录函数属性/元信息(按需要扩展)
# include "ir/IR.h"
namespace ir {
Function : : Function ( std : : string name , std : : shared_ptr < FunctionType > func_type )
: Value ( std : : move ( func_type ) , std : : move ( name ) ) {
entry_ = CreateBlock ( " entry " ) ;
}
// 向函数添加参数的实现。
Argument * Function : : AddArgument ( std : : unique_ptr < Argument > arg ) { // 独占所有权,自动释放内存
if ( ! arg ) return nullptr ; // 1. 检查参数是否为空
auto * ptr = arg . get ( ) ; // 2. 获取原始指针(用于返回)
arguments_ . push_back ( std : : move ( arg ) ) ; // 3. 将参数所有权转移到函数的参数列表中,arg已经是空指针了, 不能再使用arg了
return ptr ; // 4. 返回参数指针,方便调用者使用
}
// 获取函数参数列表的实现。
const std : : vector < std : : unique_ptr < Argument > > & Function : : GetArguments ( ) const {
return arguments_ ;
}
BasicBlock * Function : : CreateBlock ( const std : : string & name ) {
auto block = std : : make_unique < BasicBlock > ( name ) ;
auto * ptr = block . get ( ) ;
ptr - > SetParent ( this ) ;
blocks_ . push_back ( std : : move ( block ) ) ;
if ( ! entry_ ) {
entry_ = ptr ;
}
return ptr ;
}
BasicBlock * Function : : GetEntry ( ) { return entry_ ; }
const BasicBlock * Function : : GetEntry ( ) const { return entry_ ; }
const std : : vector < std : : unique_ptr < BasicBlock > > & Function : : GetBlocks ( ) const {
return blocks_ ;
}
} // namespace ir