Example #1
0
        private void CheckForReadonlyAssignments(ASTAssign n)
        {
            if (n.LValue.Descriptor is FormalDescriptor)
            {
                FormalDescriptor descriptor = (FormalDescriptor)n.LValue.Descriptor;
                if (String.Compare(descriptor.Modifier, READONLY_MODIFIER, true) == 0)
                {
                    ReportError(n.Location, "Parameter '{0}' is marked as readonly and can not be assigned.", descriptor.Name);
                }
            }

            bool isDerefField = (n.LValue is ASTDereferenceField && ((ASTDereferenceField)n.LValue).Object is ASTIdentifier);
            bool isDerefArray = (n.LValue is ASTDereferenceArray && ((ASTDereferenceArray)n.LValue).Array is ASTIdentifier);

            if (isDerefArray || isDerefField)
            {
                var item        = isDerefField ? ((ASTDereferenceField)n.LValue).Object : ((ASTDereferenceArray)n.LValue).Array;
                var id          = ((ASTIdentifier)item).ID;
                var curMethDesc = _scopeMgr.Find(_currentMethod.Name, d => d is MethodDescriptor) as MethodDescriptor;
                var formalMatch = curMethDesc.Formals.Where(p => p.Name.Equals(id, StringComparison.OrdinalIgnoreCase)).SingleOrDefault();

                if (formalMatch != null)
                {
                    if (!string.IsNullOrEmpty(formalMatch.Modifier) &&
                        formalMatch.Modifier.Equals(READONLY_MODIFIER, StringComparison.OrdinalIgnoreCase))
                    {
                        ReportError(n.Location, "Parameter '{0}' passed to method '{1}' is marked as readonly and cannot be assigned.", formalMatch.Name, _currentMethod.Name);
                    }
                }
            }
        }
Example #2
0
        public FormalDescriptor AddFormal(string name, CFlatType type, string modifier)
        {
            var descriptor = new FormalDescriptor(type, name, modifier);

            CurrentScope.Descriptors.Add(name, descriptor);
            return(descriptor);
        }