Changeset b91bfde


Ignore:
Timestamp:
Apr 12, 2021, 11:53:30 AM (6 months ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
arm-eh, jacob/cs343-translation, master, new-ast-unique-expr
Children:
73f4d08
Parents:
ecfd758
Message:

Added and used some helpers. The widely useful one is a from_string helper for constants.

Location:
src
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Expr.cpp

    recfd758 rb91bfde  
    260260}
    261261
     262ConstantExpr * ConstantExpr::from_string( const CodeLocation & loc, const std::string & str ) {
     263        const Type * charType = new BasicType( BasicType::Char );
     264        // Adjust the length of the string for the terminator.
     265        const Expr * strSize = from_ulong( loc, str.size() + 1 );
     266        const Type * strType = new ArrayType( charType, strSize, FixedLen, StaticDim );
     267        const std::string strValue = "\"" + str + "\"";
     268        return new ConstantExpr( loc, strType, strValue, std::nullopt );
     269}
     270
    262271ConstantExpr * ConstantExpr::null( const CodeLocation & loc, const Type * ptrType ) {
    263272        return new ConstantExpr{
  • src/AST/Expr.hpp

    recfd758 rb91bfde  
    438438        long long int intValue() const;
    439439
    440         /// generates a boolean constant of the given bool
     440        /// Generates a boolean constant of the given bool.
    441441        static ConstantExpr * from_bool( const CodeLocation & loc, bool b );
    442         /// generates an integer constant of the given int
     442        /// Generates an integer constant of the given int.
    443443        static ConstantExpr * from_int( const CodeLocation & loc, int i );
    444         /// generates an integer constant of the given unsigned long int
     444        /// Generates an integer constant of the given unsigned long int.
    445445        static ConstantExpr * from_ulong( const CodeLocation & loc, unsigned long i );
    446         /// generates a null pointer value for the given type. void * if omitted.
     446        /// Generates a string constant from the given string (char type, unquoted string).
     447        static ConstantExpr * from_string( const CodeLocation & loc, const std::string & string );
     448        /// Generates a null pointer value for the given type. void * if omitted.
    447449        static ConstantExpr * null( const CodeLocation & loc, const Type * ptrType = nullptr );
    448450
  • src/SynTree/Constant.cc

    recfd758 rb91bfde  
    4242}
    4343
     44Constant Constant::from_string( const std::string & str ) {
     45        Type * charType = new BasicType( noQualifiers, BasicType::Char );
     46        // Adjust the length of the string for the terminator.
     47        Expression * strSize = new ConstantExpr( Constant::from_ulong( str.size() + 1 ) );
     48        Type * strType = new ArrayType( noQualifiers, charType, strSize, false, false );
     49        const std::string strValue = "\"" + str + "\"";
     50        return Constant( strType, strValue, std::nullopt );
     51}
     52
    4453Constant Constant::null( Type * ptrtype ) {
    4554        if ( nullptr == ptrtype ) {
  • src/SynTree/Constant.h

    recfd758 rb91bfde  
    4747        /// generates an integer constant of the given unsigned long int
    4848        static Constant from_ulong( unsigned long i );
     49        /// generates a string constant from the given string (char type, unquoted string)
     50        static Constant from_string( const std::string & string );
    4951
    5052        /// generates a null pointer value for the given type. void * if omitted.
  • src/Virtual/ExpandCasts.cc

    recfd758 rb91bfde  
    7777
    7878        class VirtualCastCore {
    79                 Type * pointer_to_pvt(int level_of_indirection) {
     79                CastExpr * cast_to_type_id( Expression * expr, int level_of_indirection ) {
    8080                        Type * type = new StructInstType(
    8181                                Type::Qualifiers( Type::Const ), pvt_decl );
     
    8383                                type = new PointerType( noQualifiers, type );
    8484                        }
    85                         return type;
     85                        return new CastExpr( expr, type );
    8686                }
    8787
     
    253253                Expression * result = new CastExpr(
    254254                        new ApplicationExpr( VariableExpr::functionPointer( vcast_decl ), {
    255                                         new CastExpr(
    256                                                 new AddressExpr( new VariableExpr( type_id ) ),
    257                                                 pointer_to_pvt(1)
    258                                         ),
    259                                         new CastExpr(
    260                                                 castExpr->get_arg(),
    261                                                 pointer_to_pvt(2)
    262                                         )
     255                                cast_to_type_id( new AddressExpr( new VariableExpr( type_id ) ), 1 ),
     256                                cast_to_type_id( castExpr->get_arg(), 2 ),
    263257                        } ),
    264258                        castExpr->get_result()->clone()
  • src/Virtual/Tables.cc

    recfd758 rb91bfde  
    172172
    173173Attribute * linkonce( const std::string & subsection ) {
    174         const std::string section = "\".gnu.linkonce." + subsection + "\"";
    175         // Adjust for terminator and quotes.
    176         size_t str_size = section.size() + 1 - 2;
     174        const std::string section = ".gnu.linkonce." + subsection;
    177175        return new Attribute( "section", {
    178                 new ConstantExpr( Constant(
    179                         new ArrayType(
    180                                 noQualifiers,
    181                                 new BasicType( noQualifiers, BasicType::Char ),
    182                                 new ConstantExpr( Constant::from_ulong( str_size ) ),
    183                                 false, false ),
    184                         section,
    185                         std::nullopt
    186                 ) ),
     176                new ConstantExpr( Constant::from_string( section ) ),
    187177        } );
    188178}
Note: See TracChangeset for help on using the changeset viewer.