MMRScriptVarDict.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. /**
  213. * @brief Find exact matching function/variable
  214. * @param name = name of function to look for
  215. * @param argTypes = argument types the function was declared with
  216. * null to look for a variable
  217. * @returns null: no matching function/variable found
  218. * else: the matching function/variable
  219. */
  220. public TokenDeclVar FindExact(string name, TokenType[] argTypes)
  221. {
  222. // Look for list of stuff that matches the given name.
  223. Dictionary<ArgTypes, TDVEntry> typedic;
  224. if(!master.TryGetValue(name, out typedic))
  225. return null;
  226. // Loop through all fields/methods declared by that name, regardless of arg signature.
  227. foreach(TDVEntry entry in typedic.Values)
  228. {
  229. if(entry.count > this.count)
  230. continue;
  231. TokenDeclVar var = entry.var;
  232. // Get argument types of declaration.
  233. // fields are always null
  234. // methods are always non-null, though may be zero-length
  235. TokenType[] declArgs = (var.argDecl == null) ? null : var.argDecl.types;
  236. // Convert any key args to string args.
  237. declArgs = KeyTypesToStringTypes(declArgs);
  238. // If both are null, they are signature-less (ie, both are fields), and so match.
  239. if((declArgs == null) && (argTypes == null))
  240. return var;
  241. // If calling a delegate, it is a match, regardless of delegate arg types.
  242. // If it turns out the arg types do not match, the compiler will give an error
  243. // trying to cast the arguments to the delegate arg types.
  244. // We don't allow overloading same field name with different delegate types.
  245. if((declArgs == null) && (argTypes != null))
  246. {
  247. TokenType fieldType = var.type;
  248. if(fieldType is TokenTypeSDTypeDelegate)
  249. return var;
  250. }
  251. // If not both null, no match, keep looking.
  252. if((declArgs == null) || (argTypes == null))
  253. continue;
  254. // Both not null, match argument types to make sure we have correct overload.
  255. int i = declArgs.Length;
  256. if(i != argTypes.Length)
  257. continue;
  258. while(--i >= 0)
  259. {
  260. string da = declArgs[i].ToString();
  261. string ga = argTypes[i].ToString();
  262. if(da == "key")
  263. da = "string";
  264. if(ga == "key")
  265. ga = "string";
  266. if(da != ga)
  267. break;
  268. }
  269. if(i < 0)
  270. return var;
  271. }
  272. // No match.
  273. return null;
  274. }
  275. /**
  276. * @brief Replace any TokenTypeKey elements with TokenTypeStr so that
  277. * it doesn't matter if functions are declared with key or string,
  278. * they will accept either.
  279. * @param argTypes = argument types as declared in source code
  280. * @returns argTypes with any key replaced by string
  281. */
  282. private static TokenType[] KeyTypesToStringTypes(TokenType[] argTypes)
  283. {
  284. if(argTypes != null)
  285. {
  286. int i;
  287. int nats = argTypes.Length;
  288. for(i = nats; --i >= 0;)
  289. {
  290. if(argTypes[i] is TokenTypeKey)
  291. break;
  292. }
  293. if(i >= 0)
  294. {
  295. TokenType[] at = new TokenType[nats];
  296. for(i = nats; --i >= 0;)
  297. {
  298. at[i] = argTypes[i];
  299. if(argTypes[i] is TokenTypeKey)
  300. {
  301. at[i] = new TokenTypeStr(argTypes[i]);
  302. }
  303. }
  304. return at;
  305. }
  306. }
  307. return argTypes;
  308. }
  309. // foreach goes through all the TokenDeclVars that were added
  310. // IEnumerable
  311. public IEnumerator GetEnumerator()
  312. {
  313. return new VarDictEnumerator(this.master, this.count);
  314. }
  315. private class VarDictEnumerator: IEnumerator
  316. {
  317. private IEnumerator masterEnum;
  318. private IEnumerator typedicEnum;
  319. private int count;
  320. public VarDictEnumerator(Dictionary<string, Dictionary<ArgTypes, TDVEntry>> master, int count)
  321. {
  322. masterEnum = master.Values.GetEnumerator();
  323. this.count = count;
  324. }
  325. // IEnumerator
  326. public void Reset()
  327. {
  328. masterEnum.Reset();
  329. typedicEnum = null;
  330. }
  331. // IEnumerator
  332. public bool MoveNext()
  333. {
  334. while(true)
  335. {
  336. if(typedicEnum != null)
  337. {
  338. while(typedicEnum.MoveNext())
  339. {
  340. if(((TDVEntry)typedicEnum.Current).count <= this.count)
  341. return true;
  342. }
  343. typedicEnum = null;
  344. }
  345. if(!masterEnum.MoveNext())
  346. return false;
  347. Dictionary<ArgTypes, TDVEntry> ctd;
  348. ctd = (Dictionary<ArgTypes, TDVEntry>)masterEnum.Current;
  349. typedicEnum = ctd.Values.GetEnumerator();
  350. }
  351. }
  352. // IEnumerator
  353. public object Current
  354. {
  355. get
  356. {
  357. return ((TDVEntry)typedicEnum.Current).var;
  358. }
  359. }
  360. }
  361. }
  362. }