The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

732 lines
23KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. #pragma once
  19. //==============================================================================
  20. struct ClassDatabase
  21. {
  22. //==============================================================================
  23. struct MemberInfo
  24. {
  25. enum CodeLocationType
  26. {
  27. declaration = 0,
  28. addedToParent,
  29. setBoundsParamX,
  30. setBoundsParamY,
  31. setBoundsParamW,
  32. setBoundsParamH,
  33. // WARNING! When you change any of these, also update the copy that lives in the live editing code
  34. numCodeLocationTypes
  35. };
  36. MemberInfo() {}
  37. MemberInfo (const MemberInfo& other)
  38. : name (other.name), type (other.type)
  39. {
  40. for (int i = 0; i < numCodeLocationTypes; ++i)
  41. locations[i] = other.locations[i];
  42. }
  43. MemberInfo (const String& nm, const String& ty)
  44. : name (nm), type (ty)
  45. {
  46. }
  47. MemberInfo (const ValueTree& v)
  48. : name (v [Ids::name].toString()),
  49. type (v [Ids::class_].toString())
  50. {
  51. for (int i = 0; i < numCodeLocationTypes; ++i)
  52. locations[i] = v [getIdentifierForCodeLocationType (i)].toString();
  53. }
  54. const String& getName() const { return name; }
  55. const String& getType() const { return type; }
  56. const SourceCodeRange& getLocation (CodeLocationType t) const
  57. {
  58. return locations[t];
  59. }
  60. void setLocation (CodeLocationType t, const SourceCodeRange& range)
  61. {
  62. locations[t] = range;
  63. }
  64. void mergeWith (const MemberInfo& other)
  65. {
  66. jassert (name == other.name);
  67. if (other.type.isNotEmpty())
  68. type = other.type;
  69. for (int i = 0; i < numCodeLocationTypes; ++i)
  70. if (other.locations[i].isValid())
  71. locations[i] = other.locations[i];
  72. }
  73. void nudgeAllCodeRanges (const String& file, const int insertPoint, const int delta)
  74. {
  75. for (int i = 0; i < numCodeLocationTypes; ++i)
  76. locations[i].nudge (file, insertPoint, delta);
  77. }
  78. void fileContentChanged (const String& file)
  79. {
  80. for (int i = 0; i < numCodeLocationTypes; ++i)
  81. locations[i].fileContentChanged (file);
  82. }
  83. ValueTree toValueTree() const
  84. {
  85. ValueTree m (Ids::MEMBER);
  86. m.setProperty (Ids::name, name, nullptr);
  87. m.setProperty (Ids::class_, type, nullptr);
  88. for (int i = 0; i < numCodeLocationTypes; ++i)
  89. locations[i].writeToValueTree (m, getIdentifierForCodeLocationType (i));
  90. return m;
  91. }
  92. private:
  93. String name, type;
  94. SourceCodeRange locations [numCodeLocationTypes];
  95. static Identifier getIdentifierForCodeLocationType (int typeIndex)
  96. {
  97. // (These need to remain in order)
  98. static_assert (setBoundsParamX + 1 == setBoundsParamY && setBoundsParamY + 1 == setBoundsParamW
  99. && setBoundsParamW + 1 == setBoundsParamH, "");
  100. static const Identifier ids[] =
  101. {
  102. "declaration",
  103. "addedToParent",
  104. "setBoundsParamX",
  105. "setBoundsParamY",
  106. "setBoundsParamW",
  107. "setBoundsParamH"
  108. };
  109. return ids [typeIndex];
  110. }
  111. };
  112. //==============================================================================
  113. struct MethodInfo
  114. {
  115. MethodInfo() {}
  116. MethodInfo (const MethodInfo& other)
  117. : name (other.name), returnType (other.returnType),
  118. declaration (other.declaration), definition (other.definition),
  119. numArgs (other.numArgs), flags (other.flags)
  120. {
  121. }
  122. String name, returnType;
  123. SourceCodeRange declaration, definition;
  124. int numArgs, flags;
  125. enum
  126. {
  127. isConstructor = 1,
  128. isDefaultConstructor = 2,
  129. isTemplated = 4,
  130. isPublic = 8
  131. };
  132. MethodInfo (const ValueTree& v)
  133. : name (v[Ids::name].toString()),
  134. returnType (v[Ids::returnType].toString()),
  135. declaration (v[Ids::declaration].toString()),
  136. definition (v[Ids::definition].toString()),
  137. numArgs (v[Ids::numArgs]),
  138. flags (v[Ids::flags])
  139. {
  140. }
  141. ValueTree toValueTree() const
  142. {
  143. ValueTree m (Ids::METHOD);
  144. m.setProperty (Ids::name, name, nullptr);
  145. m.setProperty (Ids::returnType, returnType, nullptr);
  146. m.setProperty (Ids::numArgs, numArgs, nullptr);
  147. m.setProperty (Ids::flags, flags, nullptr);
  148. declaration.writeToValueTree (m, Ids::declaration);
  149. definition.writeToValueTree (m, Ids::definition);
  150. return m;
  151. }
  152. void nudgeAllCodeRanges (const String& file, const int insertPoint, const int delta)
  153. {
  154. declaration.nudge (file, insertPoint, delta);
  155. definition.nudge (file, insertPoint, delta);
  156. }
  157. void fileContentChanged (const String& file)
  158. {
  159. declaration.fileContentChanged (file);
  160. definition.fileContentChanged (file);
  161. }
  162. };
  163. //==============================================================================
  164. struct InstantiationFlags
  165. {
  166. InstantiationFlags()
  167. : isAbstract (false),
  168. inAnonymousNamespace (false),
  169. noDefaultConstructor (false)
  170. {}
  171. bool canBeInstantiated() const noexcept
  172. {
  173. return ! (isAbstract || inAnonymousNamespace || noDefaultConstructor);
  174. }
  175. String getReasonForUnavailability() const
  176. {
  177. if (isAbstract) return "This class is abstract";
  178. if (noDefaultConstructor) return "This class has no default constructor";
  179. if (inAnonymousNamespace) return "This class is declared inside an anonymous namespace";
  180. return String();
  181. }
  182. bool isDisallowed (const InstantiationFlags& disallowedFlags) const
  183. {
  184. return ! ((disallowedFlags.isAbstract && isAbstract)
  185. || (disallowedFlags.inAnonymousNamespace && inAnonymousNamespace)
  186. || (disallowedFlags.noDefaultConstructor && noDefaultConstructor));
  187. }
  188. bool isAbstract;
  189. bool inAnonymousNamespace;
  190. bool noDefaultConstructor;
  191. };
  192. //==============================================================================
  193. struct Class
  194. {
  195. Class() {}
  196. ~Class() {}
  197. Class (const Class& other)
  198. : className (other.className), members (other.members),
  199. methods (other.methods), classDeclaration (other.classDeclaration),
  200. instantiationFlags (other.instantiationFlags)
  201. {
  202. }
  203. Class (const String& name, const InstantiationFlags& flags,
  204. const Array<MemberInfo>& m,
  205. const Array<MethodInfo>& meth,
  206. const SourceCodeRange& classDeclarationRange)
  207. : className (name),
  208. members (m), methods (meth),
  209. classDeclaration (classDeclarationRange),
  210. instantiationFlags (flags)
  211. {
  212. }
  213. Class& operator= (const Class& other)
  214. {
  215. className = other.className;
  216. members = other.members;
  217. methods = other.methods;
  218. classDeclaration = other.classDeclaration;
  219. instantiationFlags = other.instantiationFlags;
  220. return *this;
  221. }
  222. const String& getName() const noexcept { return className; }
  223. const InstantiationFlags& getInstantiationFlags() const
  224. {
  225. return instantiationFlags;
  226. }
  227. void setInstantiationFlags (const InstantiationFlags& newFlags)
  228. {
  229. instantiationFlags = newFlags;
  230. }
  231. const SourceCodeRange& getClassDeclarationRange() const
  232. {
  233. return classDeclaration;
  234. }
  235. const MemberInfo* findMember (const String& memberName) const
  236. {
  237. for (auto& m : members)
  238. if (m.getName() == memberName)
  239. return &m;
  240. return nullptr;
  241. }
  242. MemberInfo* findMember (const String& memberName)
  243. {
  244. return const_cast<MemberInfo*> (static_cast<const Class&>(*this).findMember (memberName));
  245. }
  246. const MethodInfo* getDefaultConstructor() const
  247. {
  248. for (const MethodInfo& m : methods)
  249. if ((m.flags & MethodInfo::isDefaultConstructor) != 0)
  250. return &m;
  251. return nullptr;
  252. }
  253. const MethodInfo* getConstructor() const
  254. {
  255. if (const MethodInfo* m = getDefaultConstructor())
  256. return m;
  257. for (const MethodInfo& m : methods)
  258. if ((m.flags & MethodInfo::isConstructor) != 0)
  259. return &m;
  260. return nullptr;
  261. }
  262. const MethodInfo* getResizedMethod() const
  263. {
  264. for (const MethodInfo& m : methods)
  265. if (m.name == "resized" && m.numArgs == 0)
  266. return &m;
  267. return nullptr;
  268. }
  269. File getMainSourceFile() const
  270. {
  271. if (const MethodInfo* m = getResizedMethod())
  272. if (m->definition.isValid())
  273. return m->definition.file;
  274. if (const MethodInfo* m = getConstructor())
  275. if (m->definition.isValid())
  276. return m->definition.file;
  277. for (auto& m : methods)
  278. if (m.definition.isValid() && File (m.definition.file).hasFileExtension ("cpp;mm"))
  279. return m.definition.file;
  280. for (auto& m : methods)
  281. if ((m.flags & MethodInfo::isConstructor) != 0 && m.definition.isValid())
  282. return m.definition.file;
  283. for (auto& m : methods)
  284. if (m.definition.isValid() && File (m.definition.file).exists())
  285. return m.definition.file;
  286. return {};
  287. }
  288. Array<File> getAllSourceFiles() const
  289. {
  290. Array<File> files;
  291. for (const MethodInfo& m : methods)
  292. {
  293. files.addIfNotAlreadyThere (m.declaration.file);
  294. files.addIfNotAlreadyThere (m.definition.file);
  295. }
  296. return files;
  297. }
  298. bool isDeclaredInFile (const File& file) const
  299. {
  300. return file == classDeclaration.file;
  301. }
  302. void mergeWith (const Class& other)
  303. {
  304. jassert (*this == other);
  305. if (other.classDeclaration.isValid())
  306. classDeclaration = other.classDeclaration;
  307. for (auto& m : other.members)
  308. {
  309. if (auto* existing = findMember (m.getName()))
  310. existing->mergeWith (m);
  311. else
  312. members.add (m);
  313. }
  314. }
  315. void nudgeAllCodeRanges (const String& file, int index, int delta)
  316. {
  317. for (MemberInfo& m : members) m.nudgeAllCodeRanges (file, index, delta);
  318. for (MethodInfo& m : methods) m.nudgeAllCodeRanges (file, index, delta);
  319. classDeclaration.nudge (file, index, delta);
  320. }
  321. void fileContentChanged (const String& file)
  322. {
  323. for (MemberInfo& m : members) m.fileContentChanged (file);
  324. for (MethodInfo& m : methods) m.fileContentChanged (file);
  325. classDeclaration.fileContentChanged (file);
  326. }
  327. Class (const ValueTree& v)
  328. {
  329. className = v[Ids::name];
  330. instantiationFlags.isAbstract = v[Ids::abstract];
  331. instantiationFlags.inAnonymousNamespace = v[Ids::anonymous];
  332. instantiationFlags.noDefaultConstructor = v[Ids::noDefConstructor];
  333. classDeclaration = v [Ids::classDecl].toString();
  334. for (int i = 0; i < v.getNumChildren(); ++i)
  335. members.add (MemberInfo (v.getChild(i)));
  336. }
  337. ValueTree toValueTree() const
  338. {
  339. ValueTree v (Ids::CLASS);
  340. v.setProperty (Ids::name, className, nullptr);
  341. v.setProperty (Ids::abstract, instantiationFlags.isAbstract, nullptr);
  342. v.setProperty (Ids::anonymous, instantiationFlags.inAnonymousNamespace, nullptr);
  343. v.setProperty (Ids::noDefConstructor, instantiationFlags.noDefaultConstructor, nullptr);
  344. classDeclaration.writeToValueTree (v, Ids::classDecl);
  345. for (const MemberInfo& m : members)
  346. v.appendChild (m.toValueTree(), nullptr);
  347. return v;
  348. }
  349. bool operator== (const Class& other) const noexcept { return className == other.className; }
  350. bool operator!= (const Class& other) const noexcept { return ! operator== (other); }
  351. bool operator< (const Class& other) const noexcept { return className < other.className; }
  352. const Array<MemberInfo>& getMembers() const { return members; }
  353. private:
  354. String className;
  355. Array<MemberInfo> members;
  356. Array<MethodInfo> methods;
  357. SourceCodeRange classDeclaration;
  358. InstantiationFlags instantiationFlags;
  359. JUCE_LEAK_DETECTOR (Class)
  360. JUCE_DECLARE_WEAK_REFERENCEABLE (Class)
  361. };
  362. //==============================================================================
  363. struct Namespace
  364. {
  365. Namespace() : name ("Global Namespace") {}
  366. Namespace (const String& n, const String& full) : name (n), fullName (full) {}
  367. bool isEmpty() const noexcept
  368. {
  369. for (const auto& n : namespaces)
  370. if (! n.isEmpty())
  371. return false;
  372. return components.size() == 0;
  373. }
  374. int getTotalClassesAndNamespaces() const
  375. {
  376. int total = components.size();
  377. for (const auto& n : namespaces)
  378. total += n.getTotalClassesAndNamespaces();
  379. return total;
  380. }
  381. void add (const Class& c, const String::CharPointerType& localName)
  382. {
  383. auto nextDoubleColon = CharacterFunctions::find (localName, CharPointer_ASCII ("::"));
  384. if (nextDoubleColon.isEmpty())
  385. merge (c);
  386. else
  387. getOrCreateNamespace (String (localName, nextDoubleColon))->add (c, nextDoubleColon + 2);
  388. }
  389. bool containsRecursively (const Class& c) const
  390. {
  391. if (components.contains (c))
  392. return true;
  393. for (const auto& n : namespaces)
  394. if (n.containsRecursively (c))
  395. return true;
  396. return false;
  397. }
  398. const Class* findClass (const String& className) const
  399. {
  400. for (auto& c : components)
  401. if (c.getName() == className)
  402. return &c;
  403. for (auto& n : namespaces)
  404. if (auto* c = n.findClass (className))
  405. return c;
  406. return nullptr;
  407. }
  408. const MemberInfo* findClassMemberInfo (const String& className, const String& memberName) const
  409. {
  410. if (auto* classInfo = findClass (className))
  411. return classInfo->findMember (memberName);
  412. return nullptr;
  413. }
  414. void findClassesDeclaredInFile (Array<WeakReference<Class>>& results, const File& file)
  415. {
  416. for (int i = 0; i < components.size(); ++i)
  417. {
  418. auto& c = components.getReference (i);
  419. if (c.isDeclaredInFile (file))
  420. results.add (&c);
  421. }
  422. for (int i = 0; i < namespaces.size(); ++i)
  423. namespaces.getReference (i).findClassesDeclaredInFile (results, file);
  424. }
  425. void merge (const Namespace& other)
  426. {
  427. if (components.size() == 0)
  428. {
  429. components = other.components;
  430. }
  431. else
  432. {
  433. for (const auto& c : other.components)
  434. merge (c);
  435. }
  436. for (const auto& n : other.namespaces)
  437. getOrCreateNamespace (n.name)->merge (n);
  438. }
  439. void merge (const Class& c)
  440. {
  441. const int existing = components.indexOf (c);
  442. if (existing < 0)
  443. components.add (c);
  444. else
  445. components.getReference (existing).mergeWith (c);
  446. }
  447. Namespace* findNamespace (const String& targetName)
  448. {
  449. for (int i = 0; i < namespaces.size(); ++i)
  450. {
  451. auto& n = namespaces.getReference (i);
  452. if (n.name == targetName)
  453. return &n;
  454. }
  455. return nullptr;
  456. }
  457. Namespace* createNamespace (const String& newName)
  458. {
  459. namespaces.add (Namespace (newName, fullName + "::" + newName));
  460. return findNamespace (newName);
  461. }
  462. Namespace* getOrCreateNamespace (const String& newName)
  463. {
  464. if (auto* existing = findNamespace (newName))
  465. return existing;
  466. return createNamespace (newName);
  467. }
  468. void addInstantiableClasses (SortedSet<Class>& classes) const
  469. {
  470. for (const auto& c : components)
  471. if (c.getInstantiationFlags().canBeInstantiated())
  472. classes.add (c);
  473. for (const auto& n : namespaces)
  474. n.addInstantiableClasses (classes);
  475. }
  476. void swapWith (Namespace& other) noexcept
  477. {
  478. name.swapWith (other.name);
  479. components.swapWith (other.components);
  480. namespaces.swapWith (other.namespaces);
  481. }
  482. void nudgeAllCodeRanges (const String& file, int index, int delta)
  483. {
  484. for (int i = 0; i < components.size(); ++i)
  485. components.getReference (i).nudgeAllCodeRanges (file, index, delta);
  486. for (int i = 0; i < namespaces.size(); ++i)
  487. namespaces.getReference (i).nudgeAllCodeRanges (file, index, delta);
  488. }
  489. void fileContentChanged (const String& file)
  490. {
  491. for (int i = 0; i < components.size(); ++i)
  492. components.getReference (i).fileContentChanged (file);
  493. for (int i = 0; i < namespaces.size(); ++i)
  494. namespaces.getReference (i).fileContentChanged (file);
  495. }
  496. bool matches (const Namespace& other) const
  497. {
  498. if (name == other.name
  499. && components == other.components
  500. && namespaces.size() == other.namespaces.size())
  501. {
  502. for (int i = namespaces.size(); --i >= 0;)
  503. if (! namespaces.getReference (i).matches (other.namespaces.getReference (i)))
  504. return false;
  505. return true;
  506. }
  507. return false;
  508. }
  509. void getAllClassNames (StringArray& results, const InstantiationFlags& disallowedFlags) const
  510. {
  511. for (const auto& c : components)
  512. if (c.getInstantiationFlags().isDisallowed (disallowedFlags))
  513. results.add (c.getName());
  514. for (const auto& n : namespaces)
  515. n.getAllClassNames (results, disallowedFlags);
  516. }
  517. ValueTree toValueTree() const
  518. {
  519. ValueTree v (Ids::CLASSLIST);
  520. v.setProperty (Ids::name, name, nullptr);
  521. for (const auto& c : components) v.appendChild (c.toValueTree(), nullptr);
  522. for (const auto& n : namespaces) v.appendChild (n.toValueTree(), nullptr);
  523. return v;
  524. }
  525. void loadFromValueTree (const ValueTree& v)
  526. {
  527. name = v[Ids::name];
  528. for (int i = 0; i < v.getNumChildren(); ++i)
  529. {
  530. const ValueTree c (v.getChild(i));
  531. if (c.hasType (Ids::CLASS))
  532. components.add (Class (c));
  533. else if (c.hasType (Ids::CLASSLIST))
  534. createNamespace (c[Ids::name])->loadFromValueTree (c);
  535. }
  536. }
  537. bool operator== (const Namespace& other) const noexcept { return name == other.name; }
  538. bool operator!= (const Namespace& other) const noexcept { return ! operator== (other); }
  539. bool operator< (const Namespace& other) const noexcept { return name < other.name; }
  540. String name, fullName;
  541. SortedSet<Class> components;
  542. SortedSet<Namespace> namespaces;
  543. JUCE_LEAK_DETECTOR (Namespace)
  544. };
  545. struct ClassList
  546. {
  547. ClassList() {}
  548. void clear()
  549. {
  550. Namespace newNamespace;
  551. globalNamespace.swapWith (newNamespace);
  552. }
  553. void registerComp (const Class& comp)
  554. {
  555. globalNamespace.add (comp, comp.getName().getCharPointer());
  556. }
  557. void merge (const ClassList& other)
  558. {
  559. globalNamespace.merge (other.globalNamespace);
  560. }
  561. void swapWith (ClassList& other) noexcept
  562. {
  563. globalNamespace.swapWith (other.globalNamespace);
  564. }
  565. //==============================================================================
  566. ValueTree toValueTree() const
  567. {
  568. return globalNamespace.toValueTree();
  569. }
  570. static ClassList fromValueTree (const ValueTree& v)
  571. {
  572. ClassList l;
  573. l.globalNamespace.loadFromValueTree (v);
  574. return l;
  575. }
  576. Namespace globalNamespace;
  577. bool operator== (const ClassList& other) const noexcept { return globalNamespace.matches (other.globalNamespace); }
  578. bool operator!= (const ClassList& other) const noexcept { return ! operator== (other); }
  579. private:
  580. JUCE_LEAK_DETECTOR (ClassList)
  581. };
  582. };