Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2006 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 public class CType {
     18 
     19     String baseType;
     20     boolean isConst;
     21     boolean isPointer;
     22 
     23     public CType() {
     24     }
     25 
     26     public CType(String baseType) {
     27     setBaseType(baseType);
     28     }
     29 
     30     public CType(String baseType, boolean isConst, boolean isPointer) {
     31     setBaseType(baseType);
     32     setIsConst(isConst);
     33     setIsPointer(isPointer);
     34     }
     35 
     36     public String getDeclaration() {
     37     return baseType + (isPointer ? " *" : "");
     38     }
     39 
     40     public void setIsConst(boolean isConst) {
     41     this.isConst = isConst;
     42     }
     43 
     44     public boolean isConst() {
     45     return isConst;
     46     }
     47 
     48     public void setIsPointer(boolean isPointer) {
     49     this.isPointer = isPointer;
     50     }
     51 
     52     public boolean isPointer() {
     53     return isPointer;
     54     }
     55 
     56     boolean isVoid() {
     57     String baseType = getBaseType();
     58     return baseType.equals("GLvoid") ||
     59         baseType.equals("void");
     60     }
     61 
     62     public boolean isConstCharPointer() {
     63         return isConst && isPointer && baseType.equals("char");
     64     }
     65 
     66     public boolean isTypedPointer() {
     67     return isPointer() && !isVoid() && !isConstCharPointer();
     68     }
     69 
     70     public void setBaseType(String baseType) {
     71     this.baseType = baseType;
     72     }
     73 
     74     public String getBaseType() {
     75     return baseType;
     76     }
     77 
     78     @Override
     79     public String toString() {
     80     String s = "";
     81     if (isConst()) {
     82         s += "const ";
     83     }
     84     s += baseType;
     85     if (isPointer()) {
     86         s += "*";
     87     }
     88 
     89     return s;
     90     }
     91 
     92     @Override
     93     public int hashCode() {
     94     return baseType.hashCode() ^ (isPointer ? 2 : 0) ^ (isConst ? 1 : 0);
     95     }
     96 
     97     @Override
     98     public boolean equals(Object o) {
     99     if (o != null && o instanceof CType) {
    100         CType c = (CType)o;
    101         return baseType.equals(c.baseType) &&
    102         isPointer() == c.isPointer() &&
    103         isConst() == c.isConst();
    104     }
    105     return false;
    106     }
    107 }
    108