MMRScriptVarDict.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. /**
  31. * @brief Collection of variable/function/method definitions
  32. */
  33. namespace OpenSim.Region.ScriptEngine.Yengine
  34. {
  35. public class VarDict: IEnumerable
  36. {
  37. public VarDict outerVarDict; // next outer VarDict to search
  38. public TokenDeclSDTypeClass thisClass; // this VarDict is for members of thisClass
  39. private struct ArgTypes
  40. {
  41. public TokenType[] argTypes;
  42. public bool CanBeCalledBy(TokenType[] calledBy)
  43. {
  44. if((argTypes == null) && (calledBy == null))
  45. return true;
  46. if((argTypes == null) || (calledBy == null))
  47. return false;
  48. if(argTypes.Length != calledBy.Length)
  49. return false;
  50. for(int i = argTypes.Length; --i >= 0;)
  51. {
  52. if(!TypeCast.IsAssignableFrom(argTypes[i], calledBy[i]))
  53. return false;
  54. }
  55. return true;
  56. }
  57. public override bool Equals(Object that)
  58. {
  59. if(that == null)
  60. return false;
  61. if(that.GetType() != typeof(ArgTypes))
  62. return false;
  63. TokenType[] at = this.argTypes;
  64. TokenType[] bt = ((ArgTypes)that).argTypes;
  65. if((at == null) && (bt == null))
  66. return true;
  67. if((at == null) || (bt == null))
  68. return false;
  69. if(at.Length != bt.Length)
  70. return false;
  71. for(int i = at.Length; --i >= 0;)
  72. {
  73. if(at[i].ToString() != bt[i].ToString())
  74. return false;
  75. }
  76. return true;
  77. }
  78. public override int GetHashCode()
  79. {
  80. TokenType[] at = this.argTypes;
  81. if(at == null)
  82. return -1;
  83. int hc = 0;
  84. for(int i = at.Length; --i >= 0;)
  85. {
  86. int c = (hc < 0) ? 1 : 0;
  87. hc = hc * 2 + c;
  88. hc ^= at[i].ToString().GetHashCode();
  89. }
  90. return hc;
  91. }
  92. }
  93. private struct TDVEntry
  94. {
  95. public int count;
  96. public TokenDeclVar var;
  97. }
  98. private bool isFrozen = false;
  99. private bool locals;
  100. private Dictionary<string, Dictionary<ArgTypes, TDVEntry>> master = new Dictionary<string, Dictionary<ArgTypes, TDVEntry>>();
  101. private int count = 0;
  102. private VarDict frozenLocals = null;
  103. /**
  104. * @brief Constructor.
  105. * @param locals = false: cannot be frozen, allows forward references
  106. * true: can be frozen, thus forbidding forward references
  107. */
  108. public VarDict(bool locals)
  109. {
  110. this.locals = locals;
  111. }
  112. /**
  113. * @brief Add new variable to the dictionary.
  114. */
  115. public bool AddEntry(TokenDeclVar var)
  116. {
  117. if(isFrozen)
  118. {
  119. throw new Exception("var dict is frozen");
  120. }
  121. // Make sure we have a sub-dictionary based on the bare name (ie, no signature)
  122. Dictionary<ArgTypes, TDVEntry> typedic;
  123. if(!master.TryGetValue(var.name.val, out typedic))
  124. {
  125. typedic = new Dictionary<ArgTypes, TDVEntry>();
  126. master.Add(var.name.val, typedic);
  127. }
  128. // See if there is an entry in the sub-dictionary that matches the argument signature.
  129. // Note that fields have null argument lists.
  130. // Methods always have a non-null argument list, even if only 0 entries long.
  131. ArgTypes types;
  132. types.argTypes = (var.argDecl == null) ? null : KeyTypesToStringTypes(var.argDecl.types);
  133. if(typedic.ContainsKey(types))
  134. return false;
  135. // It is unique, add to its name-specific sub-dictionary.
  136. TDVEntry entry;
  137. entry.count = ++count;
  138. entry.var = var;
  139. typedic.Add(types, entry);
  140. return true;
  141. }
  142. public int Count
  143. {
  144. get
  145. {
  146. return count;
  147. }
  148. }
  149. /**
  150. * @brief If this is not a local variable frame, just return the frame as is.
  151. * If this is a local variable frame, return a version that is frozen,
  152. * ie, one that does not contain any future additions.
  153. */
  154. public VarDict FreezeLocals()
  155. {
  156. // If not local var frame, return original frame as is.
  157. // This will allow forward references as the future additions
  158. // will be seen by lookups done in this dictionary.
  159. if(!locals)
  160. return this;
  161. // If local var frame, return a copy frozen at this point.
  162. // This disallows forward referenes as those future additions
  163. // will not be seen by lookups done in the frozen dictionary.
  164. if((frozenLocals == null) || (frozenLocals.count != this.count))
  165. {
  166. // Make a copy of the current var dictionary frame.
  167. // We copy a reference to the dictionary, and though it may
  168. // contain additions made after this point, those additions
  169. // will have a count .gt. frozen count and will be ignored.
  170. frozenLocals = new VarDict(true);
  171. frozenLocals.outerVarDict = this.outerVarDict;
  172. frozenLocals.thisClass = this.thisClass;
  173. frozenLocals.master = this.master;
  174. frozenLocals.count = this.count;
  175. frozenLocals.frozenLocals = frozenLocals;
  176. // Mark it as being frozen.
  177. // - assert fail if any attempt is made to add to it
  178. // - ignore any additions to the dictionary with greater count
  179. frozenLocals.isFrozen = true;
  180. }
  181. return frozenLocals;
  182. }
  183. /**
  184. * @brief Find all functions/variables that are callable
  185. * @param name = name of function/variable to look for
  186. * @param argTypes = the argument types the function is being called with
  187. * null to look for a variable
  188. * @returns null: no matching function/variable found
  189. * else: list of matching functions/variables
  190. * for variables, always of length 1
  191. */
  192. private List<TokenDeclVar> found = new List<TokenDeclVar>();
  193. public TokenDeclVar[] FindCallables(string name, TokenType[] argTypes)
  194. {
  195. argTypes = KeyTypesToStringTypes(argTypes);
  196. TokenDeclVar var = FindExact(name, argTypes);
  197. if(var != null)
  198. return new TokenDeclVar[] { var };
  199. Dictionary<ArgTypes, TDVEntry> typedic;
  200. if(!master.TryGetValue(name, out typedic))
  201. return null;
  202. found.Clear();
  203. foreach(KeyValuePair<ArgTypes, TDVEntry> kvp in typedic)
  204. {
  205. if((kvp.Value.count <= this.count) && kvp.Key.CanBeCalledBy(argTypes))
  206. {
  207. found.Add(kvp.Value.var);
  208. }
  209. }
  210. return (found.Count > 0) ? found.ToArray() : null;
  211. }
  212. public bool HasAnyExact(string name)
  213. {
  214. return master.TryGetValue(name, out Dictionary<ArgTypes, TDVEntry> typedic);
  215. }
  216. /**
  217. * @brief Find exact matching function/variable
  218. * @param name = name of function to look for
  219. * @param argTypes = argument types the function was declared with
  220. * null to look for a variable
  221. * @returns null: no matching function/variable found
  222. * else: the matching function/variable
  223. */
  224. public TokenDeclVar FindExact(string name, TokenType[] argTypes)
  225. {
  226. // Look for list of stuff that matches the given name.
  227. Dictionary<ArgTypes, TDVEntry> typedic;
  228. if(!master.TryGetValue(name, out typedic))
  229. return null;
  230. // Loop through all fields/methods declared by that name, regardless of arg signature.
  231. foreach(TDVEntry entry in typedic.Values)
  232. {
  233. if(entry.count > this.count)
  234. continue;
  235. TokenDeclVar var = entry.var;
  236. // Get argument types of declaration.
  237. // fields are always null
  238. // methods are always non-null, though may be zero-length
  239. TokenType[] declArgs = (var.argDecl == null) ? null : var.argDecl.types;
  240. // Convert any key args to string args.
  241. declArgs = KeyTypesToStringTypes(declArgs);
  242. // If both are null, they are signature-less (ie, both are fields), and so match.
  243. if((declArgs == null) && (argTypes == null))
  244. return var;
  245. // If calling a delegate, it is a match, regardless of delegate arg types.
  246. // If it turns out the arg types do not match, the compiler will give an error
  247. // trying to cast the arguments to the delegate arg types.
  248. // We don't allow overloading same field name with different delegate types.
  249. if((declArgs == null) && (argTypes != null))
  250. {
  251. TokenType fieldType = var.type;
  252. if(fieldType is TokenTypeSDTypeDelegate)
  253. return var;
  254. }
  255. // If not both null, no match, keep looking.
  256. if((declArgs == null) || (argTypes == null))
  257. continue;
  258. // Both not null, match argument types to make sure we have correct overload.
  259. int i = declArgs.Length;
  260. if(i != argTypes.Length)
  261. continue;
  262. while(--i >= 0)
  263. {
  264. string da = declArgs[i].ToString();
  265. string ga = argTypes[i].ToString();
  266. if(da == "key")
  267. da = "string";
  268. if(ga == "key")
  269. ga = "string";
  270. if(da != ga)
  271. break;
  272. }
  273. if(i < 0)
  274. return var;
  275. }
  276. // No match.
  277. return null;
  278. }
  279. /**
  280. * @brief Replace any TokenTypeKey elements with TokenTypeStr so that
  281. * it doesn't matter if functions are declared with key or string,
  282. * they will accept either.
  283. * @param argTypes = argument types as declared in source code
  284. * @returns argTypes with any key replaced by string
  285. */
  286. private static TokenType[] KeyTypesToStringTypes(TokenType[] argTypes)
  287. {
  288. if(argTypes != null)
  289. {
  290. int i;
  291. int nats = argTypes.Length;
  292. for(i = nats; --i >= 0;)
  293. {
  294. if(argTypes[i] is TokenTypeKey)
  295. break;
  296. }
  297. if(i >= 0)
  298. {
  299. TokenType[] at = new TokenType[nats];
  300. for(i = nats; --i >= 0;)
  301. {
  302. at[i] = argTypes[i];
  303. if(argTypes[i] is TokenTypeKey)
  304. {
  305. at[i] = new TokenTypeStr(argTypes[i]);
  306. }
  307. }
  308. return at;
  309. }
  310. }
  311. return argTypes;
  312. }
  313. // foreach goes through all the TokenDeclVars that were added
  314. // IEnumerable
  315. public IEnumerator GetEnumerator()
  316. {
  317. return new VarDictEnumerator(this.master, this.count);
  318. }
  319. private class VarDictEnumerator: IEnumerator
  320. {
  321. private IEnumerator masterEnum;
  322. private IEnumerator typedicEnum;
  323. private int count;
  324. public VarDictEnumerator(Dictionary<string, Dictionary<ArgTypes, TDVEntry>> master, int count)
  325. {
  326. masterEnum = master.Values.GetEnumerator();
  327. this.count = count;
  328. }
  329. // IEnumerator
  330. public void Reset()
  331. {
  332. masterEnum.Reset();
  333. typedicEnum = null;
  334. }
  335. // IEnumerator
  336. public bool MoveNext()
  337. {
  338. while(true)
  339. {
  340. if(typedicEnum != null)
  341. {
  342. while(typedicEnum.MoveNext())
  343. {
  344. if(((TDVEntry)typedicEnum.Current).count <= this.count)
  345. return true;
  346. }
  347. typedicEnum = null;
  348. }
  349. if(!masterEnum.MoveNext())
  350. return false;
  351. Dictionary<ArgTypes, TDVEntry> ctd;
  352. ctd = (Dictionary<ArgTypes, TDVEntry>)masterEnum.Current;
  353. typedicEnum = ctd.Values.GetEnumerator();
  354. }
  355. }
  356. // IEnumerator
  357. public object Current
  358. {
  359. get
  360. {
  361. return ((TDVEntry)typedicEnum.Current).var;
  362. }
  363. }
  364. }
  365. }
  366. }