4 #include "typeInfo.h"
\r
7 void addGenericInstantiation(
\r
8 std::vector<std::vector<Type>> & insts,
\r
9 const std::vector<Type> & newInst)
\r
13 insts.push_back(newInst);
\r
16 for (auto inst : insts)
\r
18 for (int i = 0; i < inst.size(); i++)
\r
20 if (inst[i] != newInst[i])
\r
22 insts.push_back(newInst);
\r
29 Program instantiateGenerics(const Program & p)
\r
33 // Find generic instantiations
\r
35 Visitor findGenericInstantiations;
\r
36 findGenericInstantiations.onExpr =
\r
37 [&](const Expr & e, const std::shared_ptr<Context> ctx)
\r
39 if (e.type == ExprType::Func && !e._func.genericInstantiation.empty())
\r
41 auto f = findFunctionPtr(e._func.functionName, e._func.namespacePrefixes, ctx);
\r
44 if (std::get<0>(*f)->genericTypeNames.empty())
\r
45 throw "Trying to instantiate non-generic function";
\r
46 if (e._func.genericInstantiation.size() != std::get<0>(*f)->genericTypeNames.size())
\r
47 throw "Trying to instantiate function with wrong number of types";
\r
48 addGenericInstantiation(std::get<0>(*f)->genericInstantiations, e._func.genericInstantiation);
\r
51 // TODO: generic methods
\r
53 findGenericInstantiations.onType =
\r
54 [&](const Type & t, const std::shared_ptr<Context> ctx)
\r
56 if (!t.genericInstantiation.empty())
\r
58 auto s = findStructPtr(t.name, t.namespacePrefixes, ctx);
\r
61 if (std::get<0>(*s)->genericTypeNames.empty())
\r
62 throw "Trying to instantiate non-generic struct";
\r
63 if (t.genericInstantiation.size() != std::get<0>(*s)->genericTypeNames.size())
\r
64 throw "Trying to instantiate struct with wrong number of types";
\r
65 addGenericInstantiation(std::get<0>(*s)->genericInstantiations, t.genericInstantiation);
\r
69 Visit v(findGenericInstantiations);
\r
75 std::string genericAppendix(const std::vector<Type> & ts)
\r
77 std::stringstream sstr;
\r
82 for (auto m : t.modifiers)
\r
84 if (m.type == TypeModifierType::Array)
\r
88 sstr << m._arraySize;
\r
90 else if (m.type == TypeModifierType::Pointer)
\r
95 if (!t.genericInstantiation.empty())
\r
97 sstr << genericAppendix(t.genericInstantiation);
\r