| 1145 | } |
| 1146 | |
| 1147 | void SortTree(HLSLTree *tree) |
| 1148 | { |
| 1149 | // Stable sort so that statements are in this order: |
| 1150 | // const scalars for arrays, structs, declarations, functions, techniques. |
| 1151 | // but their relative order is preserved. |
| 1152 | |
| 1153 | HLSLRoot *root = tree->GetRoot(); |
| 1154 | |
| 1155 | HLSLStatement *constScalarDeclarations = NULL; |
| 1156 | HLSLStatement *lastConstScalarDeclaration = NULL; |
| 1157 | |
| 1158 | HLSLStatement *structs = NULL; |
| 1159 | HLSLStatement *lastStruct = NULL; |
| 1160 | |
| 1161 | HLSLStatement *constDeclarations = NULL; |
| 1162 | HLSLStatement *lastConstDeclaration = NULL; |
| 1163 | |
| 1164 | HLSLStatement *declarations = NULL; |
| 1165 | HLSLStatement *lastDeclaration = NULL; |
| 1166 | |
| 1167 | HLSLStatement *functions = NULL; |
| 1168 | HLSLStatement *lastFunction = NULL; |
| 1169 | |
| 1170 | HLSLStatement *other = NULL; |
| 1171 | HLSLStatement *lastOther = NULL; |
| 1172 | |
| 1173 | #define AppendToList(statement, list, listLast) \ |
| 1174 | if (list == NULL) list = statement; \ |
| 1175 | if (listLast != NULL) listLast->nextStatement = statement; \ |
| 1176 | listLast = statement; |
| 1177 | |
| 1178 | HLSLStatement *statement = root->statement; |
| 1179 | while (statement != NULL) { |
| 1180 | HLSLStatement *nextStatement = statement->nextStatement; |
| 1181 | statement->nextStatement = NULL; |
| 1182 | |
| 1183 | if (statement->nodeType == HLSLNodeType_Struct) { |
| 1184 | AppendToList(statement, structs, lastStruct); |
| 1185 | } |
| 1186 | else if (statement->nodeType == HLSLNodeType_Declaration || |
| 1187 | statement->nodeType == HLSLNodeType_Buffer) { |
| 1188 | // There are cases where a struct uses a const array size, |
| 1189 | // so those need to be ordered prior to the struct. |
| 1190 | if (statement->nodeType == HLSLNodeType_Declaration) { |
| 1191 | HLSLDeclaration *decl = (HLSLDeclaration *)statement; |
| 1192 | |
| 1193 | if (decl->type.flags & HLSLTypeFlag_Const) { |
| 1194 | // this is a global scalar, so best to order first |
| 1195 | if (IsScalarType(decl->type.baseType)) { |
| 1196 | AppendToList(statement, constScalarDeclarations, lastConstScalarDeclaration); |
| 1197 | } |
| 1198 | else { |
| 1199 | AppendToList(statement, constDeclarations, lastConstDeclaration); |
| 1200 | } |
| 1201 | } |
| 1202 | else { |
| 1203 | AppendToList(statement, declarations, lastDeclaration); |
| 1204 | } |
no test coverage detected