/// <summary> /// Get class, instantiate and call constructor /// </summary> /// <param name="n"></param> public override void VisitInstantiateClass(ASTInstantiateClass n) { n.Actuals.Visit(this); var info = _typeManager.GetBuilderInfo(n.ClassName); _gen.Emit(OpCodes.Newobj, info.ConstructorBuilder.Builder); _lastWalkedType = info.Builder; }
public override void VisitInstantiateClass(ASTInstantiateClass n) { ClassDescriptor desc = _scopeMgr.GetType(n.ClassName) as ClassDescriptor; if (desc != null) { var cls = (ClassDescriptor)_scopeMgr.Find(n.ClassName, p => p is ClassDescriptor); var func = cls.Methods.Where(prop => prop.Name.Equals(n.ClassName, StringComparison.OrdinalIgnoreCase)).SingleOrDefault().Type as TypeFunction; //Check if the class we're working with has a constructor of the same name if (func != null) { CheckSubTree(n.Actuals); //check the method signature of the constructor to make sure the correct arguments are passed in ActualBuilder builder = new ActualBuilder(); n.Actuals.Visit(builder); if (func.AcceptCall(builder.Actuals)) { //hooray, the code is valid MethodDescriptor ctorDescriptor = (MethodDescriptor)_scopeMgr.Find(n.ClassName, p => p is MethodDescriptor, func.Scope); _lastSeenType = desc.Type; n.ClassDescriptor = desc; n.Descriptor = ctorDescriptor; } else { ReportError(n.Location, "Invalid parameters for constructor '{0}'", n.ClassName); } } else { ReportError(n.Location, "No constructor found for class '{0}'.", n.ClassName); } } else { ReportError(n.Location, "The name '{0}' is not a class.", n.ClassName); } }