Improve javadoc for ctfAdapter in Tmf.Core
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / internal / ctf / core / event / metadata / IOStructGen.java
index 40588ebf36a22521393962c000b940e27eef0d1d..b9533b2299d35a0b1d59ea10eb5b5cd57643d654 100644 (file)
@@ -28,6 +28,7 @@ import org.eclipse.linuxtools.ctf.core.event.EventDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.ArrayDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.Encoding;
 import org.eclipse.linuxtools.ctf.core.event.types.EnumDeclaration;
+import org.eclipse.linuxtools.ctf.core.event.types.FloatDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.IDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.SequenceDeclaration;
@@ -36,7 +37,6 @@ import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.VariantDeclaration;
 import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
 import org.eclipse.linuxtools.ctf.parser.CTFParser;
-import org.eclipse.linuxtools.internal.ctf.core.Activator;
 import org.eclipse.linuxtools.internal.ctf.core.event.metadata.exceptions.ParseException;
 import org.eclipse.linuxtools.internal.ctf.core.trace.Stream;
 
@@ -79,11 +79,20 @@ public class IOStructGen {
     // Constructor
     // ------------------------------------------------------------------------
 
+    /**
+     * Constuctor
+     * @param tree the tree (antlr generated) with the parsed TSDL data.
+     * @param trace the trace containing the places to put all the read metadata
+     */
     public IOStructGen(CommonTree tree, CTFTrace trace) {
         this.trace = trace;
         this.tree = tree;
     }
 
+    /**
+     * Parse the tree and populate the trace defined in the constructor.
+     * @throws ParseException
+     */
     public void generate() throws ParseException {
         parseRoot(tree);
     }
@@ -214,14 +223,11 @@ public class IOStructGen {
             }
             for (CommonTree event : events) {
                 parseEvent(event);
-                CommonTree name = (CommonTree) event.getChild(0).getChild(1)
-                        .getChild(0).getChild(0);
-                if (DEBUG_) {
-                    out.write("Name = " + name); //$NON-NLS-1$
-                }
-                CommonTree id = (CommonTree) event.getChild(1).getChild(1)
-                        .getChild(0).getChild(0);
                 if (DEBUG_) {
+                    CommonTree name = (CommonTree) event.getChild(0).getChild(1)
+                            .getChild(0).getChild(0);
+                    CommonTree id = (CommonTree) event.getChild(1).getChild(1)
+                            .getChild(0).getChild(0);
                     out.write("Name = " + name + " Id = " + id + '\n'); //$NON-NLS-1$ //$NON-NLS-2$
                 }
             }
@@ -348,19 +354,19 @@ public class IOStructGen {
 
         String left = concatenateUnaryStrings(leftStrings);
 
-        if (left.equals("major")) { //$NON-NLS-1$
+        if (left.equals(CTFStrings.MAJOR)) {
             if (trace.majortIsSet()) {
                 throw new ParseException("major is already set"); //$NON-NLS-1$
             }
 
             trace.setMajor(getMajorOrMinor(rightNode));
-        } else if (left.equals("minor")) { //$NON-NLS-1$
+        } else if (left.equals(CTFStrings.MINOR)) {
             if (trace.minorIsSet()) {
                 throw new ParseException("minor is already set"); //$NON-NLS-1$
             }
 
             trace.setMinor(getMajorOrMinor(rightNode));
-        } else if (left.equals("uuid")) { //$NON-NLS-1$
+        } else if (left.equals(CTFStrings.UUID_STRING)) {
             UUID uuid = getUUID(rightNode);
 
             /*
@@ -376,7 +382,7 @@ public class IOStructGen {
                 trace.setUUID(uuid);
             }
 
-        } else if (left.equals("byte_order")) { //$NON-NLS-1$
+        } else if (left.equals(CTFStrings.BYTE_ORDER)) {
             ByteOrder byteOrder = getByteOrder(rightNode);
 
             /*
@@ -393,7 +399,7 @@ public class IOStructGen {
             } else {
                 trace.setByteOrder(byteOrder);
             }
-        } else if (left.equals("packet.header")) { //$NON-NLS-1$
+        } else if (left.equals(CTFStrings.PACKET_HEADER)) {
             if (trace.packetHeaderIsSet()) {
                 throw new ParseException("packet.header already defined"); //$NON-NLS-1$
             }
@@ -448,9 +454,9 @@ public class IOStructGen {
             }
         }
 
-        if (stream.idIsSet()) {
+        if (stream.isIdSet()) {
             if (!trace.packetHeaderIsSet()
-                    || !trace.getPacketHeader().hasField("stream_id")) { //$NON-NLS-1$
+                    || !trace.getPacketHeader().hasField(CTFStrings.STREAM_ID)) {
                 throw new ParseException(
                         "Stream has an ID, but there is no stream_id field in packet header."); //$NON-NLS-1$
             }
@@ -484,16 +490,16 @@ public class IOStructGen {
 
         String left = concatenateUnaryStrings(leftStrings);
 
-        if (left.equals("id")) { //$NON-NLS-1$
-            if (stream.idIsSet()) {
+        if (left.equals(CTFStrings.ID)) {
+            if (stream.isIdSet()) {
                 throw new ParseException("stream id already defined"); //$NON-NLS-1$
             }
 
             long streamID = getStreamID(rightNode);
 
             stream.setId(streamID);
-        } else if (left.equals("event.header")) { //$NON-NLS-1$
-            if (stream.eventHeaderIsSet()) {
+        } else if (left.equals(CTFStrings.EVENT_HEADER)) {
+            if (stream.isEventHeaderSet()) {
                 throw new ParseException("event.header already defined"); //$NON-NLS-1$
             }
 
@@ -512,8 +518,8 @@ public class IOStructGen {
             }
 
             stream.setEventHeader((StructDeclaration) eventHeaderDecl);
-        } else if (left.equals("event.context")) { //$NON-NLS-1$
-            if (stream.eventContextIsSet()) {
+        } else if (left.equals(CTFStrings.EVENT_CONTEXT)) {
+            if (stream.isEventContextSet()) {
                 throw new ParseException("event.context already defined"); //$NON-NLS-1$
             }
 
@@ -532,8 +538,8 @@ public class IOStructGen {
             }
 
             stream.setEventContext((StructDeclaration) eventContextDecl);
-        } else if (left.equals("packet.context")) { //$NON-NLS-1$
-            if (stream.packetContextIsSet()) {
+        } else if (left.equals(CTFStrings.PACKET_CONTEXT)) {
+            if (stream.isPacketContextSet()) {
                 throw new ParseException("packet.context already defined"); //$NON-NLS-1$
             }
 
@@ -648,7 +654,7 @@ public class IOStructGen {
 
         String left = concatenateUnaryStrings(leftStrings);
 
-        if (left.equals("name")) { //$NON-NLS-1$
+        if (left.equals(CTFStrings.NAME2)) {
             if (event.nameIsSet()) {
                 throw new ParseException("name already defined"); //$NON-NLS-1$
             }
@@ -656,7 +662,7 @@ public class IOStructGen {
             String name = getEventName(rightNode);
 
             event.setName(name);
-        } else if (left.equals("id")) { //$NON-NLS-1$
+        } else if (left.equals(CTFStrings.ID)) {
             if (event.idIsSet()) {
                 throw new ParseException("id already defined"); //$NON-NLS-1$
             }
@@ -664,7 +670,7 @@ public class IOStructGen {
             long id = getEventID(rightNode);
 
             event.setId(id);
-        } else if (left.equals("stream_id")) { //$NON-NLS-1$
+        } else if (left.equals(CTFStrings.STREAM_ID)) {
             if (event.streamIsSet()) {
                 throw new ParseException("stream id already defined"); //$NON-NLS-1$
             }
@@ -678,7 +684,7 @@ public class IOStructGen {
             }
 
             event.setStream(stream);
-        } else if (left.equals("context")) { //$NON-NLS-1$
+        } else if (left.equals(CTFStrings.CONTEXT)) {
             if (event.contextIsSet()) {
                 throw new ParseException("context already defined"); //$NON-NLS-1$
             }
@@ -697,7 +703,7 @@ public class IOStructGen {
             }
 
             event.setContext((StructDeclaration) contextDecl);
-        } else if (left.equals("fields")) { //$NON-NLS-1$
+        } else if (left.equals(CTFStrings.FIELDS_STRING)) {
             if (event.fieldsIsSet()) {
                 throw new ParseException("fields already defined"); //$NON-NLS-1$
             }
@@ -720,7 +726,11 @@ public class IOStructGen {
              */
             final StructDeclaration fields = (StructDeclaration) fieldsDecl;
             event.setFields(fields);
+        }
+        else if (left.equals(CTFStrings.LOGLEVEL2)){
 
+            long logLevel = parseUnaryInteger((CommonTree) rightNode.getChild(0)) ;
+            event.setLogLevel(logLevel);
         } else {
             throw new ParseException("Unknown event attribute : " + left); //$NON-NLS-1$
         }
@@ -1132,9 +1142,10 @@ public class IOStructGen {
 
         switch (firstChild.getType()) {
         case CTFParser.FLOATING_POINT:
-            Activator
-                    .getDefault()
-                    .log("parseTypeSpecifierList: floating_point not implemented yet"); //$NON-NLS-1$
+//            Activator
+//                    .getDefault()
+//                    .log("parseTypeSpecifierList: floating_point not implemented yet"); //$NON-NLS-1$
+            declaration = parseFloat(firstChild);
             break;
         case CTFParser.INTEGER:
             declaration = parseInteger(firstChild);
@@ -1174,6 +1185,87 @@ public class IOStructGen {
         return declaration;
     }
 
+    private IDeclaration parseFloat(CommonTree floatingPoint) throws ParseException {
+        assert (floatingPoint.getType() == CTFParser.INTEGER);
+
+        List<CommonTree> children = floatingPoint.getChildren();
+
+        /*
+         * If the integer has no attributes, then it is missing the size
+         * attribute which is required
+         */
+        if (children == null) {
+            throw new ParseException("float: missing size attribute"); //$NON-NLS-1$
+        }
+
+        /* The return value */
+        FloatDeclaration floatDeclaration = null;
+        ByteOrder byteOrder = trace.getByteOrder();
+        long alignment = 0;
+        int exponent = 8;
+        int mantissa = 24;
+
+        /* Iterate on all integer children */
+        for (CommonTree child : children) {
+            switch (child.getType()) {
+            case CTFParser.CTF_EXPRESSION_VAL:
+                /*
+                 * An assignment expression must have 2 children, left and right
+                 */
+                assert (child.getChildCount() == 2);
+
+                CommonTree leftNode = (CommonTree) child.getChild(0);
+                assert (leftNode.getType() == CTFParser.CTF_LEFT);
+                CommonTree rightNode = (CommonTree) child.getChild(1);
+                assert (rightNode.getType() == CTFParser.CTF_RIGHT);
+
+                List<CommonTree> leftStrings = leftNode.getChildren();
+                assert (leftStrings != null);
+
+                if (!isUnaryString(leftStrings.get(0))) {
+                    throw new ParseException(
+                            "Left side of ctf expression must be a string"); //$NON-NLS-1$
+                }
+                String left = concatenateUnaryStrings(leftStrings);
+
+                if (left.equals(CTFStrings.EXP_DIG)) {
+                    exponent = (int) parseUnaryInteger((CommonTree) rightNode.getChild(0));
+                } else if (left.equals(CTFStrings.BYTE_ORDER)) {
+                    byteOrder = getByteOrder(rightNode);
+                } else if (left.equals(CTFStrings.MANT_DIG)) {
+                    mantissa = (int) parseUnaryInteger((CommonTree) rightNode.getChild(0));
+                } else if (left.equals(CTFStrings.ALIGN)) {
+                    alignment = getAlignment(rightNode);
+                } else {
+                    throw new ParseException("Float: unknown attribute " + left); //$NON-NLS-1$
+                }
+
+                break;
+            default:
+                childTypeError(child);
+                break;
+            }
+        }
+        int size = mantissa + exponent;
+        if (size == 0) {
+            throw new ParseException("Float missing size attribute"); //$NON-NLS-1$
+        }
+
+        if (alignment == 0) {
+            if ((size % 8) == 0) {
+                alignment = 1;
+            } else {
+                alignment = 8;
+            }
+        }
+
+        floatDeclaration = new FloatDeclaration(exponent, mantissa, byteOrder, alignment);
+
+        assert (floatDeclaration != null);
+        return floatDeclaration;
+
+    }
+
     /**
      * Parses a type specifier list as a user-declared type.
      *
@@ -1262,11 +1354,11 @@ public class IOStructGen {
 
                 if (left.equals("signed")) { //$NON-NLS-1$
                     signed = getSigned(rightNode);
-                } else if (left.equals("byte_order")) { //$NON-NLS-1$
+                } else if (left.equals(CTFStrings.BYTE_ORDER)) {
                     byteOrder = getByteOrder(rightNode);
                 } else if (left.equals("size")) { //$NON-NLS-1$
                     size = getSize(rightNode);
-                } else if (left.equals("align")) { //$NON-NLS-1$
+                } else if (left.equals(CTFStrings.ALIGN)) {
                     alignment = getAlignment(rightNode);
                 } else if (left.equals("base")) { //$NON-NLS-1$
                     base = getBase(rightNode);
@@ -1298,7 +1390,7 @@ public class IOStructGen {
         }
 
         integerDeclaration = new IntegerDeclaration((int) size, signed, base,
-                byteOrder, encoding, clock);
+                byteOrder, encoding, clock, alignment);
 
         assert (integerDeclaration != null);
         return integerDeclaration;
@@ -1456,7 +1548,6 @@ public class IOStructGen {
                 throw new ParseException("struct " + structName //$NON-NLS-1$
                         + " already defined."); //$NON-NLS-1$
             }
-
             /* Create the declaration */
             structDeclaration = new StructDeclaration(structAlign);
 
@@ -2366,9 +2457,9 @@ public class IOStructGen {
         if (isUnaryString(firstChild)) {
             String strval = concatenateUnaryStrings(rightNode.getChildren());
 
-            if (strval.equals("true") || strval.equals("TRUE")) { //$NON-NLS-1$ //$NON-NLS-2$
+            if (strval.equals(CTFStrings.TRUE) || strval.equals(CTFStrings.TRUE2)) {
                 ret = true;
-            } else if (strval.equals("false") || strval.equals("FALSE")) { //$NON-NLS-1$ //$NON-NLS-2$
+            } else if (strval.equals(CTFStrings.FALSE) || strval.equals(CTFStrings.FALSE2)) {
                 ret = false;
             } else {
                 throw new ParseException("Invalid boolean value " //$NON-NLS-1$
@@ -2414,11 +2505,11 @@ public class IOStructGen {
         if (isUnaryString(firstChild)) {
             String strval = concatenateUnaryStrings(rightNode.getChildren());
 
-            if (strval.equals("le")) { //$NON-NLS-1$
+            if (strval.equals(CTFStrings.LE)) {
                 return ByteOrder.LITTLE_ENDIAN;
-            } else if (strval.equals("be") || strval.equals("network")) { //$NON-NLS-1$ //$NON-NLS-2$
+            } else if (strval.equals(CTFStrings.BE) || strval.equals(CTFStrings.NETWORK)) {
                 return ByteOrder.BIG_ENDIAN;
-            } else if (strval.equals("native")) { //$NON-NLS-1$
+            } else if (strval.equals(CTFStrings.NATIVE)) {
                 return trace.getByteOrder();
             } else {
                 throw new ParseException("Invalid value for byte order"); //$NON-NLS-1$
@@ -2530,18 +2621,18 @@ public class IOStructGen {
         } else if (isUnaryString(firstChild)) {
             String strval = concatenateUnaryStrings(rightNode.getChildren());
 
-            if (strval.equals("decimal") || strval.equals("dec") //$NON-NLS-1$ //$NON-NLS-2$
-                    || strval.equals("d") || strval.equals("i") //$NON-NLS-1$ //$NON-NLS-2$
-                    || strval.equals("u")) { //$NON-NLS-1$
+            if (strval.equals(CTFStrings.DECIMAL) || strval.equals(CTFStrings.DEC)
+                    || strval.equals(CTFStrings.DEC_CTE) || strval.equals(CTFStrings.INT_MOD)
+                    || strval.equals(CTFStrings.UNSIGNED_CTE)) {
                 return 10;
-            } else if (strval.equals("hexadecimal") || strval.equals("hex") //$NON-NLS-1$ //$NON-NLS-2$
-                    || strval.equals("x") || strval.equals("X") //$NON-NLS-1$ //$NON-NLS-2$
-                    || strval.equals("p")) { //$NON-NLS-1$
+            } else if (strval.equals(CTFStrings.HEXADECIMAL) || strval.equals(CTFStrings.HEX)
+                    || strval.equals(CTFStrings.X) || strval.equals(CTFStrings.X2)
+                    || strval.equals(CTFStrings.POINTER)) {
                 return 16;
-            } else if (strval.equals("octal") || strval.equals("oct") //$NON-NLS-1$ //$NON-NLS-2$
-                    || strval.equals("o")) { //$NON-NLS-1$
+            } else if (strval.equals(CTFStrings.OCTAL) || strval.equals(CTFStrings.OCT)
+                    || strval.equals(CTFStrings.OCTAL_CTE)) {
                 return 8;
-            } else if (strval.equals("binary") || strval.equals("b")) { //$NON-NLS-1$ //$NON-NLS-2$
+            } else if (strval.equals(CTFStrings.BINARY) || strval.equals(CTFStrings.BIN)) {
                 return 2;
             } else {
                 throw new ParseException("Invalid value for base"); //$NON-NLS-1$
@@ -2568,11 +2659,11 @@ public class IOStructGen {
         if (isUnaryString(firstChild)) {
             String strval = concatenateUnaryStrings(rightNode.getChildren());
 
-            if (strval.equals("UTF8")) { //$NON-NLS-1$
+            if (strval.equals(CTFStrings.UTF8)) {
                 return Encoding.UTF8;
-            } else if (strval.equals("ASCII")) { //$NON-NLS-1$
+            } else if (strval.equals(CTFStrings.ASCII)) {
                 return Encoding.ASCII;
-            } else if (strval.equals("none")) { //$NON-NLS-1$
+            } else if (strval.equals(CTFStrings.NONE)) {
                 return Encoding.NONE;
             } else {
                 throw new ParseException("Invalid value for encoding"); //$NON-NLS-1$
This page took 0.029998 seconds and 5 git commands to generate.