nany
atom.h
1 #pragma once
2 #include "libnanyc.h"
3 #include <yuni/core/smartptr/intrusive.h>
4 #include <yuni/core/string.h>
5 #include <yuni/core/noncopyable.h>
6 #include <yuni/core/flags.h>
7 #include <yuni/core/tribool.h>
8 #include "details/utils/clid.h"
9 #include "vardef.h"
10 #include "nany/nany.h"
11 #include "details/ir/fwd.h"
12 #include "signature.h"
13 #include <unordered_map>
14 #include <unordered_set>
15 #include <map>
16 #include "libnanyc-config.h"
17 #include "details/atom/classdef.h"
18 
19 
20 
21 namespace ny {
22 
23 
24 struct AtomMap;
25 struct ClassdefTable;
26 struct ClassdefTableView;
27 namespace ir { struct Sequence; }
28 
29 
30 } // namespace ny
31 
32 
33 namespace ny {
34 
35 
37 struct Atom final
38  : public Yuni::IIntrusiveSmartPtr<Atom, false, Yuni::Policy::SingleThreaded>
39  , Yuni::NonCopyable<Atom> {
40 
41  enum class Type : yuint8 {
43  namespacedef,
45  funcdef,
47  classdef,
49  vardef,
51  typealias,
53  unit,
54  };
55 
56  enum class Flags : uint32_t {
58  suggestInReport,
60  captureVariables,
62  error,
64  pushCapturedVariables,
66  recursive,
67 
69  instanciating,
70  };
71 
72  template<Flags F> struct FlagAutoSwitch final {
73  FlagAutoSwitch(Atom& atom) : atom(atom) {
74  atom.flags += F;
75  }
76  ~FlagAutoSwitch() {
77  atom.flags -= F;
78  }
79  Atom& atom;
80  };
81 
82  enum class Category : uint32_t {
84  special,
86  funcoperator,
88  view,
90  classParent,
92  ctor,
94  clone,
96  dtor,
98  defvarInit,
100  functor,
102  capturedVar,
104  propget,
106  propset,
108  propsetCustom,
110  unittest,
111  };
112 
113  struct Parameters final {
115  uint size() const;
117  bool empty() const;
119  bool append(const CLID&, const AnyString& name);
121  uint32_t findByName(const AnyString& name, uint32_t offset = 0) const;
122 
123  const std::pair<AnyString, Vardef>& operator [] (uint index) const;
124 
125  void swap(Parameters&);
126 
128  const AnyString& name(uint index) const;
130  const Vardef& vardef(uint index) const;
131 
133  // \param callbakc void()(uint32_t index, const AnyString& name, const Vardef& vardef)
134  template<class T> void each(const T& callback) const;
135 
137  // \TODO ishortcircuitvalue: this property is only used twice...
138  bool shortcircuitValue = false;
139 
140  private:
141  struct Data final {
142  uint32_t count = 0u;
143  std::pair<AnyString, Vardef> params[config::maxFuncDeclParameterCount];
144  };
145  std::unique_ptr<Data> pData;
146  };
147 
148 public:
150  static void extractNames(AnyString& keyword, AnyString& varname, const AnyString& name);
151 
152 public:
153  explicit Atom(const AnyString& name, Type type);
154  explicit Atom(Atom& rootparent, const AnyString& name, Type type);
155  ~Atom();
156 
158 
159  AnyString name() const;
162  CLID clid() const;
163 
165  bool isNamespace() const;
167  bool isClass() const;
169  bool isFunction() const;
171  bool isOperator() const;
173  bool isSpecial() const;
175  bool isView() const;
177  bool isMemberVariable() const;
179  bool isClassMember() const;
181  bool isAnonymousClass() const;
183  bool isMemberVarDefaultInit() const;
185  bool isTypeAlias() const;
187  bool isUnit() const;
189  bool isCtor() const;
191  bool isDtor() const;
193  bool isCloneCtor() const;
195  bool isFunctor() const;
197  bool callable() const;
199  bool isCapturedVariable() const;
201  bool isProperty() const;
203  bool isPropertyGet() const;
205  bool isPropertySet() const;
207  bool isPropertySetCustom() const;
209  bool isUnittest() const;
210 
212  bool isPublicOrPublished() const;
213 
215  bool hasReturnType() const;
216 
218  bool hasGenericParameters() const;
219 
224  bool isContextual() const;
225 
227  bool canAccessTo(const Atom&) const;
228 
230  bool canCaptureVariabes() const;
231 
233  Atom* parentScope();
234 
236  const Atom* parentScope() const;
237 
245  bool nameLookupFromParentScope(std::vector<std::reference_wrapper<Atom>>& list, const AnyString& name);
246 
254  bool nameLookupOnChildren(std::vector<std::reference_wrapper<Atom>>& list, const AnyString& name,
255  bool* singleHop = nullptr);
256 
264  bool propertyLookupOnChildren(std::vector<std::reference_wrapper<Atom>>& list,
265  const AnyString& prefix, const AnyString& name);
266 
268  bool hasMember(const AnyString& name) const;
269 
280  template<class C> void eachChild(const C& callback);
281  template<class C> void eachChild(const C& callback) const;
282 
293  template<class C> void eachChild(const AnyString& needle, const C& callback);
294  template<class C> void eachChild(const AnyString& needle, const C& callback) const;
295 
296  uint32_t findClassAtom(Atom*& out, const AnyString& name);
297  uint32_t findFuncAtom(Atom*& out, const AnyString& name);
298  uint32_t findVarAtom(Atom*& out, const AnyString& name);
299  Atom* findNamespaceAtom(const AnyString& name);
300 
301  const Atom* findChildByMemberFieldID(uint32_t field) const;
302 
309  void renameChild(const AnyString& from, const AnyString& to);
310 
314  bool findParent(const Atom& atom) const;
315 
317  uint32_t childrenCount() const;
318 
320  bool hasChildren() const;
322 
323 
325 
326  uint64_t runtimeSizeof() const;
329 
331 
332  YString fullname() const;
335  void retrieveFullname(YString& out, const ClassdefTableView* table = nullptr,
336  bool parentName = true) const;
337 
339  // \internal mainly used for convenience and debugging purposes
340  YString caption() const;
342  // \internal mainly used for convenience and debugging purposes
343  YString caption(const ClassdefTableView&) const;
345  void retrieveCaption(YString& out, const ClassdefTableView& table) const;
346 
348  void printTree(const ClassdefTableView&) const;
349 
357  AnyString keyword() const;
359 
360 
361 public:
362  struct Instances final {
363  struct Ref final {
364  Ref(Instances& ref, uint32_t index): m_ref(ref), m_index(index) {}
366  ir::Sequence& ircode();
368  ir::Sequence* ircodeIfExists();
370  AnyString symbolname() const;
372  uint32_t id() const;
376  void update(YString&& symbol, const Classdef& rettype);
378  uint32_t invalidate(const Signature&);
379 
380  private:
381  Instances& m_ref;
382  uint32_t m_index;
383  };
384 
386  YString print(const AtomMap&) const;
387 
391  Ref create(const Signature& signature, Atom* remapAtom);
392 
394  Yuni::Tribool::Value isValid(const Signature& signature, uint32_t& iid, Classdef&, Atom*& remapAtom) const;
395 
397  uint32_t size() const;
398 
400  Ref operator [] (uint32_t index);
401 
402  private:
403  void update(uint32_t index, Yuni::String&& symbol, const Classdef& rettype);
404  void invalidate(uint32_t index, const Signature&);
405 
406  struct Metadata final {
407  std::unique_ptr<ir::Sequence> ircode;
408  Classdef rettype;
409  Atom* remapAtom = nullptr;
410  Yuni::String symbol;
411  };
413  std::vector<Metadata> m_instances;
415  std::unordered_map<Signature, uint32_t> m_instancesIDs;
416  }
417  instances;
418 
420  uint32_t atomid = 0u;
422  Yuni::Flags<Flags> flags = {Flags::suggestInReport};
424  Yuni::Flags<Category> category;
425 
427  struct {
429  // \note This value is used internally for mapping
430  uint16_t fieldindex = 0;
432  uint16_t effectiveFieldIndex = 0;
433  }
434  varinfo;
435 
437  struct {
439  bool isInstanciated = false;
441  // (can be used to retrieve the generic type parameter index when the atom is a typedef)
442  uint16_t nextFieldIndex = 0;
443 
445  struct {
446  uint32_t atomid = 0;
447  uint32_t instanceid = (uint32_t) - 1;
448  } dtor;
449 
451  struct {
452  uint32_t atomid = 0;
453  uint32_t instanceid = (uint32_t) - 1;
454  } clone;
455  }
456  classinfo;
457 
459  const Type type;
461  nyvisibility_t visibility = nyv_public;
463  Atom* parent = nullptr;
464 
466  struct {
468  YString filename;
470  uint32_t line = 0;
472  uint32_t offset = 0;
473  }
474  origin;
475 
485  uint32_t localVariablesCount = 0u;
487  Atom* scopeForNameResolution = nullptr;
488 
490  struct {
492  ir::Sequence* ircode = nullptr;
494  // \warning offset of the operands of the blueprint, not the opcode value
495  uint32_t offset = 0;
497  uint32_t stackSizeExtra = 0;
499  bool owned = false;
500  }
501  opcodes;
502 
504  // \TODO builtinalias: this property is only used twice
505  AnyString builtinalias;
507  nytype_t builtinMapping = nyt_void;
508 
510  std::unique_ptr<std::unordered_set<AnyString>> candidatesForCapture;
511 
512 private:
514  std::multimap<AnyString, yuni::Ref<Atom>> m_children;
516  AnyString m_name;
517  // nakama !
518  friend struct AtomMap;
519 
520 }; // class Atom
521 
522 
523 } // namespace ny
524 
525 #include "atom.hxx"
Yuni::Flags< Flags > flags
Atom flags.
Definition: atom.h:422
Vardef returnType
The return type.
Definition: atom.h:477
YString filename
File source.
Definition: atom.h:468
Yuni::Flags< Category > category
Atom Category.
Definition: atom.h:424
Parameters parameters
Parameters (for functions)
Definition: atom.h:479
Definition: clid.h:11
Definition: signature.h:15
Class definition.
Definition: classdef.h:24
Definition: atom.h:363
Definition: ast.cpp:6
Definition: vardef.h:11
const Type type
Flag to determine whether this entry is a blueprint or a namespace.
Definition: atom.h:459
Category
Definition: atom.h:82
Parameters tmplparams
Template parameters.
Definition: atom.h:481
Atoms.
Definition: atom-map.h:17
Type
Definition: atom.h:41
Definition: atom.h:72
Definition of a single class or function.
Definition: atom.h:37
Definition: atom.h:113
Parameters tmplparamsForPrinting
Template parameters just for printing informations (such as errors)
Definition: atom.h:483
std::unique_ptr< std::unordered_set< AnyString > > candidatesForCapture
List of potential candidates for being captured.
Definition: atom.h:510
Flags
Definition: atom.h:56
AnyString builtinalias
Builtin alias (empty if none)
Definition: atom.h:505
Definition: sequence.h:22
Definition: atom.h:362
Definition: classdef-table-view.h:9