Used size_t for object size instead of signed int.
BUG=crbug 179653
R=aedla@chromium.org, kbr@chromium.org

Review URL: https://codereview.appspot.com/8834048

git-svn-id: http://angleproject.googlecode.com/svn/trunk@2211 736b8ea6-26fd-11df-bfd4-992fa37f6226
diff --git a/src/compiler/Intermediate.cpp b/src/compiler/Intermediate.cpp
index 53aaa0f..a2b3dde 100644
--- a/src/compiler/Intermediate.cpp
+++ b/src/compiler/Intermediate.cpp
@@ -1022,11 +1022,11 @@
     const TTypeList* fields = leftNodeType.getStruct();
 
     size_t structSize = fields->size();
-    int index = 0;
+    size_t index = 0;
 
     for (size_t j = 0; j < structSize; j++) {
-        int size = (*fields)[j]->getObjectSize();
-        for (int i = 0; i < size; i++) {
+        size_t size = (*fields)[j]->getObjectSize();
+        for (size_t i = 0; i < size; i++) {
             if ((*fields)[j]->getBasicType() == EbtStruct) {
                 if (!CompareStructure(*(*fields)[j], &rightUnionArray[index], &leftUnionArray[index]))
                     return false;
@@ -1047,10 +1047,10 @@
         TType typeWithoutArrayness = leftNodeType;
         typeWithoutArrayness.clearArrayness();
 
-        int arraySize = leftNodeType.getArraySize();
+        size_t arraySize = leftNodeType.getArraySize();
 
-        for (int i = 0; i < arraySize; ++i) {
-            int offset = typeWithoutArrayness.getObjectSize() * i;
+        for (size_t i = 0; i < arraySize; ++i) {
+            size_t offset = typeWithoutArrayness.getObjectSize() * i;
             if (!CompareStruct(typeWithoutArrayness, &rightUnionArray[offset], &leftUnionArray[offset]))
                 return false;
         }
@@ -1070,7 +1070,7 @@
 TIntermTyped* TIntermConstantUnion::fold(TOperator op, TIntermTyped* constantNode, TInfoSink& infoSink)
 {
     ConstantUnion *unionArray = getUnionArrayPointer();
-    int objectSize = getType().getObjectSize();
+    size_t objectSize = getType().getObjectSize();
 
     if (constantNode) {  // binary operations
         TIntermConstantUnion *node = constantNode->getAsConstantUnion();
@@ -1080,13 +1080,13 @@
         // for a case like float f = 1.2 + vec4(2,3,4,5);
         if (constantNode->getType().getObjectSize() == 1 && objectSize > 1) {
             rightUnionArray = new ConstantUnion[objectSize];
-            for (int i = 0; i < objectSize; ++i)
+            for (size_t i = 0; i < objectSize; ++i)
                 rightUnionArray[i] = *node->getUnionArrayPointer();
             returnType = getType();
         } else if (constantNode->getType().getObjectSize() > 1 && objectSize == 1) {
             // for a case like float f = vec4(2,3,4,5) + 1.2;
             unionArray = new ConstantUnion[constantNode->getType().getObjectSize()];
-            for (int i = 0; i < constantNode->getType().getObjectSize(); ++i)
+            for (size_t i = 0; i < constantNode->getType().getObjectSize(); ++i)
                 unionArray[i] = *getUnionArrayPointer();
             returnType = node->getType();
             objectSize = constantNode->getType().getObjectSize();
@@ -1100,14 +1100,14 @@
             case EOpAdd:
                 tempConstArray = new ConstantUnion[objectSize];
                 {// support MSVC++6.0
-                    for (int i = 0; i < objectSize; i++)
+                    for (size_t i = 0; i < objectSize; i++)
                         tempConstArray[i] = unionArray[i] + rightUnionArray[i];
                 }
                 break;
             case EOpSub:
                 tempConstArray = new ConstantUnion[objectSize];
                 {// support MSVC++6.0
-                    for (int i = 0; i < objectSize; i++)
+                    for (size_t i = 0; i < objectSize; i++)
                         tempConstArray[i] = unionArray[i] - rightUnionArray[i];
                 }
                 break;
@@ -1117,7 +1117,7 @@
             case EOpMatrixTimesScalar:
                 tempConstArray = new ConstantUnion[objectSize];
                 {// support MSVC++6.0
-                    for (int i = 0; i < objectSize; i++)
+                    for (size_t i = 0; i < objectSize; i++)
                         tempConstArray[i] = unionArray[i] * rightUnionArray[i];
                 }
                 break;
@@ -1142,7 +1142,7 @@
             case EOpDiv:
                 tempConstArray = new ConstantUnion[objectSize];
                 {// support MSVC++6.0
-                    for (int i = 0; i < objectSize; i++) {
+                    for (size_t i = 0; i < objectSize; i++) {
                         switch (getType().getBasicType()) {
             case EbtFloat:
                 if (rightUnionArray[i] == 0.0f) {
@@ -1208,7 +1208,7 @@
             case EOpLogicalAnd: // this code is written for possible future use, will not get executed currently
                 tempConstArray = new ConstantUnion[objectSize];
                 {// support MSVC++6.0
-                    for (int i = 0; i < objectSize; i++)
+                    for (size_t i = 0; i < objectSize; i++)
                         tempConstArray[i] = unionArray[i] && rightUnionArray[i];
                 }
                 break;
@@ -1216,7 +1216,7 @@
             case EOpLogicalOr: // this code is written for possible future use, will not get executed currently
                 tempConstArray = new ConstantUnion[objectSize];
                 {// support MSVC++6.0
-                    for (int i = 0; i < objectSize; i++)
+                    for (size_t i = 0; i < objectSize; i++)
                         tempConstArray[i] = unionArray[i] || rightUnionArray[i];
                 }
                 break;
@@ -1224,7 +1224,7 @@
             case EOpLogicalXor:
                 tempConstArray = new ConstantUnion[objectSize];
                 {// support MSVC++6.0
-                    for (int i = 0; i < objectSize; i++)
+                    for (size_t i = 0; i < objectSize; i++)
                         switch (getType().getBasicType()) {
             case EbtBool: tempConstArray[i].setBConst((unionArray[i] == rightUnionArray[i]) ? false : true); break;
             default: assert(false && "Default missing");
@@ -1270,7 +1270,7 @@
                     if (!CompareStructure(node->getType(), node->getUnionArrayPointer(), unionArray))
                         boolNodeFlag = true;
                 } else {
-                    for (int i = 0; i < objectSize; i++) {
+                    for (size_t i = 0; i < objectSize; i++) {
                         if (unionArray[i] != rightUnionArray[i]) {
                             boolNodeFlag = true;
                             break;  // break out of for loop
@@ -1296,7 +1296,7 @@
                     if (CompareStructure(node->getType(), node->getUnionArrayPointer(), unionArray))
                         boolNodeFlag = true;
                 } else {
-                    for (int i = 0; i < objectSize; i++) {
+                    for (size_t i = 0; i < objectSize; i++) {
                         if (unionArray[i] == rightUnionArray[i]) {
                             boolNodeFlag = true;
                             break;  // break out of for loop
@@ -1331,7 +1331,7 @@
         //
         TIntermConstantUnion *newNode = 0;
         ConstantUnion* tempConstArray = new ConstantUnion[objectSize];
-        for (int i = 0; i < objectSize; i++) {
+        for (size_t i = 0; i < objectSize; i++) {
             switch(op) {
                 case EOpNegative:
                     switch (getType().getBasicType()) {
@@ -1362,11 +1362,11 @@
 
 TIntermTyped* TIntermediate::promoteConstantUnion(TBasicType promoteTo, TIntermConstantUnion* node)
 {
-    int size = node->getType().getObjectSize();
+    size_t size = node->getType().getObjectSize();
 
     ConstantUnion *leftUnionArray = new ConstantUnion[size];
 
-    for (int i=0; i < size; i++) {
+    for (size_t i = 0; i < size; i++) {
 
         switch (promoteTo) {
             case EbtFloat:
diff --git a/src/compiler/OutputGLSLBase.cpp b/src/compiler/OutputGLSLBase.cpp
index 169e79f..35901cf 100644
--- a/src/compiler/OutputGLSLBase.cpp
+++ b/src/compiler/OutputGLSLBase.cpp
@@ -152,10 +152,10 @@
     }
     else
     {
-        int size = type.getObjectSize();
+        size_t size = type.getObjectSize();
         bool writeType = size > 1;
         if (writeType) out << getTypeName(type) << "(";
-        for (int i = 0; i < size; ++i, ++pConstUnion)
+        for (size_t i = 0; i < size; ++i, ++pConstUnion)
         {
             switch (pConstUnion->getType())
             {
diff --git a/src/compiler/OutputHLSL.cpp b/src/compiler/OutputHLSL.cpp
index ab34b3b..c7ce5f2 100644
--- a/src/compiler/OutputHLSL.cpp
+++ b/src/compiler/OutputHLSL.cpp
@@ -2304,11 +2304,12 @@
 {
     TString string;
 
-    for (int component = 0; component < type.getObjectSize(); component++)
+    size_t size = type.getObjectSize();
+    for (size_t component = 0; component < size; component++)
     {
         string += "0";
 
-        if (component < type.getObjectSize() - 1)
+        if (component + 1 < size)
         {
             string += ", ";
         }
@@ -2458,27 +2459,30 @@
     }
     else
     {
-        int remainingComponents = ctorType.getObjectSize();
-        int parameterIndex = 0;
+        size_t remainingComponents = ctorType.getObjectSize();
+        size_t parameterIndex = 0;
 
         while (remainingComponents > 0)
         {
             const TType &parameter = ctorParameters[parameterIndex];
-            bool moreParameters = parameterIndex < (int)ctorParameters.size() - 1;
+            const size_t parameterSize = parameter.getObjectSize();
+            bool moreParameters = parameterIndex + 1 < ctorParameters.size();
 
             constructor += "x" + str(parameterIndex);
 
             if (parameter.isScalar())
             {
-                remainingComponents -= parameter.getObjectSize();
+                ASSERT(parameterSize <= remainingComponents);
+                remainingComponents -= parameterSize;
             }
             else if (parameter.isVector())
             {
-                if (remainingComponents == parameter.getObjectSize() || moreParameters)
+                if (remainingComponents == parameterSize || moreParameters)
                 {
-                    remainingComponents -= parameter.getObjectSize();
+                    ASSERT(parameterSize <= remainingComponents);
+                    remainingComponents -= parameterSize;
                 }
-                else if (remainingComponents < parameter.getNominalSize())
+                else if (remainingComponents < static_cast<size_t>(parameter.getNominalSize()))
                 {
                     switch (remainingComponents)
                     {
@@ -2495,9 +2499,10 @@
             }
             else if (parameter.isMatrix() || parameter.getStruct())
             {
-                ASSERT(remainingComponents == parameter.getObjectSize() || moreParameters);
+                ASSERT(remainingComponents == parameterSize || moreParameters);
+                ASSERT(parameterSize <= remainingComponents);
                 
-                remainingComponents -= parameter.getObjectSize();
+                remainingComponents -= parameterSize;
             }
             else UNREACHABLE();
 
@@ -2554,7 +2559,7 @@
     }
     else
     {
-        int size = type.getObjectSize();
+        size_t size = type.getObjectSize();
         bool writeType = size > 1;
         
         if (writeType)
@@ -2562,7 +2567,7 @@
             out << typeString(type) << "(";
         }
 
-        for (int i = 0; i < size; i++, constUnion++)
+        for (size_t i = 0; i < size; i++, constUnion++)
         {
             switch (constUnion->getType())
             {
diff --git a/src/compiler/ParseHelper.cpp b/src/compiler/ParseHelper.cpp
index 2dbda03..b7abb83 100644
--- a/src/compiler/ParseHelper.cpp
+++ b/src/compiler/ParseHelper.cpp
@@ -489,7 +489,7 @@
     // again, there is an extra argument, so 'overfull' will become true.
     //
 
-    int size = 0;
+    size_t size = 0;
     bool constType = true;
     bool full = false;
     bool overFull = false;
@@ -1036,13 +1036,7 @@
             return true;
         }
         if (initializer->getAsConstantUnion()) { 
-            ConstantUnion* unionArray = variable->getConstPointer();
-
-            if (type.getObjectSize() == 1 && type.getBasicType() != EbtStruct) {
-                *unionArray = (initializer->getAsConstantUnion()->getUnionArrayPointer())[0];
-            } else {
-                variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
-            }
+            variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
         } else if (initializer->getAsSymbolNode()) {
             const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol());
             const TVariable* tVar = static_cast<const TVariable*>(symbol);
@@ -1315,7 +1309,7 @@
     ConstantUnion* constArray = new ConstantUnion[fields.num];
 
     for (int i = 0; i < fields.num; i++) {
-        if (fields.offsets[i] >= node->getType().getObjectSize()) {
+        if (fields.offsets[i] >= node->getType().getNominalSize()) {
             std::stringstream extraInfoStream;
             extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
             std::string extraInfo = extraInfoStream.str();
@@ -1388,9 +1382,8 @@
         index = 0;
     }
 
-    int arrayElementSize = arrayElementType.getObjectSize();
-
     if (tempConstantNode) {
+         size_t arrayElementSize = arrayElementType.getObjectSize();
          ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
          typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
     } else {
@@ -1412,12 +1405,9 @@
 TIntermTyped* TParseContext::addConstStruct(TString& identifier, TIntermTyped* node, const TSourceLoc& line)
 {
     const TTypeList* fields = node->getType().getStruct();
-    TIntermTyped *typedNode;
-    int instanceSize = 0;
-    unsigned int index = 0;
-    TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
 
-    for ( index = 0; index < fields->size(); ++index) {
+    size_t instanceSize = 0;
+    for (size_t index = 0; index < fields->size(); ++index) {
         if ((*fields)[index]->getFieldName() == identifier) {
             break;
         } else {
@@ -1425,6 +1415,8 @@
         }
     }
 
+    TIntermTyped* typedNode = 0;
+    TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
     if (tempConstantNode) {
          ConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
 
diff --git a/src/compiler/SymbolTable.cpp b/src/compiler/SymbolTable.cpp
index cd625da..cbc7deb 100644
--- a/src/compiler/SymbolTable.cpp
+++ b/src/compiler/SymbolTable.cpp
@@ -17,6 +17,7 @@
 
 #include <stdio.h>
 #include <algorithm>
+#include <climits>
 
 TType::TType(const TPublicType &p) :
             type(p.type), precision(p.precision), qualifier(p.qualifier), size(p.size), matrix(p.matrix), array(p.array), arraySize(p.arraySize),
@@ -69,16 +70,44 @@
     }
 }
 
-int TType::getStructSize() const
+size_t TType::getObjectSize() const
+{
+    size_t totalSize = 0;
+
+    if (getBasicType() == EbtStruct)
+        totalSize = getStructSize();
+    else if (matrix)
+        totalSize = size * size;
+    else
+        totalSize = size;
+
+    if (isArray()) {
+        size_t arraySize = std::max(getArraySize(), getMaxArraySize());
+        if (arraySize > INT_MAX / totalSize)
+            totalSize = INT_MAX;
+        else
+            totalSize *= arraySize;
+    }
+
+    return totalSize;
+}
+
+size_t TType::getStructSize() const
 {
     if (!getStruct()) {
         assert(false && "Not a struct");
         return 0;
     }
 
-    if (structureSize == 0)
-        for (TTypeList::const_iterator tl = getStruct()->begin(); tl != getStruct()->end(); tl++)
-            structureSize += (*tl)->getObjectSize();
+    if (structureSize == 0) {
+        for (TTypeList::const_iterator tl = getStruct()->begin(); tl != getStruct()->end(); tl++) {
+            size_t fieldSize = (*tl)->getObjectSize();
+            if (fieldSize > INT_MAX - structureSize)
+                structureSize = INT_MAX;
+            else
+                structureSize += fieldSize;
+        }
+    }
 
     return structureSize;
 }
diff --git a/src/compiler/Types.h b/src/compiler/Types.h
index 83ab44c..cb3528c 100644
--- a/src/compiler/Types.h
+++ b/src/compiler/Types.h
@@ -56,22 +56,7 @@
     int getNominalSize() const { return size; }
     void setNominalSize(int s) { size = s; }
     // Full size of single instance of type
-    int getObjectSize() const
-    {
-        int totalSize;
-
-        if (getBasicType() == EbtStruct)
-            totalSize = getStructSize();
-        else if (matrix)
-            totalSize = size * size;
-        else
-            totalSize = size;
-
-        if (isArray())
-            totalSize *= std::max(getArraySize(), getMaxArraySize());
-
-        return totalSize;
-    }
+    size_t getObjectSize() const;
 
     bool isMatrix() const { return matrix ? true : false; }
     void setMatrix(bool m) { matrix = m; }
@@ -173,7 +158,7 @@
 
 private:
     void buildMangledName(TString&);
-    int getStructSize() const;
+    size_t getStructSize() const;
     void computeDeepestStructNesting();
 
     TBasicType type      : 6;
@@ -187,7 +172,7 @@
     TType* arrayInformationType;
 
     TTypeList* structure;      // 0 unless this is a struct
-    mutable int structureSize;
+    mutable size_t structureSize;
     int deepestStructNesting;
 
     TString *fieldName;         // for structure field names
diff --git a/src/compiler/intermOut.cpp b/src/compiler/intermOut.cpp
index 80f45fd..13aa96a 100644
--- a/src/compiler/intermOut.cpp
+++ b/src/compiler/intermOut.cpp
@@ -316,9 +316,9 @@
 {
     TInfoSinkBase& out = sink;
 
-    int size = node->getType().getObjectSize();
+    size_t size = node->getType().getObjectSize();
 
-    for (int i = 0; i < size; i++) {
+    for (size_t i = 0; i < size; i++) {
         OutputTreeText(out, node, depth);
         switch (node->getUnionArrayPointer()[i].getType()) {
             case EbtBool:
diff --git a/src/compiler/parseConst.cpp b/src/compiler/parseConst.cpp
index 4e78e21..1cc5db8 100644
--- a/src/compiler/parseConst.cpp
+++ b/src/compiler/parseConst.cpp
@@ -38,16 +38,16 @@
     bool visitLoop(Visit visit, TIntermLoop*);
     bool visitBranch(Visit visit, TIntermBranch*);
 
-    int index;
+    size_t index;
     ConstantUnion *unionArray;
     TType type;
     TOperator constructorType;
     bool singleConstantParam;
     TInfoSink& infoSink;
     TSymbolTable& symbolTable;
-    int size; // size of the constructor ( 4 for vec4)
+    size_t size; // size of the constructor ( 4 for vec4)
     bool isMatrix;
-    int matrixSize; // dimension of the matrix (nominal size and not the instance size)
+    size_t matrixSize; // dimension of the matrix (nominal size and not the instance size)
 };
 
 //
@@ -159,16 +159,16 @@
     }
 
     ConstantUnion* leftUnionArray = unionArray;
-    int instanceSize = type.getObjectSize();
+    size_t instanceSize = type.getObjectSize();
 
     if (index >= instanceSize)
         return;
 
     if (!singleConstantParam) {
-        int size = node->getType().getObjectSize();
+        size_t size = node->getType().getObjectSize();
     
         ConstantUnion *rightUnionArray = node->getUnionArrayPointer();
-        for (int i=0; i < size; i++) {
+        for (size_t i = 0; i < size; i++) {
             if (index >= instanceSize)
                 return;
             leftUnionArray[index] = rightUnionArray[i];
@@ -176,11 +176,11 @@
             (index)++;
         }
     } else {
-        int totalSize = index + size;
+        size_t totalSize = index + size;
         ConstantUnion *rightUnionArray = node->getUnionArrayPointer();
         if (!isMatrix) {
-            int count = 0;
-            for (int i = index; i < totalSize; i++) {
+            size_t count = 0;
+            for (size_t i = index; i < totalSize; i++) {
                 if (i >= instanceSize)
                     return;
 
@@ -192,9 +192,9 @@
                     count++;
             }
         } else {  // for matrix constructors
-            int count = 0;
-            int element = index;
-            for (int i = index; i < totalSize; i++) {
+            size_t count = 0;
+            size_t element = index;
+            for (size_t i = index; i < totalSize; i++) {
                 if (i >= instanceSize)
                     return;
                 if (element - i == 0 || (i - element) % (matrixSize + 1) == 0 )