XPath2Expression.cs 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252
  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);
  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. // for (int i = 0; i < Condition.Count; i++)
  703. // Condition [i] = Condition [i].Compile (compiler);
  704. condition = condition.Compile (compiler);
  705. // FIXME: check if condition is constant, and returns trueExpr or falseExpr
  706. TrueExpr = TrueExpr.Compile (compiler);
  707. FalseExpr = FalseExpr.Compile (compiler);
  708. return this;
  709. }
  710. public override SequenceType StaticType {
  711. get {
  712. if (Context == null)
  713. return SequenceType.AnyType;
  714. if (computedReturnType == null)
  715. computedReturnType = SequenceType.ComputeCommonBase (TrueExpr.StaticType, FalseExpr.StaticType);
  716. return computedReturnType;
  717. }
  718. }
  719. public override XPathSequence Evaluate (XPathSequence iter)
  720. {
  721. // foreach (ExprSingle expr in Condition) {
  722. // if (expr.EvaluateAsBoolean (iter))
  723. // return TrueExpr.Evaluate (iter);
  724. // }
  725. if (condition.EvaluateAsBoolean (iter))
  726. return TrueExpr.Evaluate (iter);
  727. return FalseExpr.Evaluate (iter);
  728. }
  729. #endregion
  730. }
  731. // logical expr
  732. internal abstract class BinaryOperationExpr : ExprSingle
  733. {
  734. protected BinaryOperationExpr (ExprSingle left, ExprSingle right)
  735. {
  736. this.left = left;
  737. this.right = right;
  738. }
  739. ExprSingle left, right;
  740. public ExprSingle Left {
  741. get { return left; }
  742. set { left = value; }
  743. }
  744. public ExprSingle Right{
  745. get { return right; }
  746. set { right = value; }
  747. }
  748. internal override void CheckReference (XQueryASTCompiler compiler)
  749. {
  750. left.CheckReference (compiler);
  751. right.CheckReference (compiler);
  752. }
  753. #region CompileAndEvaluate
  754. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  755. {
  756. Left = Left.Compile (compiler);
  757. Right = Right.Compile (compiler);
  758. return this;
  759. }
  760. #endregion
  761. }
  762. internal class OrExpr : BinaryOperationExpr
  763. {
  764. public OrExpr (ExprSingle left, ExprSingle right)
  765. : base (left, right)
  766. {
  767. }
  768. #region CompileAndEvaluate
  769. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  770. {
  771. base.CompileCore (compiler);
  772. // FIXME: check constant value and return true or false
  773. return this;
  774. }
  775. public override SequenceType StaticType {
  776. get { return SequenceType.Boolean; }
  777. }
  778. public override bool EvaluateAsBoolean (XPathSequence iter)
  779. {
  780. return Left.EvaluateAsBoolean (iter) || Right.EvaluateAsBoolean (iter);
  781. }
  782. public override XPathSequence Evaluate (XPathSequence iter)
  783. {
  784. return new SingleItemIterator (EvaluateAsBoolean (iter) ?AtomicTrue : AtomicFalse, iter);
  785. }
  786. /*
  787. - compiler -
  788. return leftExprBool (context) || rightExprBool (context);
  789. */
  790. #endregion
  791. }
  792. internal class AndExpr : BinaryOperationExpr
  793. {
  794. public AndExpr (ExprSingle left, ExprSingle right)
  795. : base (left, right)
  796. {
  797. }
  798. #region CompileAndEvaluate
  799. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  800. {
  801. base.CompileCore (compiler);
  802. // FIXME: check constant value and return true or false
  803. return this;
  804. }
  805. public override SequenceType StaticType {
  806. get { return SequenceType.Boolean; }
  807. }
  808. public override bool EvaluateAsBoolean (XPathSequence iter)
  809. {
  810. return Left.EvaluateAsBoolean (iter) && Right.EvaluateAsBoolean (iter);
  811. }
  812. public override XPathSequence Evaluate (XPathSequence iter)
  813. {
  814. return new SingleItemIterator (EvaluateAsBoolean (iter) ? AtomicTrue : AtomicFalse, iter);
  815. }
  816. /*
  817. - compiler -
  818. return leftExprBool (context) && rightExprBool (context);
  819. */
  820. #endregion
  821. }
  822. // TypeOperation expr
  823. internal abstract class TypeOperationExpr : ExprSingle
  824. {
  825. protected TypeOperationExpr (ExprSingle expr, SequenceType type)
  826. {
  827. this.expr = expr;
  828. this.type = type;
  829. }
  830. ExprSingle expr;
  831. SequenceType type;
  832. public ExprSingle Expr {
  833. get { return expr; }
  834. set { expr = value; }
  835. }
  836. public SequenceType TargetType {
  837. get { return type; }
  838. }
  839. internal override void CheckReference (XQueryASTCompiler compiler)
  840. {
  841. expr.CheckReference (compiler);
  842. compiler.CheckSchemaType (type);
  843. }
  844. }
  845. internal abstract class AtomicTypeOperationExpr : ExprSingle
  846. {
  847. protected AtomicTypeOperationExpr (ExprSingle expr, XmlTypeCode type, bool optional)
  848. {
  849. this.expr = expr;
  850. // this.typeCode = type;
  851. // this.optional = optional;
  852. this.targetType = SequenceType.Create (type, optional ? Occurence.Optional : Occurence.One);
  853. }
  854. ExprSingle expr;
  855. // XmlTypeCode typeCode;
  856. // bool optional;
  857. SequenceType targetType;
  858. internal ExprSingle Expr {
  859. get { return expr; }
  860. set { expr = value; }
  861. }
  862. /*
  863. public XmlTypeCode TypeCode {
  864. get { return typeCode; }
  865. }
  866. public bool Optional {
  867. get { return optional; }
  868. }
  869. */
  870. internal SequenceType TargetType {
  871. get { return targetType; }
  872. }
  873. internal override void CheckReference (XQueryASTCompiler compiler)
  874. {
  875. expr.CheckReference (compiler);
  876. }
  877. }
  878. internal class InstanceOfExpr : TypeOperationExpr
  879. {
  880. public InstanceOfExpr (ExprSingle expr, SequenceType type)
  881. : base (expr, type)
  882. {
  883. }
  884. #region CompileAndEvaluate
  885. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  886. {
  887. Expr = Expr.Compile (compiler);
  888. // FIXME: check return type and if it never matches then return false
  889. return this;
  890. }
  891. public override SequenceType StaticType {
  892. get { return SequenceType.Boolean; }
  893. }
  894. public override bool EvaluateAsBoolean (XPathSequence iter)
  895. {
  896. bool occured = false;
  897. bool onlyOnce = (TargetType.Occurence == Occurence.One || TargetType.Occurence == Occurence.Optional);
  898. bool required = (TargetType.Occurence == Occurence.One || TargetType.Occurence == Occurence.OneOrMore);
  899. foreach (XPathItem item in iter) {
  900. if (occured && onlyOnce)
  901. return false;
  902. if (!TargetType.IsInstance (item))
  903. return false;
  904. }
  905. return occured || !required;
  906. }
  907. public override XPathSequence Evaluate (XPathSequence iter)
  908. {
  909. return new SingleItemIterator (EvaluateAsBoolean (iter) ? AtomicTrue : AtomicFalse, iter);
  910. }
  911. #endregion
  912. }
  913. internal class TreatExpr : TypeOperationExpr
  914. {
  915. public TreatExpr (ExprSingle expr, SequenceType type)
  916. : base (expr, type)
  917. {
  918. }
  919. #region CompileAndEvaluate
  920. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  921. {
  922. Expr = Expr.Compile (compiler);
  923. // FIXME: check return type and if it never matches then return false
  924. return this;
  925. }
  926. public override SequenceType StaticType {
  927. get { return SequenceType.AnyType; }
  928. }
  929. public override XPathSequence Evaluate (XPathSequence iter)
  930. {
  931. if (TargetType.CanConvert (iter))
  932. return iter;
  933. else
  934. throw new XmlQueryException (String.Format ("Cannot treat as {1}", TargetType));
  935. }
  936. #endregion
  937. }
  938. internal class CastableExpr : AtomicTypeOperationExpr
  939. {
  940. public CastableExpr (ExprSingle expr, XmlTypeCode atomicType, bool optional)
  941. : base (expr, atomicType, optional)
  942. {
  943. }
  944. #region CompileAndEvaluate
  945. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  946. {
  947. Expr = Expr.Compile (compiler);
  948. // FIXME: check return type and if it never matches then return boolean
  949. return this;
  950. }
  951. public override SequenceType StaticType {
  952. get { return SequenceType.Boolean; }
  953. }
  954. public override XPathSequence Evaluate (XPathSequence iter)
  955. {
  956. return new SingleItemIterator (new XPathAtomicValue (EvaluateAsBoolean (iter), XmlSchemaSimpleType.XsBoolean), iter);
  957. }
  958. public override bool EvaluateAsBoolean (XPathSequence iter)
  959. {
  960. bool occured = false;
  961. bool onlyOnce = (TargetType.Occurence == Occurence.One || TargetType.Occurence == Occurence.Optional);
  962. bool required = (TargetType.Occurence == Occurence.One || TargetType.Occurence == Occurence.OneOrMore);
  963. foreach (XPathItem item in iter) {
  964. if (occured && onlyOnce)
  965. return false;
  966. if (!TargetType.CanConvert (item))
  967. return false;
  968. }
  969. return occured || !required;
  970. }
  971. #endregion
  972. }
  973. internal class CastExpr : AtomicTypeOperationExpr
  974. {
  975. public CastExpr (ExprSingle expr, XmlTypeCode atomicType, bool optional)
  976. : base (expr, atomicType, optional)
  977. {
  978. }
  979. #region CompileAndEvaluate
  980. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  981. {
  982. Expr = Expr.Compile (compiler);
  983. // FIXME: check return type and if it never matches then return boolean
  984. return this;
  985. }
  986. public override SequenceType StaticType {
  987. get { return TargetType; }
  988. }
  989. public override XPathSequence Evaluate (XPathSequence iter)
  990. {
  991. if (TargetType.CanConvert (iter))
  992. return new ConvertingIterator (iter, TargetType);
  993. else
  994. throw new XmlQueryException (String.Format ("Cannot cast as {1}", TargetType));
  995. }
  996. #endregion
  997. }
  998. // ComparisonExpr
  999. internal class ComparisonExpr : BinaryOperationExpr
  1000. {
  1001. public ComparisonExpr (ExprSingle left, ExprSingle right, ComparisonOperator oper)
  1002. : base (left, right)
  1003. {
  1004. }
  1005. #region CompileAndEvaluate
  1006. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  1007. {
  1008. Left = Left.Compile (compiler);
  1009. Right = Right.Compile (compiler);
  1010. // FIXME: check return type and if it never matches then return boolean
  1011. return this;
  1012. }
  1013. public override SequenceType StaticType {
  1014. get { return SequenceType.Boolean; }
  1015. }
  1016. public override bool EvaluateAsBoolean (XPathSequence iter)
  1017. {
  1018. throw new NotImplementedException ();
  1019. }
  1020. public override XPathSequence Evaluate (XPathSequence iter)
  1021. {
  1022. return new SingleItemIterator (EvaluateAsBoolean (iter) ? AtomicTrue : AtomicFalse, iter);
  1023. }
  1024. #endregion
  1025. }
  1026. public enum ComparisonOperator {
  1027. ValueEQ,
  1028. ValueNE,
  1029. ValueLT,
  1030. ValueLE,
  1031. ValueGT,
  1032. ValueGE,
  1033. GeneralEQ,
  1034. GeneralNE,
  1035. GeneralLT,
  1036. GeneralLE,
  1037. GeneralGT,
  1038. GeneralGE,
  1039. NodeIs,
  1040. NodeFWD,
  1041. NodeBWD
  1042. }
  1043. // Range
  1044. internal class RangeExpr : BinaryOperationExpr
  1045. {
  1046. public RangeExpr (ExprSingle left, ExprSingle right)
  1047. : base (left, right)
  1048. {
  1049. }
  1050. #region CompileAndEvaluate
  1051. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  1052. {
  1053. Left = Left.Compile (compiler);
  1054. Right = Right.Compile (compiler);
  1055. return this;
  1056. }
  1057. public override SequenceType StaticType {
  1058. get { return SequenceType.IntegerList; }
  1059. }
  1060. public override XPathSequence Evaluate (XPathSequence iter)
  1061. {
  1062. int start = Left.EvaluateAsInt (iter);
  1063. int end = Right.EvaluateAsInt (iter);
  1064. return new IntegerRangeIterator (iter, start, end);
  1065. }
  1066. public override void Serialize (XPathSequence iter)
  1067. {
  1068. int start = Left.EvaluateAsInt (iter);
  1069. int end = Right.EvaluateAsInt (iter);
  1070. for (int i = start; i <= end; i++) {
  1071. iter.Context.Writer.WriteValue (i);
  1072. if (i < end)
  1073. iter.Context.Writer.WriteWhitespace (" ");
  1074. }
  1075. }
  1076. #endregion
  1077. }
  1078. // arithmetic operation expr
  1079. public enum ArithmeticOperator {
  1080. Plus,
  1081. Minus,
  1082. Mul,
  1083. Div,
  1084. IDiv,
  1085. IMod
  1086. }
  1087. internal class ArithmeticOperationExpr : BinaryOperationExpr
  1088. {
  1089. public ArithmeticOperationExpr (ExprSingle left, ExprSingle right, ArithmeticOperator oper)
  1090. : base (left, right)
  1091. {
  1092. }
  1093. #region CompileAndEvaluate
  1094. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  1095. {
  1096. Left = Left.Compile (compiler);
  1097. Right = Right.Compile (compiler);
  1098. return this;
  1099. }
  1100. // FIXME: It can be optimized by comparing l/r value types.
  1101. public override SequenceType StaticType {
  1102. get { return SequenceType.AnyType; }
  1103. }
  1104. public override XPathSequence Evaluate (XPathSequence iter)
  1105. {
  1106. XPathAtomicValue lvalue = Left.EvaluateAsAtomic (iter);
  1107. XPathAtomicValue rvalue = Right.EvaluateAsAtomic (iter);
  1108. throw new NotImplementedException ();
  1109. }
  1110. #endregion
  1111. }
  1112. internal class MinusExpr : ExprSingle
  1113. {
  1114. public MinusExpr (ExprSingle expr)
  1115. {
  1116. this.expr = expr;
  1117. }
  1118. ExprSingle expr;
  1119. public ExprSingle Expr {
  1120. get { return expr; }
  1121. set { expr = value; }
  1122. }
  1123. internal override void CheckReference (XQueryASTCompiler compiler)
  1124. {
  1125. expr.CheckReference (compiler);
  1126. }
  1127. #region CompileAndEvaluate
  1128. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  1129. {
  1130. Expr = Expr.Compile (compiler);
  1131. return this;
  1132. }
  1133. public override SequenceType StaticType {
  1134. get { return Expr.StaticType; }
  1135. }
  1136. public override XPathSequence Evaluate (XPathSequence iter)
  1137. {
  1138. throw new NotImplementedException ();
  1139. }
  1140. #endregion
  1141. }
  1142. // aggregation expr
  1143. public enum AggregationType {
  1144. Union,
  1145. Intersect,
  1146. Except
  1147. }
  1148. internal class GroupExpr : BinaryOperationExpr
  1149. {
  1150. public GroupExpr (ExprSingle left, ExprSingle right, AggregationType aggrType)
  1151. : base (left, right)
  1152. {
  1153. this.aggrType = aggrType;
  1154. }
  1155. AggregationType aggrType;
  1156. public AggregationType AggregationType {
  1157. get { return aggrType; }
  1158. }
  1159. #region CompileAndEvaluate
  1160. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  1161. {
  1162. Left = Left.Compile (compiler);
  1163. Right = Right.Compile (compiler);
  1164. return this;
  1165. }
  1166. // FIXME: It can be optimized by comparing l/r value types.
  1167. public override SequenceType StaticType {
  1168. get { return SequenceType.AnyType; }
  1169. }
  1170. // only applicable against node-sets
  1171. public override XPathSequence Evaluate (XPathSequence iter)
  1172. {
  1173. XPathSequence lvalue = Left.EvaluateOrdered (iter);
  1174. XPathSequence rvalue = Right.EvaluateOrdered (iter);
  1175. /*
  1176. TBD (yield earlier node, skipping one of the same nodes)
  1177. - or -
  1178. TBD (yield earlier node, skipping non-intersection nodes)
  1179. - or -
  1180. TBD (yield earlier node, skipping both of the same nodes)
  1181. */
  1182. throw new NotImplementedException ();
  1183. }
  1184. #endregion
  1185. }
  1186. // validate expr
  1187. internal class ValidateExpr : ExprSingle
  1188. {
  1189. XmlSchemaContentProcessing schemaMode;
  1190. ExprSequence expr;
  1191. public ValidateExpr (XmlSchemaContentProcessing schemaMode, ExprSequence expr)
  1192. {
  1193. this.schemaMode = schemaMode;
  1194. this.expr = expr;
  1195. }
  1196. public ExprSequence Expr {
  1197. get { return expr; }
  1198. }
  1199. public XmlSchemaContentProcessing SchemaMode {
  1200. get { return schemaMode; }
  1201. }
  1202. internal override void CheckReference (XQueryASTCompiler compiler)
  1203. {
  1204. expr.CheckReference (compiler);
  1205. }
  1206. #region CompileAndEvaluate
  1207. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  1208. {
  1209. for (int i = 0; i < expr.Count; i++)
  1210. expr [i] = expr [i].Compile (compiler);
  1211. return this;
  1212. }
  1213. public override SequenceType StaticType {
  1214. get { return SequenceType.AnyType; }
  1215. }
  1216. public override XPathSequence Evaluate (XPathSequence iter)
  1217. {
  1218. // TBD (see 3.13).
  1219. throw new NotImplementedException ();
  1220. }
  1221. #endregion
  1222. }
  1223. // Path expr
  1224. internal abstract class PathExpr : ExprSingle
  1225. {
  1226. }
  1227. // '/'
  1228. internal class PathRootExpr : PathExpr
  1229. {
  1230. public PathRootExpr ()
  1231. {
  1232. }
  1233. internal override void CheckReference (XQueryASTCompiler compiler)
  1234. {
  1235. }
  1236. #region CompileAndEvaluate
  1237. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  1238. {
  1239. return this;
  1240. }
  1241. public override SequenceType StaticType {
  1242. get { return SequenceType.Document; }
  1243. }
  1244. public override XPathSequence Evaluate (XPathSequence iter)
  1245. {
  1246. if (!iter.MoveNext ())
  1247. return new XPathEmptySequence (iter.Context);
  1248. XPathNavigator nav = iter.Current as XPathNavigator;
  1249. if (nav == null)
  1250. return new XPathEmptySequence (iter.Context);
  1251. nav = nav.Clone ();
  1252. nav.MoveToRoot ();
  1253. return new SingleItemIterator (nav, iter);
  1254. }
  1255. #endregion
  1256. }
  1257. internal abstract class PathStepExpr : PathExpr
  1258. {
  1259. ExprSingle first;
  1260. ExprSingle next;
  1261. public PathStepExpr (ExprSingle first, ExprSingle next)
  1262. {
  1263. this.first = first;
  1264. this.next = next;
  1265. }
  1266. public ExprSingle First {
  1267. get { return first; }
  1268. set { first = value; }
  1269. }
  1270. public ExprSingle Next {
  1271. get { return next; }
  1272. set { next = value; }
  1273. }
  1274. internal override void CheckReference (XQueryASTCompiler compiler)
  1275. {
  1276. first.CheckReference (compiler);
  1277. next.CheckReference (compiler);
  1278. }
  1279. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  1280. {
  1281. first = first.Compile (compiler);
  1282. next = next.Compile (compiler);
  1283. return this;
  1284. }
  1285. }
  1286. // 'foo/bar'
  1287. internal class PathSlashExpr : PathStepExpr
  1288. {
  1289. public PathSlashExpr (ExprSingle first, ExprSingle next)
  1290. : base (first, next)
  1291. {
  1292. }
  1293. #region CompileAndEvaluate
  1294. // FIXME: It can be optimized by comparing l/r value types.
  1295. public override SequenceType StaticType {
  1296. get { return SequenceType.Node; }
  1297. }
  1298. public override XPathSequence Evaluate (XPathSequence iter)
  1299. {
  1300. return new PathStepIterator (First.Evaluate (iter), this);
  1301. }
  1302. #endregion
  1303. }
  1304. // 'foo//bar'
  1305. internal class PathSlash2Expr : PathStepExpr
  1306. {
  1307. public PathSlash2Expr (ExprSingle first, ExprSingle next)
  1308. : base (first, next)
  1309. {
  1310. }
  1311. #region CompileAndEvaluate
  1312. // FIXME: It can be optimized by comparing l/r value types.
  1313. public override SequenceType StaticType {
  1314. get { return SequenceType.Node; }
  1315. }
  1316. public override XPathSequence Evaluate (XPathSequence iter)
  1317. {
  1318. // return new DescendantPathIterator (iter, this);
  1319. return new PathStepIterator (
  1320. new DescendantOrSelfIterator (First.Evaluate (iter)), this);
  1321. }
  1322. #endregion
  1323. }
  1324. internal class AxisStepExpr : PathExpr
  1325. {
  1326. static AxisStepExpr parentStep;
  1327. static AxisStepExpr ()
  1328. {
  1329. parentStep = new AxisStepExpr (XPathAxis.Parent, null);
  1330. }
  1331. public static AxisStepExpr ParentStep {
  1332. get { return parentStep; }
  1333. }
  1334. public AxisStepExpr (XPathAxis axis, XPath2NodeTest test)
  1335. {
  1336. this.axis = axis;
  1337. if (test == null)
  1338. nameTest = XmlQualifiedName.Empty;
  1339. else {
  1340. if (test.NameTest != null)
  1341. this.nameTest = test.NameTest;
  1342. else
  1343. this.kindTest = test.KindTest;
  1344. }
  1345. }
  1346. XPathAxis axis;
  1347. XmlQualifiedName nameTest;
  1348. KindTest kindTest;
  1349. public XPathAxis Axis {
  1350. get { return axis; }
  1351. }
  1352. public XmlQualifiedName NameTest {
  1353. get { return nameTest; }
  1354. set { nameTest = value; }
  1355. }
  1356. public KindTest KindTest {
  1357. get { return kindTest; }
  1358. set { kindTest = value; }
  1359. }
  1360. internal override void CheckReference (XQueryASTCompiler compiler)
  1361. {
  1362. if (KindTest != null)
  1363. KindTest.CheckReference (compiler);
  1364. }
  1365. #region CompileAndEvaluate
  1366. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  1367. {
  1368. if (KindTest != null)
  1369. KindTest.Compile (compiler);
  1370. return this;
  1371. }
  1372. public override SequenceType StaticType {
  1373. get {
  1374. switch (Axis.AxisType) {
  1375. case XPathAxisType.Attribute:
  1376. return SequenceType.Attribute;
  1377. case XPathAxisType.Namespace:
  1378. return SequenceType.Namespace;
  1379. }
  1380. // FIXME: we can more filtering by KindTest
  1381. return SequenceType.Node;
  1382. }
  1383. }
  1384. public override XPathSequence Evaluate (XPathSequence iter)
  1385. {
  1386. XPathSequence argIter = null;
  1387. switch (Axis.AxisType) {
  1388. case XPathAxisType.Child:
  1389. argIter = new ChildIterator (iter); break;
  1390. case XPathAxisType.Descendant:
  1391. argIter = new DescendantIterator (iter); break;
  1392. case XPathAxisType.Attribute:
  1393. argIter = new AttributeIterator (iter); break;
  1394. case XPathAxisType.Self:
  1395. argIter = new SingleItemIterator (iter.Current, iter); break;
  1396. case XPathAxisType.DescendantOrSelf:
  1397. argIter = new DescendantOrSelfIterator (iter); break;
  1398. case XPathAxisType.FollowingSibling:
  1399. argIter = new FollowingSiblingIterator (iter); break;
  1400. case XPathAxisType.Following:
  1401. argIter = new FollowingIterator (iter); break;
  1402. case XPathAxisType.Parent:
  1403. argIter = new ParentIterator (iter); break;
  1404. case XPathAxisType.Ancestor:
  1405. argIter = new AncestorIterator (iter); break;
  1406. case XPathAxisType.PrecedingSibling:
  1407. argIter = new PrecedingSiblingIterator (iter); break;
  1408. case XPathAxisType.Preceding:
  1409. argIter = new PrecedingIterator (iter); break;
  1410. case XPathAxisType.AncestorOrSelf:
  1411. argIter = new AncestorOrSelfIterator (iter); break;
  1412. case XPathAxisType.Namespace: // only applicable under XPath 2.0: not XQuery 1.0
  1413. argIter = new NamespaceIterator (iter); break;
  1414. }
  1415. return new AxisIterator (argIter, this);
  1416. }
  1417. internal bool Matches (XPathNavigator nav)
  1418. {
  1419. if (nameTest != null)
  1420. return nameTest == XmlQualifiedName.Empty ||
  1421. ((nameTest.Name == nav.LocalName || nameTest.Name == "*") &&
  1422. (nameTest.Namespace == nav.NamespaceURI || nameTest.Namespace == "*"));
  1423. else
  1424. return kindTest.Matches (nav);
  1425. }
  1426. #endregion
  1427. }
  1428. internal class FilterStepExpr : PathExpr
  1429. {
  1430. public FilterStepExpr (ExprSingle expr, PredicateList predicates)
  1431. {
  1432. this.expr = expr;
  1433. this.predicates = predicates;
  1434. }
  1435. ExprSingle expr;
  1436. PredicateList predicates;
  1437. public ExprSingle Expr {
  1438. get { return expr; }
  1439. set { expr = value; }
  1440. }
  1441. public PredicateList Predicates {
  1442. get { return predicates; }
  1443. }
  1444. internal override void CheckReference (XQueryASTCompiler compiler)
  1445. {
  1446. expr.CheckReference (compiler);
  1447. foreach (ExprSequence seq in predicates)
  1448. seq.CheckReference (compiler);
  1449. }
  1450. #region CompileAndEvaluate
  1451. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  1452. {
  1453. Expr = Expr.Compile (compiler);
  1454. foreach (ExprSequence seq in Predicates)
  1455. for (int i = 0; i < seq.Count; i++)
  1456. seq [i] = seq [i].Compile (compiler);
  1457. return this;
  1458. }
  1459. public override SequenceType StaticType {
  1460. get { return Expr.StaticType; }
  1461. }
  1462. public override XPathSequence Evaluate (XPathSequence iter)
  1463. {
  1464. return new FilteredIterator (iter, this);
  1465. }
  1466. #endregion
  1467. }
  1468. // predicates == exprsequence list == list of list of exprsingle
  1469. internal class PredicateList : CollectionBase
  1470. {
  1471. public void Add (ExprSequence expr)
  1472. {
  1473. List.Add (expr);
  1474. }
  1475. public void Insert (int pos, ExprSequence expr)
  1476. {
  1477. List.Insert (pos, expr);
  1478. }
  1479. public ExprSequence this [int i] {
  1480. get { return (ExprSequence) List [i]; }
  1481. }
  1482. }
  1483. internal class XPath2NodeTest
  1484. {
  1485. public XPath2NodeTest (XmlQualifiedName nameTest)
  1486. {
  1487. this.NameTest = nameTest;
  1488. }
  1489. public XPath2NodeTest (KindTest kindTest)
  1490. {
  1491. this.KindTest = kindTest;
  1492. }
  1493. public XmlQualifiedName NameTest;
  1494. public KindTest KindTest;
  1495. }
  1496. internal class EnclosedExpr : ExprSingle
  1497. {
  1498. ExprSequence expr;
  1499. public EnclosedExpr (ExprSequence expr)
  1500. {
  1501. this.expr = expr;
  1502. }
  1503. public ExprSequence Expr {
  1504. get { return expr; }
  1505. }
  1506. internal override void CheckReference (XQueryASTCompiler compiler)
  1507. {
  1508. expr.CheckReference (compiler);
  1509. }
  1510. #region CompileAndEvaluate
  1511. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  1512. {
  1513. if (Expr.Count == 0)
  1514. return Expr [0].Compile (compiler);
  1515. for (int i = 0; i < Expr.Count; i++)
  1516. Expr [i] = Expr [i].Compile (compiler);
  1517. return this;
  1518. }
  1519. // FIXME: can be optimized by checking all items in Expr
  1520. public override SequenceType StaticType {
  1521. get { return SequenceType.AnyType; }
  1522. }
  1523. public override XPathSequence Evaluate (XPathSequence iter)
  1524. {
  1525. return new ExprSequenceIterator (iter, Expr);
  1526. }
  1527. #endregion
  1528. }
  1529. // PrimaryExpr
  1530. internal abstract class PrimaryExpr : ExprSingle
  1531. {
  1532. internal override void CheckReference (XQueryASTCompiler compiler)
  1533. {
  1534. }
  1535. #region CompileAndEvaluate
  1536. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  1537. {
  1538. return this;
  1539. }
  1540. public override XPathSequence Evaluate (XPathSequence iter)
  1541. {
  1542. return new SingleItemIterator (EvaluateAsAtomic (iter), iter.Context);
  1543. }
  1544. #endregion
  1545. }
  1546. internal class StringLiteralExpr : PrimaryExpr
  1547. {
  1548. string literal;
  1549. public StringLiteralExpr (string literal)
  1550. {
  1551. this.literal = literal;
  1552. }
  1553. public string Literal {
  1554. get { return literal; }
  1555. }
  1556. #region CompileAndEvaluate
  1557. XmlSchemaSimpleType stringType = XmlSchemaType.GetBuiltInSimpleType (new XmlQualifiedName ("string", XmlSchema.Namespace));
  1558. public override SequenceType StaticType {
  1559. get { return SequenceType.AtomicString; }
  1560. }
  1561. public override string EvaluateAsString (XPathSequence iter)
  1562. {
  1563. return Literal;
  1564. }
  1565. public override XPathAtomicValue EvaluateAsAtomic (XPathSequence iter)
  1566. {
  1567. return new XPathAtomicValue (Literal, stringType);
  1568. }
  1569. #endregion
  1570. }
  1571. internal class DecimalLiteralExpr : PrimaryExpr
  1572. {
  1573. decimal value;
  1574. public DecimalLiteralExpr (decimal value)
  1575. {
  1576. this.value = value;
  1577. }
  1578. public decimal Value {
  1579. get { return value; }
  1580. }
  1581. #region CompileAndEvaluate
  1582. XmlSchemaSimpleType decimalType = XmlSchemaType.GetBuiltInSimpleType (new XmlQualifiedName ("decimal", XmlSchema.Namespace));
  1583. public override SequenceType StaticType {
  1584. get { return SequenceType.Decimal; }
  1585. }
  1586. public override XPathAtomicValue EvaluateAsAtomic (XPathSequence iter)
  1587. {
  1588. return new XPathAtomicValue (Value, decimalType);
  1589. }
  1590. #endregion
  1591. }
  1592. internal class DoubleLiteralExpr : PrimaryExpr
  1593. {
  1594. double value;
  1595. public DoubleLiteralExpr (double value)
  1596. {
  1597. this.value = value;
  1598. }
  1599. public double Value {
  1600. get { return value; }
  1601. }
  1602. #region CompileAndEvaluate
  1603. XmlSchemaSimpleType doubleType = XmlSchemaType.GetBuiltInSimpleType (new XmlQualifiedName ("double", XmlSchema.Namespace));
  1604. public override SequenceType StaticType {
  1605. get { return SequenceType.Double; }
  1606. }
  1607. public override XPathAtomicValue EvaluateAsAtomic (XPathSequence iter)
  1608. {
  1609. return new XPathAtomicValue (Value, doubleType);
  1610. }
  1611. #endregion
  1612. }
  1613. internal class VariableReferenceExpr : PrimaryExpr
  1614. {
  1615. XmlQualifiedName varName;
  1616. public VariableReferenceExpr (XmlQualifiedName varName)
  1617. {
  1618. this.varName = varName;
  1619. }
  1620. public XmlQualifiedName VariableName {
  1621. get { return varName; }
  1622. }
  1623. // FIXME: variable name must be stacked in any area
  1624. // whereever variables are defined.
  1625. internal override void CheckReference (XQueryASTCompiler compiler)
  1626. {
  1627. compiler.CheckVariableName (varName);
  1628. }
  1629. #region CompileAndEvaluate
  1630. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  1631. {
  1632. // FIXME: try to resolve static context variable and return the actual value expression
  1633. return this;
  1634. }
  1635. public override SequenceType StaticType {
  1636. get { return SequenceType.AnyType; }
  1637. }
  1638. public override XPathSequence Evaluate (XPathSequence iter)
  1639. {
  1640. XPathSequence variable = iter.Context.ResolveVariable (VariableName);
  1641. // FIXME: if Evaluate() accepts XPathSequence, then XPathSequence must be public class (to make IXPath2Variable public).
  1642. return variable;
  1643. }
  1644. #endregion
  1645. }
  1646. internal class ParenthesizedExpr : PrimaryExpr
  1647. {
  1648. ExprSequence expr;
  1649. public ParenthesizedExpr (ExprSequence expr)
  1650. {
  1651. if (expr == null)
  1652. expr = new ExprSequence ();
  1653. this.expr = expr;
  1654. }
  1655. ExprSequence Expr {
  1656. get { return expr; }
  1657. }
  1658. #region CompileAndEvaluate
  1659. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  1660. {
  1661. if (Expr.Count == 1)
  1662. return Expr [0].Compile (compiler);
  1663. for (int i = 0; i < Expr.Count; i++)
  1664. Expr [i] = Expr [i].Compile (compiler);
  1665. return this;
  1666. }
  1667. // FIXME: can be optimized by checking all items in Expr
  1668. public override SequenceType StaticType {
  1669. get { return SequenceType.AnyType; }
  1670. }
  1671. public override XPathSequence Evaluate (XPathSequence iter)
  1672. {
  1673. switch (Expr.Count) {
  1674. case 0:
  1675. return new XPathEmptySequence (iter.Context);
  1676. case 1:
  1677. return Expr [0].Evaluate (iter);
  1678. default:
  1679. return new ExprSequenceIterator (iter, Expr);
  1680. }
  1681. }
  1682. #endregion
  1683. }
  1684. // "."
  1685. internal class ContextItemExpr : PrimaryExpr
  1686. {
  1687. public ContextItemExpr ()
  1688. {
  1689. }
  1690. #region CompileAndEvaluate
  1691. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  1692. {
  1693. return this;
  1694. }
  1695. public override SequenceType StaticType {
  1696. get { return SequenceType.AnyType; }
  1697. }
  1698. public override XPathSequence Evaluate (XPathSequence iter)
  1699. {
  1700. return new SingleItemIterator (iter.Context.CurrentItem, iter);
  1701. }
  1702. #endregion
  1703. }
  1704. internal abstract class FunctionCallExprBase : PrimaryExpr
  1705. {
  1706. XmlQualifiedName name;
  1707. ExprSequence args;
  1708. public FunctionCallExprBase (XmlQualifiedName name, ExprSequence args)
  1709. {
  1710. if (args == null)
  1711. throw new ArgumentNullException (String.Format ("Function argument expressions for {0} is null.", name));
  1712. this.name = name;
  1713. this.args = args;
  1714. }
  1715. public XmlQualifiedName Name {
  1716. get { return name; }
  1717. }
  1718. public ExprSequence Args {
  1719. get { return args; }
  1720. }
  1721. internal override void CheckReference (XQueryASTCompiler compiler)
  1722. {
  1723. compiler.CheckFunctionName (name);
  1724. }
  1725. #region CompileAndEvaluate
  1726. /*
  1727. internal static FunctionCallExpr Create (
  1728. XmlQualifiedName name,
  1729. ExprSingle [] args,
  1730. XQueryStaticContext ctx)
  1731. {
  1732. switch (name.Namespace) {
  1733. case XQueryFunction.Namespace:
  1734. switch (name.Name) {
  1735. case "node-name":
  1736. return new FnNodeNameCall (ctx, args);
  1737. case "nilled":
  1738. return new FnNilledCall (ctx, args);
  1739. case "string":
  1740. return new FnStringCall (ctx, args);
  1741. case "data":
  1742. return new FnDataCall (ctx, args);
  1743. case "base-uri":
  1744. return new FnBaseUriCall (ctx, args);
  1745. case "document-uri":
  1746. return new FnDocumentUriCall (ctx, args);
  1747. case "error":
  1748. return new FnErrorCall (ctx, args);
  1749. case "trace":
  1750. return new FnTraceCall (ctx, args);
  1751. case "abs":
  1752. return new FnAbsCall (ctx, args);
  1753. case "ceiling":
  1754. return new FnCeilingCall (ctx, args);
  1755. case "floor":
  1756. return new FnFloorCall (ctx, args);
  1757. case "round":
  1758. return new FnRoundCall (ctx, args);
  1759. case "round-half-to-even":
  1760. return new FnRoundHalfToEvenCall (ctx, args);
  1761. case "codepoints-to-string":
  1762. return new FnCodepointsToStringCall (ctx, args);
  1763. case "string-to-codepoints":
  1764. return new FnStringCallToCodepointsCall (ctx, args);
  1765. }
  1766. goto default;
  1767. case XmlSchema.XdtNamespace:
  1768. case XmlSchema.Namespace:
  1769. XmlSchemaType type = XmlSchemaType.GetBuiltInSimpleType (name);
  1770. if (type != null)
  1771. return new AtomicConstructorCall (ctx, SequenceType.Create (type, Occurence.One), args);
  1772. type = XmlSchemaType.GetBuiltInComplexType (name);
  1773. if (type == null)
  1774. goto default;
  1775. return null;
  1776. default:
  1777. XQueryFunction func = ctx.CompileContext.InEffectFunctions [name];
  1778. if (func != null)
  1779. return new CustomFunctionCallExpression (ctx, args, func);
  1780. return null;
  1781. }
  1782. }
  1783. */
  1784. internal void CheckArguments (XQueryASTCompiler compiler)
  1785. {
  1786. if (args.Count < MinArgs || args.Count > MaxArgs)
  1787. // FIXME: add more info
  1788. throw new XmlQueryCompileException (String.Format ("{0} is invalid for the number of {1} function argument. MinArgs = {2}, MaxArgs = {3}.", args.Count, name, MinArgs, MaxArgs));
  1789. }
  1790. public abstract int MinArgs { get; }
  1791. public abstract int MaxArgs { get; }
  1792. #endregion
  1793. }
  1794. // This class is used only in AST
  1795. internal class FunctionCallExpr : FunctionCallExprBase
  1796. {
  1797. public FunctionCallExpr (XmlQualifiedName name, ExprSequence args)
  1798. : base (name, args)
  1799. {
  1800. }
  1801. #region CompileAndEvaluate
  1802. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  1803. {
  1804. // resolve function
  1805. return new CustomFunctionCallExpr (Args, compiler.ResolveFunction (Name)).Compile (compiler);
  1806. }
  1807. public override XPathSequence Evaluate (XPathSequence iter)
  1808. {
  1809. throw new InvalidOperationException ("XQuery internal error. Should not happen.");
  1810. }
  1811. public override SequenceType StaticType {
  1812. get { throw new InvalidOperationException ("XQuery internal error. Should not happen."); }
  1813. }
  1814. public override int MinArgs {
  1815. get { throw new InvalidOperationException ("XQuery internal error. Should not happen."); }
  1816. }
  1817. public override int MaxArgs {
  1818. get { throw new InvalidOperationException ("XQuery internal error. Should not happen."); }
  1819. }
  1820. #endregion
  1821. }
  1822. #region CompileAndEvaluate
  1823. // It is instantiated per function call expression.
  1824. // (e.g. the example below contains 4 FunctionCallExpression instances:
  1825. // "replace(node-name (node-before(/*)), 'foo', node-name($var))"
  1826. internal class CustomFunctionCallExpr : FunctionCallExprBase
  1827. {
  1828. public CustomFunctionCallExpr (ExprSequence args, XQueryFunction function)
  1829. : base (function.Name, args)
  1830. {
  1831. this.function = function;
  1832. }
  1833. XQueryFunction function;
  1834. public XQueryFunction Function {
  1835. get { return function; }
  1836. }
  1837. public override int MinArgs {
  1838. get { return function.MinArgs; }
  1839. }
  1840. public override int MaxArgs {
  1841. get { return function.MaxArgs; }
  1842. }
  1843. public override SequenceType StaticType {
  1844. get { return function.ReturnType; }
  1845. }
  1846. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  1847. {
  1848. CheckArguments (compiler);
  1849. for (int i = 0; i < Args.Count; i++)
  1850. Args [i] = Args [i].Compile (compiler);
  1851. return this;
  1852. }
  1853. public override XPathSequence Evaluate (XPathSequence iter)
  1854. {
  1855. return Function.Evaluate (iter, Args);
  1856. }
  1857. // FIXME: add all overrides that delegates to XQueryFunction
  1858. }
  1859. #endregion
  1860. // Ordered / Unordered
  1861. internal class OrderSpecifiedExpr : ExprSingle
  1862. {
  1863. bool ordered;
  1864. ExprSequence expr;
  1865. public OrderSpecifiedExpr (ExprSequence expr, bool ordered)
  1866. {
  1867. this.ordered = ordered;
  1868. this.expr = expr;
  1869. }
  1870. public ExprSequence Expr {
  1871. get { return expr; }
  1872. }
  1873. public bool Ordered {
  1874. get { return ordered; }
  1875. }
  1876. internal override void CheckReference (XQueryASTCompiler compiler)
  1877. {
  1878. expr.CheckReference (compiler);
  1879. }
  1880. #region CompileAndEvaluate
  1881. public override SequenceType StaticType {
  1882. // FIXME: could be optimized by checking all the expressions
  1883. get { return SequenceType.AnyType; }
  1884. }
  1885. public override bool RequireSorting {
  1886. get { return Ordered; }
  1887. }
  1888. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  1889. {
  1890. for (int i = 0; i < Expr.Count; i++)
  1891. Expr [i] = Expr [i].Compile (compiler);
  1892. return this;
  1893. }
  1894. public override XPathSequence Evaluate (XPathSequence iter)
  1895. {
  1896. throw new NotImplementedException ();
  1897. }
  1898. #endregion
  1899. }
  1900. }
  1901. #endif