00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #ifndef CFRONT_CANALYZER_HPP
00035 #define CFRONT_CANALYZER_HPP
00036
00037 #include "AstCommon.hpp"
00038 #include <antlr/ASTFactory.hpp>
00039 #include <antlr/RecognitionException.hpp>
00040 #include "GccIntegerAttribute.hpp"
00041 #include "CTokenTypes.hpp"
00042 #include "Environment.hpp"
00043 #include "BasicType.hpp"
00044 #include "PointerType.hpp"
00045 #include "FunctionType.hpp"
00046 #include "ArrayType.hpp"
00047 #include "StructType.hpp"
00048 #include "UnionType.hpp"
00049 #include "EnumType.hpp"
00050 #include "VectorType.hpp"
00051 #include "TypedefType.hpp"
00052 #include "CFrontError.hpp"
00053
00054 #include <cassert>
00055 #include <sstream>
00056 #include <vector>
00057 #include <climits>
00058
00059
00060 namespace cfront {
00061
00062 class EvalFailedException: public std::exception {
00063 public:
00064 EvalFailedException(int operatorType)
00065 : std::exception(), operatorType_(operatorType) {}
00066 int getOperatorType() const { return operatorType_; }
00067 protected:
00068 int operatorType_;
00069 };
00070
00072 struct DeclSpec {
00073 Type* type;
00074 NameDescription::StorageClassSpecTag ssTag;
00075 DeclSpec() : type(NULL), ssTag(NameDescription::SS_NONE) {}
00076 };
00077
00079 enum PpuVectorExtTag {
00080 ALTIVEC_NONE,
00081 ALTIVEC_VECTOR,
00082 ALTIVEC_PIXEL,
00083 ALTIVEC_BOOL
00084 };
00085
00087 enum AssignmentErrorTag {
00088 ASSIGN_ERR_NO_ERROR,
00089 ASSIGN_ERR_CONST,
00090 ASSIGN_ERR_INCOMPATIBLE,
00091 ASSIGN_ERR_INCOMPATIBLE_POINTER,
00092 ASSIGN_ERR_POINTER_FROM_INTEGER,
00093 ASSIGN_ERR_INTEGER_FROM_POINTER,
00094 };
00095
00096 typedef std::vector<Extension*> Attributes;
00097
00098 typedef long long int eint;
00099
00100
00104 class CAnalyzer {
00105 public:
00106 void initialize()
00107 {
00108 supportVectorExt_ = true;
00109
00110 currentEnv_ = new Environment(NULL);
00111 currentFunctionType_ = NULL;
00112 nameCount_ = 0;
00113 }
00114
00115 void setASTFactory(antlr::ASTFactory* astFactory)
00116 {
00117 astFactory_ = astFactory;
00118 }
00119
00120 void setSupportVectorExtension(bool b)
00121 {
00122 supportVectorExt_ = b;
00123 }
00124
00125 bool isSupportedVectorExt()
00126 {
00127 return supportVectorExt_;
00128 }
00129
00130 bool isTypedefName(const std::string& name)
00131 {
00132 NameDescription* nd;
00133 Environment* env = currentEnv_->lookupOrdinaryName(name, &nd);
00134 if (env != NULL) {
00135 #ifdef PARSER_DEBUG
00136 std::cout << "Search typedef name: " << name << " [" <<
00137 (nd->getStorageClassSpec() == NameDescription::TYPEDEF)
00138 << "]\n";
00139 #endif
00140 return nd->getStorageClassSpec() == NameDescription::TYPEDEF;
00141 } else {
00142
00143
00144
00145 return false;
00146
00147 }
00148 }
00149
00154 const Type* getOriginalType(const Type* t, bool withFuncArrPointerConv)
00155 {
00156 const Type* retType;
00157 const TypedefType* tyd = dynamic_cast<const TypedefType*>(t);
00158 if (tyd != NULL) {
00159 Type* qualifiedBaseType = tyd->getBaseType()->clone();
00160 addQualifier(qualifiedBaseType, tyd->getQualFlags());
00161 retType = getOriginalType(qualifiedBaseType,
00162 withFuncArrPointerConv);
00163 } else {
00164 retType = t;
00165 }
00166 if (withFuncArrPointerConv) {
00167 const FunctionType* ft = dynamic_cast<const FunctionType*>(retType);
00168 if (ft != NULL) {
00169 return new PointerType(ft, 0);
00170 }
00171 const ArrayType* at = dynamic_cast<const ArrayType*>(retType);
00172 if (at != NULL) {
00173 const Type* bt = getOriginalType(at->getBaseType(), true);
00174 return new PointerType(bt, 0);
00175 }
00176 }
00177 return retType;
00178 }
00179
00180 std::string getDeclaratorName(const antlr::RefAST declNode) const
00181 {
00182 return declNode->toString();
00183 }
00184
00185 Environment* getCurrentEnv() const
00186 {
00187 return currentEnv_;
00188 }
00189
00193 void addEnv()
00194 {
00195 currentEnv_ = new Environment(currentEnv_);
00196 }
00197
00202 Environment* removeEnv()
00203 {
00204 Environment* ret = currentEnv_;
00205 assert(ret != NULL);
00206 currentEnv_ = ret->getOuterEnv();
00207 return ret;
00208 }
00209
00210 void setCurrentFunctionType(FunctionType* ft)
00211 {
00212 currentFunctionType_ = ft;
00213 }
00214
00215 FunctionType* getCurrentFunctionType() const
00216 {
00217 return currentFunctionType_;
00218 }
00219
00231 Type* buildPointerDeclaratorType(std::list<PointerType*>& pointerTypes,
00232 Type**& lastPointerType,
00233 Type* baseType)
00234 {
00235 if (pointerTypes.empty()) {
00236 return baseType;
00237 }
00238 Type* nextType = baseType;
00239 for (std::list<PointerType*>::reverse_iterator i =
00240 pointerTypes.rbegin(); i != pointerTypes.rend(); ++i) {
00241 PointerType* t = *i;
00242 t->setBaseType(nextType);
00243 nextType = t;
00244 }
00245 if (lastPointerType != NULL) {
00246 *lastPointerType = pointerTypes.back();
00247 }
00248 return pointerTypes.front();
00249 }
00250
00258 Type* buildDeclaratorPostfixType(std::list<Type*>& types,
00259 Type**& lastType, Type* baseType)
00260 {
00261 if (types.empty()) {
00262 return baseType;
00263 }
00264 Type* nextType = baseType;
00265 for (std::list<Type*>::reverse_iterator i = types.rbegin();
00266 i != types.rend(); ++i) {
00267 Type* t = *i;
00268 ArrayType* at;
00269 FunctionType* ft;
00270 if ((at = dynamic_cast<ArrayType*>(t)) != NULL) {
00271 at->setBaseType(nextType);
00272 } else if ((ft = dynamic_cast<FunctionType*>(t)) != NULL) {
00273 ft->setReturnType(nextType);
00274 }
00275 nextType = t;
00276 }
00277 if (lastType != NULL && *lastType == NULL) {
00278 *lastType = types.back();
00279 }
00280 return types.front();
00281 }
00282
00286 Type* buildCompositeDeclaratorType(Type* innerLastType,
00287 Type* innerDeclType, Type* baseType)
00288 {
00289 PointerType* pt = dynamic_cast<PointerType*>(innerLastType);
00290 if (pt != NULL) {
00291 pt->setBaseType(baseType);
00292 return innerDeclType;
00293 }
00294 FunctionType* ft = dynamic_cast<FunctionType*>(innerLastType);
00295 if (ft != NULL) {
00296 ft->setReturnType(baseType);
00297 return innerDeclType;
00298 }
00299 ArrayType* at = dynamic_cast<ArrayType*>(innerLastType);
00300 if (at != NULL) {
00301 at->setBaseType(baseType);
00302 return innerDeclType;
00303 } else {
00304 return baseType;
00305 }
00306 }
00307
00311 void addQualifier(Type* t, QualFlags qflags)
00312 {
00313 #define MERGE_QFS(t, qf) (t)->setQualFlags((t)->getQualFlags() | (qf))
00314 BasicType* bt = dynamic_cast<BasicType*>(t);
00315 if (bt != NULL) {
00316 MERGE_QFS(bt, qflags);
00317 return;
00318 }
00319 PointerType* pt = dynamic_cast<PointerType*>(t);
00320 if (pt != NULL) {
00321 MERGE_QFS(pt, qflags);
00322 return;
00323 }
00324 FieldInclusionType* ft = dynamic_cast<FieldInclusionType*>(t);
00325 if (ft != NULL) {
00326 MERGE_QFS(ft, qflags);
00327 return;
00328 }
00329 EnumType* et = dynamic_cast<EnumType*>(t);
00330 if (et != NULL) {
00331 MERGE_QFS(et, qflags);
00332 return;
00333 }
00334 TypedefType* tt = dynamic_cast<TypedefType*>(t);
00335 if (tt != NULL) {
00336 MERGE_QFS(tt, qflags);
00337 return;
00338 }
00339 VectorType* vt = dynamic_cast<VectorType*>(t);
00340 if (vt != NULL) {
00341 MERGE_QFS(vt, qflags);
00342 return;
00343 }
00344 #undef MERGE_QFS
00345 }
00346
00347 bool addNameAndType(const std::string& name, Type* t,
00348 NameDescription::StorageClassSpecTag ssTag,
00349 NameDescription::DeclarationLocation dloc,
00350 Attributes* attrs)
00351 {
00352 if (ssTag == NameDescription::TYPEDEF) {
00353 #ifdef PARSER_DEBUG
00354 std::cout << "Added typedef name: " << name << std::endl;
00355 #endif
00356 TypeDefinition* def = new TypeDefinition(t, name);
00357 t = new TypedefType(def);
00358 } else {
00359 #ifdef PARSER_DEBUG
00360 std::cout << "Added ordinary name: " << name << " [" <<
00361 t->toString() << "]\n";
00362 #endif
00363 }
00364 #ifdef PARSER_DEBUG
00365 std::cout << currentEnv_->toString() << std::endl;
00366 #endif
00367 NameDescription* nd;
00368 if (currentEnv_->addOrdinaryName(name, t, ssTag, dloc, &nd)) {
00369 if (attrs != NULL) {
00370 setVariableAttribute(nd, *attrs);
00371 }
00372 return true;
00373 } else {
00374 return false;
00375 }
00376 }
00377
00378 void makeBasicType(Type** t, BasicType::PrimitiveTag primTypeTag)
00379 {
00380 if (*t != NULL) {
00381 delete *t;
00382 }
00383 *t = new BasicType(primTypeTag);
00384 }
00385
00386 bool isGccBuiltinName(const std::string& name) const;
00387
00388 Extension* parseGccAttribute(const std::string& name, RefNode paramNode,
00389 RefNode locationNode)
00390 {
00391 if (name == "aligned") {
00392 if (paramNode != NULL) {
00393 try {
00394 eint v = evalConstExpr(antlr::RefAST(paramNode->getFirstChild()));
00395 return new GccIntegerAttribute("aligned", v);
00396 } catch (const EvalFailedException& e) {
00397 }
00398 }
00399 WARNING((ctrump_error_code) -1, "syntax error of attribute `aligned'"
00400 , logs_, locationNode);
00401 } else if (name == "packed" || name == "__packed__") {
00402 return new GccIntegerAttribute("packed", 1);
00403 } else if (name == "altivec") {
00404 if (paramNode->getType() == CTokenTypes::NAttributeParamIdentifier) {
00405 const std::string& vecName =
00406 paramNode->getFirstChild()->getText();
00407 enum PpuVectorExtTag vt = ALTIVEC_NONE;
00408 if (vecName == "vector__") {
00409 vt = ALTIVEC_VECTOR;
00410 } else if (vecName == "pixel__") {
00411 vt = ALTIVEC_PIXEL;
00412 } else if (vecName == "bool__") {
00413 vt = ALTIVEC_BOOL;
00414 } else {
00415 WARNING((ctrump_error_code) -1, "ignored unknown altivec attribute"
00416 , logs_, locationNode);
00417 }
00418 if (vt != ALTIVEC_NONE) {
00419 return new GccIntegerAttribute("altivec", vt);
00420 }
00421 WARNING((ctrump_error_code) -1, "ignored unsupported altivec attribute"
00422 , logs_, locationNode);
00423 }
00424 } else if (name == "spu_vector") {
00425 return new GccIntegerAttribute("spu_vector", 1);
00426 } else {
00427 WARNING((ctrump_error_code) -1, "ignored unknown attribute"
00428 , logs_, locationNode);
00429 }
00430 return NULL;
00431 }
00432
00433 void setVariableAttribute(NameDescription* nd, Attributes& attrs)
00434 {
00435 for (Attributes::iterator i = attrs.begin(); i != attrs.end(); ++i) {
00436 GccIntegerAttribute* gattr = dynamic_cast<GccIntegerAttribute*>(*i);
00437 if (gattr != NULL) {
00438 const std::string& name = gattr->getName();
00439 if (name == "aligned" || name == "packed") {
00440 nd->addExtension(gattr);
00441 } else {
00442 #ifdef PARSER_DEBUG
00443 std::cout << "Ignored non-variable attribute `" << name
00444 << "'\n";
00445 #endif
00446 }
00447 }
00448 }
00449 }
00450
00455 Type* setTypeAttribute(Type* t, Attributes& attrs, RefNode locationNode)
00456 {
00457 Type* rt = t;
00458 for (Attributes::iterator i = attrs.begin(); i != attrs.end(); ++i) {
00459 GccIntegerAttribute* gattr = dynamic_cast<GccIntegerAttribute*>(*i);
00460 if (gattr != NULL) {
00461 const std::string& name = gattr->getName();
00462 if (name == "aligned" || name == "packed") {
00463 t->addExtension(gattr);
00464 } else if (name == "altivec" || name == "spu_vector") {
00465 BasicType* bt = dynamic_cast<BasicType*>(t);
00466 if (bt == NULL) {
00467 ERROR(CTRUMP_ERR_ILLEGAL_VECTOR_TYPE, "vector types must be a non-typedefed arithmetic type"
00468 , logs_, locationNode);
00469 }
00470 BasicType::PrimitiveTag pt = bt->getPrimitiveTag();
00471 if (name == "altivec") {
00472 switch (gattr->getValue()) {
00473 case ALTIVEC_VECTOR:
00474 rt = new VectorType(pt);
00475 break;
00476 case ALTIVEC_BOOL:
00477 case ALTIVEC_PIXEL:
00478 default:
00479
00480 throw InternalError("setTypeAttribute: unsupported ALTIVEC type", &locationNode->getLocation());
00481 break;
00482 }
00483 } else {
00484 rt = new VectorType(pt);
00485 }
00486 } else {
00487 #ifdef PARSER_DEBUG
00488 std::cout << "Ignored non-typed attribute `" << name
00489 << "'\n";
00490 #endif
00491 }
00492 }
00493 }
00494 return rt;
00495 }
00496
00497 FieldInclusionType*
00498 checkStructOrUnionDefinition(const std::string& name, bool isStruct,
00499 bool isDefinition, RefNode locationNode)
00500 {
00501 FieldInclusionType* ft = NULL;
00502 NameDescription* nd = NULL;
00503 Environment* foundEnv = currentEnv_->lookupTagName(name, &nd);
00504 if (foundEnv == NULL) {
00505 if (isStruct) {
00506 StructDefinition* def = new StructDefinition(name);
00507 ft = new StructType(def);
00508 } else {
00509 UnionDefinition* def = new UnionDefinition(name);
00510 ft = new UnionType(def);
00511 }
00512 currentEnv_->addTagName(name, ft, NameDescription::NONE);
00513 } else {
00514 Type* t = nd->getType();
00515 if (isStruct) {
00516 StructType* st = dynamic_cast<StructType*>(t);
00517 if (st == NULL) {
00518 ERROR1(CTRUMP_ERR_WRONG_TAG_NAME, "`%s' defined as wrong kind of tag"
00519 , logs_, locationNode);
00520 }
00521 ft = st->clone();
00522 } else {
00523 UnionType* ut = dynamic_cast<UnionType*>(t);
00524 if (ut == NULL) {
00525 ERROR1(CTRUMP_ERR_WRONG_TAG_NAME, "`%s' defined as wrong kind of tag"
00526 , logs_, locationNode);
00527 }
00528 ft = ut->clone();
00529 }
00530 if (isDefinition && ! ft->getDefinition()->isIncomplete()) {
00531 ERROR1(CTRUMP_ERR_REDEFINITION_TAG, "redefinition of `%s'"
00532 , logs_, locationNode);
00533 }
00534 }
00535 return ft;
00536 }
00537
00538 void initializeTypes();
00539
00543 std::string generateName()
00544 {
00545 std::stringstream ss;
00546 ss << "#" << nameCount_++;
00547 return ss.str();
00548 }
00549
00553 static eint parseNumber(std::string::const_iterator& i, int base)
00554 {
00555
00556 char buf[256];
00557 int idx = 0;
00558 char c;
00559 while (isxdigit(c = *i)) {
00560 buf[idx] = c;
00561 ++idx;
00562 ++i;
00563 }
00564 buf[idx] = '\0';
00565 return std::strtol(buf, NULL, base);
00566 }
00567
00568 static eint parseEscape(std::string::const_iterator& i)
00569 {
00570 char c = *i;
00571 eint retVal;
00572 switch (c) {
00573 case 'a': retVal = '\007'; break;
00574 case 'b': retVal = '\b'; break;
00575 case 'f': retVal = '\f'; break;
00576 case 'n': retVal = '\n'; break;
00577 case 'r': retVal = '\r'; break;
00578 case 't': retVal = '\t'; break;
00579 case 'v': retVal = '\013'; break;
00580 case '"': retVal = '\"'; break;
00581 case '\'': retVal = '\''; break;
00582 case '\\': retVal = '\\'; break;
00583 case '?': retVal = '?'; break;
00584
00585 case 'u':
00586 case 'U':
00587 return parseNumber(++i, 16);
00588 case 'x':
00589 return parseNumber(++i, 16);
00590 default:
00591 if ('0' <= c && c <= '7') {
00592 return parseNumber(i, 8);
00593 }
00594 break;
00595 }
00596 ++i;
00597 return retVal;
00598 }
00599
00600 void parseCharConst(RefNode cNode)
00601 {
00602 const std::string& litStr = cNode->getText();
00603 std::string::const_iterator i = litStr.begin();
00604 eint val = 0;
00605 if (*i == 'L') ++i;
00606 ++i;
00607 if (*i == '\\') {
00608 val = parseEscape(++i);
00609 } else {
00610 val = *i;
00611 }
00612 cNode->getValueRef().signedValue.type = new BasicType(BasicType::SINT);
00613 cNode->getValueRef().signedValue.ival = val;
00614 }
00615
00616 static void parseDoubleQuotedString(std::string::const_iterator& i,
00617 std::stringstream& ss)
00618 {
00619 assert(*i == '"');
00620 ++i;
00621 char c;
00622 while ((c = *i) != '"') {
00623 if (*i == '\\') {
00624 ss << (char) parseEscape(++i);
00625 } else {
00626 ss << *i;
00627 ++i;
00628 }
00629 }
00630 ++i;
00631 }
00632
00633 void parseStringConstant(RefNode sNode, std::stringstream& ss,
00634 bool* isWide )
00635 {
00636 const std::string& litStr = sNode->getText();
00637 std::string::const_iterator i = litStr.begin();
00638 size_t len = litStr.size();
00639 assert(len >= 2);
00640 if (*i == 'L') {
00641 *isWide = true;
00642 ++i;
00643 }
00644 parseDoubleQuotedString(i, ss);
00645 }
00646
00647 void parseIntegerConstant(RefNode intNode, int base)
00648 {
00649 std::string str = intNode->getText();
00650 char* endptr;
00651 long long val = strtoll(str.c_str(), &endptr, base);
00652
00653 bool isUnsigned = false;
00654 unsigned int numLongPostfix = 0;
00655 char postfix;
00656 while ((postfix = *endptr) != '\0') {
00657 switch (postfix) {
00658 case 'U':
00659 case 'u':
00660 if (numLongPostfix != 0) {
00661
00662 ERROR1(CTRUMP_ERR_INVALID_CONSTANT, "invalid integer constant `%s'"
00663 , logs_, intNode);
00664 } else {
00665 isUnsigned = true;
00666 }
00667 break;
00668 case 'L':
00669 case 'l':
00670 numLongPostfix++;
00671 break;
00672 default:
00673 ERROR1(CTRUMP_ERR_INVALID_CONSTANT, "invalid integer constant `%s'"
00674 , logs_, intNode);
00675 }
00676 ++endptr;
00677 }
00678 if (numLongPostfix > 2) {
00679 ERROR1(CTRUMP_ERR_INVALID_CONSTANT, "invalid integer constant `%s'"
00680 , logs_, intNode);
00681 }
00682
00683
00684 long long targetIntMax = INT_MAX;
00685 long long targetUintMax = UINT_MAX;
00686 long long targetLongMax = LONG_MAX;
00687 unsigned long long targetUlongMax = ULONG_MAX;
00688 unsigned long long targetLLongMax = LLONG_MAX;
00689
00690 int ttype = intNode->getType();
00691
00692 if (ttype == CTokenTypes::OctalIntConst && val == 0) {
00693 ttype = CTokenTypes::DecimalIntConst;
00694 }
00695
00696 BasicType::PrimitiveTag pt = BasicType::NONE;
00697 if (ttype == CTokenTypes::DecimalIntConst && !isUnsigned) {
00698 if (numLongPostfix >= 1 || val > targetIntMax) {
00699 if (numLongPostfix == 2 || val > targetLongMax) {
00700 pt = BasicType::SLLONG;
00701 intNode->getValueRef().signedValue.ival = val;
00702 } else {
00703 pt = BasicType::SLONG;
00704 intNode->getValueRef().signedValue.ival = val;
00705 }
00706 } else {
00707 pt = BasicType::SINT;
00708 intNode->getValueRef().signedValue.ival = val;
00709 }
00710 } else if (ttype == CTokenTypes::OctalIntConst &&
00711 ttype == CTokenTypes::HexadecimalIntConst &&
00712 !isUnsigned) {
00713 if (numLongPostfix >= 1 || val > targetUintMax) {
00714 unsigned long long ullval = strtoull(str.c_str(), NULL, base);
00715 if (numLongPostfix == 2 || ullval > targetUlongMax) {
00716 if (ullval > targetLLongMax) {
00717 pt = BasicType::ULLONG;
00718 intNode->getValueRef().unsignedValue.ival = val;
00719 } else {
00720 pt = BasicType::SLLONG;
00721 intNode->getValueRef().signedValue.ival = val;
00722 }
00723 } else if (val > targetLongMax) {
00724 pt = BasicType::ULONG;
00725 intNode->getValueRef().unsignedValue.ival = val;
00726 } else {
00727 pt = BasicType::SLONG;
00728 intNode->getValueRef().signedValue.ival = val;
00729 }
00730 } else if (val > targetIntMax) {
00731 pt = BasicType::UINT;
00732 intNode->getValueRef().unsignedValue.ival = val;
00733 } else {
00734 pt = BasicType::SINT;
00735 intNode->getValueRef().signedValue.ival = val;
00736 }
00737 } else {
00738
00739 if (numLongPostfix >= 1 || val > targetUintMax) {
00740 unsigned long long ullval = strtoull(str.c_str(), NULL, base);
00741 if (numLongPostfix == 2 || ullval > targetUlongMax) {
00742 pt = BasicType::ULLONG;
00743 intNode->getValueRef().unsignedValue.ival = val;
00744 } else {
00745 pt = BasicType::ULONG;
00746 intNode->getValueRef().unsignedValue.ival = val;
00747 }
00748 } else {
00749 pt = BasicType::UINT;
00750 intNode->getValueRef().unsignedValue.ival = val;
00751 }
00752 }
00753
00754 if (pt != BasicType::NONE) {
00755 intNode->getValueRef().signedValue.type = new BasicType(pt);
00756 } else {
00757 throw InternalError("parseIntegerConstant");
00758 }
00759 }
00760
00761 bool isConstantZeroValue(RefNode constExprNode)
00762 {
00763 const Type* t = constExprNode->getValueRef().type;
00764 const BasicType* bt =
00765 dynamic_cast<const BasicType*>(getOriginalType(t, true));
00766 if (bt == NULL) {
00767 return false;
00768 }
00769 if (! BasicType::isIntegerTypeTag(bt->getPrimitiveTag())) {
00770 return false;
00771 }
00772 eint v;
00773 try {
00774 v = evalConstExpr(antlr::RefAST(constExprNode));
00775 } catch (const EvalFailedException& e) {
00776 return false;
00777 }
00778 return v == 0;
00779 }
00780
00787 RefNode makeImplicitCastNode(RefNode expr,
00788 const Type* castTo, const Type* rand)
00789 {
00790 #ifdef NO_IMPLICIT_CAST
00791 (void) castTo;
00792 (void) rand;
00793 return expr;
00794 #else
00795 if (!castTo->isCompatibleType(rand)) {
00796
00797 RefNode n = RefValueNode(astFactory_->make((new antlr::ASTArray(2))->add(antlr::RefAST(astFactory_->create(CTokenTypes::NImplicitCast,"ICast")))->add(antlr::RefAST(expr))));
00798 n->getValueRef().type = castTo;
00799 return n;
00800 } else {
00801 return expr;
00802 }
00803 #endif
00804 }
00805
00806 const Type* getInitializerListElemType(const Type* listType, int elemIndex,
00807 RefNode locationNode)
00808 {
00809 const Type* ot = getOriginalType(listType, false);
00810 const ArrayType* at = dynamic_cast<const ArrayType*>(ot);
00811 if (at != NULL) {
00812 return at->getBaseType();
00813 }
00814 const StructType* st = dynamic_cast<const StructType*>(ot);
00815 if (st != NULL) {
00816 StructDefinition* sd =
00817 static_cast<StructDefinition*>(st->getDefinition());
00818 return sd->getNthFieldType(elemIndex);
00819 }
00820 const VectorType* vt = dynamic_cast<const VectorType*>(ot);
00821 if (vt != NULL) {
00822
00823 return new BasicType(vt->getPrimitiveTag());
00824 }
00825 if (elemIndex == 0) {
00826 return listType;
00827 }
00828
00829 WARNING((ctrump_error_code) -1, "excess elements in scalar initializer"
00830 , logs_, locationNode);
00831 return listType;
00832 }
00833
00834 void checkArrayInitializer(Type* t, int numElems, RefNode locationNode)
00835 {
00836 ArrayType* at = dynamic_cast<ArrayType*>(t);
00837 if (at != NULL) {
00838 ArrayType::LengthSpec lspec = at->getLengthSpec();
00839 switch (lspec) {
00840 case ArrayType::NOT_SPECIFIED:
00841 at->setLengthValue(numElems);
00842 break;
00843 case ArrayType::FIXED:
00844 {
00845 int len = at->getLengthValue();
00846 if (numElems > len) {
00847
00848 ERROR(CTRUMP_ERR_EXCESS_INITIALIZER_ELEM, "excess elements in array initializer"
00849 , logs_, locationNode);
00850 }
00851 }
00852 break;
00853 case ArrayType::VARIABLE:
00854 case ArrayType::VARIABLE_NOT_SPECIFIED:
00855 ERROR(CTRUMP_ERR_VLA_WITH_INITIALIZER, "variable-sized object may not be initialized"
00856 , logs_, locationNode);
00857 break;
00858 case ArrayType::INCOMPLETE:
00859
00860 break;
00861 }
00862 }
00863 }
00864
00870 const Type* checkPointerArithmeticAdditive(const Type* pt, const Type* it)
00871 {
00872 const PointerType* pt1 = dynamic_cast<const PointerType*>(pt);
00873 if (pt1 != NULL && it->isIntegerType()) {
00874 return pt1->getBaseType();
00875 }
00876 return NULL;
00877 }
00878
00879 const Type* checkPointerSubtraction(const Type* lt, const Type* rt)
00880 {
00881 const PointerType* plt = dynamic_cast<const PointerType*>(lt);
00882 const PointerType* prt = dynamic_cast<const PointerType*>(rt);
00883 if (plt != NULL && prt != NULL && plt->isCompatibleType(prt)) {
00884
00885 return new BasicType(BasicType::SLONG);
00886 }
00887 return NULL;
00888 }
00889
00890 const Type* checkBinaryArithmetic(const Type* lt, const Type* rt,
00891 RefNode locationNode)
00892 {
00893 BasicType::PrimitiveTag pt = typeBinaryExpr(lt, rt);
00894 if (pt == BasicType::NONE) {
00895
00896 ERROR1(CTRUMP_ERR_INVALID_OPERAND, "invalid operands to binary `%s'"
00897 , logs_, locationNode);
00898 throw UncontinuableError();
00899 }
00900 return new BasicType(pt);
00901 }
00902
00903
00904 const Type* typeCastExpr(RefNode ec, RefNode es)
00905 {
00906 const Type* tc = getOriginalType(ec->getValueRef().type, true);
00907 const Type* ts = convertUnaryExprType(es->getValueRef().type);
00908 ts = getOriginalType(ts, true);
00909 assert(tc != NULL && ts != NULL);
00910
00911 if (tc->isVoidType()) {
00912 return tc;
00913 }
00914 if (tc->isIntegerType()) {
00915 const PointerType* pts = dynamic_cast<const PointerType*>(ts);
00916 if (pts != NULL) {
00917 return tc;
00918 }
00919 }
00920 if (tc->isArithmeticType()) {
00921 if (! ts->isArithmeticType()) {
00922 ERROR(CTRUMP_ERR_INVALID_CAST, "invalid type casting"
00923 , logs_, ec);
00924 throw UncontinuableError();
00925 }
00926 return tc;
00927 }
00928
00929 const PointerType* ptc = dynamic_cast<const PointerType*>(tc);
00930 if (ptc != NULL) {
00931 if (ts->isIntegerType()) {
00932 return tc;
00933 }
00934 const PointerType* pts = dynamic_cast<const PointerType*>(ts);
00935 if (pts == NULL) {
00936 ERROR(CTRUMP_ERR_INVALID_CAST, "invalid type casting"
00937 , logs_, ec);
00938 }
00939 const Type* pbtc = getOriginalType(ptc->getBaseType(), true);
00940 if (pbtc->isVoidType()) {
00941 return tc;
00942 }
00943 const Type* pbts = getOriginalType(pts->getBaseType(), true);
00944 if (pbts->isVoidType()) {
00945 return tc;
00946 }
00947 const VectorType* vbtc = dynamic_cast<const VectorType*>(pbtc);
00948 if (vbtc != NULL) {
00949 if (pbts->isArithmeticType()) {
00950 return tc;
00951 }
00952 }
00953 return tc;
00954 }
00955 const VectorType* vtc = dynamic_cast<const VectorType*>(tc);
00956 if (vtc != NULL) {
00957 const PointerType* pts = dynamic_cast<const PointerType*>(ts);
00958 if (pts != NULL) {
00959
00960 return tc;
00961 }
00962 const VectorType* vts = dynamic_cast<const VectorType*>(ts);
00963 if (vts == NULL) {
00964 ERROR(CTRUMP_ERR_INVALID_CAST, "invalid type casting"
00965 , logs_, ec);
00966 }
00967 return tc;
00968 }
00969 throw InternalError("typeCastExpr", &ec->getLocation());
00970 }
00971
00972 AssignmentErrorTag checkAssignment(const Type* lt, RefNode rn)
00973 {
00974 const Type* rt = getOriginalType(rn->getValueRef().type, true);
00975 const EnumType* let = dynamic_cast<const EnumType*>(lt);
00976 if (let != NULL) {
00977 if ((let->getQualFlags() & Const) != 0) {
00978 return ASSIGN_ERR_CONST;
00979 }
00980 if (rt->isArithmeticType()) {
00981 return ASSIGN_ERR_NO_ERROR;
00982 }
00983 const PointerType* rpt = dynamic_cast<const PointerType*>(rt);
00984 if (rpt != NULL) {
00985 return ASSIGN_ERR_INTEGER_FROM_POINTER;
00986 }
00987 return ASSIGN_ERR_INCOMPATIBLE;
00988 }
00989 const BasicType* lbt = dynamic_cast<const BasicType*>(lt);
00990 if (lbt != NULL) {
00991 if ((lbt->getQualFlags() & Const) != 0) {
00992 return ASSIGN_ERR_CONST;
00993 }
00994 if (rt->isArithmeticType()) {
00995 return ASSIGN_ERR_NO_ERROR;
00996 }
00997 const PointerType* rpt = dynamic_cast<const PointerType*>(rt);
00998 if (rpt != NULL) {
00999 if (lbt->getPrimitiveTag() == BasicType::BOOL) {
01000 return ASSIGN_ERR_NO_ERROR;
01001 } else {
01002 return ASSIGN_ERR_INTEGER_FROM_POINTER;
01003 }
01004 }
01005 return ASSIGN_ERR_INCOMPATIBLE;
01006 }
01007 const FieldInclusionType* lfit =
01008 dynamic_cast<const FieldInclusionType*>(lt);
01009 if (lfit != NULL) {
01010 if ((lfit->getQualFlags() & Const) != 0) {
01011 return ASSIGN_ERR_CONST;
01012 }
01013 if (!lfit->isCompatibleType(rt)) {
01014
01015 }
01016 return ASSIGN_ERR_NO_ERROR;
01017 }
01018 const PointerType* lpt = dynamic_cast<const PointerType*>(lt);
01019 if (lpt != NULL) {
01020 if (isConstantZeroValue(rn)) {
01021 return ASSIGN_ERR_NO_ERROR;
01022 }
01023 const Type* lpbt = getOriginalType(lpt->getBaseType(), true);
01024 const PointerType* rpt = dynamic_cast<const PointerType*>(rt);
01025 if (rpt != NULL) {
01026 if (lpbt->isVoidType()) {
01027 return ASSIGN_ERR_NO_ERROR;
01028 }
01029 const Type* rpbt = getOriginalType(rpt->getBaseType(), true);
01030 if (rpbt->isVoidType()) {
01031 return ASSIGN_ERR_NO_ERROR;
01032 }
01033 if (lpbt->isCompatibleType(rpbt)) {
01034 return ASSIGN_ERR_NO_ERROR;
01035 }
01036 return ASSIGN_ERR_INCOMPATIBLE_POINTER;
01037 }
01038 if (rt->isIntegerType()) {
01039 return ASSIGN_ERR_POINTER_FROM_INTEGER;
01040 }
01041 return ASSIGN_ERR_INCOMPATIBLE;
01042 }
01043 const VectorType* lvt = dynamic_cast<const VectorType*>(lt);
01044 if (lvt != NULL) {
01045 const VectorType* rvt = dynamic_cast<const VectorType*>(rt);
01046 if (rvt != NULL) {
01047 return ASSIGN_ERR_NO_ERROR;
01048 }
01049 return ASSIGN_ERR_INCOMPATIBLE;
01050 }
01051 return ASSIGN_ERR_INCOMPATIBLE;
01052 }
01053
01054 RefNode typeAssignmentExpr(RefNode en)
01055 {
01056 RefNode e1 = RefNode(en->getFirstChild());
01057 RefNode e2 = RefNode(e1->getNextSibling());
01058 RefNode ne1 = typeExpr(e1);
01059 e2 = typeExpr(e2);
01060
01061 const Type* t1 = getOriginalType(ne1->getValueRef().type, true);
01062 const Type* t2 = getOriginalType(e2->getValueRef().type, true);
01063 AssignmentErrorTag err = checkAssignment(t1, e2);
01064 switch (err) {
01065 case ASSIGN_ERR_CONST:
01066 ERROR1(CTRUMP_ERR_VIOLATE_CONSTNESS, "assignment of read-only variable/location `%s'"
01067 , logs_, en);
01068 throw UncontinuableError();
01069 break;
01070 case ASSIGN_ERR_INCOMPATIBLE:
01071 ERROR(CTRUMP_ERR_INCOMPATIBLE_ASSIGN, "incompatible types in assignment"
01072 , logs_, en);
01073 throw UncontinuableError();
01074 break;
01075 case ASSIGN_ERR_INCOMPATIBLE_POINTER:
01076
01077 WARNING((ctrump_error_code) -1, "assignment from incompatible pointer type"
01078 , logs_, en);
01079 break;
01080 case ASSIGN_ERR_POINTER_FROM_INTEGER:
01081
01082 WARNING((ctrump_error_code) -1, "assignment makes pointer from integer without a cast"
01083 , logs_, en);
01084 break;
01085 case ASSIGN_ERR_INTEGER_FROM_POINTER:
01086
01087 WARNING((ctrump_error_code) -1, "assignment makes integer from pointer without a cast"
01088 , logs_, en);
01089 break;
01090 default:
01091 break;
01092 }
01093 e2 = makeImplicitCastNode(e2, t1, t2);
01094 ne1->setNextSibling(antlr::RefAST(e2));
01095 en->setFirstChild(antlr::RefAST(ne1));
01096 en->getValueRef().type = t1;
01097 return en;
01098 }
01099
01100 RefNode typeComparisonExpr(RefNode en)
01101 {
01102 RefNode e1 = RefNode(en->getFirstChild());
01103 RefNode e2 = RefNode(e1->getNextSibling());
01104 RefNode ne1 = typeExpr(e1);
01105 e2 = typeExpr(e2);
01106
01107 const Type* t1 = getOriginalType(ne1->getValueRef().type, true);
01108 const Type* t2 = getOriginalType(e2->getValueRef().type, true);
01109
01110 do {
01111 if (t1->isArithmeticType() && t2->isArithmeticType()) {
01112 break;
01113 }
01114 const PointerType* pt1 = dynamic_cast<const PointerType*>(t1);
01115 const PointerType* pt2 = dynamic_cast<const PointerType*>(t2);
01116 if (pt1 != NULL && pt2 != NULL) {
01117 const Type* bpt1 = getOriginalType(pt1->getBaseType(), true);
01118 const Type* bpt2 = getOriginalType(pt2->getBaseType(), true);
01119 if (bpt1->isCompatibleType(bpt2)) {
01120 break;
01121 }
01122 if (bpt1->isVoidType()) {
01123 e2 = makeImplicitCastNode(e2, t1, t2);
01124 break;
01125 }
01126 if (bpt2->isVoidType()) {
01127 ne1 = makeImplicitCastNode(ne1, t2, t1);
01128 break;
01129 }
01130 } else if (pt1 != NULL && isConstantZeroValue(e2)) {
01131 break;
01132 } else if (pt2 != NULL && isConstantZeroValue(e1)) {
01133 break;
01134 }
01135 ERROR1(CTRUMP_ERR_INVALID_OPERAND, "invalid operands to binary `%s'"
01136 , logs_, en);
01137 return en;
01138 } while (0);
01139
01140 ne1->setNextSibling(antlr::RefAST(e2));
01141 en->setFirstChild(antlr::RefAST(ne1));
01142 en->getValueRef().type = new BasicType(BasicType::SINT);
01143 return en;
01144 }
01145
01149 const Type* convertUnaryExprType(const Type* t)
01150 {
01151 const Type* t1 = getOriginalType(t, true);
01152 const BasicType* bt = dynamic_cast<const BasicType*>(t1);
01153 if (bt != NULL) {
01154 BasicType::PrimitiveTag pt = bt->getPrimitiveTag();
01155 if (pt <= BasicType::USHORT) {
01156
01157 return new BasicType(BasicType::SINT);
01158 } else {
01159 return t;
01160 }
01161 }
01162
01163 const ArrayType* art = dynamic_cast<const ArrayType*>(t1);
01164 if (art != NULL) {
01165
01166 return new PointerType(art->getBaseType(), art->getQualFlags());
01167 }
01168
01169 const FunctionType* ft = dynamic_cast<const FunctionType*>(t1);
01170 if (ft != NULL) {
01171 return new PointerType(ft, 0);
01172 }
01173
01174 return t;
01175 }
01176
01177 RefNode typeUnaryExpr(RefNode e)
01178 {
01179 const Type* et = e->getValueRef().type;
01180 const Type* tt = convertUnaryExprType(et);
01181 return makeImplicitCastNode(e, tt, et);
01182 }
01183
01184 BasicType::PrimitiveTag getArithmeticTypeTag(const Type* t)
01185 {
01186 const EnumType* et = dynamic_cast<const EnumType*>(t);
01187 if (et != NULL) {
01188 return BasicType::SINT;
01189 }
01190 const BasicType* bt = dynamic_cast<const BasicType*>(t);
01191 if (bt != NULL) {
01192 BasicType::PrimitiveTag tag = bt->getPrimitiveTag();
01193 if (BasicType::isArithmeticTypeTag(tag)) {
01194 return tag;
01195 }
01196 }
01197 return BasicType::NONE;
01198 }
01199
01200 BasicType::PrimitiveTag
01201 typeBinaryExpr(const Type* type1, const Type* type2)
01202 {
01203 BasicType::PrimitiveTag t1 = getArithmeticTypeTag(type1);
01204 BasicType::PrimitiveTag t2 = getArithmeticTypeTag(type2);
01205 if (t1 == BasicType::NONE || t2 == BasicType::NONE) {
01206 return BasicType::NONE;
01207 }
01208
01209 if (t1 == t2) return t1;
01210 if (t1 == BasicType::LDOUBLE) return t1;
01211 if (t2 == BasicType::LDOUBLE) return t2;
01212 if (t1 == BasicType::DOUBLE) return t1;
01213 if (t2 == BasicType::DOUBLE) return t2;
01214 if (t1 == BasicType::FLOAT) return t1;
01215 if (t2 == BasicType::FLOAT) return t2;
01216 if (t1 == BasicType::ULLONG) return t1;
01217 if (t2 == BasicType::ULLONG) return t2;
01218
01219 #define SIGNED_UNSIGNED_CONV_LONG(ta, tb, bta) \
01220 do { \
01221 if (ta == BasicType::SLLONG) { \
01222 if (tb == BasicType::ULONG && \
01223 (targetInfo_->getSize(TargetInfo::LLONG) == \
01224 targetInfo_->getSize(TargetInfo::LONG))) { \
01225 return BasicType::ULLONG; \
01226 } else { \
01227 return ta; \
01228 } \
01229 } \
01230 } while (0)
01231
01232 SIGNED_UNSIGNED_CONV_LONG(t1, t2, bt1);
01233 SIGNED_UNSIGNED_CONV_LONG(t2, t1, bt2);
01234
01235 #undef SIGNED_UNSIGNED_CONV_LONG
01236
01237 if (t1 == BasicType::ULONG) return t1;
01238 if (t2 == BasicType::ULONG) return t2;
01239
01240
01241 #define SIGNED_UNSIGNED_CONV_INT(ta, tb, bta) \
01242 do { \
01243 if (ta == BasicType::SLONG) { \
01244 if (tb == BasicType::UINT && \
01245 (targetInfo_->getSize(TargetInfo::LONG) == \
01246 targetInfo_->getSize(TargetInfo::INT))) { \
01247 return BasicType::ULONG; \
01248 } else { \
01249 return ta; \
01250 } \
01251 } \
01252 } while (0)
01253
01254 SIGNED_UNSIGNED_CONV_INT(t1, t2, bt1);
01255 SIGNED_UNSIGNED_CONV_INT(t2, t1, bt2);
01256
01257 #undef SIGNED_UNSIGNED_CONV_INT
01258
01259 if (t1 == BasicType::UINT) return t1;
01260 if (t2 == BasicType::UINT) return t2;
01261
01262 if (t1 == BasicType::SINT) return t1;
01263 if (t2 == BasicType::SINT) return t2;
01264
01265
01266 return BasicType::NONE;
01267 }
01268
01269 RefNode typeFunctionArguments(ParameterTypes::const_iterator& i,
01270 ParameterTypes::const_iterator& end,
01271 RefNode argNode, bool hasVararg,
01272 RefNode locationNode)
01273 {
01274 if (i == end) {
01275 if (hasVararg) {
01276 return typeUnknownTypeFunctionArguments(argNode, locationNode);
01277 } else if (argNode != NULL) {
01278 ERROR1(CTRUMP_ERR_TOO_MANY_ARGS, "too many arguments to function `%s'"
01279 , logs_, locationNode);
01280 throw UncontinuableError();
01281 } else {
01282 return NULL;
01283 }
01284 } else {
01285 if (argNode == NULL) {
01286 ERROR1(CTRUMP_ERR_TOO_FEW_ARGS, "too few arguments to function `%s'"
01287 , logs_, locationNode);
01288 throw UncontinuableError();
01289 }
01290 RefNode newEn = typeFunctionArgument(argNode, *i, locationNode);
01291 RefNode nextAn =
01292 typeFunctionArguments(++i, end,
01293 RefNode(argNode->getNextSibling()),
01294 hasVararg, locationNode);
01295 argNode->setFirstChild(antlr::RefAST(newEn));
01296 argNode->setNextSibling(antlr::RefAST(nextAn));
01297 return argNode;
01298 }
01299 }
01300
01301 RefNode typeUnknownTypeFunctionArguments(RefNode argNode,
01302 RefNode locationNode)
01303 {
01304 if (argNode == NULL) {
01305 return argNode;
01306 } else {
01307 RefNode newEn = typeFunctionArgument(argNode, NULL, locationNode);
01308 RefNode nextAn =
01309 typeUnknownTypeFunctionArguments(RefNode(argNode->getNextSibling()),
01310 locationNode);
01311 argNode->setFirstChild(antlr::RefAST(newEn));
01312 argNode->setNextSibling(antlr::RefAST(nextAn));
01313 return argNode;
01314 }
01315 }
01316
01321 RefNode typeFunctionArgument(RefNode argNode, const Type* paramType,
01322 RefNode locationNode)
01323 {
01324 const Type* t;
01325 assert(argNode->getType() == CTokenTypes::NArgument);
01326 RefNode en = RefNode(argNode->getFirstChild());
01327 #ifdef PARSER_DEBUG
01328 std::cout << argNode->toStringList() << std::endl;
01329 #endif
01330 en = typeUnaryExpr(typeExpr(en));
01331
01332 const Type* argType = getOriginalType(en->getValueRef().type, true);
01333 if (paramType != NULL) {
01334 paramType = getOriginalType(convertUnaryExprType(paramType), true);
01335 AssignmentErrorTag err = checkAssignment(paramType, en);
01336 switch (err) {
01337 case ASSIGN_ERR_INCOMPATIBLE:
01338 {
01339
01340
01341
01342
01343 const VectorType* pvt =
01344 dynamic_cast<const VectorType*>(paramType);
01345 if (pvt != NULL) {
01346 if (argType->isArithmeticType()) {
01347 t = argType;
01348 break;
01349 }
01350 const VectorType* avt =
01351 dynamic_cast<const VectorType*>(argType);
01352 if (avt != NULL) {
01353 t = argType;
01354 break;
01355 }
01356 }
01357 ERROR1(CTRUMP_ERR_INCOMPATIBLE_ARG_TYPE, "incompatible type for argument of `%s'"
01358 , logs_, locationNode);
01359 throw UncontinuableError();
01360 }
01361 break;
01362 case ASSIGN_ERR_INCOMPATIBLE_POINTER:
01363
01364 break;
01365 default:
01366 t = paramType;
01367 break;
01368 }
01369 } else {
01370 const BasicType* abt = dynamic_cast<const BasicType*>(argType);
01371 if (abt != NULL &&
01372 abt->getPrimitiveTag() == BasicType::FLOAT) {
01373 t = new BasicType(BasicType::DOUBLE);
01374 } else {
01375 t = argType;
01376 }
01377 }
01378
01379
01380 return makeImplicitCastNode(en, t, argType);
01381 }
01382
01383 const Type* checkCondOperands(RefNode e1, RefNode e2)
01384 {
01385 const Type* t1 = getOriginalType(e1->getValueRef().type, true);
01386 const Type* t2 = getOriginalType(e2->getValueRef().type, true);
01387 const BasicType* bt1 = dynamic_cast<const BasicType*>(t1);
01388 if (bt1 != NULL) {
01389 const BasicType* bt2 = dynamic_cast<const BasicType*>(t2);
01390 if (bt2 != NULL) {
01391 BasicType::PrimitiveTag p1 = bt1->getPrimitiveTag();
01392 BasicType::PrimitiveTag p2 = bt2->getPrimitiveTag();
01393 BasicType::PrimitiveTag resTag = typeBinaryExpr(bt1, bt2);
01394 if (resTag != BasicType::NONE) {
01395 return new BasicType(resTag);
01396 } else if (p1 == BasicType::VOID && p2 == BasicType::VOID) {
01397 return bt1;
01398 }
01399 }
01400 }
01401 const PointerType* pt1 = dynamic_cast<const PointerType*>(t1);
01402 if (pt1 != NULL) {
01403 const PointerType* pt2 = dynamic_cast<const PointerType*>(t2);
01404 if (pt2 != NULL) {
01405 const Type* pbt1 = getOriginalType(pt1->getBaseType(), true);
01406 const Type* pbt2 = getOriginalType(pt2->getBaseType(), true);
01407 if (pbt1->isVoidType()) {
01408 return t1;
01409 }
01410 if (pbt2->isVoidType()) {
01411 return t2;
01412 }
01413 if (pbt1->isCompatibleType(pbt2)) {
01414
01415 QualFlags qf = pt1->getQualFlags() | pt2->getQualFlags();
01416 return new PointerType(pt1->getBaseType(), qf);
01417 }
01418 }
01419 if (isConstantZeroValue(e2)) {
01420 return pt1;
01421 }
01422 }
01423 const PointerType* pt2 = dynamic_cast<const PointerType*>(t2);
01424 if (pt2 != NULL) {
01425 if (isConstantZeroValue(e1)) {
01426 return pt2;
01427 }
01428 }
01429 if (t1->isCompatibleType(t2)) {
01430 return t1;
01431 }
01432 return NULL;
01433 }
01434
01443 RefNode typeExpr(RefNode en)
01444 {
01445 assert(en != NULL);
01446
01447 int nt = en->getType();
01448 if (nt == CTokenTypes::IDENT) {
01449
01450 return en;
01451 } else if (en->getValueRef().type != NULL) {
01452
01453 return en;
01454 }
01455
01456 #ifdef PARSER_DEBUG
01457 std::cout << "e: `" << (en != NULL ? en->toStringList() : "NULL") << "' (" << nt << ")\n";
01458 #endif
01459 const Type* t = NULL;
01460 switch (nt) {
01461
01462 case CTokenTypes::CharLiteral:
01463 case CTokenTypes::WCharLiteral:
01464 t = en->getValueRef().signedValue.type;
01465 break;
01466 case CTokenTypes::NStringConstant:
01467 case CTokenTypes::NWideStringConstant:
01468 t = new PointerType(new BasicType(BasicType::CHAR, Const), Const);
01469 break;
01470 case CTokenTypes::OctalIntConst:
01471 case CTokenTypes::DecimalIntConst:
01472 case CTokenTypes::HexadecimalIntConst:
01473 t = en->getValueRef().signedValue.type;
01474 break;
01475 case CTokenTypes::FloatDoubleConst:
01476 t = new BasicType(BasicType::FLOAT);
01477 break;
01478 case CTokenTypes::DoubleDoubleConst:
01479 t = new BasicType(BasicType::DOUBLE);
01480 break;
01481 case CTokenTypes::LongDoubleConst:
01482 t = new BasicType(BasicType::LDOUBLE);
01483 break;
01484 case CTokenTypes::NSizeofType:
01485
01486 t = new BasicType(BasicType::UINT);
01487 break;
01488 }
01489 if (t != NULL) {
01490 en->getValueRef().type = t;
01491 return en;
01492 }
01493
01494
01495 RefNode e1 = RefNode(en->getFirstChild());
01496
01497 switch (nt) {
01498 case CTokenTypes::NUnary:
01499 {
01500 RefNode rand = RefNode(e1->getNextSibling());
01501 rand = typeExpr(rand);
01502 int opt = e1->getType();
01503 if (opt == CTokenTypes::AND) {
01504 const Type* randType =
01505 getOriginalType(rand->getValueRef().type, false);
01506 t = new PointerType(randType, 0);
01507 } else if (opt == CTokenTypes::MULT) {
01508 const Type* randType =
01509 getOriginalType(rand->getValueRef().type, true);
01510 const PointerType* pt =
01511 dynamic_cast<const PointerType*>(randType);
01512 if (pt == NULL) {
01513 ERROR1(CTRUMP_ERR_INVALID_DEREFERENCE, "invalid type argument of `%s'"
01514 , logs_, e1);
01515 }
01516 t = pt->getBaseType();
01517 } else {
01518 rand = typeUnaryExpr(rand);
01519 t = rand->getValueRef().type;
01520
01521 }
01522 e1->setNextSibling(antlr::RefAST(rand));
01523 break;
01524 }
01525 case CTokenTypes::NParen:
01526 case CTokenTypes::PLUS_PLUS:
01527 case CTokenTypes::MINUS_MINUS:
01528 case CTokenTypes::NPlusPlusPostfix:
01529 case CTokenTypes::NMinusMinusPostfix:
01530 e1 = typeExpr(e1);
01531 t = convertUnaryExprType(e1->getValueRef().type);
01532 break;
01533
01534 case CTokenTypes::SIZEOF:
01535
01536 e1 = typeExpr(e1);
01537 t = new BasicType(BasicType::UINT);
01538 break;
01539
01540 case CTokenTypes::VA_ARG:
01541 e1 = typeExpr(e1);
01542 t = RefNode(e1->getNextSibling())->getValueRef().type;
01543 break;
01544
01545 case CTokenTypes::NCast:
01546 {
01547 RefNode es = RefNode(e1->getNextSibling());
01548 es = typeExpr(es);
01549 t = typeCastExpr(e1, es);
01550
01551 break;
01552 }
01553 case CTokenTypes::NCompoundLiteral:
01554 {
01555
01556 t = e1->getValueRef().type;
01557 break;
01558 }
01559 case CTokenTypes::DOT:
01560 case CTokenTypes::DEREF:
01561 {
01562 e1 = typeExpr(e1);
01563 const Type* t1 = getOriginalType(e1->getValueRef().type, true);
01564 if (nt == CTokenTypes::DEREF) {
01565 const PointerType* pt1 =
01566 dynamic_cast<const PointerType*>(t1);
01567 if (pt1 == NULL) {
01568 ERROR1(CTRUMP_ERR_INVALID_DEREFERENCE, "invalid type argument of `%s'"
01569 , logs_, en);
01570 throw UncontinuableError();
01571 }
01572 t1 = getOriginalType(pt1->getBaseType(), true);
01573 }
01574 const FieldInclusionType* ft1 =
01575 dynamic_cast<const FieldInclusionType*>(t1);
01576 antlr::RefAST e2 = e1->getNextSibling();
01577 const std::string& fieldName = e2->getText();
01578 if (ft1 == NULL) {
01579 ERROR1(CTRUMP_ERR_INVALID_MEMBER_ACCESS, "request for member `%s' in something not a structure or union"
01580 , logs_, en);
01581 throw UncontinuableError();
01582 }
01583 SUDefinition* def = ft1->getDefinition();
01584 Environment* env = def->getEnv();
01585 if (env == NULL) {
01586 ERROR1(CTRUMP_ERR_DEREFERENCE_INCOMPLETE_TYPE, "dereferencing pointer to incomplete type"
01587 , logs_, en);
01588 }
01589 NameDescription* nd;
01590 Environment* foundEnv = env->lookupOrdinaryName(fieldName, &nd);
01591 if (foundEnv == NULL) {
01592 ERROR1(CTRUMP_ERR_INVALID_MEMBER_ACCESS, "request for member `%s' in something not a structure or union"
01593 , logs_, en);
01594 }
01595 t = nd->getType();
01596 break;
01597 }
01598 case CTokenTypes::NCall:
01599 {
01600 RefNode ne1 = typeUnaryExpr(typeExpr(e1));
01601 const Type* t1 = getOriginalType(ne1->getValueRef().type, true);
01602 const PointerType* pft1 = dynamic_cast<const PointerType*>(t1);
01603 if (pft1 == NULL) {
01604 ERROR1(CTRUMP_ERR_INVALID_FUNCTION_CALL, "called object `%s' is not a function"
01605 , logs_, e1);
01606 throw UncontinuableError();
01607 }
01608 const Type* pbt1 = getOriginalType(pft1->getBaseType(), false);
01609 const FunctionType* ft1 =
01610 dynamic_cast<const FunctionType*>(pbt1);
01611 if (ft1 == NULL) {
01612 ERROR1(CTRUMP_ERR_INVALID_FUNCTION_CALL, "called object `%s' is not a function"
01613 , logs_, e1);
01614 throw UncontinuableError();
01615 }
01616 RefNode an = RefNode(e1->getNextSibling());
01617 const ParameterTypes& pts = ft1->getParameterTypes();
01618 ParameterTypes::const_iterator i = pts.begin();
01619 ParameterTypes::const_iterator end = pts.end();
01620 RefNode newAn =
01621 typeFunctionArguments(i, end, an, ft1->hasVararg(), e1);
01622 ne1->setNextSibling(antlr::RefAST(newAn));
01623 t = ft1->getReturnType();
01624 break;
01625 }
01626 }
01627 if (t != NULL) {
01628 en->setFirstChild(antlr::RefAST(e1));
01629 en->getValueRef().type = t;
01630 return en;
01631 }
01632
01633
01634 RefNode ne1 = typeExpr(e1);
01635 RefNode e2 = RefNode(e1->getNextSibling());
01636
01637 if (nt == CTokenTypes::QUERY) {
01638 RefNode ne2 = typeExpr(e2);
01639 RefNode e3 = RefNode(e2->getNextSibling());
01640 e3 = typeExpr(e3);
01641 t = checkCondOperands(e2, e3);
01642 if (t == NULL) {
01643 ERROR(CTRUMP_ERR_TYPE_MISMATCHED_COND, "type mismatched in conditional expression"
01644 , logs_, en);
01645 }
01646 ne2->setNextSibling(antlr::RefAST(e3));
01647 ne1->setNextSibling(antlr::RefAST(ne2));
01648 en->setFirstChild(antlr::RefAST(ne1));
01649 en->getValueRef().type = t;
01650 return en;
01651 }
01652
01653 e2 = typeExpr(e2);
01654 switch (nt) {
01655 case CTokenTypes::EQ:
01656 case CTokenTypes::MULT_EQ:
01657 case CTokenTypes::DIV_EQ:
01658 case CTokenTypes::MOD_EQ:
01659 case CTokenTypes::PLUS_EQ:
01660 case CTokenTypes::MINUS_EQ:
01661 case CTokenTypes::LSHIFT_EQ:
01662 case CTokenTypes::RSHIFT_EQ:
01663 case CTokenTypes::AND_EQ:
01664 case CTokenTypes::XOR_EQ:
01665 case CTokenTypes::OR_EQ:
01666 return typeAssignmentExpr(en);
01667 case CTokenTypes::EQ_EQ:
01668 case CTokenTypes::NOT_EQ:
01669 return typeComparisonExpr(en);
01670 }
01671
01672 ne1 = typeUnaryExpr(ne1);
01673 e2 = typeUnaryExpr(e2);
01674
01675 const Type* at1 = getOriginalType(ne1->getValueRef().type, true);
01676 const Type* at2 = getOriginalType(e2->getValueRef().type, true);
01677 switch (nt) {
01678 case CTokenTypes::PLUS:
01679 case CTokenTypes::MINUS:
01680 {
01681 t = checkPointerArithmeticAdditive(at1, at2);
01682 if (t != NULL) {
01683 t = new PointerType(t, 0);
01684 break;
01685 }
01686 t = checkPointerArithmeticAdditive(at2, at1);
01687 if (t != NULL) {
01688 t = new PointerType(t, 0);
01689 break;
01690 }
01691 if (nt == CTokenTypes::MINUS) {
01692 t = checkPointerSubtraction(at1, at2);
01693 if (t != NULL) {
01694 break;
01695 }
01696 }
01697 t = checkBinaryArithmetic(at1, at2, en);
01698 ne1 = makeImplicitCastNode(ne1, t, at1);
01699 e2 = makeImplicitCastNode(e2, t, at2);
01700 break;
01701 }
01702 case CTokenTypes::OR_OR:
01703 case CTokenTypes::AND_AND:
01704 {
01705 if (!at1->isScalarType() || !at2->isScalarType()) {
01706 ERROR1(CTRUMP_ERR_INVALID_OPERAND, "invalid operands to binary `%s'"
01707 , logs_, en);
01708 }
01709 t = new BasicType(BasicType::SINT);
01710 break;
01711 }
01712 case CTokenTypes::LESS:
01713 case CTokenTypes::GREATER:
01714 case CTokenTypes::LESS_EQ:
01715 case CTokenTypes::GREATER_EQ:
01716 {
01717 const PointerType* pt1 = dynamic_cast<const PointerType*>(at1);
01718 const PointerType* pt2 = dynamic_cast<const PointerType*>(at2);
01719 if (pt1 != NULL && pt2 != NULL) {
01720 if (pt1->getBaseType()->
01721 isCompatibleType(pt2->getBaseType())) {
01722 t = at1;
01723 break;
01724 }
01725 }
01726 }
01727
01728 case CTokenTypes::OR:
01729 case CTokenTypes::XOR:
01730 case CTokenTypes::AND:
01731 case CTokenTypes::LSHIFT:
01732 case CTokenTypes::RSHIFT:
01733 case CTokenTypes::MULT:
01734 case CTokenTypes::DIV:
01735 case CTokenTypes::MOD:
01736 {
01737 t = checkBinaryArithmetic(at1, at2, en);
01738 ne1 = makeImplicitCastNode(ne1, t, at1);
01739 e2 = makeImplicitCastNode(e2, t, at2);
01740 break;
01741 }
01742 case CTokenTypes::OPEN_SQUARE:
01743 {
01744 t = checkPointerArithmeticAdditive(at1, at2);
01745 if (t == NULL) {
01746 t = checkPointerArithmeticAdditive(at2, at1);
01747 if (t == NULL) {
01748 ERROR1(CTRUMP_ERR_INVALID_SUBSCRIPTING, "subscripted value is neither array nor pointer"
01749 , logs_, en);
01750 throw UncontinuableError();
01751 }
01752 }
01753 break;
01754 }
01755 case CTokenTypes::COMMA:
01756 t = at2;
01757 break;
01758 }
01759 if (t != NULL) {
01760 ne1->setNextSibling(antlr::RefAST(e2));
01761 en->setFirstChild(antlr::RefAST(ne1));
01762 en->getValueRef().type = t;
01763 return en;
01764 }
01765 throw InternalError("typeExpr");
01766 }
01767
01768
01775 eint evalConstExpr(antlr::RefAST en) throw(EvalFailedException)
01776 {
01777 if (en == NULL) return 0;
01778 int nt = en->getType();
01779 #ifdef PARSER_DEBUG
01780 std::cout << "e: `" << (en != NULL ? en->toStringList() : "NULL") << "' (" << nt << ")\n";
01781 #endif
01782 switch (nt) {
01783 case CTokenTypes::IDENT:
01784 {
01785 NameDescription* nd;
01786 Environment* env =
01787 currentEnv_->lookupOrdinaryName(en->getText(), &nd);
01788 assert(env != NULL);
01789 if (nd->getLocation() == NameDescription::ENUM) {
01790 return nd->getConstantValue();
01791 }
01792 throw EvalFailedException(nt);
01793 }
01794 case CTokenTypes::CharLiteral:
01795 case CTokenTypes::WCharLiteral:
01796 case CTokenTypes::OctalIntConst:
01797 return strtoll(en->getText().c_str(), NULL, 8);
01798 case CTokenTypes::DecimalIntConst:
01799 return strtoll(en->getText().c_str(), NULL, 10);
01800 case CTokenTypes::HexadecimalIntConst:
01801 return strtoll(en->getText().c_str(), NULL, 16);
01802 case CTokenTypes::NParen:
01803 case CTokenTypes::NImplicitCast:
01804 return evalConstExpr(en->getFirstChild());
01805 case CTokenTypes::NUnary:
01806 {
01807 antlr::RefAST codeNode = en->getFirstChild();
01808 antlr::RefAST randNode = codeNode->getNextSibling();
01809 switch (codeNode->getType()) {
01810 case CTokenTypes::AND:
01811 case CTokenTypes::MULT:
01812 throw EvalFailedException(nt);
01813 case CTokenTypes::PLUS: return evalConstExpr(randNode);
01814 case CTokenTypes::MINUS: return - evalConstExpr(randNode);
01815 case CTokenTypes::COMPL: return ~ evalConstExpr(randNode);
01816 case CTokenTypes::NOT: return ! evalConstExpr(randNode);
01817 default:
01818 throw InternalError("evalConstExpr: invalid unary expr");
01819 }
01820 }
01821
01822 case CTokenTypes::NSizeofType:
01823 return RefNode(en->getFirstChild())->getValueRef().type->
01824 getSize(*targetInfo_);
01825 case CTokenTypes::SIZEOF:
01826 {
01827 RefNode e1 = RefNode(en->getFirstChild());
01828 const Type* t = e1->getValueRef().type;
01829 assert(t != NULL);
01830 return t->getSize(*targetInfo_);
01831 }
01832 case CTokenTypes::NCast:
01833
01834 return evalConstExpr(en->getFirstChild()->getNextSibling());
01835
01836 case CTokenTypes::PLUS_PLUS:
01837 case CTokenTypes::MINUS_MINUS:
01838 case CTokenTypes::OPEN_SQUARE:
01839 case CTokenTypes::NPlusPlusPostfix:
01840 case CTokenTypes::NMinusMinusPostfix:
01841 case CTokenTypes::DOT:
01842 case CTokenTypes::DEREF:
01843 case CTokenTypes::NCall:
01844 case CTokenTypes::NStringConstant:
01845 case CTokenTypes::NWideStringConstant:
01846 case CTokenTypes::FloatDoubleConst:
01847 case CTokenTypes::DoubleDoubleConst:
01848 case CTokenTypes::LongDoubleConst:
01849 case CTokenTypes::NCompoundLiteral:
01850 case CTokenTypes::VA_ARG:
01851 throw EvalFailedException(nt);
01852 }
01853
01854
01855 antlr::RefAST e1 = en->getFirstChild();
01856 antlr::RefAST e2 = e1->getNextSibling();
01857 eint v1 = evalConstExpr(e1);
01858 eint v2 = evalConstExpr(e2);
01859 switch (nt) {
01860 case CTokenTypes::OR_OR: return v1 || v2;
01861 case CTokenTypes::AND_AND: return v1 && v2;
01862 case CTokenTypes::OR: return v1 | v2;
01863 case CTokenTypes::XOR: return v1 ^ v2;
01864 case CTokenTypes::AND: return v1 & v2;
01865 case CTokenTypes::EQ_EQ: return v1 == v2;
01866 case CTokenTypes::NOT_EQ: return v1 != v2;
01867 case CTokenTypes::LESS: return v1 < v2;
01868 case CTokenTypes::GREATER: return v1 > v2;
01869 case CTokenTypes::LESS_EQ: return v1 <= v2;
01870 case CTokenTypes::GREATER_EQ: return v1 >= v2;
01871 case CTokenTypes::LSHIFT: return v1 << v2;
01872 case CTokenTypes::RSHIFT: return v1 >> v2;
01873 case CTokenTypes::PLUS: return v1 + v2;
01874 case CTokenTypes::MINUS: return v1 - v2;
01875 case CTokenTypes::MULT: return v1 * v2;
01876 case CTokenTypes::DIV: return v1 / v2;
01877 case CTokenTypes::MOD: return v1 % v2;
01878 case CTokenTypes::COMMA: return v2;
01879 case CTokenTypes::QUERY:
01880 if (v1) {
01881 return v2;
01882 } else {
01883 return evalConstExpr(e2->getNextSibling());
01884 }
01885 }
01886
01887 throw EvalFailedException(nt);
01888 }
01889
01890 void setLogs(Logs* logs)
01891 {
01892 logs_ = logs;
01893 }
01894
01895 void setTargetInfo(TargetInfo* info)
01896 {
01897 targetInfo_ = info;
01898 }
01899
01900 protected:
01901 Environment* currentEnv_;
01902
01903 FunctionType* currentFunctionType_;
01904
01905 static const char * const gccBuiltinNames[];
01906
01907 bool supportVectorExt_;
01908
01909 Logs* logs_;
01910
01911 TargetInfo* targetInfo_;
01912
01913 antlr::ASTFactory* astFactory_;
01914
01915 private:
01916 unsigned int nameCount_;
01917
01918 };
01919
01920
01921
01922
01923
01924
01925
01926
01927
01928
01929
01930
01931
01932
01933
01934
01935
01936
01937
01938
01939
01940
01941
01942
01943
01944 }
01945
01946 #endif