X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/029bc65b46de676c6733fff684000c7363eda512..11b0723c033fa7d6452faae6da15d8848e705fbd:/Parser.hpp diff --git a/Parser.hpp b/Parser.hpp index dc3c907..f48e81b 100644 --- a/Parser.hpp +++ b/Parser.hpp @@ -50,6 +50,7 @@ #include #include +#include #include #include "location.hh" @@ -239,13 +240,18 @@ _finline std::ostream &operator <<(std::ostream &lhs, const CYWord &rhs) { } struct CYIdentifier : + CYNext, CYWord { CYIdentifier *replace_; + size_t offset_; + size_t usage_; CYIdentifier(const char *word) : CYWord(word), - replace_(NULL) + replace_(NULL), + offset_(0), + usage_(0) { } @@ -283,7 +289,7 @@ struct CYLabel : virtual void Output(CYOutput &out, CYFlags flags) const; }; -struct CStringLess : +struct CYCStringLess : std::binary_function { _finline bool operator ()(const char *lhs, const char *rhs) const { @@ -295,38 +301,49 @@ struct CYIdentifierValueLess : std::binary_function { _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const { - return CStringLess()(lhs->Word(), rhs->Word()); + return CYCStringLess()(lhs->Word(), rhs->Word()); } }; enum CYIdentifierFlags { CYIdentifierArgument, - CYIdentifierInternal, - CYIdentifierVariable + CYIdentifierVariable, + CYIdentifierOther, + CYIdentifierMagic, }; +typedef std::set CYCStringSet; typedef std::set CYIdentifierValueSet; -typedef std::set CYIdentifierAddressSet; typedef std::map CYIdentifierAddressFlagsMap; +struct CYIdentifierUsage { + CYIdentifier *identifier_; + size_t usage_; +}; + +typedef std::vector CYIdentifierUsageVector; + struct CYScope { CYScope *parent_; - CYIdentifierValueSet identifiers_; + CYIdentifierAddressFlagsMap internal_; - unsigned offset_; + CYIdentifierValueSet identifiers_; CYScope() : - parent_(NULL), - offset_(0) + parent_(NULL) { } - void Add(CYContext &context, CYIdentifierAddressSet &external); + virtual ~CYScope() { + } + + void Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags); + virtual CYIdentifier *Lookup(CYContext &context, CYIdentifier *identifier); + void Merge(CYContext &context, CYIdentifier *identifier); void Scope(CYContext &context, CYStatement *&statements); }; struct CYProgram : - CYScope, CYThing { CYStatement *statements_; @@ -337,20 +354,46 @@ struct CYProgram : } virtual void Replace(CYContext &context); - virtual void Output(CYOutput &out) const; }; +struct CYContext { + apr_pool_t *pool_; + CYOptions &options_; + CYScope *scope_; + CYIdentifierUsageVector rename_; + + CYContext(apr_pool_t *pool, CYOptions &options) : + pool_(pool), + options_(options), + scope_(NULL) + { + } + + virtual ~CYContext() { + } + + template + void Replace(Type_ *&value) { + for (;;) if (value == NULL) + break; + else { + Type_ *replace(value->Replace(*this)); + if (replace != value) + value = replace; + else break; + } + } +}; + struct CYBlock : CYStatement, CYThing { CYStatement *statements_; - CYScope *scope_; - CYBlock(CYStatement *statements, CYScope *scope = NULL) : - statements_(statements), - scope_(scope) + CYBlock(CYStatement *statements) : + statements_(statements) { } @@ -1323,9 +1366,7 @@ struct CYWhile : virtual void Output(CYOutput &out, CYFlags flags) const; }; -struct CYFunction : - CYScope -{ +struct CYFunction { CYIdentifier *name_; CYFunctionParameter *parameters_; CYBlock code_; @@ -1333,14 +1374,15 @@ struct CYFunction : CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) : name_(name), parameters_(parameters), - code_(statements, this) + code_(statements) { } virtual ~CYFunction() { } - virtual void Replace_(CYContext &context); + void Inject(CYContext &context); + virtual void Replace_(CYContext &context, bool outer); virtual void Output(CYOutput &out, CYFlags flags) const; };