示例#1
0
文件: Type.cpp 项目: P-i-N/metagen
Type *Type::CreateFromString(Node *parent, const std::string &name)
{
  if (name.empty())
    return new Type(parent, TypePrimitive::Unknown, "__unknown");;

  size_t start = 0;
  size_t end = name.size() - 1;

  int numDereferences = 0;
  bool isReference = false;
  bool isConst = false;
  
  static const std::string constPrefix = "const ";
  if (!name.compare(0, constPrefix.size(), constPrefix))
  {
    isConst = true;
    start += constPrefix.size();
  }

  if (name[end] == '&')
  {
    isReference = true;
    --end;
  }

  while (name[end] == '*')
  {
    ++numDereferences;
    --end;

    if (!end)
      return nullptr;
  }

  while (name[start] == ' ' && start < end)
    ++start;

  while (name[end] == ' ' && end > start)
    --end;

  std::string decayedName = name.substr(start, end - start + 1);

  if (decayedName.empty())
    return nullptr;

  start = 0;
  end = decayedName.size() - 1;
  
  if (decayedName[end] == ']')
  {
    end = decayedName.find(" [");
    if (end == std::string::npos)
      return nullptr;

    decayedName = decayedName.substr(0, end);
  }

  start = 0;
  end = decayedName.size() - 1;

  if (decayedName[end] == '>')
  {
    end = decayedName.find('<');
    if (end == std::string::npos)
      return nullptr;

    decayedName = decayedName.substr(0, end);
  }

  if (decayedName.empty())
    return nullptr;

  Type *t = nullptr;

  TypePrimitive tp = TypePrimitiveFromString(decayedName);
  if (tp != TypePrimitive::Unknown)
  {
    t = new Type(parent, tp, decayedName);
  }
  else
  {
    Namespace *ns = Node::TryGetNamespace(parent);
    if (ns)
    {
      Type *complexType = ns->FindTypeByName(decayedName);
      if (complexType)
      {
        if (complexType->GetPrimitive() == TypePrimitive::Class)
          t = new Type(complexType->GetClass());
        else if (complexType->GetPrimitive() == TypePrimitive::TypeDef)
        {
      
        }
      }
    }
  }

  if (!t)
    t = new Type(parent, TypePrimitive::Unknown, decayedName);

  t->m_NumDereferences = numDereferences;
  t->m_IsReference = isReference;
  t->m_IsConst = isConst;

  return t;
}