Changeset 15936a6d in tol


Ignore:
Timestamp:
Sep 1, 2009, 8:33:24 AM (17 years ago)
Author:
Víctor de Buen <vdebuen@…>
Branches:
1941-docker-image-to-test, devel, master
Children:
8cf5460
Parents:
992be58
Message:

Adding static members and methods to classes
Forward declarations of classes

git-svn-id: ​https://www.tol-project.org/svn/tolp/trunk@1289 be4846ae-f051-483a-831f-b64e7d08527f

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tol/btol/bgrammar/class.cpp

    r992be58 r15936a6d  
    3535#include <tol/tol_blanguag.h>
    3636#include <tol/tol_bnameblock.h>
     37#include <tol/tol_bcodgra.h>
     38
    3739
    3840//--------------------------------------------------------------------
    … …  
    4244BTraceInit("class.cpp");
    4345const BClass* BClass::currentClassBuildingInstance_ = NULL;
     46const BClass* BClass::currentStatic_ = NULL;
    4447
    4548//--------------------------------------------------------------------
    … …  
    9598  BCore* car;
    9699  parent_ = parent;
    97   BToken* tok = (BToken*)branch->car();
    98100  isGood_ = true;
    99   if((tok->TokenType()==MONARY)&&(tok->Name()=="static"))
     101  BToken* tok = NULL;
     102  if(parent && branch && branch->car())
     103  {
     104    tok = (BToken*)branch->car();
     105  }
     106  else
     107  {
     108    isGood_ = false;
     109  }
     110  if(isGood_ && (tok->TokenType()==MONARY) && (tok->Name()=="Static"))
    100111  {
    101112    isStatic_ = true;
    102     branch_ = branch = branch->cdr();
    103   }
    104   if((tok->TokenType()==BINARY)&&(tok->Name()=="="))
    105   {
    106   //If is a '=' expression then is not a method and has default value
    107     isMethod_ = false;
    108     BText expression = BParser::Unparse(branch,"","");
    109     if(expression[0]=='[')
    110     {
    111       expression = expression.SubString(2,expression.Length()-3);
    112     }
    113     else if(expression[0]=='{')
    114     {
    115       expression = expression.SubString(1,expression.Length()-2);
    116     }
    117     int posEq = expression.Find("=");
    118     int posSp = expression.Find(" ");
    119     name_ = expression.SubString(posSp+1,posEq-1);
    120     declaration_ = expression.SubString(0,posEq-1);
    121     definition_ = expression.SubString(posEq+1,expression.Length());
    122   }
    123   else if((tok->TokenType()==BINARY)&&(tok->Name()=="#F#"))
    124   {
    125   //If is a '#F#' expression then is a method and has default value
    126     isMethod_ = true;
    127     cdr = branch->cdr();
    128     if(cdr)
    129     {
    130       car = cdr->car();
    131       if(car && car->IsListClass())
    132       {
    133         declaration_ = BParser::Unparse((List*)car,"","");
    134         if(((List*)car)->cdr() &&
    135            ((List*)car)->cdr()->car() &&
    136            ((List*)car)->cdr()->car()->IsListClass())
     113    if(!(branch = branch->cdr()))
     114    {
     115      isGood_ = false;
     116    }
     117    else
     118    {
     119      if(branch->car()->IsListClass())
     120      {
     121        branch = ((List*)branch->car());
     122      }
     123      else
     124      {
     125        isGood_ = false;
     126      }
     127    }
     128    if(isGood_)
     129    {
     130      tok = (BToken*)branch->car();
     131    }
     132    branch_ = branch;
     133  }
     134  if(isGood_)
     135  {
     136    if((tok->TokenType()==BINARY)&&(tok->Name()=="="))
     137    {
     138    //If is a '=' expression then is not a method and has default value
     139      isMethod_ = false;
     140      BText expression = BParser::Unparse(branch,"","");
     141      if(expression[0]=='[')
     142      {
     143        expression = expression.SubString(2,expression.Length()-3);
     144      }
     145      else if(expression[0]=='{')
     146      {
     147        expression = expression.SubString(1,expression.Length()-2);
     148      }
     149      int posEq = expression.Find("=");
     150      int posSp = expression.Find(" ");
     151      name_ = expression.SubString(posSp+1,posEq-1);
     152      declaration_ = expression.SubString(0,posEq-1);
     153      definition_ = expression.SubString(posEq+1,expression.Length());
     154    }
     155    else if((tok->TokenType()==BINARY)&&(tok->Name()=="#F#"))
     156    {
     157    //If is a '#F#' expression then is a method and has default value
     158      isMethod_ = true;
     159      cdr = branch->cdr();
     160      if(cdr)
     161      {
     162        car = cdr->car();
     163        if(car && car->IsListClass())
    137164        {
    138           car = ((List*)((List*)car)->cdr()->car())->car();
    139           tok = (BToken*)car;
    140           name_ = tok->Name();
    141           cdr = cdr->cdr();
    142           if(cdr)
     165          declaration_ = BParser::Unparse((List*)car,"","");
     166          if(((List*)car)->cdr() &&
     167             ((List*)car)->cdr()->car() &&
     168             ((List*)car)->cdr()->car()->IsListClass())
    143169          {
    144             car = cdr->car();
    145             if(car && car->IsListClass())
     170            car = ((List*)((List*)car)->cdr()->car())->car();
     171            tok = (BToken*)car;
     172            name_ = tok->Name();
     173            cdr = cdr->cdr();
     174            if(cdr)
    146175            {
    147               definition_  = BParser::Unparse((List*)car,"","");
    148             }
    149             else
    150             {
    151               isGood_ = false;
     176              car = cdr->car();
     177              if(car && car->IsListClass())
     178              {
     179                definition_  = BParser::Unparse((List*)car,"","");
     180              }
     181              else
     182              {
     183                isGood_ = false;
     184              }
    152185            }
    153186          }
    … …  
    155188      }
    156189    }
    157   }
    158   else if(tok->TokenType()==TYPE)
    159   {
    160   //If is a 'Type name ...' expression then hasn't default value
    161     declaration_ = BParser::Unparse(branch,"","");
    162     car = branch->cdr()->car();
    163     car = (car->IsListClass())?((List*)car)->car():car;
    164     tok = (BToken*)car;
    165     name_ = tok->Name();
    166   //If main token is FUNCTION then member is a method
    167     isMethod_ = tok->TokenType()==FUNCTION;
    168   }
    169   else if((tok->TokenType()==BINARY)&&(tok->Name()=="::"))
    170   {
    171     declaration_ = BParser::Unparse(branch,"","");
    172     BText simpleDec = declaration_;
    173     isMethod_ = simpleDec.Find('(')>=0;
    174     simpleDec.Replace("("," ");
    175     simpleDec.Compact();
    176     int p0 = simpleDec.Find(' ');
    177     if(p0>0)
    178     {
    179       int p1 = simpleDec.Find(' ', p0+1);
    180       if(p1<0) { p1 = simpleDec.Length()+1; }
    181       if(p0+1<p1-1)
    182       {
    183         name_ = simpleDec.SubString(p0+1,p1-1);
    184       }
    185     }
    186   }
    187   //Cleaning open and close parenthesis
    188   if(declaration_[0]=='{')
    189   {
    190     declaration_=declaration_.SubString(1,declaration_.Length()-2);
     190    else if(tok->TokenType()==TYPE)
     191    {
     192    //If is a 'Type name ...' expression then hasn't default value
     193      declaration_ = BParser::Unparse(branch,"","");
     194      car = branch->cdr()->car();
     195      car = (car->IsListClass())?((List*)car)->car():car;
     196      tok = (BToken*)car;
     197      name_ = tok->Name();
     198    //If main token is FUNCTION then member is a method
     199      isMethod_ = tok->TokenType()==FUNCTION;
     200    }
     201    else if((tok->TokenType()==BINARY)&&(tok->Name()=="::"))
     202    {
     203      declaration_ = BParser::Unparse(branch,"","");
     204      BText simpleDec = declaration_;
     205      isMethod_ = simpleDec.Find('(')>=0;
     206      simpleDec.Replace("("," ");
     207      simpleDec.Compact();
     208      int p0 = simpleDec.Find(' ');
     209      if(p0>0)
     210      {
     211        int p1 = simpleDec.Find(' ', p0+1);
     212        if(p1<0) { p1 = simpleDec.Length()+1; }
     213        if(p0+1<p1-1)
     214        {
     215          name_ = simpleDec.SubString(p0+1,p1-1);
     216        }
     217      }
     218    } 
     219    //Cleaning open and close parenthesis
     220    if(declaration_[0]=='{')
     221    {
     222      declaration_=declaration_.SubString(1,declaration_.Length()-2);
     223    }
    191224  }
    192225  //Checking mandatory information: name and declaration.
    193226  //Definition is optional
    194   if(!name_.HasName() || !declaration_.HasName())
     227  if(!isGood_ || !name_.HasName() || !declaration_.HasName())
    195228  {
    196229    isGood_ = false;
    197230    Error(I2("Wrong syntax in member declaration ",
    198231             "Sintaxis incorrecta en declaración de miembro ")+
    199           BParser::Unparse(branch,"","\n")+
     232          BParser::Unparse(branch_,"","\n")+
    200233          I2(" of Class ", "de Class ")+parent->getName());
    201234  }
    … …  
    249282//--------------------------------------------------------------------
    250283{
    251   if(!method_ && isMethod_ && definition_.HasName())
     284  if(!isStatic_ && !method_ && isMethod_ && definition_.HasName())
    252285  {
    253286    BGrammar::IncLevel();
    … …  
    293326    {
    294327      static_ = obj;
     328      if(isMethod_)
     329      {
     330        if(obj->Mode()==BOBJECTMODE)
     331        {
     332          obj = UCode(obj)->Contens().Operator();
     333        }
     334        ((BUserFunction*)obj)->staticOwner_=(const BClass*)parent_;
     335      }
    295336      static_->IncNRefs();
    296337      return(1);
    … …  
    950991      class_->PutName(name);
    951992      BGrammar::AddObject(class_);
    952       assert(!class_ || FindClass(name,-1));
    953993    }
    954994  }
    … …  
    10831123}
    10841124
     1125//--------------------------------------------------------------------
     1126static BClass* FindClassInNameBlock(
     1127  const BText& name,
     1128  BUserNameBlock*& unb )
     1129//--------------------------------------------------------------------
     1130{
     1131  BClass* cls = NULL;
     1132  BSyntaxObject* obj;
     1133  int pos = name.Find("::",0);
     1134  if(pos>=0)
     1135  {
     1136    BText prefix;
     1137    BText sufix;
     1138    prefix = name.SubString(0,pos-1);
     1139    sufix  = name.SubString(pos+2,name.Length());
     1140    if(!unb)
     1141    {
     1142      obj = GraNameBlock()->FindVariable(prefix);
     1143      if(obj && obj->Grammar()==GraNameBlock())
     1144      {
     1145        unb = (BUserNameBlock*)obj;
     1146      }
     1147    }
     1148    else
     1149    {
     1150      BNameBlock& nb = unb->Contens();
     1151      obj = nb.Member(prefix);
     1152      if(obj && obj->Grammar()==GraNameBlock())
     1153      {
     1154        unb = (BUserNameBlock*)obj;
     1155      }
     1156      else
     1157      {
     1158        unb = NULL;
     1159      }
     1160    }
     1161    if(unb)
     1162    {
     1163      cls = FindClassInNameBlock(sufix, unb);         
     1164    }
     1165  }
     1166  else if(unb)
     1167  {
     1168    BNameBlock& nb = unb->Contens();
     1169    obj = nb.Member(name);
     1170    if(obj && obj->Mode()==BCLASSMODE)
     1171    {
     1172      cls = (BClass*)obj;
     1173    }
     1174  }
     1175  return(cls);
     1176}
    10851177
    10861178//--------------------------------------------------------------------
    … …  
    10931185{
    10941186  BSyntaxObject* result = NULL;
    1095   BClass* bcls = BStackManager::FindClass(name);
    1096   if(!bcls)
     1187  BUserNameBlock* unb = NULL;
     1188  BClass* cls = FindClassInNameBlock(name, unb);
     1189  if(!cls)
     1190  {
     1191    cls = BStackManager::FindClass(name);
     1192  }
     1193  if(!cls)
    10971194  {
    10981195    const BNameBlock* cns = BNameBlock::Current();
    … …  
    11021199      if(result && (result->Mode()==BCLASSMODE))
    11031200      {
    1104         bcls = (BClass*)result;
     1201        cls = (BClass*)result;
    11051202      }
    11061203      if(!result)
    … …  
    11111208    }
    11121209  }
    1113   if(!bcls)
     1210  if(!cls)
    11141211  {
    11151212    BObjClassify oc(GraAnything(),BCLASSMODE);
    1116     bcls= (BClass*)BGrammar::SymbolTable().Search(oc, name);
    1117   }
    1118   if(!bcls)
     1213    cls= (BClass*)BGrammar::SymbolTable().Search(oc, name);
     1214  }
     1215  if(!cls)
    11191216  {
    11201217    result = BNameBlock::UsingMember(name);
    11211218    if(result && (result->Mode()==BCLASSMODE))
    11221219    {
    1123       bcls = (BClass*)result;
    1124     }
    1125   }
    1126   if(bcls && (defined>=0) && (bcls->isDefined_!=defined))
    1127   {
    1128     bcls = NULL;
    1129   }
    1130   return(bcls);
    1131 }
    1132 
    1133 //--------------------------------------------------------------------
    1134 BSyntaxObject* BClass::FindMethod(const BText& memberName) const
     1220      cls = (BClass*)result;
     1221    }
     1222  }
     1223  if(cls && (defined>=0) && (cls->isDefined_!=defined))
     1224  {
     1225    cls = NULL;
     1226  }
     1227  return(cls);
     1228}
     1229
     1230//--------------------------------------------------------------------
     1231static bool checkNonPrivate(
     1232  const BClass* cls,
     1233  const BText& memberName,
     1234  bool fullAccess)
     1235//--------------------------------------------------------------------
     1236{
     1237  if(!fullAccess && (memberName[0]=='_')&&(memberName[1]!='.'))
     1238  {
     1239    Error(I2("Cannot access to private member ",
     1240             "No se puede acceder al miembro privado ")+memberName +
     1241          I2(" of Class ",
     1242             " de Class ")+cls->Name());
     1243    return(false);
     1244  }
     1245  else
     1246  {
     1247    return(true);
     1248  }
     1249}
     1250//--------------------------------------------------------------------
     1251BSyntaxObject* BClass::FindMethod(const BText& memberName, bool fullAccess) const
    11351252//--------------------------------------------------------------------
    11361253{
    11371254  BSyntaxObject* result = NULL;
    11381255  BMember* mbr = FindMember(memberName);
    1139   if(mbr && mbr->method_)
     1256  if(mbr && mbr->method_ && checkNonPrivate(this, memberName, fullAccess))
    11401257  {
    11411258    result = mbr->method_;
    … …  
    11451262
    11461263//--------------------------------------------------------------------
    1147 BSyntaxObject* BClass::FindStaticMethod(const BText& methodName) const
     1264BSyntaxObject* BClass::FindStaticMethod(const BText& methodName, bool fullAccess) const
    11481265//--------------------------------------------------------------------
    11491266{
    11501267  BSyntaxObject* result = NULL;
    11511268  BMember* mbr = FindMember(methodName);
    1152   if(mbr && mbr->isStatic_ && mbr->isMethod_)
     1269  if(mbr && mbr->isStatic_ && mbr->isMethod_  &&
     1270     checkNonPrivate(this, methodName, fullAccess))
    11531271  {
    1154     result = mbr->method_;
     1272    result = mbr->static_;
     1273    result->PutNameBlock(NULL);
    11551274  }
    11561275  return(result);
    … …  
    11581277
    11591278//--------------------------------------------------------------------
    1160 BSyntaxObject* BClass::FindStaticMemeber(const BText& memberName) const
     1279BSyntaxObject* BClass::FindStaticMemeber(const BText& memberName, bool fullAccess) const
    11611280//--------------------------------------------------------------------
    11621281{
    11631282  BSyntaxObject* result = NULL;
    11641283  BMember* mbr = FindMember(memberName);
    1165   if(mbr && mbr->static_ && !mbr->isMethod_)
     1284  if(mbr && mbr->static_ && !mbr->isMethod_  &&
     1285     checkNonPrivate(this, memberName, fullAccess))
    11661286  {
    11671287    result = mbr->static_;
     1288    result->PutNameBlock(NULL);
    11681289  }
    11691290  return(result);
    11701291}
    11711292
     1293//--------------------------------------------------------------------
     1294BSyntaxObject* BClass::FindStatic(const BText& methodName, bool fullAccess) const
     1295//--------------------------------------------------------------------
     1296{
     1297  BSyntaxObject* result = NULL;
     1298  BMember* mbr = FindMember(methodName);
     1299  if(mbr && mbr->isStatic_  && checkNonPrivate(this, methodName, fullAccess))
     1300  {
     1301    result = mbr->static_;
     1302    result->PutNameBlock(NULL);
     1303  }
     1304  return(result);
     1305}
     1306
Note: See TracChangeset for help on using the changeset viewer.