[yast-commit] r39955 - in /branches/tmp/mvidner/ui-as-namespace/core/libyui/src: Y2UINamespace.cc Y2UINamespace.h

Author: mvidner Date: Fri Aug 3 18:57:25 2007 New Revision: 39955 URL: http://svn.opensuse.org/viewcvs/yast?rev=39955&view=rev Log: Implemented resolution of overloaded functions Modified: branches/tmp/mvidner/ui-as-namespace/core/libyui/src/Y2UINamespace.cc branches/tmp/mvidner/ui-as-namespace/core/libyui/src/Y2UINamespace.h Modified: branches/tmp/mvidner/ui-as-namespace/core/libyui/src/Y2UINamespace.cc URL: http://svn.opensuse.org/viewcvs/yast/branches/tmp/mvidner/ui-as-namespace/co... ============================================================================== --- branches/tmp/mvidner/ui-as-namespace/core/libyui/src/Y2UINamespace.cc (original) +++ branches/tmp/mvidner/ui-as-namespace/core/libyui/src/Y2UINamespace.cc Fri Aug 3 18:57:25 2007 @@ -646,6 +646,7 @@ constTypePtr Y2UIFunction::wantedParameterType () const { // we do not help them with type conversion + // hey but we could, at least for non-overloaded functions return Type::Any; // FIXME make this the default behavior in parent class } @@ -678,11 +679,15 @@ bool Y2UIFunction::finishParameters () { + // Aha, perl could give us any garbage. + // Hmm, how do we cope with giving garbage to ycp modules? + // - it heeds wantedParameterType + // Funny, why is this function not called? + y2internal ("FIXME finishParameters not implemented"); return true; } - YCPValue Y2UIFunction::evaluateCall () { if ( ! m_comp->ui () ) @@ -741,7 +746,79 @@ string Y2UIFunction::name() const { - return("Y2UI"); + // FIXME. when is this called at all? + return("Y2UI"); +} + +// error reporting helper +static +void no_match (const char * name, constTypePtr type, + vector<SymbolEntryPtr>::iterator b, + vector<SymbolEntryPtr>::iterator e) +{ + y2error ("Function %s called with wrong type %s", + name, type->toString().c_str()); + y2error ("TODO: display the candidates"); // use b and e +} + + +Y2UIOverloadedFunction::Y2UIOverloadedFunction ( + Y2UINamespace* instance, YUIComponent* comp, + unsigned pos_offset, + vector<SymbolEntryPtr>::iterator candidates_b, + vector<SymbolEntryPtr>::iterator candidates_e, + bool play_macro_blocks) : + Y2UIFunction (instance, comp, pos_offset, play_macro_blocks), + m_candidates_b (candidates_b), + m_candidates_e (candidates_e) +{ + // still to do: m_position is just the first candidate +} + +bool Y2UIOverloadedFunction::finishParameters () +{ + FunctionTypePtr real_tp = new FunctionType (Type::Unspec); //return type + if (!m_param1.isNull()) { + real_tp->concat (Type::vt2type (m_param1->valuetype ())); + if (!m_param2.isNull()) { + real_tp->concat (Type::vt2type (m_param2->valuetype ())); + if (!m_param3.isNull()) { + real_tp->concat (Type::vt2type (m_param3->valuetype ())); + if (!m_param4.isNull()) { + real_tp->concat (Type::vt2type (m_param4->valuetype ())); + + if (!m_param5.isNull()) { + real_tp->concat (Type::vt2type (m_param5->valuetype ())); + } + } + } + } + } + y2debug ("Actual type: %s", real_tp->toString().c_str()); + + vector<SymbolEntryPtr>::iterator + b = m_candidates_b, + e = m_candidates_e, + it; + + for (it = b; it != e; ++it) { + SymbolEntryPtr se_p = *it; + constFunctionTypePtr cand_type = se_p->type(); + int m = real_tp->match (cand_type); + y2debug ("Candidate: %s MATCH: %d", + se_p->toString().c_str(), m); + if (m == 0) + break; + } + + if (it == e) { + // any candidate has a good name. report actual used signature. + no_match ((*b)->name(), real_tp, b, e); + return false; + } + // found + m_position += (it - b); + return true; } /** @@ -761,33 +838,63 @@ Y2Function* Y2UINamespace::createFunctionCall (const string name, constFunctionTypePtr type) { - symbols_t::iterator it; - y2debug ("Creating function call for %s", name.c_str ()); + y2debug ("whose type is %s", type? type->toString().c_str() : "unknown"); - for ( it = m_symbols.begin (); it != m_symbols.end (); it++) + // overloading: functions with same name must be adjacent in m_symbols + + symbols_t::iterator + b = m_symbols.begin (), + e = m_symbols.end (), + it = b; + SymbolEntryPtr p; + + // 1: find start of range of overloaded functions + for ( ; it != e; it++) { - SymbolEntryPtr p = (*it); - if ( p->name () == name ) // FIXME overload && p->type () == type ) - { - y2debug ("Found symbol '%s' (%s) %d" - , p->name(), p->type()->toString ().c_str (), p->position ()); + p = (*it); + if ( p->name () == name ) break; - } } - if (it == m_symbols.end ()) + if (it == e) { y2error ("No such function %s", name.c_str ()); return NULL; } bool play_macro_blocks = (name == "UserInput" || name == "TimeoutUserInput" || name == "WaitForEvent"); + + symbols_t::iterator rb = it; + // 2: find end of range of overloaded functions or the exact match + for ( ; it != e; it++) + { + p = (*it); + if ( p->name () != name ) + break; + // type comparison, ugh, use match + if (type && p->type () == type) + { + // found exact match + // BTW, p->position() may change as we go, don't get confused. + // What matters is our own counting. + y2debug ("Found symbol '%s' @%d", p->toString ().c_str (), p->position ()); + return new Y2UIFunction (this, m_comp, it - b, play_macro_blocks); + } + } + symbols_t::iterator re = it; + // [rb, re) have matching name + + if (type) + { + // we have exhausted the candidates without finding a match + y2error ("Incompatible yast2-core?"); + no_match (name.c_str (), type, rb, re); + return NULL; + } - // FIXME: - // here we assume the linear adding of symbols to m_symbols - // this holds so far, but might fall apart easily - return new Y2UIFunction (this, m_comp, it- m_symbols.begin (), play_macro_blocks); + y2debug ("overloaded %s, %u@%u", name.c_str(), re - rb, rb - b); + return new Y2UIOverloadedFunction (this, m_comp, rb - b, rb, re, play_macro_blocks); } void Y2UINamespace::registerFunctions() Modified: branches/tmp/mvidner/ui-as-namespace/core/libyui/src/Y2UINamespace.h URL: http://svn.opensuse.org/viewcvs/yast/branches/tmp/mvidner/ui-as-namespace/co... ============================================================================== --- branches/tmp/mvidner/ui-as-namespace/core/libyui/src/Y2UINamespace.h (original) +++ branches/tmp/mvidner/ui-as-namespace/core/libyui/src/Y2UINamespace.h Fri Aug 3 18:57:25 2007 @@ -36,8 +36,13 @@ class Y2UINamespace; +/** + * A call of a UI:: function + */ class Y2UIFunction: public Y2Function { + protected: + unsigned int m_position; Y2UINamespace* m_instance; YUIComponent* m_comp; @@ -61,6 +66,32 @@ string name () const; }; +/** + * A call of a UI:: function where we don't know the signature beforehand + */ +class Y2UIOverloadedFunction: public Y2UIFunction +{ + /** A range of SymbolEntries with a matching name. + * We will eventually find the right one + */ + vector<SymbolEntryPtr>::iterator m_candidates_b; + vector<SymbolEntryPtr>::iterator m_candidates_e; + + public: + Y2UIOverloadedFunction ( + Y2UINamespace* instance, YUIComponent* comp, + unsigned pos_offset, + vector<SymbolEntryPtr>::iterator candidates_b, + vector<SymbolEntryPtr>::iterator candidates_e, + bool play_macro_blocks); + + // attachParameter, appendParameter, reset from parent: fine + // wantedParameterType from parent: kind of fine + + //! the main job + bool finishParameters (); +}; + class Y2UINamespace: public Y2Namespace { private: -- To unsubscribe, e-mail: yast-commit+unsubscribe@opensuse.org For additional commands, e-mail: yast-commit+help@opensuse.org
participants (1)
-
mvidner@svn.opensuse.org