All Downloads are FREE. Search and download functionalities are using the official Maven repository.

lt.compiler.semantic.SClassDef Maven / Gradle / Ivy

Go to download

The latte-lang compiler project, which contains compiler and runtime required library.

The newest version!
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2016 KuiGang Wang
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package lt.compiler.semantic;

import lt.compiler.LineCol;

import java.util.ArrayList;
import java.util.List;

/**
 * class definition
 */
public class SClassDef extends SRefTypeDef {
        public static final int OBJECT = 2;
        public static final int FUN = 1;
        public static final int NORMAL = 0;

        private final int type;
        private final List constructors = new ArrayList();
        private SClassDef parent;
        private final List superInterfaces = new ArrayList();
        private final List staticStatements = new ArrayList();
        private final List staticExceptionTable = new ArrayList();
        private final InvokableMeta staticInvokableMeta = new InvokableMeta();
        private boolean isDataClass;

        public SClassDef(int type, LineCol lineCol) {
                super(lineCol);
                if (type != OBJECT && type != FUN && type != NORMAL) throw new IllegalArgumentException();
                this.type = type;
        }

        public void setParent(SClassDef parent) {
                this.parent = parent;
        }

        public List constructors() {
                return constructors;
        }

        public SClassDef parent() {
                return parent;
        }

        public List superInterfaces() {
                return superInterfaces;
        }

        public List staticStatements() {
                return staticStatements;
        }

        public List staticExceptionTable() {
                return staticExceptionTable;
        }

        public boolean isDataClass() {
                return isDataClass;
        }

        public void setIsDataClass(boolean isDataClass) {
                this.isDataClass = isDataClass;
        }

        public int classType() {
                return type;
        }

        public InvokableMeta staticMeta() {
                return staticInvokableMeta;
        }

        @Override
        public String toString() {
                StringBuilder sb = new StringBuilder();
                for (SModifier m : modifiers()) {
                        sb.append(m.toString().toLowerCase()).append(" ");
                }
                sb.append("class ").append(fullName());
                if (parent() != null) sb.append(" extends ").append(parent().fullName());
                if (!superInterfaces().isEmpty())
                        sb.append(" implements ");
                boolean isFirst = true;
                for (SInterfaceDef i : superInterfaces()) {
                        if (isFirst) {
                                isFirst = false;
                        } else {
                                sb.append(",");
                        }
                        sb.append(i.fullName());
                }
                return sb.toString();
        }

        @Override
        public boolean isAssignableFrom(STypeDef cls) {
                if (super.isAssignableFrom(cls)) return true;

                // java.lang.Object is assignable from all interface types
                if (cls instanceof SInterfaceDef && this.fullName().equals("java.lang.Object")) {
                        return true;
                }

                if (cls instanceof SClassDef) {
                        while (cls != null) {
                                if (cls.equals(this)) return true;
                                cls = ((SClassDef) cls).parent();
                        }
                        return false;
                }
                return false;
        }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy