public NamespaceBuilder (string sname, TypeResolveContext ctxt) { if (sname == null) throw new ArgumentNullException ("sname"); if (ctxt == null) throw new ArgumentNullException ("ctxt"); this.sname = sname; trc = ctxt; trc.SetNamespace (RealName); Type t = ctxt.Driver.LookupExistingFQN (StructureName); if (t != null) { paramstype = new UserType (t); nsparams = new ExistingNamespaceParams (t); } else { paramstype = new UserType (StructureName); // will be defined either by parameters {} or at end // of parsing the namespace. // FIXME: if we have two separate namespace {} sections, // I think they will both make their own StructureBuilder // instance, and those will clash. nsparams = null; } }
public StructureBuilder (NamespaceBuilder ns, CodeLinePragma loc, TypeAttributes attr) : base (BundleManagerBase.DefaultStructureClass, ns, loc, attr) { BaseClass = new UserType (typeof (StructureTemplate)); ns.SetUserParams (this); }
public FieldInfo (int idx, UserType type, string name, CodeLinePragma line) { Idx = idx; Type = type; Name = name; Line = line; }
public ArgInfo (int idx, UserType type, ArgCount count, string name, bool is_default, bool is_ordered) { Idx = idx; Type = type; Count = count; Name = name; switch (count) { case ArgCount.ZeroOrMore: Flags = new CodeBinaryOperatorExpression (Optional, Or, Multi); break; case ArgCount.OneOrMore: Flags = Multi; break; case ArgCount.Optional: Flags = Optional; break; case ArgCount.Standard: Flags = Standard; break; default: throw new Exception ("Unsupported ArgCount kind " + Count.ToString ()); } if (is_ordered) { Flags = new CodeBinaryOperatorExpression (Flags, Or, Ordered); if (is_default) Flags = new CodeBinaryOperatorExpression (Flags, Or, DefOrd); } else if (is_default) { Flags = new CodeBinaryOperatorExpression (Flags, Or, Default); } }
public void AddInterfaceImpl (UserType itype) { if (implements == null) implements = new List<UserType> (); implements.Add (itype); }
public TheResult (string name, NamespaceBuilder ns, string ename, CodeLinePragma loc, TypeAttributes attr) : base (name, ns, loc, attr) { etype = new UserType (ename); BaseClass = new UserType (typeof (EnumResult<>)); BaseClass.AddTypeArgument (etype); }
public MetaRuleBuilder (string name, NamespaceBuilder ns, CodeLinePragma loc, TypeAttributes attr) { rb = new RuleBuilder (name, ns, loc, attr); tmpl = new RuleTemplateBuilder (name + "RTemplate", rb, ns, loc, attr); ns.AddMetaRule (this); BaseClass = RuleType; }
public void AddStructureParam (UserType type, string name, string dflt) { if (sparams.ContainsKey (name)) throw ExHelp.App ("Redefinition of structure parameter `{0}'", name); sparams[name] = StructureParameterKind.Structure; structtypes[name] = type; defaults[name] = dflt; }
public void AddTypeArgument (UserType arg) { Type sys = t as Type; if (sys != null && !sys.IsGenericType) throw ExHelp.InvalidOp ("Cannot add type argument on known non-generic type {0}", sys); if (typeargs == null) typeargs = new List<UserType> (); typeargs.Add (arg); }
public override bool Resolve (TypeResolveContext trc, bool errors) { if (base.Resolve (trc, errors)) return true; if (RuleType != null) if (RuleType.Resolve (trc, errors)) return true; if (BaseClass.Equals (TTType)) return false; basestruct = BaseClass.ResolveUsedStructureType (trc, errors); return false; }
// Return an expression that yields an appropriate StructureTemplate // instance of type bstruct from our current context. stmpl is an expression // that returns an instance of the structure that we are bound to. // Typical usage: // // UserType bstruct = BaseClass.ResolveUsedStructureType (trc, errors); // ... // ctor.BaseConstructorArgs.Add (ContextualStructRef (bstruct, stmpl)); // // If bstruct is null, CDH.Null is returned -- so in the above case, we // will pass null to the base constructor, which is appropriate since it // needs no structure. public CodeExpression ContextualStructRef (UserType bstruct, CodeExpression stmpl) { if (bstruct == null) return CDH.Null; if (bstruct.Equals (NS.ParamsType)) // Sweet! We are referring to ourselves. return stmpl; // We need some nontrivial different structure // type. Search our structure for a member that points to // a structure of that type. string foundparam = null; foreach (string param in Params.Parameters) { if (Params[param] != StructureParameterKind.Structure) continue; if (!bstruct.Equals (Params.StructParamType (param))) continue; // FIXME: better model that handles this. if (foundparam != null) throw ExHelp.App ("Ambiguous structure chain: structure {0} contains two parameters " + "of type {1}: {2} and {3}", Params, bstruct, foundparam, param); foundparam = param; } if (foundparam == null) throw ExHelp.App ("Missing structure chain: structure {0} needs a parameter of type {1} " + "to allow chaining of type {2} to its base class {3}", Params, bstruct, this, BaseClass); return new CodeFieldReferenceExpression (stmpl, foundparam); }
public bool Equals (UserType other) { if (other == null) return false; if (IsUser) { if (other.IsSystem) return false; if ((t as string) != (other.t as string)) return false; } else { if (other.IsUser) return false; if (!AsSystem.Equals (other.AsSystem)) return false; } if (typeargs != null) { if (other.typeargs == null) return false; if (typeargs.Count != other.typeargs.Count) return false; for (int i = 0; i < typeargs.Count; i++) if (!typeargs[i].Equals (other.typeargs[i])) return false; } else { if (other.typeargs != null) return false; } return true; }
public UserType ResolveUsedStructureType (TypeResolveContext trc, bool errors) { if (!resolved) { if (errors) throw new InvalidOperationException (); return null; } UserType ret; if (IsUser) { // User type. Use our lookup tables StructureBoundItem sbi = trc.Driver.GetUserTypeItem ((string) t) as StructureBoundItem; if (sbi == null) { if (errors) throw ExHelp.App ("Expected structure bound item but got {0} for {1}", trc.Driver.GetUserTypeItem ((string) t), this); return null; } if (!sbi.UsesStructure) return null; ret = sbi.NS.ParamsType; } else { // System type. Use reflection. Type type = (Type) t; object[] attrs = type.GetCustomAttributes (typeof (StructureBindingAttribute), false); if (attrs.Length == 0) throw ExHelp.App ("Expected type {0} to have a StructureBindingAttribute " + "but it didn't", type); StructureBindingAttribute attr = (StructureBindingAttribute) attrs[0]; if (!attr.UsesStructure) return null; ret = new UserType (attr.StructureType); } if (ret.Resolve (trc, errors)) { if (errors) throw ExHelp.App ("Failed to resolve bound structure type {0}", ret); return null; } return ret; }
public void AddArgument (UserType type, ArgCount count, string name, bool is_default, bool is_ordered) { arguments.Add (new ArgInfo (arguments.Count, type, count, name, is_default, is_ordered)); }
// FIXME: Resurrect the 'implementation code' idea? // It was kind of useful I think. public ResultBuilder (string name, NamespaceBuilder ns, CodeLinePragma loc, TypeAttributes attr) : base (name, ns, loc, attr) { BaseClass = new UserType (typeof (Result)); }
public void AddCompositeField (bool is_default, UserType type, string name, CodeLinePragma line) { if (is_default) { if (comp_default_idx >= 0) throw new Exception ("Can't have more than one default field in a composite result!"); comp_default_idx = fields.Count; } fields.Add (new FieldInfo (fields.Count, type, name, line)); }
public void SetBuildFunc (bool is_manual, UserType ret_class, string ret_arg, string context_arg, NativeCode code) { bf_manual = is_manual; bf_ret_class = ret_class; bf_ret_arg = ret_arg; bf_context_arg = context_arg; bf_code = code; fp.Add (code); }
public static CodeParameterDeclarationExpression Param(UserType type, string name) { return(Param(type.AsCodeDom, name)); }
public bool Resolve (TypeResolveContext trc, bool errors) { if (Type.Resolve (trc, errors)) return true; arg_class = Type.ResultConversion; if (arg_class == null) { conv = RuleArgConversion.None; arg_class = Type; } else { // The arg type is some primitive type (bool, string) // that we must convert from a result wrapper Type type = Type.AsSystem; if (type == null) throw new Exception ("Can't handle convertible user types"); if (type.IsValueType) conv = RuleArgConversion.ToValueType; else conv = RuleArgConversion.ToRefType; } return arg_class.Resolve (trc, errors); }
void AddNamespaceParam (string ns) { // FIXME: we can't distinguish references to A.Foo and B.Foo! // Need a syntax for this. UserType t = new UserType (NamespaceBuilder.MakeStructureName (ns)); string paramname = ns; int i = ns.LastIndexOf ('.'); if (i >= 0) paramname = ns.Substring (i + 1); cur_nsstruct.AddStructureParam (t, paramname, ns); }
public static CodeParameterDeclarationExpression Param (UserType type, string name) { return Param (type.AsCodeDom, name); }
// Setup public bool Resolve(TypeResolveContext trc, bool errors) { // We need to resolve the rule as a template now, rather // than letting NameLookupContext do it, because NLC will // be operating with an undefined set of 'usings', and // probably won't be able to find the RTemplate associated // with the rule. if (Rule.ResolveExtension("RTemplate", trc, errors)) { return(true); } // Now we need to check that, if we have a template that // uses a structure, that our provider can access such // a structure, so that the template can actually be // instantiated. // // FIXME: This code is exactly parallel to // StructureBoundItem.ContextualStructRef. UserType ttmpl = Rule.ResolveUsedStructureType(trc, errors); if (ttmpl == null) { // Great, it's doesn't use anything, so whatever. return(false); } // XXX shouldn't apply anymore -- unbound providers are now impossible. // Would they have ever been useful? // //if (prov.Structure == null) { //if (errors) // Console.Error.WriteLine ("Target {0} in provider {1} references rule {2} that " + // "is bound to structure {3}, but the provider is not " + // "bound to a structure and so can provide no context", // this, prov, Rule, ttmpl); //return true; //} if (ttmpl.Equals(prov.NS.ParamsType)) { // It just depends on our containing structure. No problem. return(false); } foreach (string param in prov.Structure.Parameters) { if (prov.Structure[param] != StructureParameterKind.Structure) { continue; } if (ttmpl.Equals(prov.Structure.StructParamType(param))) { return(false); } } if (errors) { // WORST ERROR MESSAGE EVAR Console.Error.WriteLine("Target {0} in provider {1} references rule {2} that " + "is bound to structure {3}, but the provider's containing " + "structure {4} does not have an argument referencing that structure. " + "You probably need to add another parameter to the containing structure.", this, prov, Rule, ttmpl, prov.Structure); } return(true); }
public bool Equals(UserType other) { if (other == null) { return(false); } if (IsUser) { if (other.IsSystem) { return(false); } if ((t as string) != (other.t as string)) { return(false); } } else { if (other.IsUser) { return(false); } if (!AsSystem.Equals(other.AsSystem)) { return(false); } } if (typeargs != null) { if (other.typeargs == null) { return(false); } if (typeargs.Count != other.typeargs.Count) { return(false); } for (int i = 0; i < typeargs.Count; i++) { if (!typeargs[i].Equals(other.typeargs[i])) { return(false); } } } else { if (other.typeargs != null) { return(false); } } return(true); }
public UserType ResolveUsedStructureType(TypeResolveContext trc, bool errors) { if (!resolved) { if (errors) { throw new InvalidOperationException(); } return(null); } UserType ret; if (IsUser) { // User type. Use our lookup tables StructureBoundItem sbi = trc.Driver.GetUserTypeItem((string)t) as StructureBoundItem; if (sbi == null) { if (errors) { throw ExHelp.App("Expected structure bound item but got {0} for {1}", trc.Driver.GetUserTypeItem((string)t), this); } return(null); } if (!sbi.UsesStructure) { return(null); } ret = sbi.NS.ParamsType; } else { // System type. Use reflection. Type type = (Type)t; object[] attrs = type.GetCustomAttributes(typeof(StructureBindingAttribute), false); if (attrs.Length == 0) { throw ExHelp.App("Expected type {0} to have a StructureBindingAttribute " + "but it didn't", type); } StructureBindingAttribute attr = (StructureBindingAttribute)attrs[0]; if (!attr.UsesStructure) { return(null); } ret = new UserType(attr.StructureType); } if (ret.Resolve(trc, errors)) { if (errors) { throw ExHelp.App("Failed to resolve bound structure type {0}", ret); } return(null); } return(ret); }
public UserType ResolveBoundRuleType (TypeResolveContext trc, bool errors) { if (!resolved) { if (errors) throw new InvalidOperationException (); return null; } UserType ret; if (IsUser) { // User type. Use our lookup tables RuleTemplateBuilder rtb = trc.Driver.GetUserTypeItem ((string) t) as RuleTemplateBuilder; if (errors && rtb == null) throw ExHelp.App ("Expected a rule template but got {0} for {1}", trc.Driver.GetUserTypeItem ((string) t), this); ret = new UserType (rtb.Rule.FullName); } else { // System type. Use reflection. Type type = (Type) t; object[] attrs = type.GetCustomAttributes (typeof (RuleBindingAttribute), false); if (errors && attrs.Length == 0) throw ExHelp.App ("Expected type {0} to have a RuleBindingAttribute " + "but it didn't. This type is not allowed to be a rule baseclass.", type); ret = new UserType (((RuleBindingAttribute) attrs[0]).RuleType); } if (ret.Resolve (trc, errors)) { if (errors) throw ExHelp.App ("Failed to resolve bound rule type {0}", ret); return null; } return ret; }
public MatcherBuilder (NamespaceBuilder ns, CodeLinePragma loc) : base ("RegexMatcher", ns, loc, TypeAttributes.Public) { BaseClass = new UserType (typeof (RegexMatcher)); serial = matcher_serial++; }
public static void EmitGeneralResult (CodeTypeDeclaration ctd, UserType rtype) { CodeMemberProperty grt = new CodeMemberProperty (); grt.Name = "GeneralResultType"; grt.Attributes = MemberAttributes.Public | MemberAttributes.Override; grt.Type = CDH.Type; grt.HasGet = true; grt.HasSet = false; CodeExpression rt = new CodeTypeOfExpression (rtype.AsCodeDom); grt.GetStatements.Add (new CodeMethodReturnStatement (rt)); ctd.Members.Add (grt); }
public SourcefileRuleBuilder (string name, NamespaceBuilder ns, CodeLinePragma loc, TypeAttributes attr) : base (name, ns, loc, attr) { BaseClass = new UserType (typeof (SourcefileRule)); fp.Add (name); }
public UserType ResolveSelfType (TypeResolveContext trc, bool errors) { UserType ut = new UserType (FullName); if (ut.Resolve (trc, errors) && errors) throw ExHelp.App ("Failed to resolve SelfType of {0}?", this); return ut; }
// FIXME: Resurrect the 'implementation code' idea? // It was kind of useful I think. public ResultBuilder(string name, NamespaceBuilder ns, CodeLinePragma loc, TypeAttributes attr) : base(name, ns, loc, attr) { BaseClass = new UserType(typeof(Result)); }
public SourcefileRuleBuilder(string name, NamespaceBuilder ns, CodeLinePragma loc, TypeAttributes attr) : base(name, ns, loc, attr) { BaseClass = new UserType(typeof(SourcefileRule)); fp.Add(name); }