XPath2Expression.cs 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391
  1. //
  2. // XPath2Expression.cs - abstract syntax tree for XPath 2.0
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. //
  8. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_0
  30. using System;
  31. using System.Collections;
  32. using System.Globalization;
  33. using System.Xml;
  34. using System.Xml.Query;
  35. using System.Xml.Schema;
  36. using System.Xml.XPath;
  37. using Mono.Xml.XQuery;
  38. namespace Mono.Xml.XPath2
  39. {
  40. public class ExprSequence : CollectionBase
  41. {
  42. public ExprSequence ()
  43. {
  44. }
  45. public void Add (ExprSingle expr)
  46. {
  47. List.Add (expr);
  48. }
  49. public void AddRange (ICollection items)
  50. {
  51. if (items != null)
  52. foreach (ExprSingle e in items)
  53. List.Add (e);
  54. }
  55. public void Insert (int pos, ExprSingle expr)
  56. {
  57. List.Insert (pos, expr);
  58. }
  59. public ExprSingle this [int i] {
  60. get { return List [i] as ExprSingle; }
  61. set { List [i] = value; }
  62. }
  63. internal void CheckReference (XQueryASTCompiler compiler)
  64. {
  65. foreach (ExprSingle expr in List)
  66. expr.CheckReference (compiler);
  67. }
  68. }
  69. public abstract class ExprSingle
  70. {
  71. internal abstract void CheckReference (XQueryASTCompiler compiler);
  72. #region CompileAndEvaluate
  73. internal static readonly XPathAtomicValue AtomicTrue = new XPathAtomicValue (true, XmlSchemaSimpleType.XsBoolean);
  74. internal static readonly XPathAtomicValue AtomicFalse = new XPathAtomicValue (false, XmlSchemaSimpleType.XsBoolean);
  75. XQueryStaticContext ctx;
  76. internal ExprSingle Compile (XQueryASTCompiler compiler)
  77. {
  78. this.ctx = ctx;
  79. return CompileCore (compiler);
  80. }
  81. // If internal&&protected is available in C#, it is the best signature.
  82. internal abstract ExprSingle CompileCore (XQueryASTCompiler compiler);
  83. internal XQueryStaticContext Context {
  84. get { return ctx; }
  85. }
  86. public abstract SequenceType StaticType { get; }
  87. /** <summary>
  88. This is the core part of ExprSingle. It is
  89. generally used to evaluate expression and returns
  90. XPathItem sequence (iterator). The result is unordered
  91. */
  92. public abstract XPathSequence Evaluate (XPathSequence iter);
  93. public virtual XPathSequence EvaluateOrdered (XPathSequence iter)
  94. {
  95. if (RequireSorting) {
  96. ArrayList al = new ArrayList ();
  97. foreach (XPathItem item in Evaluate (iter))
  98. al.Add (item);
  99. return new ListIterator (iter, al);
  100. }
  101. else
  102. return Evaluate (iter);
  103. }
  104. public virtual void Serialize (XPathSequence iter)
  105. {
  106. XmlWriter w = iter.Context.Writer;
  107. XPathSequence result = Evaluate (iter);
  108. bool initial = true;
  109. foreach (XPathItem item in result) {
  110. if (initial)
  111. initial = false;
  112. else
  113. w.WriteWhitespace (" ");
  114. WriteXPathItem (item, w);
  115. }
  116. }
  117. private void WriteXPathItem (XPathItem item, XmlWriter w)
  118. {
  119. if (item.IsNode) {
  120. XPathNavigator nav = item as XPathNavigator;
  121. if (w.WriteState != WriteState.Start && nav.NodeType == XPathNodeType.Root)
  122. throw new XmlQueryException ("Current output can not accept root node.");
  123. nav.WriteSubtree (w);
  124. } else
  125. w.WriteValue (item.TypedValue);
  126. }
  127. // get EBV (fn:boolean())
  128. public virtual bool EvaluateAsBoolean (XPathSequence iter)
  129. {
  130. XPathSequence result = Evaluate (iter);
  131. if (!result.MoveNext ())
  132. return false;
  133. XPathItem v = result.Current;
  134. if (v is XPathNavigator)
  135. return true;
  136. if (result.MoveNext ())
  137. return true;
  138. switch (v.XmlType.TypeCode) {
  139. case XmlTypeCode.Boolean:
  140. return v.ValueAsBoolean;
  141. case XmlTypeCode.String:
  142. case XmlTypeCode.UntypedAtomic:
  143. return v.Value != String.Empty;
  144. case XmlTypeCode.Float:
  145. return v.ValueAsSingle != Single.NaN && v.ValueAsSingle != 0.0;
  146. case XmlTypeCode.Double:
  147. return v.ValueAsDouble != Double.NaN && v.ValueAsSingle != 0.0;
  148. case XmlTypeCode.Decimal:
  149. return v.ValueAsDecimal != 0;
  150. case XmlTypeCode.Integer:
  151. case XmlTypeCode.NonPositiveInteger:
  152. case XmlTypeCode.NegativeInteger:
  153. case XmlTypeCode.Long:
  154. case XmlTypeCode.Int:
  155. case XmlTypeCode.Short:
  156. case XmlTypeCode.Byte:
  157. case XmlTypeCode.UnsignedInt:
  158. case XmlTypeCode.UnsignedShort:
  159. case XmlTypeCode.UnsignedByte:
  160. return v.ValueAsInt64 != 0;
  161. case XmlTypeCode.NonNegativeInteger:
  162. case XmlTypeCode.UnsignedLong:
  163. case XmlTypeCode.PositiveInteger:
  164. return (ulong) (v.ValueAs (typeof (ulong))) != 0;
  165. }
  166. // otherwise, return true
  167. return true;
  168. }
  169. public virtual int EvaluateAsInt (XPathSequence iter)
  170. {
  171. XPathAtomicValue v = Atomize (Evaluate (iter));
  172. return v != null ? v.ValueAsInt32 : 0;
  173. }
  174. public virtual string EvaluateAsString (XPathSequence iter)
  175. {
  176. XPathAtomicValue v = Atomize (Evaluate (iter));
  177. return v != null ? v.Value : String.Empty;
  178. }
  179. public static XPathAtomicValue Atomize (XPathItem item)
  180. {
  181. XPathNavigator nav = item as XPathNavigator;
  182. if (nav != null) {
  183. if (nav.SchemaInfo != null)
  184. return new XPathAtomicValue (nav.TypedValue, nav.SchemaInfo.SchemaType);
  185. else
  186. return new XPathAtomicValue (nav.Value, XmlSchemaSimpleType.XsString);
  187. }
  188. else
  189. return (XPathAtomicValue) item;
  190. }
  191. // FIXME: What if iter contains list value?
  192. public static XPathAtomicValue Atomize (XPathSequence iter)
  193. {
  194. if (!iter.MoveNext ())
  195. return null;
  196. XPathNavigator nav = iter.Current as XPathNavigator;
  197. if (nav != null)
  198. return new XPathAtomicValue (nav.TypedValue, nav.SchemaInfo.SchemaType);
  199. else
  200. return (XPathAtomicValue) iter.Current;
  201. }
  202. public virtual XPathAtomicValue EvaluateAsAtomic (XPathSequence iter)
  203. {
  204. return Atomize (Evaluate (iter));
  205. }
  206. public virtual bool RequireSorting {
  207. get { return false; }
  208. }
  209. #endregion
  210. }
  211. // FLWORExpr
  212. internal class FLWORExpr : ExprSingle
  213. {
  214. public FLWORExpr (ForLetClauseCollection forlet, ExprSequence whereClause, OrderSpecList orderBy, ExprSingle ret)
  215. {
  216. this.fl = forlet;
  217. if (whereClause != null)
  218. this.whereClause = new ParenthesizedExpr (whereClause);
  219. this.orderBy = orderBy;
  220. this.ret = ret;
  221. }
  222. ForLetClauseCollection fl;
  223. ExprSingle whereClause;
  224. OrderSpecList orderBy;
  225. ExprSingle ret;
  226. public ForLetClauseCollection ForLetClauses {
  227. get { return fl; }
  228. }
  229. public ExprSingle WhereClause {
  230. get { return whereClause; }
  231. }
  232. public OrderSpecList OrderBy {
  233. get { return orderBy; }
  234. }
  235. public ExprSingle ReturnExpr {
  236. get { return ret; }
  237. set { ret = value; }
  238. }
  239. // ExprSingle Overrides
  240. internal override void CheckReference (XQueryASTCompiler compiler)
  241. {
  242. foreach (ForLetClause flc in fl)
  243. foreach (ForLetSingleBody single in flc)
  244. single.CheckReference (compiler);
  245. if (whereClause != null)
  246. whereClause.CheckReference (compiler);
  247. if (orderBy != null)
  248. foreach (OrderSpec os in orderBy)
  249. os.Expression.CheckReference (compiler);
  250. ret.CheckReference (compiler);
  251. }
  252. #region CompileAndEvaluate
  253. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  254. {
  255. foreach (ForLetClause flc in ForLetClauses) {
  256. foreach (ForLetSingleBody flsb in flc) {
  257. flsb.Expression = flsb.Expression.Compile (compiler);
  258. if (flsb.ReturnType != null)
  259. compiler.CheckType (flsb.Expression, flsb.ReturnType);
  260. }
  261. }
  262. if (WhereClause != null)
  263. whereClause = whereClause.Compile (compiler);
  264. if (OrderBy != null)
  265. foreach (OrderSpec os in OrderBy)
  266. os.Expression = os.Expression.Compile (compiler);
  267. ReturnExpr = ReturnExpr.Compile (compiler);
  268. return this;
  269. }
  270. public override SequenceType StaticType {
  271. get { return ReturnExpr.StaticType; }
  272. }
  273. public override XPathSequence Evaluate (XPathSequence iter)
  274. {
  275. return new FLWORIterator (iter, this);
  276. }
  277. #endregion
  278. }
  279. internal class ForLetClauseCollection : CollectionBase
  280. {
  281. public void Add (ForLetClause clause)
  282. {
  283. List.Add (clause);
  284. }
  285. public void Insert (int pos, ForLetClause clause)
  286. {
  287. List.Insert (pos, clause);
  288. }
  289. public ForLetClause this [int i] {
  290. get { return (ForLetClause) List [i]; }
  291. }
  292. }
  293. internal class OrderSpecList : CollectionBase
  294. {
  295. bool isStable;
  296. public OrderSpecList ()
  297. {
  298. }
  299. public bool IsStable {
  300. get { return isStable; }
  301. set { isStable = value; }
  302. }
  303. public void Insert (int pos, OrderSpec os)
  304. {
  305. List.Insert (pos, os);
  306. }
  307. public void Add (OrderSpec spec)
  308. {
  309. List.Add (spec);
  310. }
  311. public OrderSpec this [int i] {
  312. get { return (OrderSpec) List [i]; }
  313. }
  314. }
  315. internal class OrderSpec
  316. {
  317. public OrderSpec (ExprSingle expr, OrderModifier modifier)
  318. {
  319. this.expr = expr;
  320. this.mod = modifier;
  321. }
  322. ExprSingle expr;
  323. OrderModifier mod;
  324. public ExprSingle Expression {
  325. get {return expr; }
  326. set { expr = value; }
  327. }
  328. public OrderModifier Modifier {
  329. get { return mod; }
  330. set { mod = value; }
  331. }
  332. }
  333. internal class OrderModifier
  334. {
  335. public OrderModifier (XmlSortOrder order, XmlSortOrder emptyOrder, string collation)
  336. {
  337. this.sortOrder = sortOrder;
  338. this.emptyOrder = emptyOrder;
  339. if (collation != null)
  340. this.coll = new CultureInfo (collation);
  341. }
  342. XmlSortOrder sortOrder;
  343. XmlSortOrder emptyOrder;
  344. CultureInfo coll;
  345. public XmlSortOrder SortOrder {
  346. get { return sortOrder; }
  347. }
  348. public XmlSortOrder EmptyOrder {
  349. get { return emptyOrder; }
  350. }
  351. public CultureInfo Collation {
  352. get { return coll; }
  353. }
  354. }
  355. internal class ForLetClause : CollectionBase
  356. {
  357. public ForLetSingleBody this [int i] {
  358. get { return (ForLetSingleBody) List [i]; }
  359. }
  360. }
  361. internal class ForClause : ForLetClause
  362. {
  363. public ForClause ()
  364. {
  365. }
  366. public void Insert (int pos, ForSingleBody body)
  367. {
  368. List.Insert (pos, body);
  369. }
  370. public void Add (ForSingleBody body)
  371. {
  372. List.Add (body);
  373. }
  374. }
  375. internal class LetClause : ForLetClause
  376. {
  377. public LetClause ()
  378. {
  379. }
  380. public void Insert (int pos, LetSingleBody body)
  381. {
  382. List.Insert (pos, body);
  383. }
  384. public void Add (LetSingleBody body)
  385. {
  386. List.Add (body);
  387. }
  388. }
  389. internal abstract class ForLetSingleBody
  390. {
  391. XmlQualifiedName varName;
  392. SequenceType type;
  393. ExprSingle expr;
  394. public ForLetSingleBody (XmlQualifiedName varName, SequenceType type, ExprSingle expr)
  395. {
  396. this.varName = varName;
  397. this.type = type;
  398. this.expr = expr;
  399. }
  400. public XmlQualifiedName VarName {
  401. get { return varName; }
  402. }
  403. public SequenceType ReturnType {
  404. get { return type; }
  405. }
  406. public ExprSingle Expression {
  407. get { return expr; }
  408. set { expr = value; }
  409. }
  410. internal void CheckReference (XQueryASTCompiler compiler)
  411. {
  412. if (type != null)
  413. compiler.CheckSchemaType (type);
  414. expr.CheckReference (compiler);
  415. }
  416. }
  417. internal class ForSingleBody : ForLetSingleBody
  418. {
  419. public ForSingleBody (XmlQualifiedName varName, SequenceType type, XmlQualifiedName positionalVar, ExprSingle expr)
  420. : base (varName, type, expr)
  421. {
  422. this.positionalVar = positionalVar;
  423. }
  424. XmlQualifiedName positionalVar;
  425. public XmlQualifiedName PositionalVar {
  426. get { return positionalVar; }
  427. }
  428. }
  429. internal class LetSingleBody : ForLetSingleBody
  430. {
  431. public LetSingleBody (XmlQualifiedName varName, SequenceType type, ExprSingle expr)
  432. : base (varName, type, expr)
  433. {
  434. }
  435. }
  436. // QuantifiedExpr
  437. internal class QuantifiedExpr : ExprSingle
  438. {
  439. QuantifiedExprBodyList body;
  440. ExprSingle satisfies;
  441. bool every;
  442. public QuantifiedExpr (bool every, QuantifiedExprBodyList body, ExprSingle satisfies)
  443. {
  444. this.every = every;
  445. this.body = body;
  446. this.satisfies = satisfies;
  447. }
  448. public bool Every {
  449. get { return every; }
  450. }
  451. public QuantifiedExprBodyList BodyList {
  452. get { return body; }
  453. }
  454. public ExprSingle Satisfies {
  455. get { return satisfies; }
  456. set { satisfies = value; }
  457. }
  458. internal override void CheckReference (XQueryASTCompiler compiler)
  459. {
  460. foreach (QuantifiedExprBody one in body) {
  461. if (one.Type != null)
  462. compiler.CheckSchemaType (one.Type);
  463. one.Expression.CheckReference (compiler);
  464. }
  465. Satisfies.CheckReference (compiler);
  466. }
  467. #region CompileAndEvaluate
  468. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  469. {
  470. Satisfies = Satisfies.Compile (compiler);
  471. for (int i = 0; i < BodyList.Count; i++) {
  472. BodyList [i].Expression = BodyList [i].Expression.Compile (compiler);
  473. if (BodyList [i].Type != null)
  474. compiler.CheckType (BodyList [i].Expression, BodyList [i].Type);
  475. }
  476. return this;
  477. }
  478. public override SequenceType StaticType {
  479. get { return SequenceType.Boolean; }
  480. }
  481. public override XPathSequence Evaluate (XPathSequence iter)
  482. {
  483. return new SingleItemIterator (EvaluateAsBoolean (iter) ? AtomicTrue : AtomicFalse, iter.Context);
  484. }
  485. public override bool EvaluateAsBoolean (XPathSequence iter)
  486. {
  487. return EvaluateQuantification (iter, BodyList.GetEnumerator ());
  488. }
  489. private bool EvaluateQuantification (XPathSequence iter, IEnumerator bodies)
  490. {
  491. if (bodies.MoveNext ()) {
  492. QuantifiedExprBody qb = bodies.Current as QuantifiedExprBody;
  493. XPathSequence seq = qb.Expression.Evaluate (iter);
  494. bool passed = false;
  495. foreach (XPathItem item in seq) {
  496. passed = true;
  497. // FIXME: consider qb.Type
  498. try {
  499. iter.Context.PushVariable (qb.VarName, item);
  500. if (EvaluateQuantification (iter, bodies)) {
  501. if (!Every)
  502. return true;
  503. }
  504. else if (Every)
  505. return false;
  506. } finally {
  507. iter.Context.PopVariable ();
  508. }
  509. }
  510. return passed;
  511. }
  512. return Satisfies.EvaluateAsBoolean (iter);
  513. }
  514. #endregion
  515. }
  516. internal class QuantifiedExprBodyList : CollectionBase
  517. {
  518. public QuantifiedExprBodyList ()
  519. {
  520. }
  521. public void Add (QuantifiedExprBody body)
  522. {
  523. List.Add (body);
  524. }
  525. public void Insert (int pos, QuantifiedExprBody body)
  526. {
  527. List.Insert (pos, body);
  528. }
  529. public QuantifiedExprBody this [int i] {
  530. get { return (QuantifiedExprBody) List [i]; }
  531. }
  532. }
  533. internal class QuantifiedExprBody
  534. {
  535. private XmlQualifiedName varName;
  536. private SequenceType type;
  537. private ExprSingle expr;
  538. public QuantifiedExprBody (XmlQualifiedName varName,
  539. SequenceType type, ExprSingle expr)
  540. {
  541. this.varName = varName;
  542. this.type = type ;
  543. this.expr = expr;
  544. }
  545. public XmlQualifiedName VarName {
  546. get { return varName; }
  547. }
  548. public SequenceType Type {
  549. get { return type; }
  550. }
  551. public ExprSingle Expression {
  552. get { return expr; }
  553. set { expr = value; }
  554. }
  555. }
  556. // TypeswitchExpr
  557. internal class TypeswitchExpr : ExprSingle
  558. {
  559. ExprSequence switchExpr;
  560. CaseClauseList caseList;
  561. XmlQualifiedName defaultVarName;
  562. ExprSingle defaultReturn;
  563. public TypeswitchExpr (ExprSequence switchExpr, CaseClauseList caseList, XmlQualifiedName defaultVarName, ExprSingle defaultReturn)
  564. {
  565. this.switchExpr = switchExpr;
  566. this.caseList = caseList;
  567. this.defaultVarName = defaultVarName;
  568. this.defaultReturn = defaultReturn;
  569. }
  570. public ExprSequence SwitchExpr {
  571. get { return switchExpr; }
  572. }
  573. public CaseClauseList Cases {
  574. get { return caseList; }
  575. }
  576. public XmlQualifiedName DefaultVarName {
  577. get { return defaultVarName; }
  578. }
  579. public ExprSingle DefaultReturn {
  580. get { return defaultReturn; }
  581. set { defaultReturn = value; }
  582. }
  583. internal override void CheckReference (XQueryASTCompiler compiler)
  584. {
  585. switchExpr.CheckReference (compiler);
  586. foreach (CaseClause cc in caseList) {
  587. compiler.CheckSchemaType (cc.Type);
  588. cc.Expr.CheckReference (compiler);
  589. }
  590. defaultReturn.CheckReference (compiler);
  591. }
  592. #region CompileAndEvaluate
  593. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  594. {
  595. for (int i = 0; i < SwitchExpr.Count; i++)
  596. SwitchExpr [i] = SwitchExpr [i].Compile (compiler);
  597. foreach (CaseClause cc in Cases)
  598. cc.Expr = cc.Expr.Compile (compiler);
  599. DefaultReturn = DefaultReturn.Compile (compiler);
  600. return this;
  601. }
  602. // FIXME: it can be optimized by checking all case clauses.
  603. public override SequenceType StaticType {
  604. get { return SequenceType.AnyType; }
  605. }
  606. public override XPathSequence Evaluate (XPathSequence iter)
  607. {
  608. // FIXME: should move to iterator?
  609. XPathSequence cond = new ExprSequenceIterator (iter, SwitchExpr);
  610. XPathSequence ret = null;
  611. foreach (CaseClause ccc in Cases) {
  612. if (ccc.Type.Matches (cond)) {
  613. if (ccc.VarName != XmlQualifiedName.Empty)
  614. iter.Context.PushVariable (ccc.VarName, cond);
  615. ret = ccc.Expr.Evaluate (iter);
  616. // FIXME: The design should make sure that in-scope variables are held on actual iteration.
  617. if (ccc.VarName != XmlQualifiedName.Empty)
  618. iter.Context.PopVariable ();
  619. return ret;
  620. }
  621. }
  622. if (DefaultVarName != XmlQualifiedName.Empty)
  623. iter.Context.PushVariable (DefaultVarName, cond);
  624. ret = DefaultReturn.Evaluate (iter);
  625. if (DefaultVarName != XmlQualifiedName.Empty)
  626. iter.Context.PopVariable ();
  627. return ret;
  628. }
  629. #endregion
  630. }
  631. internal class CaseClauseList : CollectionBase
  632. {
  633. public void Insert (int pos, CaseClause cc)
  634. {
  635. List.Insert (pos, cc);
  636. }
  637. public void Add (CaseClause cc)
  638. {
  639. List.Add (cc);
  640. }
  641. public CaseClause this [int i] {
  642. get { return (CaseClause) List [i]; }
  643. }
  644. }
  645. internal class CaseClause
  646. {
  647. public CaseClause (SequenceType type, ExprSingle expr, XmlQualifiedName varName)
  648. {
  649. this.type = type;
  650. this.expr = expr;
  651. this.varName = varName;
  652. }
  653. SequenceType type;
  654. ExprSingle expr;
  655. XmlQualifiedName varName;
  656. public SequenceType Type {
  657. get { return type; }
  658. }
  659. public ExprSingle Expr {
  660. get { return expr; }
  661. set { expr = value; }
  662. }
  663. public XmlQualifiedName VarName {
  664. get { return varName; }
  665. set { varName = value; }
  666. }
  667. }
  668. // IfExpr
  669. internal class IfExpr : ExprSingle
  670. {
  671. public IfExpr (ExprSequence condition, ExprSingle trueExpr, ExprSingle falseExpr)
  672. {
  673. this.condition = new ParenthesizedExpr (condition);
  674. this.trueExpr = trueExpr;
  675. this.falseExpr = falseExpr;
  676. }
  677. ExprSingle condition;
  678. ExprSingle trueExpr;
  679. ExprSingle falseExpr;
  680. public ExprSingle Condition {
  681. get { return condition; }
  682. set { condition = value; }
  683. }
  684. public ExprSingle TrueExpr {
  685. get { return trueExpr; }
  686. set { trueExpr = value; }
  687. }
  688. public ExprSingle FalseExpr {
  689. get { return falseExpr; }
  690. set { falseExpr = value; }
  691. }
  692. internal override void CheckReference (XQueryASTCompiler compiler)
  693. {
  694. condition.CheckReference (compiler);
  695. trueExpr.CheckReference (compiler);
  696. falseExpr.CheckReference (compiler);
  697. }
  698. #region CompileAndEvaluate
  699. SequenceType computedReturnType;
  700. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  701. {
  702. condition = condition.Compile (compiler);
  703. // FIXME: check if condition is constant, and returns trueExpr or falseExpr
  704. TrueExpr = TrueExpr.Compile (compiler);
  705. FalseExpr = FalseExpr.Compile (compiler);
  706. return this;
  707. }
  708. public override SequenceType StaticType {
  709. get {
  710. if (Context == null)
  711. return SequenceType.AnyType;
  712. if (computedReturnType == null)
  713. computedReturnType = SequenceType.ComputeCommonBase (TrueExpr.StaticType, FalseExpr.StaticType);
  714. return computedReturnType;
  715. }
  716. }
  717. public override XPathSequence Evaluate (XPathSequence iter)
  718. {
  719. if (condition.EvaluateAsBoolean (iter))
  720. return TrueExpr.Evaluate (iter);
  721. return FalseExpr.Evaluate (iter);
  722. }
  723. #endregion
  724. }
  725. // logical expr
  726. internal abstract class BinaryOperationExpr : ExprSingle
  727. {
  728. protected BinaryOperationExpr (ExprSingle left, ExprSingle right)
  729. {
  730. this.left = left;
  731. this.right = right;
  732. }
  733. ExprSingle left, right;
  734. public ExprSingle Left {
  735. get { return left; }
  736. set { left = value; }
  737. }
  738. public ExprSingle Right{
  739. get { return right; }
  740. set { right = value; }
  741. }
  742. internal override void CheckReference (XQueryASTCompiler compiler)
  743. {
  744. left.CheckReference (compiler);
  745. right.CheckReference (compiler);
  746. }
  747. #region CompileAndEvaluate
  748. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  749. {
  750. Left = Left.Compile (compiler);
  751. Right = Right.Compile (compiler);
  752. return this;
  753. }
  754. #endregion
  755. }
  756. internal class OrExpr : BinaryOperationExpr
  757. {
  758. public OrExpr (ExprSingle left, ExprSingle right)
  759. : base (left, right)
  760. {
  761. }
  762. #region CompileAndEvaluate
  763. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  764. {
  765. base.CompileCore (compiler);
  766. // FIXME: check constant value and return true or false
  767. return this;
  768. }
  769. public override SequenceType StaticType {
  770. get { return SequenceType.Boolean; }
  771. }
  772. public override bool EvaluateAsBoolean (XPathSequence iter)
  773. {
  774. return Left.EvaluateAsBoolean (iter) || Right.EvaluateAsBoolean (iter);
  775. }
  776. public override XPathSequence Evaluate (XPathSequence iter)
  777. {
  778. return new SingleItemIterator (EvaluateAsBoolean (iter) ?AtomicTrue : AtomicFalse, iter.Context);
  779. }
  780. /*
  781. - compiler -
  782. return leftExprBool (context) || rightExprBool (context);
  783. */
  784. #endregion
  785. }
  786. internal class AndExpr : BinaryOperationExpr
  787. {
  788. public AndExpr (ExprSingle left, ExprSingle right)
  789. : base (left, right)
  790. {
  791. }
  792. #region CompileAndEvaluate
  793. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  794. {
  795. base.CompileCore (compiler);
  796. // FIXME: check constant value and return true or false
  797. return this;
  798. }
  799. public override SequenceType StaticType {
  800. get { return SequenceType.Boolean; }
  801. }
  802. public override bool EvaluateAsBoolean (XPathSequence iter)
  803. {
  804. return Left.EvaluateAsBoolean (iter) && Right.EvaluateAsBoolean (iter);
  805. }
  806. public override XPathSequence Evaluate (XPathSequence iter)
  807. {
  808. return new SingleItemIterator (EvaluateAsBoolean (iter) ? AtomicTrue : AtomicFalse, iter.Context);
  809. }
  810. /*
  811. - compiler -
  812. return leftExprBool (context) && rightExprBool (context);
  813. */
  814. #endregion
  815. }
  816. // TypeOperation expr
  817. internal abstract class TypeOperationExpr : ExprSingle
  818. {
  819. protected TypeOperationExpr (ExprSingle expr, SequenceType type)
  820. {
  821. this.expr = expr;
  822. this.type = type;
  823. }
  824. ExprSingle expr;
  825. SequenceType type;
  826. public ExprSingle Expr {
  827. get { return expr; }
  828. set { expr = value; }
  829. }
  830. public SequenceType TargetType {
  831. get { return type; }
  832. }
  833. internal override void CheckReference (XQueryASTCompiler compiler)
  834. {
  835. expr.CheckReference (compiler);
  836. compiler.CheckSchemaType (type);
  837. }
  838. }
  839. internal abstract class AtomicTypeOperationExpr : ExprSingle
  840. {
  841. protected AtomicTypeOperationExpr (ExprSingle expr, XmlTypeCode type, bool optional)
  842. {
  843. this.expr = expr;
  844. this.targetType = SequenceType.Create (type, optional ? Occurence.Optional : Occurence.One);
  845. }
  846. ExprSingle expr;
  847. SequenceType targetType;
  848. internal ExprSingle Expr {
  849. get { return expr; }
  850. set { expr = value; }
  851. }
  852. /*
  853. public XmlTypeCode TypeCode {
  854. get { return typeCode; }
  855. }
  856. public bool Optional {
  857. get { return optional; }
  858. }
  859. */
  860. internal SequenceType TargetType {
  861. get { return targetType; }
  862. }
  863. internal override void CheckReference (XQueryASTCompiler compiler)
  864. {
  865. expr.CheckReference (compiler);
  866. }
  867. }
  868. internal class InstanceOfExpr : TypeOperationExpr
  869. {
  870. public InstanceOfExpr (ExprSingle expr, SequenceType type)
  871. : base (expr, type)
  872. {
  873. }
  874. #region CompileAndEvaluate
  875. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  876. {
  877. Expr = Expr.Compile (compiler);
  878. // FIXME: check return type and if it never matches then return false
  879. return this;
  880. }
  881. public override SequenceType StaticType {
  882. get { return SequenceType.Boolean; }
  883. }
  884. public override bool EvaluateAsBoolean (XPathSequence iter)
  885. {
  886. bool occured = false;
  887. bool onlyOnce = (TargetType.Occurence == Occurence.One || TargetType.Occurence == Occurence.Optional);
  888. bool required = (TargetType.Occurence == Occurence.One || TargetType.Occurence == Occurence.OneOrMore);
  889. foreach (XPathItem item in iter) {
  890. if (occured && onlyOnce)
  891. return false;
  892. if (!TargetType.IsInstance (item))
  893. return false;
  894. }
  895. return occured || !required;
  896. }
  897. public override XPathSequence Evaluate (XPathSequence iter)
  898. {
  899. return new SingleItemIterator (EvaluateAsBoolean (iter) ? AtomicTrue : AtomicFalse, iter.Context);
  900. }
  901. #endregion
  902. }
  903. internal class TreatExpr : TypeOperationExpr
  904. {
  905. public TreatExpr (ExprSingle expr, SequenceType type)
  906. : base (expr, type)
  907. {
  908. }
  909. #region CompileAndEvaluate
  910. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  911. {
  912. Expr = Expr.Compile (compiler);
  913. // FIXME: check return type and if it never matches then return false
  914. return this;
  915. }
  916. public override SequenceType StaticType {
  917. get { return SequenceType.AnyType; }
  918. }
  919. public override XPathSequence Evaluate (XPathSequence iter)
  920. {
  921. if (TargetType.CanConvert (iter))
  922. return iter;
  923. else
  924. throw new XmlQueryException (String.Format ("Cannot treat as {1}", TargetType));
  925. }
  926. #endregion
  927. }
  928. internal class CastableExpr : AtomicTypeOperationExpr
  929. {
  930. public CastableExpr (ExprSingle expr, XmlTypeCode atomicType, bool optional)
  931. : base (expr, atomicType, optional)
  932. {
  933. }
  934. #region CompileAndEvaluate
  935. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  936. {
  937. Expr = Expr.Compile (compiler);
  938. // FIXME: check return type and if it never matches then return boolean
  939. return this;
  940. }
  941. public override SequenceType StaticType {
  942. get { return SequenceType.Boolean; }
  943. }
  944. public override XPathSequence Evaluate (XPathSequence iter)
  945. {
  946. return new SingleItemIterator (new XPathAtomicValue (EvaluateAsBoolean (iter), XmlSchemaSimpleType.XsBoolean), iter.Context);
  947. }
  948. public override bool EvaluateAsBoolean (XPathSequence iter)
  949. {
  950. bool occured = false;
  951. bool onlyOnce = (TargetType.Occurence == Occurence.One || TargetType.Occurence == Occurence.Optional);
  952. bool required = (TargetType.Occurence == Occurence.One || TargetType.Occurence == Occurence.OneOrMore);
  953. foreach (XPathItem item in iter) {
  954. if (occured && onlyOnce)
  955. return false;
  956. if (!TargetType.CanConvert (item))
  957. return false;
  958. }
  959. return occured || !required;
  960. }
  961. #endregion
  962. }
  963. internal class CastExpr : AtomicTypeOperationExpr
  964. {
  965. public CastExpr (ExprSingle expr, XmlTypeCode atomicType, bool optional)
  966. : base (expr, atomicType, optional)
  967. {
  968. }
  969. #region CompileAndEvaluate
  970. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  971. {
  972. Expr = Expr.Compile (compiler);
  973. // FIXME: check return type and if it never matches then return boolean
  974. return this;
  975. }
  976. public override SequenceType StaticType {
  977. get { return TargetType; }
  978. }
  979. public override XPathSequence Evaluate (XPathSequence iter)
  980. {
  981. if (TargetType.CanConvert (iter))
  982. return new ConvertingIterator (iter, TargetType);
  983. else
  984. throw new XmlQueryException (String.Format ("Cannot cast as {1}", TargetType));
  985. }
  986. #endregion
  987. }
  988. // ComparisonExpr
  989. internal class ComparisonExpr : BinaryOperationExpr
  990. {
  991. public ComparisonExpr (ExprSingle left, ExprSingle right, ComparisonOperator oper)
  992. : base (left, right)
  993. {
  994. this.oper = oper;
  995. }
  996. ComparisonOperator oper;
  997. public ComparisonOperator Operation {
  998. get { return oper; }
  999. }
  1000. #region CompileAndEvaluate
  1001. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  1002. {
  1003. Left = Left.Compile (compiler);
  1004. Right = Right.Compile (compiler);
  1005. // FIXME: check return type and if it never matches then return boolean
  1006. return this;
  1007. }
  1008. public override SequenceType StaticType {
  1009. get { return SequenceType.Boolean; }
  1010. }
  1011. public override XPathSequence Evaluate (XPathSequence iter)
  1012. {
  1013. bool isEmpty;
  1014. bool result = EvaluateAsBoolean (iter, out isEmpty);
  1015. if (isEmpty)
  1016. return new XPathEmptySequence (iter.Context);
  1017. return new SingleItemIterator (result ? AtomicTrue : AtomicFalse, iter.Context);
  1018. }
  1019. public override bool EvaluateAsBoolean (XPathSequence iter)
  1020. {
  1021. bool isEmpty;
  1022. return EvaluateAsBoolean (iter, out isEmpty);
  1023. }
  1024. private bool EvaluateAsBoolean (XPathSequence iter, out bool isEmpty)
  1025. {
  1026. XPathSequence lseq, rseq;
  1027. isEmpty = false;
  1028. switch (Operation) {
  1029. // FIXME: it is curious but currently gmcs requires full typename.
  1030. case Mono.Xml.XPath2.ComparisonOperator.ValueEQ:
  1031. case Mono.Xml.XPath2.ComparisonOperator.ValueNE:
  1032. case Mono.Xml.XPath2.ComparisonOperator.ValueLT:
  1033. case Mono.Xml.XPath2.ComparisonOperator.ValueLE:
  1034. case Mono.Xml.XPath2.ComparisonOperator.ValueGT:
  1035. case Mono.Xml.XPath2.ComparisonOperator.ValueGE:
  1036. XPathItem itemVL = ExamineOneItem (Left.Evaluate (iter));
  1037. XPathItem itemVR = ExamineOneItem (Right.Evaluate (iter));
  1038. if (itemVL == null || itemVR == null) {
  1039. isEmpty = true;
  1040. return false;
  1041. }
  1042. return CompareAtomic (itemVL, itemVR);
  1043. case Mono.Xml.XPath2.ComparisonOperator.GeneralEQ:
  1044. case Mono.Xml.XPath2.ComparisonOperator.GeneralNE:
  1045. case Mono.Xml.XPath2.ComparisonOperator.GeneralLT:
  1046. case Mono.Xml.XPath2.ComparisonOperator.GeneralLE:
  1047. case Mono.Xml.XPath2.ComparisonOperator.GeneralGT:
  1048. case Mono.Xml.XPath2.ComparisonOperator.GeneralGE:
  1049. lseq = Left.Evaluate (iter);
  1050. rseq = Right.Evaluate (iter);
  1051. foreach (XPathItem itemGL in lseq) {
  1052. foreach (XPathItem itemGR in rseq.Clone ()) {
  1053. if (CompareAtomic (itemGL, itemGR))
  1054. return true;
  1055. }
  1056. }
  1057. return false;
  1058. case Mono.Xml.XPath2.ComparisonOperator.NodeIs:
  1059. case Mono.Xml.XPath2.ComparisonOperator.NodeFWD:
  1060. case Mono.Xml.XPath2.ComparisonOperator.NodeBWD:
  1061. XPathNavigator lnav = ExamineOneNode (Left.Evaluate (iter));
  1062. XPathNavigator rnav = ExamineOneNode (Right.Evaluate (iter));
  1063. if (lnav == null || rnav == null) {
  1064. isEmpty = true;
  1065. return false;
  1066. }
  1067. switch (Operation) {
  1068. case Mono.Xml.XPath2.ComparisonOperator.NodeIs:
  1069. return lnav.IsSamePosition (rnav);
  1070. case Mono.Xml.XPath2.ComparisonOperator.NodeFWD:
  1071. return lnav.ComparePosition (rnav) == XmlNodeOrder.Before;
  1072. case Mono.Xml.XPath2.ComparisonOperator.NodeBWD:
  1073. return lnav.ComparePosition (rnav) == XmlNodeOrder.After;
  1074. }
  1075. break;
  1076. }
  1077. throw new SystemException ("XQuery internal error: should not happen.");
  1078. }
  1079. // returns null if sequence was empty
  1080. private XPathItem ExamineOneItem (XPathSequence seq)
  1081. {
  1082. if (!seq.MoveNext ())
  1083. return null;
  1084. XPathItem item = seq.Current;
  1085. if (seq.MoveNext ())
  1086. throw new XmlQueryException ("Operand of value comparison expression must be evaluated as a sequence that contains exactly one item.");
  1087. return item;
  1088. }
  1089. // returns null if sequence was empty
  1090. private XPathNavigator ExamineOneNode (XPathSequence seq)
  1091. {
  1092. if (!seq.MoveNext ())
  1093. return null;
  1094. XPathNavigator nav = seq.Current as XPathNavigator;
  1095. if (nav == null || seq.MoveNext ())
  1096. throw new XmlQueryException ("Operand of node comparison expression must be evaluated as a sequence that contains exactly one node.");
  1097. return nav;
  1098. }
  1099. private bool CompareAtomic (XPathItem itemL, XPathItem itemR)
  1100. {
  1101. switch (Operation) {
  1102. // FIXME: it is curious but currently gmcs requires full typename.
  1103. case Mono.Xml.XPath2.ComparisonOperator.ValueEQ:
  1104. case Mono.Xml.XPath2.ComparisonOperator.GeneralEQ:
  1105. return XQueryComparisonOperator.ValueEQ (Atomize (itemL), Atomize (itemR));
  1106. case Mono.Xml.XPath2.ComparisonOperator.ValueNE:
  1107. case Mono.Xml.XPath2.ComparisonOperator.GeneralNE:
  1108. return XQueryComparisonOperator.ValueNE (Atomize (itemL), Atomize (itemR));
  1109. case Mono.Xml.XPath2.ComparisonOperator.ValueLT:
  1110. case Mono.Xml.XPath2.ComparisonOperator.GeneralLT:
  1111. return XQueryComparisonOperator.ValueLT (Atomize (itemL), Atomize (itemR));
  1112. case Mono.Xml.XPath2.ComparisonOperator.ValueLE:
  1113. case Mono.Xml.XPath2.ComparisonOperator.GeneralLE:
  1114. return XQueryComparisonOperator.ValueLE (Atomize (itemL), Atomize (itemR));
  1115. case Mono.Xml.XPath2.ComparisonOperator.ValueGT:
  1116. case Mono.Xml.XPath2.ComparisonOperator.GeneralGT:
  1117. return XQueryComparisonOperator.ValueGT (Atomize (itemL), Atomize (itemR));
  1118. case Mono.Xml.XPath2.ComparisonOperator.ValueGE:
  1119. case Mono.Xml.XPath2.ComparisonOperator.GeneralGE:
  1120. return XQueryComparisonOperator.ValueGE (Atomize (itemL), Atomize (itemR));
  1121. }
  1122. return false; // should not happen
  1123. }
  1124. #endregion
  1125. }
  1126. public enum ComparisonOperator {
  1127. ValueEQ,
  1128. ValueNE,
  1129. ValueLT,
  1130. ValueLE,
  1131. ValueGT,
  1132. ValueGE,
  1133. GeneralEQ,
  1134. GeneralNE,
  1135. GeneralLT,
  1136. GeneralLE,
  1137. GeneralGT,
  1138. GeneralGE,
  1139. NodeIs,
  1140. NodeFWD,
  1141. NodeBWD
  1142. }
  1143. // Range
  1144. internal class RangeExpr : BinaryOperationExpr
  1145. {
  1146. public RangeExpr (ExprSingle left, ExprSingle right)
  1147. : base (left, right)
  1148. {
  1149. }
  1150. #region CompileAndEvaluate
  1151. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  1152. {
  1153. Left = Left.Compile (compiler);
  1154. Right = Right.Compile (compiler);
  1155. return this;
  1156. }
  1157. public override SequenceType StaticType {
  1158. get { return SequenceType.IntegerList; }
  1159. }
  1160. public override XPathSequence Evaluate (XPathSequence iter)
  1161. {
  1162. int start = Left.EvaluateAsInt (iter);
  1163. int end = Right.EvaluateAsInt (iter);
  1164. return new IntegerRangeIterator (iter.Context, start, end);
  1165. }
  1166. public override void Serialize (XPathSequence iter)
  1167. {
  1168. int start = Left.EvaluateAsInt (iter);
  1169. int end = Right.EvaluateAsInt (iter);
  1170. for (int i = start; i <= end; i++) {
  1171. iter.Context.Writer.WriteValue (i);
  1172. if (i < end)
  1173. iter.Context.Writer.WriteWhitespace (" ");
  1174. }
  1175. }
  1176. #endregion
  1177. }
  1178. // arithmetic operation expr
  1179. public enum ArithmeticOperator {
  1180. Add,
  1181. Sub,
  1182. Mul,
  1183. Div,
  1184. IDiv,
  1185. IMod
  1186. }
  1187. internal class ArithmeticOperationExpr : BinaryOperationExpr
  1188. {
  1189. public ArithmeticOperationExpr (ExprSingle left, ExprSingle right, ArithmeticOperator oper)
  1190. : base (left, right)
  1191. {
  1192. this.oper = oper;
  1193. }
  1194. ArithmeticOperator oper;
  1195. public ArithmeticOperator Operation {
  1196. get { return oper; }
  1197. }
  1198. #region CompileAndEvaluate
  1199. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  1200. {
  1201. Left = Left.Compile (compiler);
  1202. Right = Right.Compile (compiler);
  1203. return this;
  1204. }
  1205. // FIXME: It can be optimized by comparing l/r value types.
  1206. public override SequenceType StaticType {
  1207. get { return SequenceType.AnyType; }
  1208. }
  1209. public override XPathSequence Evaluate (XPathSequence iter)
  1210. {
  1211. XPathSequence lseq = Left.Evaluate (iter);
  1212. if (!lseq.MoveNext ())
  1213. return new XPathEmptySequence (iter.Context);
  1214. XPathSequence rseq = Right.Evaluate (iter);
  1215. if (!rseq.MoveNext ())
  1216. return new XPathEmptySequence (iter.Context);
  1217. XPathAtomicValue lvalue = Atomize (lseq.Current);
  1218. XPathAtomicValue rvalue = Atomize (rseq.Current);
  1219. if (lseq.MoveNext ())
  1220. throw new XmlQueryException ("XP0006: Left operand resulted in an sequence that contains more than one item.");
  1221. if (rseq.MoveNext ())
  1222. throw new XmlQueryException ("XP0006: Left operand resulted in an sequence that contains more than one item.");
  1223. // FIXME: handle "untypedAtomic to xs:double" casting
  1224. return new SingleItemIterator (Compute (lvalue, rvalue), iter.Context);
  1225. }
  1226. private XPathAtomicValue Compute (XPathAtomicValue lvalue, XPathAtomicValue rvalue)
  1227. {
  1228. switch (Operation) {
  1229. case ArithmeticOperator.Add:
  1230. return XQueryArithmeticOperator.Add (lvalue, rvalue);
  1231. case ArithmeticOperator.Sub:
  1232. return XQueryArithmeticOperator.Subtract (lvalue, rvalue);
  1233. case ArithmeticOperator.Mul:
  1234. return XQueryArithmeticOperator.Multiply (lvalue, rvalue);
  1235. case ArithmeticOperator.Div:
  1236. return XQueryArithmeticOperator.Divide (lvalue, rvalue);
  1237. case ArithmeticOperator.IDiv:
  1238. return XQueryArithmeticOperator.IntDivide (lvalue, rvalue);
  1239. case ArithmeticOperator.IMod:
  1240. return XQueryArithmeticOperator.Remainder (lvalue, rvalue);
  1241. default:
  1242. throw new SystemException ("XQuery internal error: should not happen.");
  1243. }
  1244. }
  1245. #endregion
  1246. }
  1247. internal class MinusExpr : ExprSingle
  1248. {
  1249. public MinusExpr (ExprSingle expr)
  1250. {
  1251. this.expr = expr;
  1252. }
  1253. ExprSingle expr;
  1254. public ExprSingle Expr {
  1255. get { return expr; }
  1256. set { expr = value; }
  1257. }
  1258. internal override void CheckReference (XQueryASTCompiler compiler)
  1259. {
  1260. expr.CheckReference (compiler);
  1261. }
  1262. #region CompileAndEvaluate
  1263. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  1264. {
  1265. return new ArithmeticOperationExpr (new DecimalLiteralExpr (-1), Expr, ArithmeticOperator.Mul).Compile (compiler);
  1266. }
  1267. public override SequenceType StaticType {
  1268. get { return Expr.StaticType; }
  1269. }
  1270. public override XPathSequence Evaluate (XPathSequence iter)
  1271. {
  1272. throw new SystemException ("XQuery internal error: should not happen.");
  1273. }
  1274. #endregion
  1275. }
  1276. // aggregation expr
  1277. public enum AggregationType {
  1278. Union,
  1279. Intersect,
  1280. Except
  1281. }
  1282. internal class GroupExpr : BinaryOperationExpr
  1283. {
  1284. public GroupExpr (ExprSingle left, ExprSingle right, AggregationType aggrType)
  1285. : base (left, right)
  1286. {
  1287. this.aggrType = aggrType;
  1288. }
  1289. AggregationType aggrType;
  1290. public AggregationType AggregationType {
  1291. get { return aggrType; }
  1292. }
  1293. #region CompileAndEvaluate
  1294. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  1295. {
  1296. Left = Left.Compile (compiler);
  1297. Right = Right.Compile (compiler);
  1298. return this;
  1299. }
  1300. // FIXME: It can be optimized by comparing l/r value types.
  1301. public override SequenceType StaticType {
  1302. get { return SequenceType.AnyType; }
  1303. }
  1304. // only applicable against node-sets
  1305. public override XPathSequence Evaluate (XPathSequence iter)
  1306. {
  1307. return new GroupIterator (iter, this);
  1308. }
  1309. #endregion
  1310. }
  1311. // validate expr
  1312. internal class ValidateExpr : ExprSingle
  1313. {
  1314. XmlSchemaContentProcessing schemaMode;
  1315. ExprSequence expr;
  1316. public ValidateExpr (XmlSchemaContentProcessing schemaMode, ExprSequence expr)
  1317. {
  1318. this.schemaMode = schemaMode;
  1319. this.expr = expr;
  1320. }
  1321. public ExprSequence Expr {
  1322. get { return expr; }
  1323. }
  1324. public XmlSchemaContentProcessing SchemaMode {
  1325. get { return schemaMode; }
  1326. }
  1327. internal override void CheckReference (XQueryASTCompiler compiler)
  1328. {
  1329. expr.CheckReference (compiler);
  1330. }
  1331. #region CompileAndEvaluate
  1332. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  1333. {
  1334. for (int i = 0; i < expr.Count; i++)
  1335. expr [i] = expr [i].Compile (compiler);
  1336. return this;
  1337. }
  1338. public override SequenceType StaticType {
  1339. get { return SequenceType.AnyType; }
  1340. }
  1341. public override XPathSequence Evaluate (XPathSequence iter)
  1342. {
  1343. // TBD (see 3.13).
  1344. throw new NotImplementedException ();
  1345. }
  1346. #endregion
  1347. }
  1348. // Path expr
  1349. internal abstract class PathExpr : ExprSingle
  1350. {
  1351. }
  1352. // '/'
  1353. internal class PathRootExpr : PathExpr
  1354. {
  1355. public PathRootExpr ()
  1356. {
  1357. }
  1358. internal override void CheckReference (XQueryASTCompiler compiler)
  1359. {
  1360. }
  1361. #region CompileAndEvaluate
  1362. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  1363. {
  1364. return this;
  1365. }
  1366. public override SequenceType StaticType {
  1367. get { return SequenceType.Document; }
  1368. }
  1369. public override XPathSequence Evaluate (XPathSequence iter)
  1370. {
  1371. XPathNavigator nav = iter.Context.CurrentItem as XPathNavigator;
  1372. if (nav == null)
  1373. throw new XmlQueryException ("Context item is not a node when evaluating expression '/'.");
  1374. nav = nav.Clone ();
  1375. nav.MoveToRoot ();
  1376. return new SingleItemIterator (nav, iter.Context);
  1377. }
  1378. #endregion
  1379. }
  1380. internal abstract class PathStepExpr : PathExpr
  1381. {
  1382. ExprSingle first;
  1383. ExprSingle next;
  1384. public PathStepExpr (ExprSingle first, ExprSingle next)
  1385. {
  1386. this.first = first;
  1387. this.next = next;
  1388. }
  1389. public ExprSingle First {
  1390. get { return first; }
  1391. set { first = value; }
  1392. }
  1393. public ExprSingle Next {
  1394. get { return next; }
  1395. set { next = value; }
  1396. }
  1397. internal override void CheckReference (XQueryASTCompiler compiler)
  1398. {
  1399. first.CheckReference (compiler);
  1400. next.CheckReference (compiler);
  1401. }
  1402. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  1403. {
  1404. first = first.Compile (compiler);
  1405. next = next.Compile (compiler);
  1406. return this;
  1407. }
  1408. }
  1409. // 'foo/bar'
  1410. internal class PathSlashExpr : PathStepExpr
  1411. {
  1412. public PathSlashExpr (ExprSingle first, ExprSingle next)
  1413. : base (first, next)
  1414. {
  1415. }
  1416. #region CompileAndEvaluate
  1417. // FIXME: It can be optimized by comparing l/r value types.
  1418. public override SequenceType StaticType {
  1419. get { return SequenceType.Node; }
  1420. }
  1421. public override XPathSequence Evaluate (XPathSequence iter)
  1422. {
  1423. return new PathStepIterator (First.Evaluate (iter), this);
  1424. }
  1425. #endregion
  1426. }
  1427. // 'foo//bar'
  1428. internal class PathSlash2Expr : PathStepExpr
  1429. {
  1430. public PathSlash2Expr (ExprSingle first, ExprSingle next)
  1431. : base (first, next)
  1432. {
  1433. }
  1434. #region CompileAndEvaluate
  1435. // FIXME: It can be optimized by comparing l/r value types.
  1436. public override SequenceType StaticType {
  1437. get { return SequenceType.Node; }
  1438. }
  1439. public override XPathSequence Evaluate (XPathSequence iter)
  1440. {
  1441. XPathSequence seq = First.Evaluate (iter);
  1442. if (!seq.MoveNext ())
  1443. return new XPathEmptySequence (iter.Context);
  1444. return new PathStepIterator (
  1445. new DescendantOrSelfIterator (seq.Current as XPathNavigator, seq.Context), this);
  1446. }
  1447. #endregion
  1448. }
  1449. internal class AxisStepExpr : PathExpr
  1450. {
  1451. public AxisStepExpr (XPathAxis axis, XPath2NodeTest test)
  1452. {
  1453. this.axis = axis;
  1454. if (test == null)
  1455. nameTest = XmlQualifiedName.Empty;
  1456. else {
  1457. if (test.NameTest != null)
  1458. this.nameTest = test.NameTest;
  1459. else
  1460. this.kindTest = test.KindTest;
  1461. }
  1462. }
  1463. XPathAxis axis;
  1464. XmlQualifiedName nameTest;
  1465. KindTest kindTest;
  1466. public XPathAxis Axis {
  1467. get { return axis; }
  1468. }
  1469. public XmlQualifiedName NameTest {
  1470. get { return nameTest; }
  1471. set { nameTest = value; }
  1472. }
  1473. public KindTest KindTest {
  1474. get { return kindTest; }
  1475. set { kindTest = value; }
  1476. }
  1477. internal override void CheckReference (XQueryASTCompiler compiler)
  1478. {
  1479. if (KindTest != null)
  1480. KindTest.CheckReference (compiler);
  1481. }
  1482. #region CompileAndEvaluate
  1483. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  1484. {
  1485. if (KindTest != null)
  1486. KindTest.Compile (compiler);
  1487. return this;
  1488. }
  1489. public override SequenceType StaticType {
  1490. get {
  1491. switch (Axis.AxisType) {
  1492. case XPathAxisType.Attribute:
  1493. return SequenceType.Attribute;
  1494. case XPathAxisType.Namespace:
  1495. return SequenceType.Namespace;
  1496. }
  1497. // FIXME: we can more filtering by KindTest
  1498. return SequenceType.Node;
  1499. }
  1500. }
  1501. public override XPathSequence Evaluate (XPathSequence iter)
  1502. {
  1503. XQueryContext ctx = iter.Context;
  1504. if (iter.Position == 0) {
  1505. iter = iter.Clone ();
  1506. if (!iter.MoveNext ())
  1507. return new XPathEmptySequence (iter.Context);
  1508. }
  1509. XPathNavigator nav = iter.Current as XPathNavigator;
  1510. if (nav == null)
  1511. throw new XmlQueryException ("Node set is expected.");
  1512. NodeIterator argIter = null;
  1513. switch (Axis.AxisType) {
  1514. case XPathAxisType.Child:
  1515. argIter = new ChildIterator (nav, ctx); break;
  1516. case XPathAxisType.Descendant:
  1517. argIter = new DescendantIterator (nav, ctx); break;
  1518. case XPathAxisType.Attribute:
  1519. argIter = new AttributeIterator (nav, ctx); break;
  1520. case XPathAxisType.Self:
  1521. argIter = new SelfIterator (nav, ctx); break;
  1522. case XPathAxisType.DescendantOrSelf:
  1523. argIter = new DescendantOrSelfIterator (nav, ctx); break;
  1524. case XPathAxisType.FollowingSibling:
  1525. argIter = new FollowingSiblingIterator (nav, ctx); break;
  1526. case XPathAxisType.Following:
  1527. argIter = new FollowingIterator (nav, ctx); break;
  1528. case XPathAxisType.Parent:
  1529. argIter = new ParentIterator (nav, ctx); break;
  1530. case XPathAxisType.Ancestor:
  1531. argIter = new AncestorIterator (nav, ctx); break;
  1532. case XPathAxisType.PrecedingSibling:
  1533. argIter = new PrecedingSiblingIterator (nav, ctx); break;
  1534. case XPathAxisType.Preceding:
  1535. argIter = new PrecedingIterator (nav, ctx); break;
  1536. case XPathAxisType.AncestorOrSelf:
  1537. argIter = new AncestorOrSelfIterator (nav, ctx); break;
  1538. case XPathAxisType.Namespace: // only applicable under XPath 2.0: not XQuery 1.0
  1539. argIter = new NamespaceIterator (nav, ctx); break;
  1540. }
  1541. return new AxisIterator (argIter, this);
  1542. }
  1543. internal bool Matches (XPathNavigator nav)
  1544. {
  1545. if (nameTest != null)
  1546. return nameTest == XmlQualifiedName.Empty ||
  1547. ((nameTest.Name == nav.LocalName || nameTest.Name == "*") &&
  1548. (nameTest.Namespace == nav.NamespaceURI || nameTest.Namespace == "*"));
  1549. else
  1550. return kindTest.Matches (nav);
  1551. }
  1552. #endregion
  1553. }
  1554. internal class FilterStepExpr : PathExpr
  1555. {
  1556. public FilterStepExpr (ExprSingle expr, PredicateList predicates)
  1557. {
  1558. this.expr = expr;
  1559. this.predicates = predicates;
  1560. }
  1561. ExprSingle expr;
  1562. PredicateList predicates;
  1563. public ExprSingle Expr {
  1564. get { return expr; }
  1565. set { expr = value; }
  1566. }
  1567. public PredicateList Predicates {
  1568. get { return predicates; }
  1569. }
  1570. internal override void CheckReference (XQueryASTCompiler compiler)
  1571. {
  1572. expr.CheckReference (compiler);
  1573. foreach (ExprSequence seq in predicates)
  1574. seq.CheckReference (compiler);
  1575. }
  1576. #region CompileAndEvaluate
  1577. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  1578. {
  1579. Expr = Expr.Compile (compiler);
  1580. foreach (ExprSequence seq in Predicates)
  1581. for (int i = 0; i < seq.Count; i++)
  1582. seq [i] = seq [i].Compile (compiler);
  1583. return this;
  1584. }
  1585. public override SequenceType StaticType {
  1586. get { return Expr.StaticType; }
  1587. }
  1588. public override XPathSequence Evaluate (XPathSequence iter)
  1589. {
  1590. return new FilteredIterator (iter, this);
  1591. }
  1592. #endregion
  1593. }
  1594. // predicates == exprsequence list == list of list of exprsingle
  1595. internal class PredicateList : CollectionBase
  1596. {
  1597. public void Add (ExprSequence expr)
  1598. {
  1599. List.Add (expr);
  1600. }
  1601. public void Insert (int pos, ExprSequence expr)
  1602. {
  1603. List.Insert (pos, expr);
  1604. }
  1605. public ExprSequence this [int i] {
  1606. get { return (ExprSequence) List [i]; }
  1607. }
  1608. }
  1609. internal class XPath2NodeTest
  1610. {
  1611. public XPath2NodeTest (XmlQualifiedName nameTest)
  1612. {
  1613. this.NameTest = nameTest;
  1614. }
  1615. public XPath2NodeTest (KindTest kindTest)
  1616. {
  1617. this.KindTest = kindTest;
  1618. }
  1619. public XmlQualifiedName NameTest;
  1620. public KindTest KindTest;
  1621. }
  1622. internal class EnclosedExpr : ExprSingle
  1623. {
  1624. ExprSequence expr;
  1625. public EnclosedExpr (ExprSequence expr)
  1626. {
  1627. this.expr = expr;
  1628. }
  1629. public ExprSequence Expr {
  1630. get { return expr; }
  1631. }
  1632. internal override void CheckReference (XQueryASTCompiler compiler)
  1633. {
  1634. expr.CheckReference (compiler);
  1635. }
  1636. #region CompileAndEvaluate
  1637. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  1638. {
  1639. if (Expr.Count == 0)
  1640. return Expr [0].Compile (compiler);
  1641. for (int i = 0; i < Expr.Count; i++)
  1642. Expr [i] = Expr [i].Compile (compiler);
  1643. return this;
  1644. }
  1645. // FIXME: can be optimized by checking all items in Expr
  1646. public override SequenceType StaticType {
  1647. get { return SequenceType.AnyType; }
  1648. }
  1649. public override XPathSequence Evaluate (XPathSequence iter)
  1650. {
  1651. return new ExprSequenceIterator (iter, Expr);
  1652. }
  1653. #endregion
  1654. }
  1655. // PrimaryExpr
  1656. internal abstract class PrimaryExpr : ExprSingle
  1657. {
  1658. internal override void CheckReference (XQueryASTCompiler compiler)
  1659. {
  1660. }
  1661. #region CompileAndEvaluate
  1662. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  1663. {
  1664. return this;
  1665. }
  1666. public override XPathSequence Evaluate (XPathSequence iter)
  1667. {
  1668. return new SingleItemIterator (EvaluateAsAtomic (iter), iter.Context);
  1669. }
  1670. #endregion
  1671. }
  1672. internal class StringLiteralExpr : PrimaryExpr
  1673. {
  1674. string literal;
  1675. public StringLiteralExpr (string literal)
  1676. {
  1677. this.literal = literal;
  1678. }
  1679. public string Literal {
  1680. get { return literal; }
  1681. }
  1682. #region CompileAndEvaluate
  1683. XmlSchemaSimpleType stringType = XmlSchemaType.GetBuiltInSimpleType (new XmlQualifiedName ("string", XmlSchema.Namespace));
  1684. public override SequenceType StaticType {
  1685. get { return SequenceType.AtomicString; }
  1686. }
  1687. public override string EvaluateAsString (XPathSequence iter)
  1688. {
  1689. return Literal;
  1690. }
  1691. public override XPathAtomicValue EvaluateAsAtomic (XPathSequence iter)
  1692. {
  1693. return new XPathAtomicValue (Literal, stringType);
  1694. }
  1695. #endregion
  1696. }
  1697. internal class DecimalLiteralExpr : PrimaryExpr
  1698. {
  1699. decimal value;
  1700. public DecimalLiteralExpr (decimal value)
  1701. {
  1702. this.value = value;
  1703. }
  1704. public decimal Value {
  1705. get { return value; }
  1706. }
  1707. #region CompileAndEvaluate
  1708. XmlSchemaSimpleType decimalType = XmlSchemaType.GetBuiltInSimpleType (new XmlQualifiedName ("decimal", XmlSchema.Namespace));
  1709. public override SequenceType StaticType {
  1710. get { return SequenceType.Decimal; }
  1711. }
  1712. public override XPathAtomicValue EvaluateAsAtomic (XPathSequence iter)
  1713. {
  1714. return new XPathAtomicValue (Value, decimalType);
  1715. }
  1716. #endregion
  1717. }
  1718. internal class DoubleLiteralExpr : PrimaryExpr
  1719. {
  1720. double value;
  1721. public DoubleLiteralExpr (double value)
  1722. {
  1723. this.value = value;
  1724. }
  1725. public double Value {
  1726. get { return value; }
  1727. }
  1728. #region CompileAndEvaluate
  1729. XmlSchemaSimpleType doubleType = XmlSchemaType.GetBuiltInSimpleType (new XmlQualifiedName ("double", XmlSchema.Namespace));
  1730. public override SequenceType StaticType {
  1731. get { return SequenceType.Double; }
  1732. }
  1733. public override XPathAtomicValue EvaluateAsAtomic (XPathSequence iter)
  1734. {
  1735. return new XPathAtomicValue (Value, doubleType);
  1736. }
  1737. #endregion
  1738. }
  1739. internal class VariableReferenceExpr : PrimaryExpr
  1740. {
  1741. XmlQualifiedName varName;
  1742. public VariableReferenceExpr (XmlQualifiedName varName)
  1743. {
  1744. this.varName = varName;
  1745. }
  1746. public XmlQualifiedName VariableName {
  1747. get { return varName; }
  1748. }
  1749. // FIXME: variable name must be stacked in any area
  1750. // whereever variables are defined.
  1751. internal override void CheckReference (XQueryASTCompiler compiler)
  1752. {
  1753. compiler.CheckVariableName (varName);
  1754. }
  1755. #region CompileAndEvaluate
  1756. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  1757. {
  1758. // FIXME: try to resolve static context variable and return the actual value expression
  1759. return this;
  1760. }
  1761. public override SequenceType StaticType {
  1762. get { return SequenceType.AnyType; }
  1763. }
  1764. public override XPathSequence Evaluate (XPathSequence iter)
  1765. {
  1766. XPathSequence variable = iter.Context.ResolveVariable (VariableName);
  1767. // FIXME: if Evaluate() accepts XPathSequence, then XPathSequence must be public class (to make IXPath2Variable public).
  1768. return variable;
  1769. }
  1770. #endregion
  1771. }
  1772. internal class ParenthesizedExpr : PrimaryExpr
  1773. {
  1774. ExprSequence expr;
  1775. public ParenthesizedExpr (ExprSequence expr)
  1776. {
  1777. if (expr == null)
  1778. expr = new ExprSequence ();
  1779. this.expr = expr;
  1780. }
  1781. ExprSequence Expr {
  1782. get { return expr; }
  1783. }
  1784. #region CompileAndEvaluate
  1785. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  1786. {
  1787. if (Expr.Count == 1)
  1788. return Expr [0].Compile (compiler);
  1789. for (int i = 0; i < Expr.Count; i++)
  1790. Expr [i] = Expr [i].Compile (compiler);
  1791. return this;
  1792. }
  1793. // FIXME: can be optimized by checking all items in Expr
  1794. public override SequenceType StaticType {
  1795. get { return SequenceType.AnyType; }
  1796. }
  1797. public override XPathSequence Evaluate (XPathSequence iter)
  1798. {
  1799. switch (Expr.Count) {
  1800. case 0:
  1801. return new XPathEmptySequence (iter.Context);
  1802. case 1:
  1803. return Expr [0].Evaluate (iter);
  1804. default:
  1805. return new ExprSequenceIterator (iter, Expr);
  1806. }
  1807. }
  1808. #endregion
  1809. }
  1810. // "."
  1811. internal class ContextItemExpr : PrimaryExpr
  1812. {
  1813. public ContextItemExpr ()
  1814. {
  1815. }
  1816. #region CompileAndEvaluate
  1817. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  1818. {
  1819. return this;
  1820. }
  1821. public override SequenceType StaticType {
  1822. get { return SequenceType.AnyType; }
  1823. }
  1824. public override XPathSequence Evaluate (XPathSequence iter)
  1825. {
  1826. return new SingleItemIterator (iter.Context.CurrentItem, iter.Context);
  1827. }
  1828. #endregion
  1829. }
  1830. internal abstract class FunctionCallExprBase : PrimaryExpr
  1831. {
  1832. XmlQualifiedName name;
  1833. ExprSequence args;
  1834. public FunctionCallExprBase (XmlQualifiedName name, ExprSequence args)
  1835. {
  1836. if (args == null)
  1837. throw new ArgumentNullException (String.Format ("Function argument expressions for {0} is null.", name));
  1838. this.name = name;
  1839. this.args = args;
  1840. }
  1841. public XmlQualifiedName Name {
  1842. get { return name; }
  1843. }
  1844. public ExprSequence Args {
  1845. get { return args; }
  1846. }
  1847. internal override void CheckReference (XQueryASTCompiler compiler)
  1848. {
  1849. compiler.CheckFunctionName (name);
  1850. }
  1851. #region CompileAndEvaluate
  1852. /*
  1853. internal static FunctionCallExpr Create (
  1854. XmlQualifiedName name,
  1855. ExprSingle [] args,
  1856. XQueryStaticContext ctx)
  1857. {
  1858. switch (name.Namespace) {
  1859. case XQueryFunction.Namespace:
  1860. switch (name.Name) {
  1861. case "node-name":
  1862. return new FnNodeNameCall (ctx, args);
  1863. case "nilled":
  1864. return new FnNilledCall (ctx, args);
  1865. case "string":
  1866. return new FnStringCall (ctx, args);
  1867. case "data":
  1868. return new FnDataCall (ctx, args);
  1869. case "base-uri":
  1870. return new FnBaseUriCall (ctx, args);
  1871. case "document-uri":
  1872. return new FnDocumentUriCall (ctx, args);
  1873. case "error":
  1874. return new FnErrorCall (ctx, args);
  1875. case "trace":
  1876. return new FnTraceCall (ctx, args);
  1877. case "abs":
  1878. return new FnAbsCall (ctx, args);
  1879. case "ceiling":
  1880. return new FnCeilingCall (ctx, args);
  1881. case "floor":
  1882. return new FnFloorCall (ctx, args);
  1883. case "round":
  1884. return new FnRoundCall (ctx, args);
  1885. case "round-half-to-even":
  1886. return new FnRoundHalfToEvenCall (ctx, args);
  1887. case "codepoints-to-string":
  1888. return new FnCodepointsToStringCall (ctx, args);
  1889. case "string-to-codepoints":
  1890. return new FnStringCallToCodepointsCall (ctx, args);
  1891. }
  1892. goto default;
  1893. case XmlSchema.XdtNamespace:
  1894. case XmlSchema.Namespace:
  1895. XmlSchemaType type = XmlSchemaType.GetBuiltInSimpleType (name);
  1896. if (type != null)
  1897. return new AtomicConstructorCall (ctx, SequenceType.Create (type, Occurence.One), args);
  1898. type = XmlSchemaType.GetBuiltInComplexType (name);
  1899. if (type == null)
  1900. goto default;
  1901. return null;
  1902. default:
  1903. XQueryFunction func = ctx.CompileContext.InEffectFunctions [name];
  1904. if (func != null)
  1905. return new CustomFunctionCallExpression (ctx, args, func);
  1906. return null;
  1907. }
  1908. }
  1909. */
  1910. internal void CheckArguments (XQueryASTCompiler compiler)
  1911. {
  1912. if (args.Count < MinArgs || args.Count > MaxArgs)
  1913. // FIXME: add more info
  1914. throw new XmlQueryCompileException (String.Format ("{0} is invalid for the number of {1} function argument. MinArgs = {2}, MaxArgs = {3}.", args.Count, name, MinArgs, MaxArgs));
  1915. }
  1916. public abstract int MinArgs { get; }
  1917. public abstract int MaxArgs { get; }
  1918. #endregion
  1919. }
  1920. // This class is used only in AST
  1921. internal class FunctionCallExpr : FunctionCallExprBase
  1922. {
  1923. public FunctionCallExpr (XmlQualifiedName name, ExprSequence args)
  1924. : base (name, args)
  1925. {
  1926. }
  1927. #region CompileAndEvaluate
  1928. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  1929. {
  1930. // resolve function
  1931. return new CustomFunctionCallExpr (Args, compiler.ResolveFunction (Name)).Compile (compiler);
  1932. }
  1933. public override XPathSequence Evaluate (XPathSequence iter)
  1934. {
  1935. throw new InvalidOperationException ("XQuery internal error. Should not happen.");
  1936. }
  1937. public override SequenceType StaticType {
  1938. get { throw new InvalidOperationException ("XQuery internal error. Should not happen."); }
  1939. }
  1940. public override int MinArgs {
  1941. get { throw new InvalidOperationException ("XQuery internal error. Should not happen."); }
  1942. }
  1943. public override int MaxArgs {
  1944. get { throw new InvalidOperationException ("XQuery internal error. Should not happen."); }
  1945. }
  1946. #endregion
  1947. }
  1948. #region CompileAndEvaluate
  1949. // It is instantiated per function call expression.
  1950. // (e.g. the example below contains 4 FunctionCallExpression instances:
  1951. // "replace(node-name (node-before(/*)), 'foo', node-name($var))"
  1952. internal class CustomFunctionCallExpr : FunctionCallExprBase
  1953. {
  1954. public CustomFunctionCallExpr (ExprSequence args, XQueryFunction function)
  1955. : base (function.Name, args)
  1956. {
  1957. this.function = function;
  1958. }
  1959. XQueryFunction function;
  1960. public XQueryFunction Function {
  1961. get { return function; }
  1962. }
  1963. public override int MinArgs {
  1964. get { return function.MinArgs; }
  1965. }
  1966. public override int MaxArgs {
  1967. get { return function.MaxArgs; }
  1968. }
  1969. public override SequenceType StaticType {
  1970. get { return function.ReturnType; }
  1971. }
  1972. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  1973. {
  1974. CheckArguments (compiler);
  1975. for (int i = 0; i < Args.Count; i++)
  1976. Args [i] = Args [i].Compile (compiler);
  1977. return this;
  1978. }
  1979. public override XPathSequence Evaluate (XPathSequence iter)
  1980. {
  1981. return Function.Evaluate (iter, Args);
  1982. }
  1983. // FIXME: add all overrides that delegates to XQueryFunction
  1984. }
  1985. #endregion
  1986. // Ordered / Unordered
  1987. internal class OrderSpecifiedExpr : ExprSingle
  1988. {
  1989. bool ordered;
  1990. ExprSequence expr;
  1991. public OrderSpecifiedExpr (ExprSequence expr, bool ordered)
  1992. {
  1993. this.ordered = ordered;
  1994. this.expr = expr;
  1995. }
  1996. public ExprSequence Expr {
  1997. get { return expr; }
  1998. }
  1999. public bool Ordered {
  2000. get { return ordered; }
  2001. }
  2002. internal override void CheckReference (XQueryASTCompiler compiler)
  2003. {
  2004. expr.CheckReference (compiler);
  2005. }
  2006. #region CompileAndEvaluate
  2007. public override SequenceType StaticType {
  2008. // FIXME: could be optimized by checking all the expressions
  2009. get { return SequenceType.AnyType; }
  2010. }
  2011. public override bool RequireSorting {
  2012. get { return Ordered; }
  2013. }
  2014. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  2015. {
  2016. for (int i = 0; i < Expr.Count; i++)
  2017. Expr [i] = Expr [i].Compile (compiler);
  2018. return this;
  2019. }
  2020. public override XPathSequence Evaluate (XPathSequence iter)
  2021. {
  2022. throw new NotImplementedException ();
  2023. }
  2024. #endregion
  2025. }
  2026. }
  2027. #endif