📄 ucri.h
字号:
/* UnderC Reflection Interface (UCRI)
* UnderC C++ interpreter
* Steve Donovan, 2001,2002
* This is GPL'd software, and the usual disclaimers apply.
* See LICENCE
*
* ***UCRI version 1.0***
*/
/*! \file ucri.h
* UCRI supplies a dynamic interface to the underlying
* UnderC rich type system, beyond standard RTTI.
* You can lookup symbols, extract all entries in a given
* context (namespace or class), inspect functions and
* instantiate templates dynamically. Objects may be created
* using the class object.
*
* These classes are essentially thin wrappers over the
* existing internal implementation classes,
* e.g. XClass is a wrapper around a Class object pointer.
* I also have attempted to give a simplified view of
* UnderC internals.
*/
#ifndef __UCRI_H
#define __UCRI_H
#ifdef WITHIN_UC
# include "common.h"
#else
// I'm using forward class declarations to minimise the
// link mismatches you would get from simple typedefs.
struct Entry;
typedef Entry *PEntry;
class NamedTable;
class Function;
class Class;
class Module;
class Type;
class TemplateEntry;
// these guys are needed for fblock.h
class Table;
class Instruction;
typedef Instruction* PInstruction;
class XTrace;
# include "fblock.h"
typedef unsigned int uint;
enum { // exactly as they appear in table.h
TEMPS=1, CONSTS=2, FUNCTIONS=4, NON_STATIC=8,
VIRTUALS=16, BUILTINS=32,
CTORS=64, DTORS=128, TYPEDEFS=256,
NAMESPACES=512, CLASSES=1024, FIELDS=4096,
IMPORTS=8192, UNDEFINED=16384,
DO_PARENT=2048 };
enum RMode { // address modes
NONE,
DIRECT, // 'direct' here means 22bit offset into data area
SREL, // stack-relative (auto variables)
OREL, // object-relative (class members)
OREL_F // bit-field (will also be object-relative)
};
#endif
// *add 1.2.4 Access to underlying pcode
// just like Instruction in engine.h
// 32-bit instruction record
struct XInstruction {
uint opcode: 8;
uint rmode: 2;
int data: 22;
};
// listx looks like the UC pocket list class, and
// uses the correct allocator. If you use the typedefs,
// there should be no future problems when I can use
// a proper std::list.
#include "listx.h"
#include "export.h"
#include "xtrace.h"
class XFunction;
class XClass;
class XTemplateFun;
class XType;
typedef listx<XType *> XTList;
typedef listx<string> XStringList;
/// XType wraps Type (see types.h).
/// I've left out the means to modify the underlying type.
class EXPORT XType {
private:
Type* m_type;
public:
XType(Type* pt);
bool is_const() const;
bool is_reference() const;
bool is_pointer() const;
bool is_array() const;
bool is_unsigned() const;
bool is_number() const;
bool is_int() const;
bool is_float() const;
bool is_single() const;
bool is_long() const;
bool is_short() const;
bool is_char() const;
bool is_double() const;
bool is_signature() const;
bool is_function() const;
bool is_class() const;
bool is_object() const;
bool is_bool() const;
bool is_void() const;
bool is_namespace() const;
int pointer_depth() const;
int size() const;
/// if is_class() is true, then this is the class object.
XClass* as_class() const;
/// convert to a string representation, eg. "const char*".
char* as_str() const;
/// convert a value to a string.
/// \param s An output string
/// \param ptr A pointer to a data value
void val_as_str(string& s, void *ptr) const;
/// convert an ASCII representation into data.
/// \param buff a null-terminated char buffer
/// \param ptr a pointer to the converted value
void str_to_val(char *buff, void *ptr);
Type* type();
/// convert an ASCII representation (e.g. "int") into a type.
static XType* from_str(char *str);
/// convenient way to create a list of types.
static XTList& typelist(XType* t1,...);
};
/// XEntry wraps Entry, which is what all symbol tables
/// (classes, namespaces, function contexts, etc) contain.
/// Use it as a proxy for a variable. If it was a function
/// entry, then you can extract all members of an overloaded
/// set using nfun() and function().
class EXPORT XEntry {
private:
PEntry m_entry;
public:
XEntry(PEntry pe = NULL);
/// create a copy of the entry.
XEntry *clone();
/// given an array or pointer entry, then this is an entry representing an element.
XEntry *base_entry();
/// the symbol's name.
char* name();
/// pointer data, as an offset; the interpretation depends on the address mode.
int data();
/// converts the data into an actual pointer (the default is to use the global address space).
void* ptr(void *base=NULL);
void set_data(int x);
void set_ptr(void *p, void *base=NULL);
/// size of the entry - it's usually 1, more for arrays.
int size();
/// type of entry
XType* type();
/// the actual underlying Entry object which has been wrapped.
void* entry();
/// convert the data to a string.
void val_as_str(string& s, void *base=NULL);
/// convert a string to valid data for this entries' type.
void str_to_val(char *buff, void *base=NULL);
// for Function entries only...
/// number of functions in this overloaded set.
/// (It will be non-zero if this is a function entry)
int nfun();
/// fetch each function in the set, starting with 1
XFunction *function(int idx=1);
/// Address mode of entry.
/// This can be DIRECT (for global data), SREL (for stack-relative)
/// and OREL (for object-relative, i.e. non-static fields)
int addr_mode();
};
/// XFunction wraps Function; You can get the function declaration as
/// a string, the types of the args, the return type, and location info.
/// set_trace() will attach an XTrace-derived object to the function;
/// this will be executed whenever entering or leaving the function.
class EXPORT XFunction {
private:
Function* m_fun;
Entry* m_ref;
public:
XFunction(Function *fun = NULL);
/// short name of function.
char* name();
/// get the fully qualified name of this function.
void as_str(string& s);
/// the return type of the function
XType* ret_type();
/// the function arguments, as a list of XType*
XTList& args();
/// An alternative way to get function arguments.
/// (this will also get you the parameter names, if defined)
/// \param tl List of types
/// \param sl List of parameter names
void get_args(XTList* tl, XStringList* sl);
/// Where this function is defined.
/// \param filename A string to receive the filename
/// \return The line number
int where(string& filename);
/// Convert an address into a line number
/// \param ip A pointer to an instruction
/// \return The corresponding line number
int ip_to_line(void* ip);
/// This function's module id
/// \return A module id
/// \sa XModule
int module();
/// The function block
void* fblock();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -