1
0

MMRScriptVarDict.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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();
  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. if(!master.TryGetValue(var.name.val, out Dictionary<ArgTypes, TDVEntry> typedic))
  123. {
  124. typedic = new Dictionary<ArgTypes, TDVEntry>();
  125. master.Add(var.name.val, typedic);
  126. }
  127. // See if there is an entry in the sub-dictionary that matches the argument signature.
  128. // Note that fields have null argument lists.
  129. // Methods always have a non-null argument list, even if only 0 entries long.
  130. ArgTypes types;
  131. types.argTypes = (var.argDecl == null) ? null : KeyTypesToStringTypes(var.argDecl.types);
  132. if(typedic.ContainsKey(types))
  133. return false;
  134. // It is unique, add to its name-specific sub-dictionary.
  135. TDVEntry entry;
  136. entry.count = ++count;
  137. entry.var = var;
  138. typedic.Add(types, entry);
  139. return true;
  140. }
  141. public int Count
  142. {
  143. get
  144. {
  145. return count;
  146. }
  147. }
  148. /**
  149. * @brief If this is not a local variable frame, just return the frame as is.
  150. * If this is a local variable frame, return a version that is frozen,
  151. * ie, one that does not contain any future additions.
  152. */
  153. public VarDict FreezeLocals()
  154. {
  155. // If not local var frame, return original frame as is.
  156. // This will allow forward references as the future additions
  157. // will be seen by lookups done in this dictionary.
  158. if(!locals)
  159. return this;
  160. // If local var frame, return a copy frozen at this point.
  161. // This disallows forward referenes as those future additions
  162. // will not be seen by lookups done in the frozen dictionary.
  163. if((frozenLocals == null) || (frozenLocals.count != this.count))
  164. {
  165. // Make a copy of the current var dictionary frame.
  166. // We copy a reference to the dictionary, and though it may
  167. // contain additions made after this point, those additions
  168. // will have a count .gt. frozen count and will be ignored.
  169. frozenLocals = new VarDict(true);
  170. frozenLocals.outerVarDict = this.outerVarDict;
  171. frozenLocals.thisClass = this.thisClass;
  172. frozenLocals.master = this.master;
  173. frozenLocals.count = this.count;
  174. frozenLocals.frozenLocals = frozenLocals;
  175. // Mark it as being frozen.
  176. // - assert fail if any attempt is made to add to it
  177. // - ignore any additions to the dictionary with greater count
  178. frozenLocals.isFrozen = true;
  179. }
  180. return frozenLocals;
  181. }
  182. /**
  183. * @brief Find all functions/variables that are callable
  184. * @param name = name of function/variable to look for
  185. * @param argTypes = the argument types the function is being called with
  186. * null to look for a variable
  187. * @returns null: no matching function/variable found
  188. * else: list of matching functions/variables
  189. * for variables, always of length 1
  190. */
  191. private List<TokenDeclVar> found = new List<TokenDeclVar>();
  192. public TokenDeclVar[] FindCallables(string name, TokenType[] argTypes)
  193. {
  194. argTypes = KeyTypesToStringTypes(argTypes);
  195. TokenDeclVar var = FindExact(name, argTypes);
  196. if(var != null)
  197. return new TokenDeclVar[] { var };
  198. Dictionary<ArgTypes, TDVEntry> typedic;
  199. if(!master.TryGetValue(name, out typedic))
  200. return null;
  201. found.Clear();
  202. foreach(KeyValuePair<ArgTypes, TDVEntry> kvp in typedic)
  203. {
  204. if((kvp.Value.count <= this.count) && kvp.Key.CanBeCalledBy(argTypes))
  205. {
  206. found.Add(kvp.Value.var);
  207. }
  208. }
  209. return (found.Count > 0) ? found.ToArray() : null;
  210. }
  211. public bool HasAnyExact(string name)
  212. {
  213. return master.TryGetValue(name, out Dictionary<ArgTypes, TDVEntry> typedic);
  214. }
  215. /**
  216. * @brief Find exact matching function/variable
  217. * @param name = name of function to look for
  218. * @param argTypes = argument types the function was declared with
  219. * null to look for a variable
  220. * @returns null: no matching function/variable found
  221. * else: the matching function/variable
  222. */
  223. public TokenDeclVar FindExact(string name, TokenType[] argTypes)
  224. {
  225. // Look for list of stuff that matches the given name.
  226. Dictionary<ArgTypes, TDVEntry> typedic;
  227. if(!master.TryGetValue(name, out typedic))
  228. return null;
  229. // Loop through all fields/methods declared by that name, regardless of arg signature.
  230. foreach(TDVEntry entry in typedic.Values)
  231. {
  232. if(entry.count > this.count)
  233. continue;
  234. TokenDeclVar var = entry.var;
  235. // Get argument types of declaration.
  236. // fields are always null
  237. // methods are always non-null, though may be zero-length
  238. TokenType[] declArgs = (var.argDecl == null) ? null : var.argDecl.types;
  239. // Convert any key args to string args.
  240. declArgs = KeyTypesToStringTypes(declArgs);
  241. // If both are null, they are signature-less (ie, both are fields), and so match.
  242. if((declArgs == null) && (argTypes == null))
  243. return var;
  244. // If calling a delegate, it is a match, regardless of delegate arg types.
  245. // If it turns out the arg types do not match, the compiler will give an error
  246. // trying to cast the arguments to the delegate arg types.
  247. // We don't allow overloading same field name with different delegate types.
  248. if((declArgs == null) && (argTypes != null))
  249. {
  250. TokenType fieldType = var.type;
  251. if(fieldType is TokenTypeSDTypeDelegate)
  252. return var;
  253. }
  254. // If not both null, no match, keep looking.
  255. if((declArgs == null) || (argTypes == null))
  256. continue;
  257. // Both not null, match argument types to make sure we have correct overload.
  258. int i = declArgs.Length;
  259. if(i != argTypes.Length)
  260. continue;
  261. while(--i >= 0)
  262. {
  263. string da = declArgs[i].ToString();
  264. string ga = argTypes[i].ToString();
  265. if(da == "key")
  266. da = "string";
  267. if(ga == "key")
  268. ga = "string";
  269. if(da != ga)
  270. break;
  271. }
  272. if(i < 0)
  273. return var;
  274. }
  275. // No match.
  276. return null;
  277. }
  278. /**
  279. * @brief Replace any TokenTypeKey elements with TokenTypeStr so that
  280. * it doesn't matter if functions are declared with key or string,
  281. * they will accept either.
  282. * @param argTypes = argument types as declared in source code
  283. * @returns argTypes with any key replaced by string
  284. */
  285. private static TokenType[] KeyTypesToStringTypes(TokenType[] argTypes)
  286. {
  287. if(argTypes != null)
  288. {
  289. int i;
  290. int nats = argTypes.Length;
  291. for(i = nats; --i >= 0;)
  292. {
  293. if(argTypes[i] is TokenTypeKey)
  294. break;
  295. }
  296. if(i >= 0)
  297. {
  298. TokenType[] at = new TokenType[nats];
  299. for(i = nats; --i >= 0;)
  300. {
  301. at[i] = argTypes[i];
  302. if(argTypes[i] is TokenTypeKey)
  303. {
  304. at[i] = new TokenTypeStr(argTypes[i]);
  305. }
  306. }
  307. return at;
  308. }
  309. }
  310. return argTypes;
  311. }
  312. // foreach goes through all the TokenDeclVars that were added
  313. // IEnumerable
  314. public IEnumerator GetEnumerator()
  315. {
  316. return new VarDictEnumerator(this.master, this.count);
  317. }
  318. private class VarDictEnumerator: IEnumerator
  319. {
  320. private IEnumerator masterEnum;
  321. private IEnumerator typedicEnum;
  322. private int count;
  323. public VarDictEnumerator(Dictionary<string, Dictionary<ArgTypes, TDVEntry>> master, int count)
  324. {
  325. masterEnum = master.Values.GetEnumerator();
  326. this.count = count;
  327. }
  328. // IEnumerator
  329. public void Reset()
  330. {
  331. masterEnum.Reset();
  332. typedicEnum = null;
  333. }
  334. // IEnumerator
  335. public bool MoveNext()
  336. {
  337. while(true)
  338. {
  339. if(typedicEnum != null)
  340. {
  341. while(typedicEnum.MoveNext())
  342. {
  343. if(((TDVEntry)typedicEnum.Current).count <= this.count)
  344. return true;
  345. }
  346. typedicEnum = null;
  347. }
  348. if(!masterEnum.MoveNext())
  349. return false;
  350. Dictionary<ArgTypes, TDVEntry> ctd;
  351. ctd = (Dictionary<ArgTypes, TDVEntry>)masterEnum.Current;
  352. typedicEnum = ctd.Values.GetEnumerator();
  353. }
  354. }
  355. // IEnumerator
  356. public object Current
  357. {
  358. get
  359. {
  360. return ((TDVEntry)typedicEnum.Current).var;
  361. }
  362. }
  363. }
  364. }
  365. }