public TargetBuilder ReferenceTarget(string name) { if (targets.ContainsKey(name)) { return(targets[name]); } if (done_modifying) { throw ExHelp.InvalidOp("Cannot reference new target {0} because its " + "provider is been marked as finished", name); } TargetBuilder tb = CreateTarget(name); targets[name] = tb; return(tb); }
protected ProviderLoaderBase(string basis, string decl_loc) { if (basis == null || basis[0] != '/' || basis[basis.Length - 1] != '/') { throw ExHelp.Argument(basis, "Invalid basis string `{0}'", basis); } this.basis = basis; if (decl_loc == null) { this.decl_loc = SourceSettings.BasisToSubpath(basis); } else { this.decl_loc = decl_loc; } }
// Helper void GetOffsetRange(int id, int[] table, int chunklen, out int start, out int bound) { if (id < 0 || id >= table.Length) { throw ExHelp.Range("Id {0} is out of range (max {1})", id, table.Length); } start = table[id]; if (id < table.Length - 1) { bound = table[id + 1]; } else { bound = table[0] + chunklen; } }
public static int[] ReadRawInts(BinaryReader br, int count) { if (count < 0) { throw ExHelp.Range("Raw int count: {0}", count); } int[] res = new int[count]; byte[] b = br.ReadBytes(count * 4); for (int i = 0; i < count; i++) { res[i] = BitConverter.ToInt32(b, i * 4); } Debug("read {0} raw ints", count); return(res); }
public TargetBuilder RequestTarget(string name) { // We can request targets via a SetDefault call via ApplyTemplate // via ResolveTemplate during target fixup, at which point providers // will have their done_modifying flag set. So only signal the error // if the request would have actually created a new target, not if // it is effectively a noop. if (done_modifying && !targets.ContainsKey(name)) { throw ExHelp.InvalidOp("Cannot request target {0} because its " + "provider is now unmodifiable", name); } TargetBuilder tb = EnsureTarget(name); tb.Request(); return(tb); }
void WriteTags() { bw.Write(gb.NumTags); int nwritten = 0; foreach (string name in gb.GetTags()) { int id = gb.GetTagId(name); bw.Write(name); bw.Write(id); nwritten++; } if (nwritten != gb.NumTags) { throw ExHelp.App("Report NumTags ({0}) is not actual ({1})?", gb.NumTags, nwritten); } }
TargetBuilder EnsureTarget(string name) { TargetBuilder tb; if (targets.ContainsKey(name)) { tb = targets[name]; } else { if (done_modifying) { throw ExHelp.InvalidOp("Cannot create new target {0} because its " + "provider is been marked as finished", name); } tb = CreateTarget(name); targets[name] = tb; } return(tb); }
public bool Prepare() { definitions_made = true; int n = 0; while (definitions_made) { definitions_made = false; foreach (Parser p in parsers.Values) { p.Resolve(false); } if (n++ > 100) { throw ExHelp.App("Infinite loop of user type declarations???"); } // definitions_made gets set if any resolve // function defines a new usertype, which might // play a role in type resolution. } bool ret = false; // Now all the user types have been defined, so // if any type resolution fails, it is an error. foreach (Parser p in parsers.Values) { ret |= p.Resolve(true); } return(ret); }
public static void Stats(BinaryReader br) { byte[] dat; int val; dat = br.ReadBytes(4); if (dat[0] != (byte)'M' || dat[1] != (byte)'B' || dat[2] != (byte)'G' || dat[3] != (byte)BinaryLoadedGraph.BinaryFormatIdent) { throw ExHelp.App("Unexpected header values: {0},{1},{2},{3}", dat[0], dat[1], dat[2], dat[3]); } Console.WriteLine(" * Header OK"); val = BinaryHelper.ReadRawInt(br); if (val != 0x01B2C3D4) { throw ExHelp.App("Endianness marker not valid: got {0:x}", val); } Console.WriteLine(" * Endianness OK"); long pstart = br.BaseStream.Position; ProjectInfo pi = (ProjectInfo)BinaryHelper.ReadObject(br); long pend = br.BaseStream.Position; Console.WriteLine(" * Project info: name {0}, version {1}, compat code {2}, bfname {3}", pi.Name, pi.Version, pi.CompatCode, pi.BuildfileName); Console.WriteLine(" * Project info entry consumes {0} bytes", pend - pstart); BinaryHelper.ExpectDelimiter(br); pstart = br.BaseStream.Position; DependentItemInfo[] dii = (DependentItemInfo[])BinaryHelper.ReadObject(br); Console.WriteLine(" * Depends on {0} files", dii.Length); dii = (DependentItemInfo[])BinaryHelper.ReadObject(br); Console.WriteLine(" * Depends on {0} bundles", dii.Length); pend = br.BaseStream.Position; Console.WriteLine(" * Dependency info consumes {0} bytes", pend - pstart); BinaryHelper.ExpectDelimiter(br); pstart = br.BaseStream.Position; int ntags = br.ReadInt32(); Console.WriteLine(" * {0} tags", ntags); for (int i = 0; i < ntags; i++) { string s = br.ReadString(); int tagid = br.ReadInt32(); Console.WriteLine(" {0} : {1}", s, tagid); } pend = br.BaseStream.Position; Console.WriteLine(" * Tag listing consumes {0} bytes", pend - pstart); BinaryHelper.ExpectDelimiter(br); int n = br.ReadInt32(); Console.WriteLine(" * Dependency data table is {0} ints ({1} bytes)", n, n * 4); dat = br.ReadBytes(n * 4); BinaryHelper.ExpectDelimiter(br); n = br.ReadInt32(); Console.WriteLine(" * Name data table is {0} bytes", n); dat = br.ReadBytes(n); BinaryHelper.ExpectDelimiter(br); pstart = br.BaseStream.Position; n = br.ReadInt32(); Console.WriteLine(" * There are {0} providers", n); for (int i = 0; i < n; i++) { int ntarg = br.ReadInt32(); string basis = br.ReadString(); string declloc = br.ReadString(); int deplen = br.ReadInt32(); dat = br.ReadBytes(ntarg * 4); int namelen = br.ReadInt32(); dat = br.ReadBytes(ntarg * 4); dat = br.ReadBytes(ntarg * 4); Console.WriteLine(" {0}: {1} targs, {2} bytes of dep, {3} bytes of name, decl {4}", basis, ntarg, deplen * 4, namelen * 4, declloc); } pend = br.BaseStream.Position; Console.WriteLine(" * Provider listing consumes {0} bytes", pend - pstart); BinaryHelper.ExpectDelimiter(br); pstart = br.BaseStream.Position; n = br.ReadInt32(); Console.WriteLine(" * There are {0} ints ({1} bytes) in the tag data table", n, n * 4); dat = br.ReadBytes(n * 4); int[] ofs = BinaryHelper.ReadRawInts(br, ntags); Console.Write(" * Tag chunk lengths:"); for (int i = 0; i < ntags - 1; i++) { Console.Write(" {0}", ofs[i + 1] - ofs[i]); } Console.WriteLine(" {0}", n - ofs[ntags - 1]); pend = br.BaseStream.Position; Console.WriteLine(" * Tag data consumes {0} bytes", pend - pstart); BinaryHelper.ExpectDelimiter(br); pstart = br.BaseStream.Position; Type[] typetab = (Type[])BinaryHelper.ReadObject(br); pend = br.BaseStream.Position; Console.WriteLine(" * Type table has {0} entries, consumes {1} bytes", typetab.Length, pend - pstart); BinaryHelper.ExpectDelimiter(br); pstart = br.BaseStream.Position; Result[] rtab = (Result[])BinaryHelper.ReadObject(br); pend = br.BaseStream.Position; Console.WriteLine(" * Result table has {0} entries, consumes {1} bytes", rtab.Length, pend - pstart); for (int i = 0; i < rtab.Length; i++) { Console.WriteLine(" {0}: {1}", i, rtab[i]); } try { br.ReadByte(); } catch (Exception) { Console.WriteLine(" * Found EOF as expected"); return; } throw ExHelp.App("Did not find EOF at position {0} as expected", br.BaseStream.Position); }
public override bool Resolve(TypeResolveContext trc, bool errors) { bool ret = base.Resolve(trc, errors); ret |= MatchType.ResolveExtension("RTemplate", trc, errors); // RegexMatcher will handle distinguishing between when // we return a Rule type and a TargetTemplate type, but we need // to check if we require a binding. bool?foo = MatchType.ResolvesAsRule(trc, errors); if (foo == null) { // MatchType unresolved, can't say. return(true); } if ((bool)foo) { // We point to a Rule, and ResolveUsedStructureType will // complain if we call it on MatchType. if (UsesStructure) { throw ExHelp.App("Odd, MatcherBuilder {0} is using its structure" + "even though it points to a plain rule {1}", this, MatchType); } return(false); } // We have a template, so see if it uses a structure UserType stype = MatchType.ResolveUsedStructureType(trc, errors); if (errors) { if (stype == null && UsesStructure) { // XXX is this actually an error? Shouldn't happen, I think. Console.Error.WriteLine("Matcher {0} is bound to structure {1} but its " + "associated template {2} is unbound", this, Params, MatchType); return(true); } else if (stype != null && !UsesStructure) { UseStructure(); } else if (stype != null && !NS.ParamsType.Equals(stype)) { // FIXME: see if Structure has a member of type stype. Console.Error.WriteLine("Matcher {0} is bound to the structure {1} but its " + "associated template {2} is bound to {3}", this, Params, MatchType, stype); return(true); } } return(ret); }
// Helper: converts a structure-bound target reference to a CodeExpression. // A "structure-bound target reference" is one of the following: // // * plain: [target basename] (valid only if 'basis' is nonnull) // * relative to a basis param: [param]/[target basename] // * relative to a structure param: [param].[structure's basis param]/[target basename] // // (We could add: [structure param].[structure param]....[basis param]/[target basename]) // // 'strukt' is an expression which, in the context of the whatever will evaluate // our return value, yields the StructureTemplate object whose parameters // will be used to evaluate bases. // // 'basis' is an optional expression which yields a string representing // the "current" basis, if that is meaningful. If not, pass null. public CodeExpression MakeTargetNameExpr(string name, CodeExpression strukt, CodeExpression basis) { int idx = name.IndexOf('/'); CodeBinaryOperatorExpression e = new CodeBinaryOperatorExpression(); e.Operator = CodeBinaryOperatorType.Add; if (idx < 0) { if (basis == null) { throw new Exception("Can't have bare target name in object without an " + "associated basis."); } e.Left = basis; e.Right = new CodePrimitiveExpression(name); } else { string param = name.Substring(0, idx); string basename = name.Substring(idx + 1); if (basename.IndexOf('/') != -1) { throw new Exception("Target references must be relative to a " + "parameter; can have at most one / in the text"); } int jdx = param.IndexOf('.'); e.Right = new CodePrimitiveExpression(basename); if (jdx < 0) { if (!nsparams.HasParam(param)) { throw ExHelp.App("Relative target name references unknown " + "structure parameter {0}.", param); } if (nsparams[param] == StructureParameterKind.Structure) { throw ExHelp.App("Target prefix parmeter {0} should be a `basis' but is a " + "`structure'. Did you forget to add a member access to the structure?", param); } if (nsparams[param] != StructureParameterKind.Basis) { throw ExHelp.App("Target prefix argument {0} must be of type `basis'.", param); } e.Left = new CodeFieldReferenceExpression(strukt, param); } else { // Member of a Structure parameter string member = param.Substring(jdx + 1); param = param.Substring(0, jdx); if (!nsparams.HasParam(param)) { throw ExHelp.App("Relative target name references unknown " + "structure parameter {0}", param); } if (nsparams[param] != StructureParameterKind.Structure) { throw ExHelp.App("Member access parameter must be of type `structure'."); } CodeExpression outer = new CodeFieldReferenceExpression(strukt, param); e.Left = new CodeFieldReferenceExpression(outer, member); } } return(e); }
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); }