static SignaturePart ApplyParamAttributes([NotNull] SignaturePart part, [NotNull] ParameterInfo pi) { // TODO: also handle VariableAttribute? if (pi.IsDefined(typeof(TableAttribute), false)) { part = SignatureBuilder.Constrained(part, Constraint.OfPrimType(PrimType.TABLE)); } if (pi.IsDefined(typeof(ObjectAttribute), false)) { part = SignatureBuilder.Constrained(part, Constraint.OfType(StdAtom.OBJECT)); } if (pi.IsOptional) { part = SignatureBuilder.Optional(part); } return(part); }
static SignaturePart ConvertForSubr( [NotNull] Type paramType, [NotNull] string name, // ReSharper disable once SuggestBaseTypeForParameter [NotNull][ItemNotNull] object[] attrs, bool isOptional, // ReSharper disable once UnusedParameter.Local object defaultValue) { // [Either], [Required], and [Decl] go on the parameter var isRequired = attrs.OfType <RequiredAttribute>().Any(); if (isRequired && isOptional) { throw new InvalidOperationException("A parameter can't be both required and optional"); } var eitherAttr = attrs.OfType <EitherAttribute>().SingleOrDefault(); var declAttr = attrs.OfType <DeclAttribute>().SingleOrDefault(); // [ZilStructuredParam] and [ZilSequenceParam] go on the element type var(isArray, elementType) = CheckArray(paramType); var structAttr = elementType.GetCustomAttribute <ZilStructuredParamAttribute>(false); var seqAttr = elementType.GetCustomAttribute <ZilSequenceParamAttribute>(false); int attrCount = (eitherAttr != null ? 1 : 0) + (declAttr != null ? 1 : 0) + (structAttr != null ? 1 : 0) + (seqAttr != null ? 1 : 0); if (attrCount > 1) { throw new InvalidOperationException( nameof(EitherAttribute) + ", " + nameof(DeclAttribute) + ", " + nameof(ZilStructuredParamAttribute) + ", or " + nameof(ZilSequenceParamAttribute) + ": pick at most one"); } SignaturePart elemPart = null; if (eitherAttr != null) { elemPart = ConvertEither(eitherAttr.Types, eitherAttr.DefaultParamDesc ?? name); } else if (declAttr != null) { elemPart = SignatureBuilder.MaybeConvertDecl(declAttr); } else if (structAttr != null) { elemPart = ConvertStruct(elementType, structAttr, name); } else if (seqAttr != null) { elemPart = ConvertSequence(elementType, name); } if (elemPart == null) { elemPart = SignatureBuilder.Identifier(name); if (elementType != typeof(ZilObject)) { elemPart = ConstrainByType(elemPart, elementType); } } if (isArray) { elemPart = SignatureBuilder.VarArgs(elemPart, isRequired); } if (isOptional) { //XXX use defaultValue somehow? elemPart = SignatureBuilder.Optional(elemPart); } return(elemPart); }
static SignaturePart ConvertSequence([NotNull] Type seqType, [NotNull] string name) { // TODO: cache the result? return(SignatureBuilder.Sequence(ConvertFields(seqType), name)); }